[glib/wip/chergert/garraylist] arraylist: add g_array_list_clear()



commit 7fdc32230b518929f8271bab3c9796fb1ea828b5
Author: Christian Hergert <christian hergert me>
Date:   Sun Sep 13 04:06:13 2015 -0700

    arraylist: add g_array_list_clear()
    
    Add helper to clear the array list quickly.

 glib/garraylist.c      |   27 +++++++++++++++++++++++++++
 glib/garraylist.h      |    4 ++++
 glib/tests/arraylist.c |    5 ++++-
 3 files changed, 35 insertions(+), 1 deletions(-)
---
diff --git a/glib/garraylist.c b/glib/garraylist.c
index 945666f..e177c9a 100644
--- a/glib/garraylist.c
+++ b/glib/garraylist.c
@@ -500,3 +500,30 @@ g_array_list_last_link (GArrayList *self)
 
   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;
+}
diff --git a/glib/garraylist.h b/glib/garraylist.h
index cd2b5f6..af0b234 100644
--- a/glib/garraylist.h
+++ b/glib/garraylist.h
@@ -78,6 +78,10 @@ GLIB_AVAILABLE_IN_2_46
 void         g_array_list_prepend      (GArrayList     *list,
                                         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_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 328088d..d8ef39a 100644
--- a/glib/tests/arraylist.c
+++ b/glib/tests/arraylist.c
@@ -77,9 +77,12 @@ test_basic (GArrayList *al)
   g_array_list_prepend (al, GSIZE_TO_POINTER (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_array_list_destroy (al);
+
   test_basic_counter = 0;
 }
 


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