arraylist: add g_array_list_clear()

Add helper to clear the array list quickly.
This commit is contained in:
Christian Hergert 2015-09-13 04:06:13 -07:00
parent 87dee43c42
commit 7fdc32230b
3 changed files with 35 additions and 1 deletions

View File

@ -500,3 +500,30 @@ g_array_list_last_link (GArrayList *self)
return &items [self->len - 1]; return &items [self->len - 1];
} }
void
g_array_list_clear (GArrayList *self)
{
GArrayListAny *any = (GArrayListAny *)self;
GArrayListEmbed *embed = (GArrayListEmbed *)self;
GArrayListAlloc *alloc = (GArrayListAlloc *)self;
g_return_if_fail (self != NULL);
if (any->destroy != NULL)
{
GList *items;
gsize i;
items = (any->mode == MODE_EMBED) ? embed->items : alloc->items;
for (i = 0; i < any->len; i++)
any->destroy (items [i].data);
}
if (any->mode == MODE_ALLOC)
g_free (alloc->items);
any->len = 0;
any->mode = MODE_EMBED;
}

View File

@ -78,6 +78,10 @@ GLIB_AVAILABLE_IN_2_46
void g_array_list_prepend (GArrayList *list, void g_array_list_prepend (GArrayList *list,
gpointer data); gpointer data);
GLIB_AVAILABLE_IN_2_46
void g_array_list_clear (GArrayList *list);
#define g_array_list_empty(list) ((list)->len == 0)
#define g_array_list_first(list) (((list)->len == 0) ? NULL : g_array_list_index((list),0)) #define g_array_list_first(list) (((list)->len == 0) ? NULL : g_array_list_index((list),0))
#define g_array_list_last(list) (((list)->len == 0) ? NULL : g_array_list_index((list),(list)->len-1)) #define g_array_list_last(list) (((list)->len == 0) ? NULL : g_array_list_index((list),(list)->len-1))

View File

@ -77,9 +77,12 @@ test_basic (GArrayList *al)
g_array_list_prepend (al, GSIZE_TO_POINTER (191919)); g_array_list_prepend (al, GSIZE_TO_POINTER (191919));
g_assert_cmpint (GPOINTER_TO_SIZE (g_array_list_index (al, 0)), ==, 191919); g_assert_cmpint (GPOINTER_TO_SIZE (g_array_list_index (al, 0)), ==, 191919);
g_array_list_destroy (al); g_array_list_clear (al);
g_assert_cmpint (al->len, ==, 0);
g_assert_cmpint (test_basic_counter, ==, 1000); g_assert_cmpint (test_basic_counter, ==, 1000);
g_array_list_destroy (al);
test_basic_counter = 0; test_basic_counter = 0;
} }