From 971af34af94f20584d2613a6fac4c080ea3676ad Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Sat, 26 Jul 2003 08:03:16 +0000 Subject: [PATCH] New functions to remove a range of elements from an array. (#94879, Nalin 2003-07-26 Matthias Clasen * glib/garray.h: * glib/garray.c (g_{,byte,pointer}_remove_range): New functions to remove a range of elements from an array. (#94879, Nalin Dahyabhai) --- ChangeLog | 4 ++ ChangeLog.pre-2-10 | 4 ++ ChangeLog.pre-2-12 | 4 ++ ChangeLog.pre-2-4 | 4 ++ ChangeLog.pre-2-6 | 4 ++ ChangeLog.pre-2-8 | 4 ++ docs/reference/ChangeLog | 5 ++ docs/reference/glib/glib-sections.txt | 3 + docs/reference/glib/tmpl/arrays.sgml | 15 ++++- docs/reference/glib/tmpl/arrays_byte.sgml | 15 ++++- docs/reference/glib/tmpl/arrays_pointer.sgml | 14 ++++- glib/garray.c | 62 +++++++++++++++++++- glib/garray.h | 9 +++ 13 files changed, 143 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index c6e1ce3bb..83b3d8f66 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,9 @@ 2003-07-26 Matthias Clasen + * glib/garray.h: + * glib/garray.c (g_{,byte,pointer}_remove_range): New functions to remove a range of elements + from an array. (#94879, Nalin Dahyabhai) + * glib/gmessages.c (g_logv): Remove the 1024 char limit in the common (non-recursive) case. diff --git a/ChangeLog.pre-2-10 b/ChangeLog.pre-2-10 index c6e1ce3bb..83b3d8f66 100644 --- a/ChangeLog.pre-2-10 +++ b/ChangeLog.pre-2-10 @@ -1,5 +1,9 @@ 2003-07-26 Matthias Clasen + * glib/garray.h: + * glib/garray.c (g_{,byte,pointer}_remove_range): New functions to remove a range of elements + from an array. (#94879, Nalin Dahyabhai) + * glib/gmessages.c (g_logv): Remove the 1024 char limit in the common (non-recursive) case. diff --git a/ChangeLog.pre-2-12 b/ChangeLog.pre-2-12 index c6e1ce3bb..83b3d8f66 100644 --- a/ChangeLog.pre-2-12 +++ b/ChangeLog.pre-2-12 @@ -1,5 +1,9 @@ 2003-07-26 Matthias Clasen + * glib/garray.h: + * glib/garray.c (g_{,byte,pointer}_remove_range): New functions to remove a range of elements + from an array. (#94879, Nalin Dahyabhai) + * glib/gmessages.c (g_logv): Remove the 1024 char limit in the common (non-recursive) case. diff --git a/ChangeLog.pre-2-4 b/ChangeLog.pre-2-4 index c6e1ce3bb..83b3d8f66 100644 --- a/ChangeLog.pre-2-4 +++ b/ChangeLog.pre-2-4 @@ -1,5 +1,9 @@ 2003-07-26 Matthias Clasen + * glib/garray.h: + * glib/garray.c (g_{,byte,pointer}_remove_range): New functions to remove a range of elements + from an array. (#94879, Nalin Dahyabhai) + * glib/gmessages.c (g_logv): Remove the 1024 char limit in the common (non-recursive) case. diff --git a/ChangeLog.pre-2-6 b/ChangeLog.pre-2-6 index c6e1ce3bb..83b3d8f66 100644 --- a/ChangeLog.pre-2-6 +++ b/ChangeLog.pre-2-6 @@ -1,5 +1,9 @@ 2003-07-26 Matthias Clasen + * glib/garray.h: + * glib/garray.c (g_{,byte,pointer}_remove_range): New functions to remove a range of elements + from an array. (#94879, Nalin Dahyabhai) + * glib/gmessages.c (g_logv): Remove the 1024 char limit in the common (non-recursive) case. diff --git a/ChangeLog.pre-2-8 b/ChangeLog.pre-2-8 index c6e1ce3bb..83b3d8f66 100644 --- a/ChangeLog.pre-2-8 +++ b/ChangeLog.pre-2-8 @@ -1,5 +1,9 @@ 2003-07-26 Matthias Clasen + * glib/garray.h: + * glib/garray.c (g_{,byte,pointer}_remove_range): New functions to remove a range of elements + from an array. (#94879, Nalin Dahyabhai) + * glib/gmessages.c (g_logv): Remove the 1024 char limit in the common (non-recursive) case. diff --git a/docs/reference/ChangeLog b/docs/reference/ChangeLog index bde27d214..aa8474e02 100644 --- a/docs/reference/ChangeLog +++ b/docs/reference/ChangeLog @@ -1,5 +1,10 @@ 2003-07-26 Matthias Clasen + * glib/tmpl/arrays.sgml: + * glib/tmpl/arrays_byte.sgml: + * glib/tmpl/arrays_pointer.sgml: + * glib/glib-sections.txt: Add g_{,byte,pointer}_array_remove_range. (#94879, Nalin Dahyabhai) + * glib/tmpl/messages.sgml: Remove the note about the message length restriction. 2003-07-24 Matthias Clasen diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt index b50e218fb..06ec35254 100644 --- a/docs/reference/glib/glib-sections.txt +++ b/docs/reference/glib/glib-sections.txt @@ -1588,6 +1588,7 @@ g_array_insert_val g_array_insert_vals g_array_remove_index g_array_remove_index_fast +g_array_remove_range g_array_sort g_array_sort_with_data g_array_index @@ -1606,6 +1607,7 @@ g_ptr_array_remove g_ptr_array_remove_index g_ptr_array_remove_fast g_ptr_array_remove_index_fast +g_ptr_array_remove_range g_ptr_array_sort g_ptr_array_sort_with_data g_ptr_array_set_size @@ -1624,6 +1626,7 @@ g_byte_array_append g_byte_array_prepend g_byte_array_remove_index g_byte_array_remove_index_fast +g_byte_array_remove_range g_byte_array_sort g_byte_array_sort_with_data g_byte_array_set_size diff --git a/docs/reference/glib/tmpl/arrays.sgml b/docs/reference/glib/tmpl/arrays.sgml index 4fefede5c..57ad5f936 100644 --- a/docs/reference/glib/tmpl/arrays.sgml +++ b/docs/reference/glib/tmpl/arrays.sgml @@ -212,9 +212,22 @@ g_array_remove_index(). @Returns: the #GArray. + + +Removes the given number of elements starting at the given index from a +#GArray. The following elements are moved to close the gap. + + +@array: a @GArray. +@index_: the index of the first element to remove. +@length: the number of elements to remove. +@Returns: the #GArray. +@Since: 2.4 + + -Sorts a #GArray using @compare_func which should be a qsort()-style comparison +Sorts a #GArray using @compare_func which should be a qsort()-style comparison function (returns -1 for first arg is less than second arg, 0 for equal, 1 if first arg is greater than second arg). diff --git a/docs/reference/glib/tmpl/arrays_byte.sgml b/docs/reference/glib/tmpl/arrays_byte.sgml index f82d3ff86..92e0abc53 100644 --- a/docs/reference/glib/tmpl/arrays_byte.sgml +++ b/docs/reference/glib/tmpl/arrays_byte.sgml @@ -125,9 +125,22 @@ g_byte_array_remove_index(). @Returns: the #GByteArray. + + +Removes the given number of bytes starting at the given index from a +#GByteArray. The following elements are moved to close the gap. + + +@array: a @GByteArray. +@index_: the index of the first byte to remove. +@length: the number of bytes to remove. +@Returns: the #GByteArray. +@Since: 2.4 + + -Sorts a byte array, using @compare_func which should be a qsort()-style +Sorts a byte array, using @compare_func which should be a qsort()-style comparison function (returns -1 for first arg is less than second arg, 0 for equal, 1 if first arg is greater than second arg). diff --git a/docs/reference/glib/tmpl/arrays_pointer.sgml b/docs/reference/glib/tmpl/arrays_pointer.sgml index e609b4405..c67c253e4 100644 --- a/docs/reference/glib/tmpl/arrays_pointer.sgml +++ b/docs/reference/glib/tmpl/arrays_pointer.sgml @@ -155,9 +155,21 @@ g_ptr_array_remove_index(). @Returns: the pointer which was removed. + + +Removes the given number of bytes starting at the given index from a +#GPtrArray. The following elements are moved to close the gap. + + +@array: a @GPtrArray. +@index_: the index of the first pointer to remove. +@length: the number of pointers to remove. +@Since: 2.4 + + -Sorts the array, using @compare_func which should be a qsort()-style comparison +Sorts the array, using @compare_func which should be a qsort()-style comparison function (returns -1 for first arg is less than second arg, 0 for equal, 1 if first arg is greater than second arg). diff --git a/glib/garray.c b/glib/garray.c index 02e3d31c4..c7c6dacaf 100644 --- a/glib/garray.c +++ b/glib/garray.c @@ -267,6 +267,32 @@ g_array_remove_index_fast (GArray* farray, return farray; } +GArray* +g_array_remove_range (GArray *farray, + guint index_, + guint length) +{ + GRealArray *array = (GRealArray*) farray; + + g_return_val_if_fail (array, NULL); + g_return_val_if_fail (index_ < array->len, NULL); + g_return_val_if_fail (index_ + length <= array->len, NULL); + + if (index_ + length != array->len) + g_memmove (g_array_elt_pos (array, index_), + g_array_elt_pos (array, index_ + length), + (array->len - (index_ + length)) * array->elt_size); + + array->len -= length; +#ifdef ENABLE_GC_FRIENDLY + g_array_elt_zero (array, array->len, length); +#else /* !ENABLE_GC_FRIENDLY */ + g_array_zero_terminate (array); +#endif /* ENABLE_GC_FRIENDLY */ + + return farray; +} + void g_array_sort (GArray *farray, GCompareFunc compare_func) @@ -414,7 +440,7 @@ g_ptr_array_maybe_expand (GRealPtrArray *array, #endif /* ENABLE_GC_FRIENDLY */ array->alloc = g_nearest_pow (array->len + len); array->alloc = MAX (array->alloc, MIN_ARRAY_SIZE); - array->pdata = g_realloc (array->pdata, sizeof(gpointer) * array->alloc); + array->pdata = g_realloc (array->pdata, sizeof (gpointer) * array->alloc); #ifdef ENABLE_GC_FRIENDLY for ( ; old_alloc < array->alloc; old_alloc++) array->pdata [old_alloc] = NULL; @@ -506,6 +532,28 @@ g_ptr_array_remove_index_fast (GPtrArray* farray, return result; } +void +g_ptr_array_remove_range (GPtrArray* farray, + guint index_, + guint length) +{ + GRealPtrArray* array = (GRealPtrArray*) farray; + + g_return_if_fail (array); + g_return_if_fail (index_ < array->len); + g_return_if_fail (index_ + length <= array->len); + + if (index_ + length != array->len) + g_memmove (&array->pdata[index_], + &array->pdata[index_ + length], + (array->len - (index_ + length)) * sizeof (gpointer)); + + array->len -= length; +#ifdef ENABLE_GC_FRIENDLY + g_array_elt_zero (array->pdata, array->len, length); +#endif /* ENABLE_GC_FRIENDLY */ +} + gboolean g_ptr_array_remove (GPtrArray* farray, gpointer data) @@ -648,6 +696,18 @@ GByteArray* g_byte_array_remove_index_fast (GByteArray *array, return array; } +GByteArray* +g_byte_array_remove_range (GByteArray *array, + guint index_, + guint length) +{ + g_return_val_if_fail (array, FALSE); + g_return_val_if_fail (index_ < array->len, FALSE); + g_return_val_if_fail (index_ + length <= array->len, FALSE); + + return g_array_remove_range ((GArray*) array, index_, length); +} + void g_byte_array_sort (GByteArray *array, GCompareFunc compare_func) diff --git a/glib/garray.h b/glib/garray.h index c29d15260..b35d99ae7 100644 --- a/glib/garray.h +++ b/glib/garray.h @@ -88,6 +88,9 @@ GArray* g_array_remove_index (GArray *array, guint index_); GArray* g_array_remove_index_fast (GArray *array, guint index_); +GArray* g_array_remove_range (GArray *array, + guint index_, + guint length); void g_array_sort (GArray *array, GCompareFunc compare_func); void g_array_sort_with_data (GArray *array, @@ -113,6 +116,9 @@ gboolean g_ptr_array_remove (GPtrArray *array, gpointer data); gboolean g_ptr_array_remove_fast (GPtrArray *array, gpointer data); +void g_ptr_array_remove_range (GPtrArray *array, + guint index_, + guint length); void g_ptr_array_add (GPtrArray *array, gpointer data); void g_ptr_array_sort (GPtrArray *array, @@ -142,6 +148,9 @@ GByteArray* g_byte_array_remove_index (GByteArray *array, guint index_); GByteArray* g_byte_array_remove_index_fast (GByteArray *array, guint index_); +GByteArray* g_byte_array_remove_range (GByteArray *array, + guint index_, + guint length); void g_byte_array_sort (GByteArray *array, GCompareFunc compare_func); void g_byte_array_sort_with_data (GByteArray *array,