diff options
author | Philip Withnall <withnall@endlessm.com> | 2018-04-19 14:46:05 +0100 |
---|---|---|
committer | Philip Withnall <withnall@endlessm.com> | 2018-05-09 13:52:05 +0100 |
commit | 2c9a84e5a39fe1e5a392581b95e25c0af830fedf (patch) | |
tree | 6f81b08211b81144330c2fff86310323d07b0bb1 | |
parent | 3e96523e6b73c4d0a11a023a7fbae01ac0d09335 (diff) | |
download | glib-2c9a84e5a39fe1e5a392581b95e25c0af830fedf.tar.gz |
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
-rw-r--r-- | glib/garray.c | 71 |
1 files 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)) @@ -1233,6 +1224,25 @@ g_ptr_array_remove_index (GPtrArray *array, } /** + * 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 * @index_: the index of the pointer to remove @@ -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); } /** |