arraylist: add O(1) access to last GList link

This helps during reverse walking a GList via compatability API.
This commit is contained in:
Christian Hergert 2015-09-13 03:35:58 -07:00
parent 2f282b9577
commit 0e9b60ce34
3 changed files with 28 additions and 1 deletions

View File

@ -165,7 +165,7 @@ g_array_list_peek (GArrayList *self)
gpointer
g_array_list_index (GArrayList *self,
guint index)
gsize index)
{
GArrayListAny *any = (GArrayListAny *)self;
GArrayListEmbed *embed = (GArrayListEmbed *)self;
@ -426,3 +426,24 @@ g_array_list_destroy (GArrayList *self)
if (any->on_heap)
g_slice_free (GArrayListAny, any);
}
const GList *
g_array_list_last_link (GArrayList *self)
{
GArrayListAny *any = (GArrayListAny *)self;
GArrayListEmbed *embed = (GArrayListEmbed *)self;
GArrayListAlloc *alloc = (GArrayListAlloc *)self;
GList *items;
g_return_val_if_fail (self != NULL, NULL);
if (self->len == 0)
return NULL;
if (any->mode == MODE_EMBED)
items = embed->items;
else
items = alloc->items;
return &items [self->len - 1];
}

View File

@ -71,6 +71,9 @@ void g_array_list_remove_index (GArrayList *list,
GLIB_AVAILABLE_IN_2_46
void g_array_list_destroy (GArrayList *list);
GLIB_AVAILABLE_IN_2_46
const GList *g_array_list_last_link (GArrayList *list);
#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))

View File

@ -50,6 +50,9 @@ test_basic (GArrayList *al)
g_assert_cmpint (GPOINTER_TO_SIZE (g_array_list_first(al)), ==, 1);
g_assert_cmpint (GPOINTER_TO_SIZE (g_array_list_last(al)), ==, 1000);
iter = g_array_list_last_link (al);
g_assert_cmpint (GPOINTER_TO_SIZE (iter->data), ==, 1000);
list = g_array_list_peek (al);
for (iter = list; iter; iter = iter->next)