1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
40 #include "gmessages.h"
47 * @short_description: arrays of arbitrary elements which grow
48 * automatically as elements are added
50 * Arrays are similar to standard C arrays, except that they grow
51 * automatically as elements are added.
53 * Array elements can be of any size (though all elements of one array
54 * are the same size), and the array can be automatically cleared to
55 * '0's and zero-terminated.
57 * To create a new array use g_array_new().
59 * To add elements to an array, use g_array_append_val(),
60 * g_array_append_vals(), g_array_prepend_val(), and
61 * g_array_prepend_vals().
63 * To access an element of an array, use g_array_index().
65 * To set the size of an array, use g_array_set_size().
67 * To free an array, use g_array_free().
70 * <title>Using a #GArray to store #gint values</title>
74 * /<!-- -->* We create a new array to store gint values.
75 * We don't want it zero-terminated or cleared to 0's. *<!-- -->/
76 * garray = g_array_new (FALSE, FALSE, sizeof (gint));
77 * for (i = 0; i < 10000; i++)
78 * g_array_append_val (garray, i);
79 * for (i = 0; i < 10000; i++)
80 * if (g_array_index (garray, gint, i) != i)
81 * g_print ("ERROR: got %d instead of %d\n",
82 * g_array_index (garray, gint, i), i);
83 * g_array_free (garray, TRUE);
88 #define MIN_ARRAY_SIZE 16
90 typedef struct _GRealArray GRealArray;
94 * @data: a pointer to the element data. The data may be moved as
95 * elements are added to the #GArray.
96 * @len: the number of elements in the #GArray not including the
97 * possible terminating zero element.
99 * Contains the public fields of an <link
100 * linkend="glib-arrays">Array</link>.
108 guint zero_terminated : 1;
116 * @t: the type of the elements.
117 * @i: the index of the element to return.
118 * @Returns: the element of the #GArray at the index given by @i.
120 * Returns the element of a #GArray at the given index. The return
121 * value is cast to the given type.
124 * <title>Getting a pointer to an element in a #GArray</title>
126 * EDayViewEvent *event;
127 * /<!-- -->* This gets a pointer to the 4th element
128 * in the array of EDayViewEvent structs. *<!-- -->/
129 * event = &g_array_index (events, EDayViewEvent, 3);
134 #define g_array_elt_len(array,i) ((array)->elt_size * (i))
135 #define g_array_elt_pos(array,i) ((array)->data + g_array_elt_len((array),(i)))
136 #define g_array_elt_zero(array, pos, len) \
137 (memset (g_array_elt_pos ((array), pos), 0, g_array_elt_len ((array), len)))
138 #define g_array_zero_terminate(array) G_STMT_START{ \
139 if ((array)->zero_terminated) \
140 g_array_elt_zero ((array), (array)->len, 1); \
143 static guint g_nearest_pow (gint num) G_GNUC_CONST;
144 static void g_array_maybe_expand (GRealArray *array,
149 * @zero_terminated: %TRUE if the array should have an extra element at
150 * the end which is set to 0.
151 * @clear_: %TRUE if #GArray elements should be automatically cleared
152 * to 0 when they are allocated.
153 * @element_size: the size of each element in bytes.
154 * @Returns: the new #GArray.
156 * Creates a new #GArray with a reference count of 1.
159 g_array_new (gboolean zero_terminated,
163 return (GArray*) g_array_sized_new (zero_terminated, clear, elt_size, 0);
168 * @zero_terminated: %TRUE if the array should have an extra element at
169 * the end with all bits cleared.
170 * @clear_: %TRUE if all bits in the array should be cleared to 0 on
172 * @element_size: size of each element in the array.
173 * @reserved_size: number of elements preallocated.
174 * @Returns: the new #GArray.
176 * Creates a new #GArray with @reserved_size elements preallocated and
177 * a reference count of 1. This avoids frequent reallocation, if you
178 * are going to add many elements to the array. Note however that the
179 * size of the array is still 0.
181 GArray* g_array_sized_new (gboolean zero_terminated,
186 GRealArray *array = g_slice_new (GRealArray);
191 array->zero_terminated = (zero_terminated ? 1 : 0);
192 array->clear = (clear ? 1 : 0);
193 array->elt_size = elt_size;
194 array->ref_count = 1;
196 if (array->zero_terminated || reserved_size != 0)
198 g_array_maybe_expand (array, reserved_size);
199 g_array_zero_terminate(array);
202 return (GArray*) array;
209 * Atomically increments the reference count of @array by one. This
210 * function is MT-safe and may be called from any thread.
212 * Returns: The passed in #GArray.
217 g_array_ref (GArray *array)
219 GRealArray *rarray = (GRealArray*) array;
220 g_return_val_if_fail (array, NULL);
222 g_atomic_int_inc (&rarray->ref_count);
231 * Atomically decrements the reference count of @array by one. If the
232 * reference count drops to 0, all memory allocated by the array is
233 * released. This function is MT-safe and may be called from any
239 g_array_unref (GArray *array)
241 GRealArray *rarray = (GRealArray*) array;
242 g_return_if_fail (array);
244 if (g_atomic_int_dec_and_test (&rarray->ref_count))
245 g_array_free (array, TRUE);
249 * g_array_get_element_size:
252 * Gets the size of the elements in @array.
254 * Returns: Size of each element, in bytes.
259 g_array_get_element_size (GArray *array)
261 GRealArray *rarray = (GRealArray*) array;
263 g_return_val_if_fail (array, 0);
265 return rarray->elt_size;
271 * @free_segment: if %TRUE the actual element data is freed as well.
272 * @Returns: the element data if @free_segment is %FALSE, otherwise
273 * %NULL. The element data should be freed using g_free().
275 * Frees the memory allocated for the #GArray. If @free_segment is
276 * %TRUE it frees the memory block holding the elements as well and
277 * also each element if @array has a @element_free_func set. Pass
278 * %FALSE if you want to free the #GArray wrapper but preserve the
279 * underlying array for use elsewhere. If the reference count of @array
280 * is greater than one, the #GArray wrapper is preserved but the size
281 * of @array will be set to zero.
283 * <note><para>If array elements contain dynamically-allocated memory,
284 * they should be freed separately.</para></note>
287 g_array_free (GArray *farray,
288 gboolean free_segment)
290 GRealArray *array = (GRealArray*) farray;
292 gboolean preserve_wrapper;
294 g_return_val_if_fail (array, NULL);
296 /* if others are holding a reference, preserve the wrapper but do free/return the data */
297 preserve_wrapper = FALSE;
298 if (g_atomic_int_get (&array->ref_count) > 1)
299 preserve_wrapper = TRUE;
303 g_free (array->data);
307 segment = (gchar*) array->data;
309 if (preserve_wrapper)
317 g_slice_free1 (sizeof (GRealArray), array);
324 * g_array_append_vals:
326 * @data: a pointer to the elements to append to the end of the array.
327 * @len: the number of elements to append.
328 * @Returns: the #GArray.
330 * Adds @len elements onto the end of the array.
333 * g_array_append_val:
335 * @v: the value to append to the #GArray.
336 * @Returns: the #GArray.
338 * Adds the value on to the end of the array. The array will grow in
339 * size automatically if necessary.
341 * <note><para>g_array_append_val() is a macro which uses a reference
342 * to the value parameter @v. This means that you cannot use it with
343 * literal values such as "27". You must use variables.</para></note>
346 g_array_append_vals (GArray *farray,
350 GRealArray *array = (GRealArray*) farray;
352 g_return_val_if_fail (array, NULL);
354 g_array_maybe_expand (array, len);
356 memcpy (g_array_elt_pos (array, array->len), data,
357 g_array_elt_len (array, len));
361 g_array_zero_terminate (array);
367 * g_array_prepend_vals:
369 * @data: a pointer to the elements to prepend to the start of the
371 * @len: the number of elements to prepend.
372 * @Returns: the #GArray.
374 * Adds @len elements onto the start of the array.
376 * This operation is slower than g_array_append_vals() since the
377 * existing elements in the array have to be moved to make space for
381 * g_array_prepend_val:
383 * @v: the value to prepend to the #GArray.
384 * @Returns: the #GArray.
386 * Adds the value on to the start of the array. The array will grow in
387 * size automatically if necessary.
389 * This operation is slower than g_array_append_val() since the
390 * existing elements in the array have to be moved to make space for
393 * <note><para>g_array_prepend_val() is a macro which uses a reference
394 * to the value parameter @v. This means that you cannot use it with
395 * literal values such as "27". You must use variables.</para></note>
398 g_array_prepend_vals (GArray *farray,
402 GRealArray *array = (GRealArray*) farray;
404 g_return_val_if_fail (array, NULL);
406 g_array_maybe_expand (array, len);
408 g_memmove (g_array_elt_pos (array, len), g_array_elt_pos (array, 0),
409 g_array_elt_len (array, array->len));
411 memcpy (g_array_elt_pos (array, 0), data, g_array_elt_len (array, len));
415 g_array_zero_terminate (array);
421 * g_array_insert_vals:
423 * @index_: the index to place the elements at.
424 * @data: a pointer to the elements to insert.
425 * @len: the number of elements to insert.
426 * @Returns: the #GArray.
428 * Inserts @len elements into a #GArray at the given index.
431 * g_array_insert_val:
433 * @i: the index to place the element at.
434 * @v: the value to insert into the array.
435 * @Returns: the #GArray.
437 * Inserts an element into an array at the given index.
439 * <note><para>g_array_insert_val() is a macro which uses a reference
440 * to the value parameter @v. This means that you cannot use it with
441 * literal values such as "27". You must use variables.</para></note>
444 g_array_insert_vals (GArray *farray,
449 GRealArray *array = (GRealArray*) farray;
451 g_return_val_if_fail (array, NULL);
453 g_array_maybe_expand (array, len);
455 g_memmove (g_array_elt_pos (array, len + index_),
456 g_array_elt_pos (array, index_),
457 g_array_elt_len (array, array->len - index_));
459 memcpy (g_array_elt_pos (array, index_), data, g_array_elt_len (array, len));
463 g_array_zero_terminate (array);
471 * @length: the new size of the #GArray.
472 * @Returns: the #GArray.
474 * Sets the size of the array, expanding it if necessary. If the array
475 * was created with @clear_ set to %TRUE, the new elements are set to 0.
478 g_array_set_size (GArray *farray,
481 GRealArray *array = (GRealArray*) farray;
483 g_return_val_if_fail (array, NULL);
485 if (length > array->len)
487 g_array_maybe_expand (array, length - array->len);
490 g_array_elt_zero (array, array->len, length - array->len);
492 else if (G_UNLIKELY (g_mem_gc_friendly) && length < array->len)
493 g_array_elt_zero (array, length, array->len - length);
497 g_array_zero_terminate (array);
503 * g_array_remove_index:
505 * @index_: the index of the element to remove.
506 * @Returns: the #GArray.
508 * Removes the element at the given index from a #GArray. The following
509 * elements are moved down one place.
512 g_array_remove_index (GArray *farray,
515 GRealArray* array = (GRealArray*) farray;
517 g_return_val_if_fail (array, NULL);
519 g_return_val_if_fail (index_ < array->len, NULL);
521 if (index_ != array->len - 1)
522 g_memmove (g_array_elt_pos (array, index_),
523 g_array_elt_pos (array, index_ + 1),
524 g_array_elt_len (array, array->len - index_ - 1));
528 if (G_UNLIKELY (g_mem_gc_friendly))
529 g_array_elt_zero (array, array->len, 1);
531 g_array_zero_terminate (array);
537 * g_array_remove_index_fast:
539 * @index_: the index of the element to remove.
540 * @Returns: the #GArray.
542 * Removes the element at the given index from a #GArray. The last
543 * element in the array is used to fill in the space, so this function
544 * does not preserve the order of the #GArray. But it is faster than
545 * g_array_remove_index().
548 g_array_remove_index_fast (GArray *farray,
551 GRealArray* array = (GRealArray*) farray;
553 g_return_val_if_fail (array, NULL);
555 g_return_val_if_fail (index_ < array->len, NULL);
557 if (index_ != array->len - 1)
558 memcpy (g_array_elt_pos (array, index_),
559 g_array_elt_pos (array, array->len - 1),
560 g_array_elt_len (array, 1));
564 if (G_UNLIKELY (g_mem_gc_friendly))
565 g_array_elt_zero (array, array->len, 1);
567 g_array_zero_terminate (array);
573 * g_array_remove_range:
575 * @index_: the index of the first element to remove.
576 * @length: the number of elements to remove.
577 * @Returns: the #GArray.
579 * Removes the given number of elements starting at the given index
580 * from a #GArray. The following elements are moved to close the gap.
585 g_array_remove_range (GArray *farray,
589 GRealArray *array = (GRealArray*) farray;
591 g_return_val_if_fail (array, NULL);
592 g_return_val_if_fail (index_ < array->len, NULL);
593 g_return_val_if_fail (index_ + length <= array->len, NULL);
595 if (index_ + length != array->len)
596 g_memmove (g_array_elt_pos (array, index_),
597 g_array_elt_pos (array, index_ + length),
598 (array->len - (index_ + length)) * array->elt_size);
600 array->len -= length;
601 if (G_UNLIKELY (g_mem_gc_friendly))
602 g_array_elt_zero (array, array->len, length);
604 g_array_zero_terminate (array);
612 * @compare_func: comparison function.
614 * Sorts a #GArray using @compare_func which should be a qsort()-style
615 * comparison function (returns less than zero for first arg is less
616 * than second arg, zero for equal, greater zero if first arg is
617 * greater than second arg).
619 * If two array elements compare equal, their order in the sorted array
620 * is undefined. If you want equal elements to keep their order – i.e.
621 * you want a stable sort – you can write a comparison function that,
622 * if two elements would otherwise compare equal, compares them by
626 g_array_sort (GArray *farray,
627 GCompareFunc compare_func)
629 GRealArray *array = (GRealArray*) farray;
631 g_return_if_fail (array != NULL);
640 * g_array_sort_with_data:
642 * @compare_func: comparison function.
643 * @user_data: data to pass to @compare_func.
645 * Like g_array_sort(), but the comparison function receives an extra
646 * user data argument.
649 g_array_sort_with_data (GArray *farray,
650 GCompareDataFunc compare_func,
653 GRealArray *array = (GRealArray*) farray;
655 g_return_if_fail (array != NULL);
657 g_qsort_with_data (array->data,
664 /* Returns the smallest power of 2 greater than n, or n if
665 * such power does not fit in a guint
668 g_nearest_pow (gint num)
672 while (n < num && n > 0)
679 g_array_maybe_expand (GRealArray *array,
682 guint want_alloc = g_array_elt_len (array, array->len + len +
683 array->zero_terminated);
685 if (want_alloc > array->alloc)
687 want_alloc = g_nearest_pow (want_alloc);
688 want_alloc = MAX (want_alloc, MIN_ARRAY_SIZE);
690 array->data = g_realloc (array->data, want_alloc);
692 if (G_UNLIKELY (g_mem_gc_friendly))
693 memset (array->data + array->alloc, 0, want_alloc - array->alloc);
695 array->alloc = want_alloc;
700 * SECTION:arrays_pointer
701 * @title: Pointer Arrays
702 * @short_description: arrays of pointers to any type of data, which
703 * grow automatically as new elements are added
705 * Pointer Arrays are similar to Arrays but are used only for storing
708 * <note><para>If you remove elements from the array, elements at the
709 * end of the array are moved into the space previously occupied by the
710 * removed element. This means that you should not rely on the index of
711 * particular elements remaining the same. You should also be careful
712 * when deleting elements while iterating over the array.</para></note>
714 * To create a pointer array, use g_ptr_array_new().
716 * To add elements to a pointer array, use g_ptr_array_add().
718 * To remove elements from a pointer array, use g_ptr_array_remove(),
719 * g_ptr_array_remove_index() or g_ptr_array_remove_index_fast().
721 * To access an element of a pointer array, use g_ptr_array_index().
723 * To set the size of a pointer array, use g_ptr_array_set_size().
725 * To free a pointer array, use g_ptr_array_free().
728 * <title>Using a #GPtrArray</title>
730 * GPtrArray *gparray;
731 * gchar *string1 = "one", *string2 = "two", *string3 = "three";
733 * gparray = g_ptr_array_new (<!-- -->);
734 * g_ptr_array_add (gparray, (gpointer) string1);
735 * g_ptr_array_add (gparray, (gpointer) string2);
736 * g_ptr_array_add (gparray, (gpointer) string3);
738 * if (g_ptr_array_index (gparray, 0) != (gpointer) string1)
739 * g_print ("ERROR: got %p instead of %p\n",
740 * g_ptr_array_index (gparray, 0), string1);
742 * g_ptr_array_free (gparray, TRUE);
747 typedef struct _GRealPtrArray GRealPtrArray;
751 * @pdata: points to the array of pointers, which may be moved when the
753 * @len: number of pointers in the array.
755 * Contains the public fields of a pointer array.
757 struct _GRealPtrArray
763 GDestroyNotify element_free_func;
768 * @array: a #GPtrArray.
769 * @index_: the index of the pointer to return.
770 * @Returns: the pointer at the given index.
772 * Returns the pointer at the given index of the pointer array.
775 static void g_ptr_array_maybe_expand (GRealPtrArray *array,
780 * @Returns: the new #GPtrArray.
782 * Creates a new #GPtrArray with a reference count of 1.
785 g_ptr_array_new (void)
787 return g_ptr_array_sized_new (0);
791 * g_ptr_array_sized_new:
792 * @reserved_size: number of pointers preallocated.
793 * @Returns: the new #GPtrArray.
795 * Creates a new #GPtrArray with @reserved_size pointers preallocated
796 * and a reference count of 1. This avoids frequent reallocation, if
797 * you are going to add many pointers to the array. Note however that
798 * the size of the array is still 0.
801 g_ptr_array_sized_new (guint reserved_size)
803 GRealPtrArray *array = g_slice_new (GRealPtrArray);
808 array->ref_count = 1;
809 array->element_free_func = NULL;
811 if (reserved_size != 0)
812 g_ptr_array_maybe_expand (array, reserved_size);
814 return (GPtrArray*) array;
818 * g_ptr_array_new_with_free_func:
819 * @element_free_func: A function to free elements with destroy @array or %NULL.
821 * Creates a new #GPtrArray with a reference count of 1 and use @element_free_func
822 * for freeing each element when the array is destroyed either via
823 * g_ptr_array_unref(), when g_ptr_array_free() is called with @free_segment
824 * set to %TRUE or when removing elements.
826 * Returns: A new #GPtrArray.
831 g_ptr_array_new_with_free_func (GDestroyNotify element_free_func)
835 array = g_ptr_array_new ();
836 g_ptr_array_set_free_func (array, element_free_func);
841 * g_ptr_array_set_free_func:
842 * @array: A #GPtrArray.
843 * @element_free_func: A function to free elements with destroy @array or %NULL.
845 * Sets a function for freeing each element when @array is destroyed
846 * either via g_ptr_array_unref(), when g_ptr_array_free() is called
847 * with @free_segment set to %TRUE or when removing elements.
852 g_ptr_array_set_free_func (GPtrArray *array,
853 GDestroyNotify element_free_func)
855 GRealPtrArray* rarray = (GRealPtrArray*) array;
857 g_return_if_fail (array);
859 rarray->element_free_func = element_free_func;
866 * Atomically increments the reference count of @array by one. This
867 * function is MT-safe and may be called from any thread.
869 * Returns: The passed in #GPtrArray.
874 g_ptr_array_ref (GPtrArray *array)
876 GRealPtrArray *rarray = (GRealPtrArray*) array;
878 g_return_val_if_fail (array, NULL);
880 g_atomic_int_inc (&rarray->ref_count);
887 * @array: A #GPtrArray.
889 * Atomically decrements the reference count of @array by one. If the
890 * reference count drops to 0, the effect is the same as calling
891 * g_ptr_array_free() with @free_segment set to %TRUE. This function
892 * is MT-safe and may be called from any thread.
897 g_ptr_array_unref (GPtrArray *array)
899 GRealPtrArray *rarray = (GRealPtrArray*) array;
900 g_return_if_fail (array);
902 if (g_atomic_int_dec_and_test (&rarray->ref_count))
903 g_ptr_array_free (array, TRUE);
908 * @array: a #GPtrArray.
909 * @free_seg: if %TRUE the actual pointer array is freed as well.
910 * @Returns: the pointer array if @free_seg is %FALSE, otherwise %NULL.
911 * The pointer array should be freed using g_free().
913 * Frees the memory allocated for the #GPtrArray. If @free_seg is %TRUE
914 * it frees the memory block holding the elements as well. Pass %FALSE
915 * if you want to free the #GPtrArray wrapper but preserve the
916 * underlying array for use elsewhere. If the reference count of @array
917 * is greater than one, the #GPtrArray wrapper is preserved but the
918 * size of @array will be set to zero.
920 * <note><para>If array contents point to dynamically-allocated
921 * memory, they should be freed separately if @free_seg is %TRUE and no
922 * #GDestroyNotify function has been set for @array.</para></note>
925 g_ptr_array_free (GPtrArray *farray,
926 gboolean free_segment)
928 GRealPtrArray *array = (GRealPtrArray*) farray;
930 gboolean preserve_wrapper;
932 g_return_val_if_fail (array, NULL);
934 /* if others are holding a reference, preserve the wrapper but do free/return the data */
935 preserve_wrapper = FALSE;
936 if (g_atomic_int_get (&array->ref_count) > 1)
937 preserve_wrapper = TRUE;
941 if (array->element_free_func != NULL)
942 g_ptr_array_foreach (farray, (GFunc) array->element_free_func, NULL);
943 g_free (array->pdata);
947 segment = array->pdata;
949 if (preserve_wrapper)
957 g_slice_free1 (sizeof (GRealPtrArray), array);
964 g_ptr_array_maybe_expand (GRealPtrArray *array,
967 if ((array->len + len) > array->alloc)
969 guint old_alloc = array->alloc;
970 array->alloc = g_nearest_pow (array->len + len);
971 array->alloc = MAX (array->alloc, MIN_ARRAY_SIZE);
972 array->pdata = g_realloc (array->pdata, sizeof (gpointer) * array->alloc);
973 if (G_UNLIKELY (g_mem_gc_friendly))
974 for ( ; old_alloc < array->alloc; old_alloc++)
975 array->pdata [old_alloc] = NULL;
980 * g_ptr_array_set_size:
981 * @array: a #GPtrArray.
982 * @length: the new length of the pointer array.
984 * Sets the size of the array. When making the array larger,
985 * newly-added elements will be set to %NULL. When making it smaller,
986 * if @array has a non-%NULL #GDestroyNotify function then it will be
987 * called for the removed elements.
990 g_ptr_array_set_size (GPtrArray *farray,
993 GRealPtrArray* array = (GRealPtrArray*) farray;
995 g_return_if_fail (array);
997 if (length > array->len)
1000 g_ptr_array_maybe_expand (array, (length - array->len));
1002 * memset (array->pdata + array->len, 0,
1003 * sizeof (gpointer) * (length - array->len));
1004 * to make it really portable. Remember (void*)NULL needn't be
1005 * bitwise zero. It of course is silly not to use memset (..,0,..).
1007 for (i = array->len; i < length; i++)
1008 array->pdata[i] = NULL;
1010 else if (length < array->len)
1011 g_ptr_array_remove_range (farray, length, array->len - length);
1013 array->len = length;
1017 * g_ptr_array_remove_index:
1018 * @array: a #GPtrArray.
1019 * @index_: the index of the pointer to remove.
1020 * @Returns: the pointer which was removed.
1022 * Removes the pointer at the given index from the pointer array. The
1023 * following elements are moved down one place. If @array has a
1024 * non-%NULL #GDestroyNotify function it is called for the removed
1028 g_ptr_array_remove_index (GPtrArray *farray,
1031 GRealPtrArray* array = (GRealPtrArray*) farray;
1034 g_return_val_if_fail (array, NULL);
1036 g_return_val_if_fail (index_ < array->len, NULL);
1038 result = array->pdata[index_];
1040 if (array->element_free_func != NULL)
1041 array->element_free_func (array->pdata[index_]);
1043 if (index_ != array->len - 1)
1044 g_memmove (array->pdata + index_, array->pdata + index_ + 1,
1045 sizeof (gpointer) * (array->len - index_ - 1));
1049 if (G_UNLIKELY (g_mem_gc_friendly))
1050 array->pdata[array->len] = NULL;
1056 * g_ptr_array_remove_index_fast:
1057 * @array: a #GPtrArray.
1058 * @index_: the index of the pointer to remove.
1059 * @Returns: the pointer which was removed.
1061 * Removes the pointer at the given index from the pointer array. The
1062 * last element in the array is used to fill in the space, so this
1063 * function does not preserve the order of the array. But it is faster
1064 * than g_ptr_array_remove_index(). If @array has a non-%NULL
1065 * #GDestroyNotify function it is called for the removed element.
1068 g_ptr_array_remove_index_fast (GPtrArray *farray,
1071 GRealPtrArray* array = (GRealPtrArray*) farray;
1074 g_return_val_if_fail (array, NULL);
1076 g_return_val_if_fail (index_ < array->len, NULL);
1078 result = array->pdata[index_];
1080 if (array->element_free_func != NULL)
1081 array->element_free_func (array->pdata[index_]);
1083 if (index_ != array->len - 1)
1084 array->pdata[index_] = array->pdata[array->len - 1];
1088 if (G_UNLIKELY (g_mem_gc_friendly))
1089 array->pdata[array->len] = NULL;
1095 * g_ptr_array_remove_range:
1096 * @array: a @GPtrArray.
1097 * @index_: the index of the first pointer to remove.
1098 * @length: the number of pointers to remove.
1100 * Removes the given number of pointers starting at the given index
1101 * from a #GPtrArray. The following elements are moved to close the
1102 * gap. If @array has a non-%NULL #GDestroyNotify function it is called
1103 * for the removed elements.
1108 g_ptr_array_remove_range (GPtrArray *farray,
1112 GRealPtrArray* array = (GRealPtrArray*) farray;
1115 g_return_if_fail (array);
1116 g_return_if_fail (index_ < array->len);
1117 g_return_if_fail (index_ + length <= array->len);
1119 if (array->element_free_func != NULL)
1121 for (n = index_; n < index_ + length; n++)
1122 array->element_free_func (array->pdata[n]);
1125 if (index_ + length != array->len)
1127 g_memmove (&array->pdata[index_],
1128 &array->pdata[index_ + length],
1129 (array->len - (index_ + length)) * sizeof (gpointer));
1132 array->len -= length;
1133 if (G_UNLIKELY (g_mem_gc_friendly))
1136 for (i = 0; i < length; i++)
1137 array->pdata[array->len + i] = NULL;
1142 * g_ptr_array_remove:
1143 * @array: a #GPtrArray.
1144 * @data: the pointer to remove.
1145 * @Returns: %TRUE if the pointer is removed. %FALSE if the pointer is
1146 * not found in the array.
1148 * Removes the first occurrence of the given pointer from the pointer
1149 * array. The following elements are moved down one place. If @array
1150 * has a non-%NULL #GDestroyNotify function it is called for the
1153 * It returns %TRUE if the pointer was removed, or %FALSE if the
1154 * pointer was not found.
1157 g_ptr_array_remove (GPtrArray *farray,
1160 GRealPtrArray* array = (GRealPtrArray*) farray;
1163 g_return_val_if_fail (array, FALSE);
1165 for (i = 0; i < array->len; i += 1)
1167 if (array->pdata[i] == data)
1169 g_ptr_array_remove_index (farray, i);
1178 * g_ptr_array_remove_fast:
1179 * @array: a #GPtrArray.
1180 * @data: the pointer to remove.
1181 * @Returns: %TRUE if the pointer was found in the array.
1183 * Removes the first occurrence of the given pointer from the pointer
1184 * array. The last element in the array is used to fill in the space,
1185 * so this function does not preserve the order of the array. But it is
1186 * faster than g_ptr_array_remove(). If @array has a non-%NULL
1187 * #GDestroyNotify function it is called for the removed element.
1189 * It returns %TRUE if the pointer was removed, or %FALSE if the
1190 * pointer was not found.
1193 g_ptr_array_remove_fast (GPtrArray *farray,
1196 GRealPtrArray* array = (GRealPtrArray*) farray;
1199 g_return_val_if_fail (array, FALSE);
1201 for (i = 0; i < array->len; i += 1)
1203 if (array->pdata[i] == data)
1205 g_ptr_array_remove_index_fast (farray, i);
1215 * @array: a #GPtrArray.
1216 * @data: the pointer to add.
1218 * Adds a pointer to the end of the pointer array. The array will grow
1219 * in size automatically if necessary.
1222 g_ptr_array_add (GPtrArray *farray,
1225 GRealPtrArray* array = (GRealPtrArray*) farray;
1227 g_return_if_fail (array);
1229 g_ptr_array_maybe_expand (array, 1);
1231 array->pdata[array->len++] = data;
1236 * @array: a #GPtrArray.
1237 * @compare_func: comparison function.
1239 * Sorts the array, using @compare_func which should be a qsort()-style
1240 * comparison function (returns less than zero for first arg is less
1241 * than second arg, zero for equal, greater than zero if irst arg is
1242 * greater than second arg).
1244 * If two array elements compare equal, their order in the sorted array
1245 * is undefined. If you want equal elements to keep their order – i.e.
1246 * you want a stable sort – you can write a comparison function that,
1247 * if two elements would otherwise compare equal, compares them by
1250 * <note><para>The comparison function for g_ptr_array_sort() doesn't
1251 * take the pointers from the array as arguments, it takes pointers to
1252 * the pointers in the array.</para></note>
1255 g_ptr_array_sort (GPtrArray *array,
1256 GCompareFunc compare_func)
1258 g_return_if_fail (array != NULL);
1260 qsort (array->pdata,
1267 * g_ptr_array_sort_with_data:
1268 * @array: a #GPtrArray.
1269 * @compare_func: comparison function.
1270 * @user_data: data to pass to @compare_func.
1272 * Like g_ptr_array_sort(), but the comparison function has an extra
1273 * user data argument.
1275 * <note><para>The comparison function for g_ptr_array_sort_with_data()
1276 * doesn't take the pointers from the array as arguments, it takes
1277 * pointers to the pointers in the array.</para></note>
1280 g_ptr_array_sort_with_data (GPtrArray *array,
1281 GCompareDataFunc compare_func,
1284 g_return_if_fail (array != NULL);
1286 g_qsort_with_data (array->pdata,
1294 * g_ptr_array_foreach:
1295 * @array: a #GPtrArray
1296 * @func: the function to call for each array element
1297 * @user_data: user data to pass to the function
1299 * Calls a function for each element of a #GPtrArray.
1304 g_ptr_array_foreach (GPtrArray *array,
1310 g_return_if_fail (array);
1312 for (i = 0; i < array->len; i++)
1313 (*func) (array->pdata[i], user_data);
1317 * SECTION:arrays_byte
1318 * @title: Byte Arrays
1319 * @short_description: arrays of bytes, which grow automatically as
1320 * elements are added
1322 * #GByteArray is based on #GArray, to provide arrays of bytes which
1323 * grow automatically as elements are added.
1325 * To create a new #GByteArray use g_byte_array_new().
1327 * To add elements to a #GByteArray, use g_byte_array_append(), and
1328 * g_byte_array_prepend().
1330 * To set the size of a #GByteArray, use g_byte_array_set_size().
1332 * To free a #GByteArray, use g_byte_array_free().
1335 * <title>Using a #GByteArray</title>
1337 * GByteArray *gbarray;
1340 * gbarray = g_byte_array_new (<!-- -->);
1341 * for (i = 0; i < 10000; i++)
1342 * g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1344 * for (i = 0; i < 10000; i++)
1346 * g_assert (gbarray->data[4*i] == 'a');
1347 * g_assert (gbarray->data[4*i+1] == 'b');
1348 * g_assert (gbarray->data[4*i+2] == 'c');
1349 * g_assert (gbarray->data[4*i+3] == 'd');
1352 * g_byte_array_free (gbarray, TRUE);
1359 * @data: a pointer to the element data. The data may be moved as
1360 * elements are added to the #GByteArray.
1361 * @len: the number of elements in the #GByteArray.
1363 * The <structname>GByteArray</structname> struct allows access to the
1364 * public fields of a <structname>GByteArray</structname>.
1369 * @Returns: the new #GByteArray.
1371 * Creates a new #GByteArray with a reference count of 1.
1373 GByteArray* g_byte_array_new (void)
1375 return (GByteArray*) g_array_sized_new (FALSE, FALSE, 1, 0);
1379 * g_byte_array_sized_new:
1380 * @reserved_size: number of bytes preallocated.
1381 * @Returns: the new #GByteArray.
1383 * Creates a new #GByteArray with @reserved_size bytes preallocated.
1384 * This avoids frequent reallocation, if you are going to add many
1385 * bytes to the array. Note however that the size of the array is still
1388 GByteArray* g_byte_array_sized_new (guint reserved_size)
1390 return (GByteArray*) g_array_sized_new (FALSE, FALSE, 1, reserved_size);
1394 * g_byte_array_free:
1395 * @array: a #GByteArray.
1396 * @free_segment: if %TRUE the actual byte data is freed as well.
1397 * @Returns: the element data if @free_segment is %FALSE, otherwise
1398 * %NULL. The element data should be freed using g_free().
1400 * Frees the memory allocated by the #GByteArray. If @free_segment is
1401 * %TRUE it frees the actual byte data. If the reference count of
1402 * @array is greater than one, the #GByteArray wrapper is preserved but
1403 * the size of @array will be set to zero.
1405 guint8* g_byte_array_free (GByteArray *array,
1406 gboolean free_segment)
1408 return (guint8*) g_array_free ((GArray*) array, free_segment);
1413 * @array: A #GByteArray.
1415 * Atomically increments the reference count of @array by one. This
1416 * function is MT-safe and may be called from any thread.
1418 * Returns: The passed in #GByteArray.
1423 g_byte_array_ref (GByteArray *array)
1425 return (GByteArray *) g_array_ref ((GArray *) array);
1429 * g_byte_array_unref:
1430 * @array: A #GByteArray.
1432 * Atomically decrements the reference count of @array by one. If the
1433 * reference count drops to 0, all memory allocated by the array is
1434 * released. This function is MT-safe and may be called from any
1440 g_byte_array_unref (GByteArray *array)
1442 g_array_unref ((GArray *) array);
1446 * g_byte_array_append:
1447 * @array: a #GByteArray.
1448 * @data: the byte data to be added.
1449 * @len: the number of bytes to add.
1450 * @Returns: the #GByteArray.
1452 * Adds the given bytes to the end of the #GByteArray. The array will
1453 * grow in size automatically if necessary.
1455 GByteArray* g_byte_array_append (GByteArray *array,
1459 g_array_append_vals ((GArray*) array, (guint8*)data, len);
1465 * g_byte_array_prepend:
1466 * @array: a #GByteArray.
1467 * @data: the byte data to be added.
1468 * @len: the number of bytes to add.
1469 * @Returns: the #GByteArray.
1471 * Adds the given data to the start of the #GByteArray. The array will
1472 * grow in size automatically if necessary.
1474 GByteArray* g_byte_array_prepend (GByteArray *array,
1478 g_array_prepend_vals ((GArray*) array, (guint8*)data, len);
1484 * g_byte_array_set_size:
1485 * @array: a #GByteArray.
1486 * @length: the new size of the #GByteArray.
1487 * @Returns: the #GByteArray.
1489 * Sets the size of the #GByteArray, expanding it if necessary.
1491 GByteArray* g_byte_array_set_size (GByteArray *array,
1494 g_array_set_size ((GArray*) array, length);
1500 * g_byte_array_remove_index:
1501 * @array: a #GByteArray.
1502 * @index_: the index of the byte to remove.
1503 * @Returns: the #GByteArray.
1505 * Removes the byte at the given index from a #GByteArray. The
1506 * following bytes are moved down one place.
1508 GByteArray* g_byte_array_remove_index (GByteArray *array,
1511 g_array_remove_index ((GArray*) array, index_);
1517 * g_byte_array_remove_index_fast:
1518 * @array: a #GByteArray.
1519 * @index_: the index of the byte to remove.
1520 * @Returns: the #GByteArray.
1522 * Removes the byte at the given index from a #GByteArray. The last
1523 * element in the array is used to fill in the space, so this function
1524 * does not preserve the order of the #GByteArray. But it is faster
1525 * than g_byte_array_remove_index().
1527 GByteArray* g_byte_array_remove_index_fast (GByteArray *array,
1530 g_array_remove_index_fast ((GArray*) array, index_);
1536 * g_byte_array_remove_range:
1537 * @array: a @GByteArray.
1538 * @index_: the index of the first byte to remove.
1539 * @length: the number of bytes to remove.
1540 * @Returns: the #GByteArray.
1542 * Removes the given number of bytes starting at the given index from a
1543 * #GByteArray. The following elements are moved to close the gap.
1548 g_byte_array_remove_range (GByteArray *array,
1552 g_return_val_if_fail (array, NULL);
1553 g_return_val_if_fail (index_ < array->len, NULL);
1554 g_return_val_if_fail (index_ + length <= array->len, NULL);
1556 return (GByteArray *)g_array_remove_range ((GArray*) array, index_, length);
1560 * g_byte_array_sort:
1561 * @array: a #GByteArray.
1562 * @compare_func: comparison function.
1564 * Sorts a byte array, using @compare_func which should be a
1565 * qsort()-style comparison function (returns less than zero for first
1566 * arg is less than second arg, zero for equal, greater than zero if
1567 * first arg is greater than second arg).
1569 * If two array elements compare equal, their order in the sorted array
1570 * is undefined. If you want equal elements to keep their order – i.e.
1571 * you want a stable sort – you can write a comparison function that,
1572 * if two elements would otherwise compare equal, compares them by
1576 g_byte_array_sort (GByteArray *array,
1577 GCompareFunc compare_func)
1579 g_array_sort ((GArray *) array, compare_func);
1583 * g_byte_array_sort_with_data:
1584 * @array: a #GByteArray.
1585 * @compare_func: comparison function.
1586 * @user_data: data to pass to @compare_func.
1588 * Like g_byte_array_sort(), but the comparison function takes an extra
1589 * user data argument.
1592 g_byte_array_sort_with_data (GByteArray *array,
1593 GCompareDataFunc compare_func,
1596 g_array_sort_with_data ((GArray *) array, compare_func, user_data);