Skip to content

Commit

Permalink
string-list: Add API to remove an item from an unsorted list
Browse files Browse the repository at this point in the history
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 Junio C Hamano committed Aug 14, 2011
1 parent aacb82d commit 86d4b52
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Documentation/technical/api-string-list.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 remove individual items of an unsorted list using
`unsorted_string_list_delete_item`.

. Finally it should free the list using `string_list_clear`.

Example:
Expand Down Expand Up @@ -112,6 +115,13 @@ write `string_list_insert(...)->util = ...;`.
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
---------------

Expand Down
9 changes: 9 additions & 0 deletions string-list.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,12 @@ int unsorted_string_list_has_string(struct string_list *list,
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--;
}
1 change: 1 addition & 0 deletions string-list.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
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 */

0 comments on commit 86d4b52

Please sign in to comment.