Permalink
Browse files

string-list: Add API to remove an item from an unsorted list

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>
  • Loading branch information...
Johannes Sixt authored and gitster committed Aug 12, 2011
1 parent aacb82d commit 86d4b528d8a4752cc689279fb6d38c8697a507bb
Showing with 20 additions and 0 deletions.
  1. +10 −0 Documentation/technical/api-string-list.txt
  2. +9 −0 string-list.c
  3. +1 −0 string-list.h
@@ -29,6 +29,9 @@ member (you need this if you add things later) and you should set the
. Can sort an unsorted list using `sort_string_list`. . 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`. . Finally it should free the list using `string_list_clear`.
Example: Example:
@@ -112,6 +115,13 @@ write `string_list_insert(...)->util = ...;`.
The above two functions need to look through all items, as opposed to their 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.
`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 Data structures
--------------- ---------------
View
@@ -185,3 +185,12 @@ int unsorted_string_list_has_string(struct string_list *list,
return unsorted_string_list_lookup(list, string) != NULL; 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--;
}
View
@@ -44,4 +44,5 @@ 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, struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
const char *string); const char *string);
void unsorted_string_list_delete_item(struct string_list *list, int i, int free_util);
#endif /* STRING_LIST_H */ #endif /* STRING_LIST_H */

0 comments on commit 86d4b52

Please sign in to comment.