summaryrefslogtreecommitdiff
path: root/garray.c
diff options
context:
space:
mode:
authorSebastian Wilhelmi <wilhelmi@ira.uka.de>1998-11-03 14:52:25 +0000
committerSebastian Wilhelmi <wilhelmi@src.gnome.org>1998-11-03 14:52:25 +0000
commit4dbf739ea9b43747f8904b3dcb4dacc95fe2d497 (patch)
treef57b251c6b73afdb9eda3519d14a830244d9953d /garray.c
parent840114ac762ef121aa1aac01d7bee92638ec2655 (diff)
downloadglib-4dbf739ea9b43747f8904b3dcb4dacc95fe2d497.tar.gz
new function for removing an entry from an array while preserving the
1998-11-03 Sebastian Wilhelmi <wilhelmi@ira.uka.de> * glib.h: * garray.h: (g_array_remove_index): new function for removing an entry from an array while preserving the order (g_array_remove_index_fast): new function for removing an entry from an array. the order might be distorted (g_ptr_array_remove_index_fast, g_ptr_array_remove_fast): new functions; working similiar to the above. (they have the semantic of the old g_ptr_array_remove[_index] functions) (g_ptr_array_remove_index, g_ptr_array_remove): new semantic. now the order of the elements in the array is not changed (g_byte_array_remove_index, g_byte_array_remove_index_fast): new functions; byte_array wrapper for g_array_remove_index[_fast]
Diffstat (limited to 'garray.c')
-rw-r--r--garray.c114
1 files changed, 112 insertions, 2 deletions
diff --git a/garray.c b/garray.c
index 4425f1d29..fd5e7b9e2 100644
--- a/garray.c
+++ b/garray.c
@@ -126,6 +126,54 @@ g_array_set_size (GArray *farray,
return farray;
}
+GArray*
+g_array_remove_index (GArray* farray,
+ guint index)
+{
+ GRealArray* array = (GRealArray*) farray;
+
+ g_return_val_if_fail (array, NULL);
+
+ g_return_val_if_fail (index >= 0 && index < array->len, NULL);
+
+ if (index != array->len - 1)
+ g_memmove (array->data + array->elt_size * index,
+ array->data + array->elt_size * (index + 1),
+ array->elt_size * (array->len - index - 1));
+
+ if (array->zero_terminated)
+ memset (array->data + array->elt_size * (array->len - 1), 0,
+ array->elt_size);
+
+ array->len -= 1;
+
+ return farray;
+}
+
+GArray*
+g_array_remove_index_fast (GArray* farray,
+ guint index)
+{
+ GRealArray* array = (GRealArray*) farray;
+
+ g_return_val_if_fail (array, NULL);
+
+ g_return_val_if_fail (index >= 0 && index < array->len, NULL);
+
+ if (index != array->len - 1)
+ g_memmove (array->data + array->elt_size * index,
+ array->data + array->elt_size * (array->len - 1),
+ array->elt_size);
+
+ if (array->zero_terminated)
+ memset (array->data + array->elt_size * (array->len - 1), 0,
+ array->elt_size);
+
+ array->len -= 1;
+
+ return farray;
+}
+
static gint
g_nearest_pow (gint num)
{
@@ -245,7 +293,7 @@ g_ptr_array_set_size (GPtrArray *farray,
gpointer
g_ptr_array_remove_index (GPtrArray* farray,
- gint index)
+ guint index)
{
GRealPtrArray* array = (GRealPtrArray*) farray;
gpointer result;
@@ -255,8 +303,33 @@ g_ptr_array_remove_index (GPtrArray* farray,
g_return_val_if_fail (index >= 0 && index < array->len, NULL);
result = array->pdata[index];
+
+ if (index != array->len - 1)
+ g_memmove (array->pdata + index, array->pdata + index + 1,
+ array->len - index - 1);
+
+ array->pdata[array->len - 1] = NULL;
+
+ array->len -= 1;
+
+ return result;
+}
+
+gpointer
+g_ptr_array_remove_index_fast (GPtrArray* farray,
+ guint index)
+{
+ GRealPtrArray* array = (GRealPtrArray*) farray;
+ gpointer result;
- array->pdata[index] = array->pdata[array->len - 1];
+ g_return_val_if_fail (array, NULL);
+
+ g_return_val_if_fail (index >= 0 && index < array->len, NULL);
+
+ result = array->pdata[index];
+
+ if (index != array->len - 1)
+ array->pdata[index] = array->pdata[array->len - 1];
array->pdata[array->len - 1] = NULL;
@@ -286,6 +359,27 @@ g_ptr_array_remove (GPtrArray* farray,
return FALSE;
}
+gboolean
+g_ptr_array_remove_fast (GPtrArray* farray,
+ gpointer data)
+{
+ GRealPtrArray* array = (GRealPtrArray*) farray;
+ int i;
+
+ g_return_val_if_fail (array, FALSE);
+
+ for (i = 0; i < array->len; i += 1)
+ {
+ if (array->pdata[i] == data)
+ {
+ g_ptr_array_remove_index_fast (farray, i);
+ return TRUE;
+ }
+ }
+
+ return FALSE;
+}
+
void
g_ptr_array_add (GPtrArray* farray,
gpointer data)
@@ -338,3 +432,19 @@ GByteArray* g_byte_array_set_size (GByteArray *array,
return array;
}
+
+GByteArray* g_byte_array_remove_index (GByteArray *array,
+ guint index)
+{
+ g_array_remove_index((GArray*) array, index);
+
+ return array;
+}
+
+GByteArray* g_byte_array_remove_index_fast (GByteArray *array,
+ guint index)
+{
+ g_array_remove_index_fast((GArray*) array, index);
+
+ return array;
+}