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"
31 #include "gobjectalias.h"
35 * SECTION:value_arrays
36 * @Short_description: A container structure to maintain an array of generic values
37 * @See_also:#GValue, #GParamSpecValueArray, g_param_spec_value_array()
38 * @Title: Value arrays
40 * The prime purpose of a #GValueArray is for it to be used as an
41 * object property that holds an array of values. A #GValueArray wraps
42 * an array of #GValue elements in order for it to be used as a boxed
43 * type through %G_TYPE_VALUE_ARRAY.
47 #ifdef DISABLE_MEM_POOLS
48 # define GROUP_N_VALUES (1) /* power of 2 !! */
50 # define GROUP_N_VALUES (8) /* power of 2 !! */
54 /* --- functions --- */
56 * g_value_array_get_nth:
57 * @value_array: #GValueArray to get a value from
58 * @index_: index of the value of interest
60 * Return a pointer to the value at @index_ containd in @value_array.
62 * Returns: pointer to a value at @index_ in @value_array
65 g_value_array_get_nth (GValueArray *value_array,
68 g_return_val_if_fail (value_array != NULL, NULL);
69 g_return_val_if_fail (index < value_array->n_values, NULL);
71 return value_array->values + index;
75 value_array_grow (GValueArray *value_array,
79 g_return_if_fail (n_values >= value_array->n_values);
81 value_array->n_values = n_values;
82 if (value_array->n_values > value_array->n_prealloced)
84 guint i = value_array->n_prealloced;
86 value_array->n_prealloced = (value_array->n_values + GROUP_N_VALUES - 1) & ~(GROUP_N_VALUES - 1);
87 value_array->values = g_renew (GValue, value_array->values, value_array->n_prealloced);
89 i = value_array->n_values;
90 memset (value_array->values + i, 0,
91 (value_array->n_prealloced - i) * sizeof (value_array->values[0]));
96 value_array_shrink (GValueArray *value_array)
98 #ifdef DISABLE_MEM_POOLS
99 if (value_array->n_prealloced >= value_array->n_values + GROUP_N_VALUES)
101 value_array->n_prealloced = (value_array->n_values + GROUP_N_VALUES - 1) & ~(GROUP_N_VALUES - 1);
102 value_array->values = g_renew (GValue, value_array->values, value_array->n_prealloced);
109 * @n_prealloced: number of values to preallocate space for
111 * Allocate and initialize a new #GValueArray, optionally preserve space
112 * for @n_prealloced elements. New arrays always contain 0 elements,
113 * regardless of the value of @n_prealloced.
115 * Returns: a newly allocated #GValueArray with 0 values
118 g_value_array_new (guint n_prealloced)
120 GValueArray *value_array = g_slice_new (GValueArray);
122 value_array->n_values = 0;
123 value_array->n_prealloced = 0;
124 value_array->values = NULL;
125 value_array_grow (value_array, n_prealloced, TRUE);
126 value_array->n_values = 0;
132 * g_value_array_free:
133 * @value_array: #GValueArray to free
135 * Free a #GValueArray including its contents.
138 g_value_array_free (GValueArray *value_array)
142 g_return_if_fail (value_array != NULL);
144 for (i = 0; i < value_array->n_values; i++)
146 GValue *value = value_array->values + i;
148 if (G_VALUE_TYPE (value) != 0) /* we allow unset values in the array */
149 g_value_unset (value);
151 g_free (value_array->values);
152 g_slice_free (GValueArray, value_array);
156 * g_value_array_copy:
157 * @value_array: #GValueArray to copy
159 * Construct an exact copy of a #GValueArray by duplicating all its
162 * Returns: Newly allocated copy of #GValueArray
165 g_value_array_copy (const GValueArray *value_array)
167 GValueArray *new_array;
170 g_return_val_if_fail (value_array != NULL, NULL);
172 new_array = g_slice_new (GValueArray);
173 new_array->n_values = 0;
174 new_array->values = NULL;
175 new_array->n_prealloced = 0;
176 value_array_grow (new_array, value_array->n_values, TRUE);
177 for (i = 0; i < new_array->n_values; i++)
178 if (G_VALUE_TYPE (value_array->values + i) != 0)
180 GValue *value = new_array->values + i;
182 g_value_init (value, G_VALUE_TYPE (value_array->values + i));
183 g_value_copy (value_array->values + i, value);
189 * g_value_array_prepend:
190 * @value_array: #GValueArray to add an element to
191 * @value: #GValue to copy into #GValueArray
193 * Insert a copy of @value as first element of @value_array.
195 * Returns: the #GValueArray passed in as @value_array
198 g_value_array_prepend (GValueArray *value_array,
201 g_return_val_if_fail (value_array != NULL, NULL);
203 return g_value_array_insert (value_array, 0, value);
207 * g_value_array_append:
208 * @value_array: #GValueArray to add an element to
209 * @value: #GValue to copy into #GValueArray
211 * Insert a copy of @value as last element of @value_array.
213 * Returns: the #GValueArray passed in as @value_array
216 g_value_array_append (GValueArray *value_array,
219 g_return_val_if_fail (value_array != NULL, NULL);
221 return g_value_array_insert (value_array, value_array->n_values, value);
225 * g_value_array_insert:
226 * @value_array: #GValueArray to add an element to
227 * @index_: insertion position, must be <= value_array->n_values
228 * @value: #GValue to copy into #GValueArray
230 * Insert a copy of @value at specified position into @value_array.
232 * Returns: the #GValueArray passed in as @value_array
235 g_value_array_insert (GValueArray *value_array,
241 g_return_val_if_fail (value_array != NULL, NULL);
242 g_return_val_if_fail (index <= value_array->n_values, value_array);
244 /* we support NULL for "value" as a shortcut for an unset value */
246 i = value_array->n_values;
247 value_array_grow (value_array, value_array->n_values + 1, FALSE);
248 if (index + 1 < value_array->n_values)
249 g_memmove (value_array->values + index + 1, value_array->values + index,
250 (i - index) * sizeof (value_array->values[0]));
251 memset (value_array->values + index, 0, sizeof (value_array->values[0]));
254 g_value_init (value_array->values + index, G_VALUE_TYPE (value));
255 g_value_copy (value, value_array->values + index);
261 * g_value_array_remove:
262 * @value_array: #GValueArray to remove an element from
263 * @index_: position of value to remove, must be < value_array->n_values
265 * Remove the value at position @index_ from @value_array.
267 * Returns: the #GValueArray passed in as @value_array
270 g_value_array_remove (GValueArray *value_array,
273 g_return_val_if_fail (value_array != NULL, NULL);
274 g_return_val_if_fail (index < value_array->n_values, value_array);
276 if (G_VALUE_TYPE (value_array->values + index) != 0)
277 g_value_unset (value_array->values + index);
278 value_array->n_values--;
279 if (index < value_array->n_values)
280 g_memmove (value_array->values + index, value_array->values + index + 1,
281 (value_array->n_values - index) * sizeof (value_array->values[0]));
282 value_array_shrink (value_array);
283 if (value_array->n_prealloced > value_array->n_values)
284 memset (value_array->values + value_array->n_values, 0, sizeof (value_array->values[0]));
290 * g_value_array_sort:
291 * @value_array: #GValueArray to sort
292 * @compare_func: function to compare elements
294 * Sort @value_array using @compare_func to compare the elements accoring to
295 * the semantics of #GCompareFunc.
297 * The current implementation uses Quick-Sort as sorting algorithm.
299 * Returns: the #GValueArray passed in as @value_array
302 g_value_array_sort (GValueArray *value_array,
303 GCompareFunc compare_func)
305 g_return_val_if_fail (compare_func != NULL, NULL);
307 if (value_array->n_values)
308 qsort (value_array->values,
309 value_array->n_values,
310 sizeof (value_array->values[0]),
316 * g_value_array_sort_with_data:
317 * @value_array: #GValueArray to sort
318 * @compare_func: function to compare elements
319 * @user_data: extra data argument provided for @compare_func
321 * Sort @value_array using @compare_func to compare the elements accoring
322 * to the semantics of #GCompareDataFunc.
325 * The current implementation uses Quick-Sort as sorting algorithm.
327 * Returns: the #GValueArray passed in as @value_array
330 g_value_array_sort_with_data (GValueArray *value_array,
331 GCompareDataFunc compare_func,
334 g_return_val_if_fail (value_array != NULL, NULL);
335 g_return_val_if_fail (compare_func != NULL, NULL);
337 if (value_array->n_values)
338 g_qsort_with_data (value_array->values,
339 value_array->n_values,
340 sizeof (value_array->values[0]),
341 compare_func, user_data);
345 #define __G_VALUE_ARRAY_C__
346 #include "gobjectaliasdef.c"