@matheusbsbs / matheus-bagliano / commits / 363d59df1a
Fork 0

path-list: add functions to work with unsorted lists

Up to now, path-lists were sorted at all times. But sometimes it is much more convenient to build the list and sort it at the end, or sort it not at all. Add path_list_append() and sort_path_list() to allow that. Also, add the unsorted_path_list_has_path() function, to do a linear search. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Feb 29, 2008 at 01:44 UTC 363d59df1a773039d0c69ad8c3109a56bb1b491d
2 files changed +37 -1
path-list.c
+30
@@ -102,3 +102,33 @@ void print_path_list(const char *text, const struct path_list *p)
102 for (i = 0; i < p->nr; i++)
103 printf("%s:%p\n", p->items[i].path, p->items[i].util);
104 }
105 +
106 +struct path_list_item *path_list_append(const char *path, struct path_list *list)
107 +{
108 + ALLOC_GROW(list->items, list->nr + 1, list->alloc);
109 + list->items[list->nr].path =
110 + list->strdup_paths ? xstrdup(path) : (char *)path;
111 + return list->items + list->nr++;
112 +}
113 +
114 +static int cmp_items(const void *a, const void *b)
115 +{
116 + const struct path_list_item *one = a;
117 + const struct path_list_item *two = b;
118 + return strcmp(one->path, two->path);
119 +}
120 +
121 +void sort_path_list(struct path_list *list)
122 +{
123 + qsort(list->items, list->nr, sizeof(*list->items), cmp_items);
124 +}
125 +
126 +int unsorted_path_list_has_path(struct path_list *list, const char *path)
127 +{
128 + int i;
129 + for (i = 0; i < list->nr; i++)
130 + if (!strcmp(path, list->items[i].path))
131 + return 1;
132 + return 0;
133 +}
134 +
path-list.h
+7 -1
@@ -13,10 +13,16 @@ struct path_list
13 };
14
15 void print_path_list(const char *text, const struct path_list *p);
16 +void path_list_clear(struct path_list *list, int free_util);
17
18 +/* Use these functions only on sorted lists: */
19 int path_list_has_path(const struct path_list *list, const char *path);
18 -void path_list_clear(struct path_list *list, int free_util);
20 struct path_list_item *path_list_insert(const char *path, struct path_list *list);
21 struct path_list_item *path_list_lookup(const char *path, struct path_list *list);
22
23 +/* Use these functions only on unsorted lists: */
24 +struct path_list_item *path_list_append(const char *path, struct path_list *list);
25 +void sort_path_list(struct path_list *list);
26 +int unsorted_path_list_has_path(struct path_list *list, const char *path);
27 +
28 #endif /* PATH_LIST_H */