[glib] garray: Factor out implementation of g_ptr_array_remove_index*()



commit 2c9a84e5a39fe1e5a392581b95e25c0af830fedf
Author: Philip Withnall <withnall endlessm com>
Date:   Thu Apr 19 14:46:05 2018 +0100

    garray: Factor out implementation of g_ptr_array_remove_index*()
    
    They were almost identically the same. This introduces no functional
    changes, but will help with upcoming additions to GPtrArray.
    
    Signed-off-by: Philip Withnall <withnall endlessm com>
    
    https://bugzilla.gnome.org/show_bug.cgi?id=795376

 glib/garray.c | 71 +++++++++++++++++++++++++----------------------------------
 1 file changed, 30 insertions(+), 41 deletions(-)
---
diff --git a/glib/garray.c b/glib/garray.c
index c54b7cb89..c03e13798 100644
--- a/glib/garray.c
+++ b/glib/garray.c
@@ -1191,23 +1191,12 @@ g_ptr_array_set_size  (GPtrArray *array,
   rarray->len = length;
 }
 
-/**
- * g_ptr_array_remove_index:
- * @array: a #GPtrArray
- * @index_: the index of the pointer to remove
- *
- * Removes the pointer at the given index from the pointer array.
- * The following elements are moved down one place. If @array has
- * a non-%NULL #GDestroyNotify function it is called for the removed
- * element.
- *
- * Returns: the pointer which was removed
- */
-gpointer
-g_ptr_array_remove_index (GPtrArray *array,
-                          guint      index_)
+static gpointer
+ptr_array_remove_index (GPtrArray *array,
+                        guint      index_,
+                        gboolean   fast)
 {
-  GRealPtrArray *rarray = (GRealPtrArray *)array;
+  GRealPtrArray *rarray = (GRealPtrArray *) array;
   gpointer result;
 
   g_return_val_if_fail (rarray, NULL);
@@ -1216,14 +1205,16 @@ g_ptr_array_remove_index (GPtrArray *array,
   g_return_val_if_fail (index_ < rarray->len, NULL);
 
   result = rarray->pdata[index_];
-  
+
   if (rarray->element_free_func != NULL)
     rarray->element_free_func (rarray->pdata[index_]);
 
-  if (index_ != rarray->len - 1)
+  if (index_ != rarray->len - 1 && !fast)
     memmove (rarray->pdata + index_, rarray->pdata + index_ + 1,
              sizeof (gpointer) * (rarray->len - index_ - 1));
-  
+  else if (index_ != rarray->len - 1)
+    rarray->pdata[index_] = rarray->pdata[rarray->len - 1];
+
   rarray->len -= 1;
 
   if (G_UNLIKELY (g_mem_gc_friendly))
@@ -1232,6 +1223,25 @@ g_ptr_array_remove_index (GPtrArray *array,
   return result;
 }
 
+/**
+ * g_ptr_array_remove_index:
+ * @array: a #GPtrArray
+ * @index_: the index of the pointer to remove
+ *
+ * Removes the pointer at the given index from the pointer array.
+ * The following elements are moved down one place. If @array has
+ * a non-%NULL #GDestroyNotify function it is called for the removed
+ * element.
+ *
+ * Returns: the pointer which was removed
+ */
+gpointer
+g_ptr_array_remove_index (GPtrArray *array,
+                          guint      index_)
+{
+  return ptr_array_remove_index (array, index_, FALSE);
+}
+
 /**
  * g_ptr_array_remove_index_fast:
  * @array: a #GPtrArray
@@ -1249,28 +1259,7 @@ gpointer
 g_ptr_array_remove_index_fast (GPtrArray *array,
                                guint      index_)
 {
-  GRealPtrArray *rarray = (GRealPtrArray *)array;
-  gpointer result;
-
-  g_return_val_if_fail (rarray, NULL);
-  g_return_val_if_fail (rarray->len == 0 || (rarray->len != 0 && rarray->pdata != NULL), NULL);
-
-  g_return_val_if_fail (index_ < rarray->len, NULL);
-
-  result = rarray->pdata[index_];
-
-  if (rarray->element_free_func != NULL)
-    rarray->element_free_func (rarray->pdata[index_]);
-
-  if (index_ != rarray->len - 1)
-    rarray->pdata[index_] = rarray->pdata[rarray->len - 1];
-
-  rarray->len -= 1;
-
-  if (G_UNLIKELY (g_mem_gc_friendly))
-    rarray->pdata[rarray->len] = NULL;
-
-  return result;
+  return ptr_array_remove_index (array, index_, TRUE);
 }
 
 /**


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