[glib] GPtrArray: Added g_ptr_array_insert()



commit 9ed0d0c5096ad1434dfff70503ff6375646d6c8a
Author: Tristan Van Berkom <tristan van berkom gmail com>
Date:   Wed Nov 27 14:34:20 2013 +0900

    GPtrArray: Added g_ptr_array_insert()
    
    Speaks for itself, I've found myself on numerous occasions
    writing my own version of this, or using a GArray of pointers.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=719395

 glib/garray.c |   36 ++++++++++++++++++++++++++++++++++++
 glib/garray.h |    4 ++++
 2 files changed, 40 insertions(+), 0 deletions(-)
---
diff --git a/glib/garray.c b/glib/garray.c
index 062d568..a9c99b2 100644
--- a/glib/garray.c
+++ b/glib/garray.c
@@ -1383,6 +1383,42 @@ g_ptr_array_add (GPtrArray *farray,
 }
 
 /**
+ * g_ptr_array_insert:
+ * @array: a #GPtrArray.
+ * @index_: the index to place the new element at, or -1 to append.
+ * @data: the pointer to add.
+ *
+ * Inserts an element into the pointer array at the given index. The 
+ * array will grow in size automatically if necessary.
+ *
+ * Since: 2.40
+ **/
+void
+g_ptr_array_insert (GPtrArray *farray,
+                    gint       index_,
+                    gpointer   data)
+{
+  GRealPtrArray* array = (GRealPtrArray*) farray;
+
+  g_return_if_fail (array);
+  g_return_if_fail (index_ >= -1);
+  g_return_if_fail (index_ <= (gint)array->len);
+
+  g_ptr_array_maybe_expand (array, 1);
+
+  if (index_ < 0)
+    index_ = array->len;
+
+  if (index_ < array->len)
+    memmove (&(array->pdata[index_ + 1]),
+             &(array->pdata[index_]),
+             (array->len - index_) * sizeof (gpointer));
+
+  array->len++;
+  array->pdata[index_] = data;
+}
+
+/**
  * g_ptr_array_sort:
  * @array: a #GPtrArray.
  * @compare_func: comparison function.
diff --git a/glib/garray.h b/glib/garray.h
index 018436f..7fe8c2f 100644
--- a/glib/garray.h
+++ b/glib/garray.h
@@ -169,6 +169,10 @@ GPtrArray *g_ptr_array_remove_range       (GPtrArray        *array,
 GLIB_AVAILABLE_IN_ALL
 void       g_ptr_array_add                (GPtrArray        *array,
                                           gpointer          data);
+GLIB_AVAILABLE_IN_2_40
+void       g_ptr_array_insert             (GPtrArray        *farray,
+                                           gint              index_,
+                                           gpointer          data);
 GLIB_AVAILABLE_IN_ALL
 void       g_ptr_array_sort               (GPtrArray        *array,
                                           GCompareFunc      compare_func);


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