Commit 2c9a84e5 authored by Philip Withnall's avatar Philip Withnall

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's avatarPhilip Withnall <withnall@endlessm.com>

https://bugzilla.gnome.org/show_bug.cgi?id=795376
parent 3e96523e
......@@ -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);
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment