Browse Source

string-list: add unsorted_string_list_lookup()

Sometimes users need to lookup a string in an unsorted string_list. In
that case they should use this function instead of the version for
sorted strings.

Signed-off-by: Stephen Boyd <bebarino@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
maint
Stephen Boyd 15 years ago committed by Junio C Hamano
parent
commit
e242148012
  1. 6
      Documentation/technical/api-string-list.txt
  2. 13
      string-list.c
  3. 3
      string-list.h

6
Documentation/technical/api-string-list.txt

@ -104,8 +104,12 @@ write `string_list_insert(...)->util = ...;`.
`unsorted_string_list_has_string`:: `unsorted_string_list_has_string`::


It's like `string_list_has_string()` but for unsorted lists. It's like `string_list_has_string()` but for unsorted lists.

`unsorted_string_list_lookup`::

It's like `string_list_lookup()` but for unsorted lists.
+ +
This function needs to look through all items, as opposed to its The above two functions need to look through all items, as opposed to their
counterpart for sorted lists, which performs a binary search. counterpart for sorted lists, which performs a binary search.


Data structures Data structures

13
string-list.c

@ -168,12 +168,19 @@ void sort_string_list(struct string_list *list)
qsort(list->items, list->nr, sizeof(*list->items), cmp_items); qsort(list->items, list->nr, sizeof(*list->items), cmp_items);
} }


int unsorted_string_list_has_string(struct string_list *list, const char *string) struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
const char *string)
{ {
int i; int i;
for (i = 0; i < list->nr; i++) for (i = 0; i < list->nr; i++)
if (!strcmp(string, list->items[i].string)) if (!strcmp(string, list->items[i].string))
return 1; return list->items + i;
return 0; return NULL;
}

int unsorted_string_list_has_string(struct string_list *list,
const char *string)
{
return unsorted_string_list_lookup(list, string) != NULL;
} }



3
string-list.h

@ -38,5 +38,6 @@ struct string_list_item *string_list_lookup(const char *string, struct string_li
struct string_list_item *string_list_append(const char *string, struct string_list *list); struct string_list_item *string_list_append(const char *string, struct string_list *list);
void sort_string_list(struct string_list *list); void sort_string_list(struct string_list *list);
int unsorted_string_list_has_string(struct string_list *list, const char *string); int unsorted_string_list_has_string(struct string_list *list, const char *string);

struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
const char *string);
#endif /* STRING_LIST_H */ #endif /* STRING_LIST_H */

Loading…
Cancel
Save