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.
20 * SECTION:value_arrays
21 * @Short_description: A container structure to maintain an array of generic values
22 * @See_also:#GValue, #GParamSpecValueArray, g_param_spec_value_array()
23 * @Title: Value arrays
25 * The prime purpose of a #GValueArray is for it to be used as an object property
26 * that holds an array of values. A #GValueArray wraps an array of #GValue elements
27 * in order for it to be used as a boxed type through %G_TYPE_VALUE_ARRAY.
35 #include "gvaluearray.h"
36 #include "gobjectalias.h"
38 #include <stdlib.h> /* qsort() */
40 #ifdef DISABLE_MEM_POOLS
41 # define GROUP_N_VALUES (1) /* power of 2 !! */
43 # define GROUP_N_VALUES (8) /* power of 2 !! */
47 /* --- functions --- */
49 * g_value_array_get_nth:
50 * @value_array: #GValueArray to get a value from
51 * @index_: index of the value of interest
53 * Return a pointer to the value at @index_ containd in @value_array.
55 * Returns: pointer to a value at @index_ in @value_array
58 g_value_array_get_nth (GValueArray *value_array,
61 g_return_val_if_fail (value_array != NULL, NULL);
62 g_return_val_if_fail (index < value_array->n_values, NULL);
64 return value_array->values + index;
68 value_array_grow (GValueArray *value_array,
72 g_return_if_fail (n_values >= value_array->n_values);
74 value_array->n_values = n_values;
75 if (value_array->n_values > value_array->n_prealloced)
77 guint i = value_array->n_prealloced;
79 value_array->n_prealloced = (value_array->n_values + GROUP_N_VALUES - 1) & ~(GROUP_N_VALUES - 1);
80 value_array->values = g_renew (GValue, value_array->values, value_array->n_prealloced);
82 i = value_array->n_values;
83 memset (value_array->values + i, 0,
84 (value_array->n_prealloced - i) * sizeof (value_array->values[0]));
89 value_array_shrink (GValueArray *value_array)
91 #ifdef DISABLE_MEM_POOLS
92 if (value_array->n_prealloced >= value_array->n_values + GROUP_N_VALUES)
94 value_array->n_prealloced = (value_array->n_values + GROUP_N_VALUES - 1) & ~(GROUP_N_VALUES - 1);
95 value_array->values = g_renew (GValue, value_array->values, value_array->n_prealloced);
102 * @n_prealloced: number of values to preallocate space for
104 * Allocate and initialize a new #GValueArray, optionally preserve space
105 * for @n_prealloced elements. New arrays always contain 0 elements,
106 * regardless of the value of @n_prealloced.
108 * Returns: a newly allocated #GValueArray with 0 values
111 g_value_array_new (guint n_prealloced)
113 GValueArray *value_array = g_slice_new (GValueArray);
115 value_array->n_values = 0;
116 value_array->n_prealloced = 0;
117 value_array->values = NULL;
118 value_array_grow (value_array, n_prealloced, TRUE);
119 value_array->n_values = 0;
125 * g_value_array_free:
126 * @value_array: #GValueArray to free
128 * Free a #GValueArray including its contents.
131 g_value_array_free (GValueArray *value_array)
135 g_return_if_fail (value_array != NULL);
137 for (i = 0; i < value_array->n_values; i++)
139 GValue *value = value_array->values + i;
141 if (G_VALUE_TYPE (value) != 0) /* we allow unset values in the array */
142 g_value_unset (value);
144 g_free (value_array->values);
145 g_slice_free (GValueArray, value_array);
149 * g_value_array_copy:
150 * @value_array: #GValueArray to copy
152 * Construct an exact copy of a #GValueArray by duplicating all its
155 * Returns: Newly allocated copy of #GValueArray
158 g_value_array_copy (const GValueArray *value_array)
160 GValueArray *new_array;
163 g_return_val_if_fail (value_array != NULL, NULL);
165 new_array = g_slice_new (GValueArray);
166 new_array->n_values = 0;
167 new_array->values = NULL;
168 new_array->n_prealloced = 0;
169 value_array_grow (new_array, value_array->n_values, TRUE);
170 for (i = 0; i < new_array->n_values; i++)
171 if (G_VALUE_TYPE (value_array->values + i) != 0)
173 GValue *value = new_array->values + i;
175 g_value_init (value, G_VALUE_TYPE (value_array->values + i));
176 g_value_copy (value_array->values + i, value);
182 * g_value_array_prepend:
183 * @value_array: #GValueArray to add an element to
184 * @value: #GValue to copy into #GValueArray
186 * Insert a copy of @value as first element of @value_array.
188 * Returns: the #GValueArray passed in as @value_array
191 g_value_array_prepend (GValueArray *value_array,
194 g_return_val_if_fail (value_array != NULL, NULL);
196 return g_value_array_insert (value_array, 0, value);
200 * g_value_array_append:
201 * @value_array: #GValueArray to add an element to
202 * @value: #GValue to copy into #GValueArray
204 * Insert a copy of @value as last element of @value_array.
206 * Returns: the #GValueArray passed in as @value_array
209 g_value_array_append (GValueArray *value_array,
212 g_return_val_if_fail (value_array != NULL, NULL);
214 return g_value_array_insert (value_array, value_array->n_values, value);
218 * g_value_array_insert:
219 * @value_array: #GValueArray to add an element to
220 * @index_: insertion position, must be <= value_array->n_values
221 * @value: #GValue to copy into #GValueArray
223 * Insert a copy of @value at specified position into @value_array.
225 * Returns: the #GValueArray passed in as @value_array
228 g_value_array_insert (GValueArray *value_array,
234 g_return_val_if_fail (value_array != NULL, NULL);
235 g_return_val_if_fail (index <= value_array->n_values, value_array);
237 /* we support NULL for "value" as a shortcut for an unset value */
239 i = value_array->n_values;
240 value_array_grow (value_array, value_array->n_values + 1, FALSE);
241 if (index + 1 < value_array->n_values)
242 g_memmove (value_array->values + index + 1, value_array->values + index,
243 (i - index) * sizeof (value_array->values[0]));
244 memset (value_array->values + index, 0, sizeof (value_array->values[0]));
247 g_value_init (value_array->values + index, G_VALUE_TYPE (value));
248 g_value_copy (value, value_array->values + index);
254 * g_value_array_remove:
255 * @value_array: #GValueArray to remove an element from
256 * @index_: position of value to remove, must be < value_array->n_values
258 * Remove the value at position @index_ from @value_array.
260 * Returns: the #GValueArray passed in as @value_array
263 g_value_array_remove (GValueArray *value_array,
266 g_return_val_if_fail (value_array != NULL, NULL);
267 g_return_val_if_fail (index < value_array->n_values, value_array);
269 if (G_VALUE_TYPE (value_array->values + index) != 0)
270 g_value_unset (value_array->values + index);
271 value_array->n_values--;
272 if (index < value_array->n_values)
273 g_memmove (value_array->values + index, value_array->values + index + 1,
274 (value_array->n_values - index) * sizeof (value_array->values[0]));
275 value_array_shrink (value_array);
276 if (value_array->n_prealloced > value_array->n_values)
277 memset (value_array->values + value_array->n_values, 0, sizeof (value_array->values[0]));
283 * g_value_array_sort:
284 * @value_array: #GValueArray to sort
285 * @compare_func: function to compare elements
287 * Sort @value_array using @compare_func to compare the elements accoring to
288 * the semantics of #GCompareFunc.
290 * The current implementation uses Quick-Sort as sorting algorithm.
292 * Returns: the #GValueArray passed in as @value_array
295 g_value_array_sort (GValueArray *value_array,
296 GCompareFunc compare_func)
298 g_return_val_if_fail (compare_func != NULL, NULL);
300 if (value_array->n_values)
301 qsort (value_array->values,
302 value_array->n_values,
303 sizeof (value_array->values[0]),
309 * g_value_array_sort_with_data:
310 * @value_array: #GValueArray to sort
311 * @compare_func: function to compare elements
312 * @user_data: extra data argument provided for @compare_func
314 * Sort @value_array using @compare_func to compare the elements accoring
315 * to the semantics of #GCompareDataFunc.
318 * The current implementation uses Quick-Sort as sorting algorithm.
320 * Returns: the #GValueArray passed in as @value_array
323 g_value_array_sort_with_data (GValueArray *value_array,
324 GCompareDataFunc compare_func,
327 g_return_val_if_fail (value_array != NULL, NULL);
328 g_return_val_if_fail (compare_func != NULL, NULL);
330 if (value_array->n_values)
331 g_qsort_with_data (value_array->values,
332 value_array->n_values,
333 sizeof (value_array->values[0]),
334 compare_func, user_data);
338 #define __G_VALUE_ARRAY_C__
339 #include "gobjectaliasdef.c"