summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: aacb82d)
raw | patch | inline | side by side (parent: aacb82d)
author | Johannes Sixt <j.sixt@viscovery.net> | |
Fri, 12 Aug 2011 05:20:00 +0000 (23:20 -0600) | ||
committer | Junio C Hamano <gitster@pobox.com> | |
Sun, 14 Aug 2011 21:19:35 +0000 (14:19 -0700) |
Teach the string-list API how to remove an entry in O(1) runtime by
moving the last entry to the vacated spot. As such, the routine works
only for unsorted lists.
Signed-off-by: Johannes Sixt <j6t@kdbg.org>
Signed-off-by: Elijah Newren <newren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
moving the last entry to the vacated spot. As such, the routine works
only for unsorted lists.
Signed-off-by: Johannes Sixt <j6t@kdbg.org>
Signed-off-by: Elijah Newren <newren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/technical/api-string-list.txt | patch | blob | history | |
string-list.c | patch | blob | history | |
string-list.h | patch | blob | history |
diff --git a/Documentation/technical/api-string-list.txt b/Documentation/technical/api-string-list.txt
index 3f575bdcff3a6b38304710b22371784c2e7443c9..ce24eb96f5efdee579f8600323731368fee4048b 100644 (file)
. Can sort an unsorted list using `sort_string_list`.
+. Can remove individual items of an unsorted list using
+ `unsorted_string_list_delete_item`.
+
. Finally it should free the list using `string_list_clear`.
Example:
The above two functions need to look through all items, as opposed to their
counterpart for sorted lists, which performs a binary search.
+`unsorted_string_list_delete_item`::
+
+ Remove an item from a string_list. The `string` pointer of the items
+ will be freed in case the `strdup_strings` member of the string_list
+ is set. The third parameter controls if the `util` pointer of the
+ items should be freed or not.
+
Data structures
---------------
diff --git a/string-list.c b/string-list.c
index 51681189e8380cc43ed26d35526a6dff78f1a24c..d9810aba421cbbc844b45ea5ca713a480a699eb2 100644 (file)
--- a/string-list.c
+++ b/string-list.c
return unsorted_string_list_lookup(list, string) != NULL;
}
+void unsorted_string_list_delete_item(struct string_list *list, int i, int free_util)
+{
+ if (list->strdup_strings)
+ free(list->items[i].string);
+ if (free_util)
+ free(list->items[i].util);
+ list->items[i] = list->items[list->nr-1];
+ list->nr--;
+}
diff --git a/string-list.h b/string-list.h
index bda69839832d2030c15bbd86c4d8d79aa3cc4785..0684cb73bfd27846182479a4e192d682a44f201a 100644 (file)
--- a/string-list.h
+++ b/string-list.h
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);
+void unsorted_string_list_delete_item(struct string_list *list, int i, int free_util);
#endif /* STRING_LIST_H */