[glib/wip/chergert/garraylist] arraylist: add O(1) access to last GList link



commit 0e9b60ce34e6d06901c812b56dd6b6180d4b98ff
Author: Christian Hergert <christian hergert me>
Date:   Sun Sep 13 03:35:58 2015 -0700

    arraylist: add O(1) access to last GList link
    
    This helps during reverse walking a GList via compatability API.

 glib/garraylist.c      |   23 ++++++++++++++++++++++-
 glib/garraylist.h      |    3 +++
 glib/tests/arraylist.c |    3 +++
 3 files changed, 28 insertions(+), 1 deletions(-)
---
diff --git a/glib/garraylist.c b/glib/garraylist.c
index 51086ff..a084622 100644
--- a/glib/garraylist.c
+++ b/glib/garraylist.c
@@ -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];
+}
diff --git a/glib/garraylist.h b/glib/garraylist.h
index caf45c3..4483855 100644
--- a/glib/garraylist.h
+++ b/glib/garraylist.h
@@ -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))
 
diff --git a/glib/tests/arraylist.c b/glib/tests/arraylist.c
index 79ab3c5..a716dc6 100644
--- a/glib/tests/arraylist.c
+++ b/glib/tests/arraylist.c
@@ -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)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]