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.
24 #include "gvaluearray.h"
26 #include <stdlib.h> /* qsort() */
28 #ifdef DISABLE_MEM_POOLS
29 # define GROUP_N_VALUES (1) /* power of 2 !! */
31 # define GROUP_N_VALUES (8) /* power of 2 !! */
35 /* --- functions --- */
37 g_value_array_get_nth (GValueArray *value_array,
40 g_return_val_if_fail (value_array != NULL, NULL);
41 g_return_val_if_fail (index < value_array->n_values, NULL);
43 return value_array->values + index;
47 value_array_grow (GValueArray *value_array,
51 g_return_if_fail (n_values >= value_array->n_values);
53 value_array->n_values = n_values;
54 if (value_array->n_values > value_array->n_prealloced)
56 guint i = value_array->n_prealloced;
58 value_array->n_prealloced = (value_array->n_values + GROUP_N_VALUES - 1) & ~(GROUP_N_VALUES - 1);
59 value_array->values = g_renew (GValue, value_array->values, value_array->n_prealloced);
61 i = value_array->n_values;
62 memset (value_array->values + i, 0,
63 (value_array->n_prealloced - i) * sizeof (value_array->values[0]));
68 value_array_shrink (GValueArray *value_array)
70 #ifdef DISABLE_MEM_POOLS
71 if (value_array->n_prealloced >= value_array->n_values + GROUP_N_VALUES)
73 value_array->n_prealloced = (value_array->n_values + GROUP_N_VALUES - 1) & ~(GROUP_N_VALUES - 1);
74 value_array->values = g_renew (GValue, value_array->values, value_array->n_prealloced);
80 g_value_array_new (guint n_prealloced)
82 GValueArray *value_array = g_new (GValueArray, 1);
84 value_array->n_values = 0;
85 value_array->n_prealloced = 0;
86 value_array->values = NULL;
87 value_array_grow (value_array, n_prealloced, TRUE);
88 value_array->n_values = 0;
94 g_value_array_free (GValueArray *value_array)
98 g_return_if_fail (value_array != NULL);
100 for (i = 0; i < value_array->n_values; i++)
102 GValue *value = value_array->values + i;
104 if (G_VALUE_TYPE (value) != 0) /* we allow unset values in the array */
105 g_value_unset (value);
107 g_free (value_array->values);
108 g_free (value_array);
112 g_value_array_copy (const GValueArray *value_array)
114 GValueArray *new_array;
117 g_return_val_if_fail (value_array != NULL, NULL);
119 new_array = g_new (GValueArray, 1);
120 new_array->n_values = 0;
121 new_array->values = NULL;
122 new_array->n_prealloced = 0;
123 value_array_grow (new_array, value_array->n_values, TRUE);
124 for (i = 0; i < new_array->n_values; i++)
125 if (G_VALUE_TYPE (value_array->values + i) != 0)
127 GValue *value = new_array->values + i;
129 g_value_init (value, G_VALUE_TYPE (value_array->values + i));
130 g_value_copy (value_array->values + i, value);
136 g_value_array_prepend (GValueArray *value_array,
139 g_return_val_if_fail (value_array != NULL, NULL);
141 return g_value_array_insert (value_array, 0, value);
145 g_value_array_append (GValueArray *value_array,
148 g_return_val_if_fail (value_array != NULL, NULL);
150 return g_value_array_insert (value_array, value_array->n_values, value);
154 g_value_array_insert (GValueArray *value_array,
160 g_return_val_if_fail (value_array != NULL, NULL);
161 g_return_val_if_fail (index <= value_array->n_values, value_array);
163 /* we support NULL for "value" as a shortcut for an unset value */
165 i = value_array->n_values;
166 value_array_grow (value_array, value_array->n_values + 1, FALSE);
167 if (index + 1 < value_array->n_values)
168 g_memmove (value_array->values + index + 1, value_array->values + index,
169 (i - index) * sizeof (value_array->values[0]));
170 memset (value_array->values + index, 0, sizeof (value_array->values[0]));
173 g_value_init (value_array->values + index, G_VALUE_TYPE (value));
174 g_value_copy (value, value_array->values + index);
180 g_value_array_remove (GValueArray *value_array,
183 g_return_val_if_fail (value_array != NULL, NULL);
184 g_return_val_if_fail (index < value_array->n_values, value_array);
186 if (G_VALUE_TYPE (value_array->values + index) != 0)
187 g_value_unset (value_array->values + index);
188 value_array->n_values--;
189 if (index < value_array->n_values)
190 g_memmove (value_array->values + index, value_array->values + index + 1,
191 (value_array->n_values - index) * sizeof (value_array->values[0]));
192 value_array_shrink (value_array);
193 if (value_array->n_prealloced > value_array->n_values)
194 memset (value_array->values + value_array->n_values, 0, sizeof (value_array->values[0]));
200 g_value_array_sort (GValueArray *value_array,
201 GCompareFunc compare_func)
203 g_return_val_if_fail (compare_func != NULL, NULL);
205 if (value_array->n_values)
206 qsort (value_array->values,
207 value_array->n_values,
208 sizeof (value_array->values[0]),
214 g_value_array_sort_with_data (GValueArray *value_array,
215 GCompareDataFunc compare_func,
218 g_return_val_if_fail (value_array != NULL, NULL);
219 g_return_val_if_fail (compare_func != NULL, NULL);
221 if (value_array->n_values)
222 g_qsort_with_data (value_array->values,
223 value_array->n_values,
224 sizeof (value_array->values[0]),
225 compare_func, user_data);