Browse Source

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>
maint
Johannes Sixt 13 years ago committed by Junio C Hamano
parent
commit
86d4b528d8
  1. 10
      Documentation/technical/api-string-list.txt
  2. 9
      string-list.c
  3. 1
      string-list.h

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

@ -29,6 +29,9 @@ member (you need this if you add things later) and you should set the @@ -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:
@ -112,6 +115,13 @@ write `string_list_insert(...)->util = ...;`. @@ -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
---------------


9
string-list.c

@ -185,3 +185,12 @@ int unsorted_string_list_has_string(struct string_list *list, @@ -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
string-list.h

@ -44,4 +44,5 @@ void sort_string_list(struct string_list *list); @@ -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 */

Loading…
Cancel
Save