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"
30 #include "gobjectalias.h"
34 * SECTION:value_arrays
36 * @Short_description: A container structure to maintain an array of
39 * @See_also:#GValue, #GParamSpecValueArray, g_param_spec_value_array()
41 * @Title: Value arrays
43 * The prime purpose of a #GValueArray is for it to be used as an
44 * object property that holds an array of values. A #GValueArray wraps
45 * an array of #GValue elements in order for it to be used as a boxed
46 * type through %G_TYPE_VALUE_ARRAY.
50 #ifdef DISABLE_MEM_POOLS
51 # define GROUP_N_VALUES (1) /* power of 2 !! */
53 # define GROUP_N_VALUES (8) /* power of 2 !! */
57 /* --- functions --- */
59 * g_value_array_get_nth:
60 * @value_array: #GValueArray to get a value from
61 * @index_: index of the value of interest
63 * Return a pointer to the value at @index_ containd in @value_array.
65 * Returns: pointer to a value at @index_ in @value_array
68 g_value_array_get_nth (GValueArray *value_array,
71 g_return_val_if_fail (value_array != NULL, NULL);
72 g_return_val_if_fail (index < value_array->n_values, NULL);
74 return value_array->values + index;
78 value_array_grow (GValueArray *value_array,
82 g_return_if_fail (n_values >= value_array->n_values);
84 value_array->n_values = n_values;
85 if (value_array->n_values > value_array->n_prealloced)
87 guint i = value_array->n_prealloced;
89 value_array->n_prealloced = (value_array->n_values + GROUP_N_VALUES - 1) & ~(GROUP_N_VALUES - 1);
90 value_array->values = g_renew (GValue, value_array->values, value_array->n_prealloced);
92 i = value_array->n_values;
93 memset (value_array->values + i, 0,
94 (value_array->n_prealloced - i) * sizeof (value_array->values[0]));
99 value_array_shrink (GValueArray *value_array)
101 #ifdef DISABLE_MEM_POOLS
102 if (value_array->n_prealloced >= value_array->n_values + GROUP_N_VALUES)
104 value_array->n_prealloced = (value_array->n_values + GROUP_N_VALUES - 1) & ~(GROUP_N_VALUES - 1);
105 value_array->values = g_renew (GValue, value_array->values, value_array->n_prealloced);
112 * @n_prealloced: number of values to preallocate space for
114 * Allocate and initialize a new #GValueArray, optionally preserve space
115 * for @n_prealloced elements. New arrays always contain 0 elements,
116 * regardless of the value of @n_prealloced.
118 * Returns: a newly allocated #GValueArray with 0 values
121 g_value_array_new (guint n_prealloced)
123 GValueArray *value_array = g_slice_new (GValueArray);
125 value_array->n_values = 0;
126 value_array->n_prealloced = 0;
127 value_array->values = NULL;
128 value_array_grow (value_array, n_prealloced, TRUE);
129 value_array->n_values = 0;
135 * g_value_array_free:
136 * @value_array: #GValueArray to free
138 * Free a #GValueArray including its contents.
141 g_value_array_free (GValueArray *value_array)
145 g_return_if_fail (value_array != NULL);
147 for (i = 0; i < value_array->n_values; i++)
149 GValue *value = value_array->values + i;
151 if (G_VALUE_TYPE (value) != 0) /* we allow unset values in the array */
152 g_value_unset (value);
154 g_free (value_array->values);
155 g_slice_free (GValueArray, value_array);
159 * g_value_array_copy:
160 * @value_array: #GValueArray to copy
162 * Construct an exact copy of a #GValueArray by duplicating all its
165 * Returns: Newly allocated copy of #GValueArray
168 g_value_array_copy (const GValueArray *value_array)
170 GValueArray *new_array;
173 g_return_val_if_fail (value_array != NULL, NULL);
175 new_array = g_slice_new (GValueArray);
176 new_array->n_values = 0;
177 new_array->values = NULL;
178 new_array->n_prealloced = 0;
179 value_array_grow (new_array, value_array->n_values, TRUE);
180 for (i = 0; i < new_array->n_values; i++)
181 if (G_VALUE_TYPE (value_array->values + i) != 0)
183 GValue *value = new_array->values + i;
185 g_value_init (value, G_VALUE_TYPE (value_array->values + i));
186 g_value_copy (value_array->values + i, value);
192 * g_value_array_prepend:
193 * @value_array: #GValueArray to add an element to
194 * @value: #GValue to copy into #GValueArray
196 * Insert a copy of @value as first element of @value_array.
198 * Returns: the #GValueArray passed in as @value_array
201 g_value_array_prepend (GValueArray *value_array,
204 g_return_val_if_fail (value_array != NULL, NULL);
206 return g_value_array_insert (value_array, 0, value);
210 * g_value_array_append:
211 * @value_array: #GValueArray to add an element to
212 * @value: #GValue to copy into #GValueArray
214 * Insert a copy of @value as last element of @value_array.
216 * Returns: the #GValueArray passed in as @value_array
219 g_value_array_append (GValueArray *value_array,
222 g_return_val_if_fail (value_array != NULL, NULL);
224 return g_value_array_insert (value_array, value_array->n_values, value);
228 * g_value_array_insert:
229 * @value_array: #GValueArray to add an element to
230 * @index_: insertion position, must be <= value_array->n_values
231 * @value: #GValue to copy into #GValueArray
233 * Insert a copy of @value at specified position into @value_array.
235 * Returns: 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 /* we support NULL for "value" as a shortcut for an unset value */
249 i = value_array->n_values;
250 value_array_grow (value_array, value_array->n_values + 1, FALSE);
251 if (index + 1 < value_array->n_values)
252 g_memmove (value_array->values + index + 1, value_array->values + index,
253 (i - index) * sizeof (value_array->values[0]));
254 memset (value_array->values + index, 0, sizeof (value_array->values[0]));
257 g_value_init (value_array->values + index, G_VALUE_TYPE (value));
258 g_value_copy (value, value_array->values + index);
264 * g_value_array_remove:
265 * @value_array: #GValueArray to remove an element from
266 * @index_: position of value to remove, must be < value_array->n_values
268 * Remove the value at position @index_ from @value_array.
270 * Returns: the #GValueArray passed in as @value_array
273 g_value_array_remove (GValueArray *value_array,
276 g_return_val_if_fail (value_array != NULL, NULL);
277 g_return_val_if_fail (index < value_array->n_values, value_array);
279 if (G_VALUE_TYPE (value_array->values + index) != 0)
280 g_value_unset (value_array->values + index);
281 value_array->n_values--;
282 if (index < value_array->n_values)
283 g_memmove (value_array->values + index, value_array->values + index + 1,
284 (value_array->n_values - index) * sizeof (value_array->values[0]));
285 value_array_shrink (value_array);
286 if (value_array->n_prealloced > value_array->n_values)
287 memset (value_array->values + value_array->n_values, 0, sizeof (value_array->values[0]));
293 * g_value_array_sort:
294 * @value_array: #GValueArray to sort
295 * @compare_func: function to compare elements
297 * Sort @value_array using @compare_func to compare the elements accoring to
298 * the semantics of #GCompareFunc.
300 * The current implementation uses Quick-Sort as sorting algorithm.
302 * Returns: the #GValueArray passed in as @value_array
305 g_value_array_sort (GValueArray *value_array,
306 GCompareFunc compare_func)
308 g_return_val_if_fail (compare_func != NULL, NULL);
310 if (value_array->n_values)
311 qsort (value_array->values,
312 value_array->n_values,
313 sizeof (value_array->values[0]),
319 * g_value_array_sort_with_data:
320 * @value_array: #GValueArray to sort
321 * @compare_func: function to compare elements
322 * @user_data: extra data argument provided for @compare_func
324 * Sort @value_array using @compare_func to compare the elements accoring
325 * to the semantics of #GCompareDataFunc.
327 * The current implementation uses Quick-Sort as sorting algorithm.
329 * Returns: the #GValueArray passed in as @value_array
332 g_value_array_sort_with_data (GValueArray *value_array,
333 GCompareDataFunc compare_func,
336 g_return_val_if_fail (value_array != NULL, NULL);
337 g_return_val_if_fail (compare_func != NULL, NULL);
339 if (value_array->n_values)
340 g_qsort_with_data (value_array->values,
341 value_array->n_values,
342 sizeof (value_array->values[0]),
343 compare_func, user_data);
347 #define __G_VALUE_ARRAY_C__
348 #include "gobjectaliasdef.c"