1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 2001 Red Hat, Inc.
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
15 * Public 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.
27 #include <stdlib.h> /* qsort() */
29 #include "gvaluearray.h"
33 * SECTION:value_arrays
34 * @short_description: A container structure to maintain an array of
36 * @see_also: #GValue, #GParamSpecValueArray, g_param_spec_value_array()
37 * @title: Value arrays
39 * The prime purpose of a #GValueArray is for it to be used as an
40 * object property that holds an array of values. A #GValueArray wraps
41 * an array of #GValue elements in order for it to be used as a boxed
42 * type through %G_TYPE_VALUE_ARRAY.
46 #ifdef DISABLE_MEM_POOLS
47 # define GROUP_N_VALUES (1) /* power of 2 !! */
49 # define GROUP_N_VALUES (8) /* power of 2 !! */
53 /* --- functions --- */
55 * g_value_array_get_nth:
56 * @value_array: #GValueArray to get a value from
57 * @index_: index of the value of interest
59 * Return a pointer to the value at @index_ containd in @value_array.
61 * Returns: (transfer none): pointer to a value at @index_ in @value_array
64 g_value_array_get_nth (GValueArray *value_array,
67 g_return_val_if_fail (value_array != NULL, NULL);
68 g_return_val_if_fail (index < value_array->n_values, NULL);
70 return value_array->values + index;
74 value_array_grow (GValueArray *value_array,
78 g_return_if_fail (n_values >= value_array->n_values);
80 value_array->n_values = n_values;
81 if (value_array->n_values > value_array->n_prealloced)
83 guint i = value_array->n_prealloced;
85 value_array->n_prealloced = (value_array->n_values + GROUP_N_VALUES - 1) & ~(GROUP_N_VALUES - 1);
86 value_array->values = g_renew (GValue, value_array->values, value_array->n_prealloced);
88 i = value_array->n_values;
89 memset (value_array->values + i, 0,
90 (value_array->n_prealloced - i) * sizeof (value_array->values[0]));
95 value_array_shrink (GValueArray *value_array)
97 #ifdef DISABLE_MEM_POOLS
98 if (value_array->n_prealloced >= value_array->n_values + GROUP_N_VALUES)
100 value_array->n_prealloced = (value_array->n_values + GROUP_N_VALUES - 1) & ~(GROUP_N_VALUES - 1);
101 value_array->values = g_renew (GValue, value_array->values, value_array->n_prealloced);
108 * @n_prealloced: number of values to preallocate space for
110 * Allocate and initialize a new #GValueArray, optionally preserve space
111 * for @n_prealloced elements. New arrays always contain 0 elements,
112 * regardless of the value of @n_prealloced.
114 * Returns: a newly allocated #GValueArray with 0 values
117 g_value_array_new (guint n_prealloced)
119 GValueArray *value_array = g_slice_new (GValueArray);
121 value_array->n_values = 0;
122 value_array->n_prealloced = 0;
123 value_array->values = NULL;
124 value_array_grow (value_array, n_prealloced, TRUE);
125 value_array->n_values = 0;
131 * g_value_array_free:
132 * @value_array: #GValueArray to free
134 * Free a #GValueArray including its contents.
137 g_value_array_free (GValueArray *value_array)
141 g_return_if_fail (value_array != NULL);
143 for (i = 0; i < value_array->n_values; i++)
145 GValue *value = value_array->values + i;
147 if (G_VALUE_TYPE (value) != 0) /* we allow unset values in the array */
148 g_value_unset (value);
150 g_free (value_array->values);
151 g_slice_free (GValueArray, value_array);
155 * g_value_array_copy:
156 * @value_array: #GValueArray to copy
158 * Construct an exact copy of a #GValueArray by duplicating all its
161 * Returns: (transfer full): Newly allocated copy of #GValueArray
164 g_value_array_copy (const GValueArray *value_array)
166 GValueArray *new_array;
169 g_return_val_if_fail (value_array != NULL, NULL);
171 new_array = g_slice_new (GValueArray);
172 new_array->n_values = 0;
173 new_array->values = NULL;
174 new_array->n_prealloced = 0;
175 value_array_grow (new_array, value_array->n_values, TRUE);
176 for (i = 0; i < new_array->n_values; i++)
177 if (G_VALUE_TYPE (value_array->values + i) != 0)
179 GValue *value = new_array->values + i;
181 g_value_init (value, G_VALUE_TYPE (value_array->values + i));
182 g_value_copy (value_array->values + i, value);
188 * g_value_array_prepend:
189 * @value_array: #GValueArray to add an element to
190 * @value: (allow-none): #GValue to copy into #GValueArray, or %NULL
192 * Insert a copy of @value as first element of @value_array. If @value is
193 * %NULL, an uninitialized value is prepended.
196 * Returns: (transfer none): the #GValueArray passed in as @value_array
199 g_value_array_prepend (GValueArray *value_array,
202 g_return_val_if_fail (value_array != NULL, NULL);
204 return g_value_array_insert (value_array, 0, value);
208 * g_value_array_append:
209 * @value_array: #GValueArray to add an element to
210 * @value: (allow-none): #GValue to copy into #GValueArray, or %NULL
212 * Insert a copy of @value as last element of @value_array. If @value is
213 * %NULL, an uninitialized value is appended.
215 * Returns: (transfer none): the #GValueArray passed in as @value_array
218 g_value_array_append (GValueArray *value_array,
221 g_return_val_if_fail (value_array != NULL, NULL);
223 return g_value_array_insert (value_array, value_array->n_values, value);
227 * g_value_array_insert:
228 * @value_array: #GValueArray to add an element to
229 * @index_: insertion position, must be <= value_array->n_values
230 * @value: (allow-none): #GValue to copy into #GValueArray, or %NULL
232 * Insert a copy of @value at specified position into @value_array. If @value
233 * is %NULL, an uninitialized value is inserted.
235 * Returns: (transfer none): the #GValueArray passed in as @value_array
238 g_value_array_insert (GValueArray *value_array,
244 g_return_val_if_fail (value_array != NULL, NULL);
245 g_return_val_if_fail (index <= value_array->n_values, value_array);
247 i = value_array->n_values;
248 value_array_grow (value_array, value_array->n_values + 1, FALSE);
249 if (index + 1 < value_array->n_values)
250 g_memmove (value_array->values + index + 1, value_array->values + index,
251 (i - index) * sizeof (value_array->values[0]));
252 memset (value_array->values + index, 0, sizeof (value_array->values[0]));
255 g_value_init (value_array->values + index, G_VALUE_TYPE (value));
256 g_value_copy (value, value_array->values + index);
262 * g_value_array_remove:
263 * @value_array: #GValueArray to remove an element from
264 * @index_: position of value to remove, must be < value_array->n_values
266 * Remove the value at position @index_ from @value_array.
268 * Returns: (transfer none): the #GValueArray passed in as @value_array
271 g_value_array_remove (GValueArray *value_array,
274 g_return_val_if_fail (value_array != NULL, NULL);
275 g_return_val_if_fail (index < value_array->n_values, value_array);
277 if (G_VALUE_TYPE (value_array->values + index) != 0)
278 g_value_unset (value_array->values + index);
279 value_array->n_values--;
280 if (index < value_array->n_values)
281 g_memmove (value_array->values + index, value_array->values + index + 1,
282 (value_array->n_values - index) * sizeof (value_array->values[0]));
283 value_array_shrink (value_array);
284 if (value_array->n_prealloced > value_array->n_values)
285 memset (value_array->values + value_array->n_values, 0, sizeof (value_array->values[0]));
291 * g_value_array_sort:
292 * @value_array: #GValueArray to sort
293 * @compare_func: (scope call): function to compare elements
295 * Sort @value_array using @compare_func to compare the elements accoring to
296 * the semantics of #GCompareFunc.
298 * The current implementation uses Quick-Sort as sorting algorithm.
300 * Returns: (transfer none): the #GValueArray passed in as @value_array
303 g_value_array_sort (GValueArray *value_array,
304 GCompareFunc compare_func)
306 g_return_val_if_fail (compare_func != NULL, NULL);
308 if (value_array->n_values)
309 qsort (value_array->values,
310 value_array->n_values,
311 sizeof (value_array->values[0]),
317 * g_value_array_sort_with_data:
318 * @value_array: #GValueArray to sort
319 * @compare_func: (scope call): function to compare elements
320 * @user_data: (closure): extra data argument provided for @compare_func
322 * Sort @value_array using @compare_func to compare the elements accoring
323 * to the semantics of #GCompareDataFunc.
325 * The current implementation uses Quick-Sort as sorting algorithm.
327 * Rename to: g_value_array_sort
328 * Returns: (transfer none): the #GValueArray passed in as @value_array
331 g_value_array_sort_with_data (GValueArray *value_array,
332 GCompareDataFunc compare_func,
335 g_return_val_if_fail (value_array != NULL, NULL);
336 g_return_val_if_fail (compare_func != NULL, NULL);
338 if (value_array->n_values)
339 g_qsort_with_data (value_array->values,
340 value_array->n_values,
341 sizeof (value_array->values[0]),
342 compare_func, user_data);