Branch data Line data Source code
1 : : /* GObject - GLib Type, Object, Parameter and Signal Library
2 : : * Copyright (C) 2001 Red Hat, Inc.
3 : : *
4 : : * SPDX-License-Identifier: LGPL-2.1-or-later
5 : : *
6 : : * This library is free software; you can redistribute it and/or
7 : : * modify it under the terms of the GNU Lesser General Public
8 : : * License as published by the Free Software Foundation; either
9 : : * version 2.1 of the License, or (at your option) any later version.
10 : : *
11 : : * This library is distributed in the hope that it will be useful,
12 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 : : * Lesser General Public License for more details.
15 : : *
16 : : * You should have received a copy of the GNU Lesser General
17 : : * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 : : */
19 : :
20 : : /*
21 : : * MT safe
22 : : */
23 : :
24 : : #include "config.h"
25 : :
26 : : #include <string.h>
27 : : #include <stdlib.h> /* qsort() */
28 : :
29 : : #include "gvaluearray.h"
30 : :
31 : :
32 : : /**
33 : : * GValueArray:
34 : : * @n_values: number of values contained in the array
35 : : * @values: array of values
36 : : *
37 : : * A `GValueArray` is a container structure to hold an array of generic values.
38 : : *
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`.
43 : : *
44 : : * `GValueArray` is deprecated in favour of `GArray` since GLib 2.32.
45 : : * It is possible to create a `GArray` that behaves like a `GValueArray`
46 : : * by using the size of `GValue` as the element size, and by setting
47 : : * [method@GObject.Value.unset] as the clear function using
48 : : * [func@GLib.Array.set_clear_func], for instance, the following code:
49 : : *
50 : : * ```c
51 : : * GValueArray *array = g_value_array_new (10);
52 : : * ```
53 : : *
54 : : * can be replaced by:
55 : : *
56 : : * ```c
57 : : * GArray *array = g_array_sized_new (FALSE, TRUE, sizeof (GValue), 10);
58 : : * g_array_set_clear_func (array, (GDestroyNotify) g_value_unset);
59 : : * ```
60 : : *
61 : : * Deprecated: 2.32: Use `GArray` instead, if possible for the given use case,
62 : : * as described above.
63 : : */
64 : :
65 : : #define GROUP_N_VALUES (8u) /* power of 2 !! */
66 : :
67 : :
68 : : /* --- functions --- */
69 : : /**
70 : : * g_value_array_get_nth:
71 : : * @value_array: #GValueArray to get a value from
72 : : * @index_: index of the value of interest
73 : : *
74 : : * Return a pointer to the value at @index_ contained in @value_array.
75 : : *
76 : : * Returns: (transfer none): pointer to a value at @index_ in @value_array
77 : : *
78 : : * Deprecated: 2.32: Use g_array_index() instead.
79 : : */
80 : : GValue*
81 : 1 : g_value_array_get_nth (GValueArray *value_array,
82 : : guint index)
83 : : {
84 : 1 : g_return_val_if_fail (value_array != NULL, NULL);
85 : 1 : g_return_val_if_fail (index < value_array->n_values, NULL);
86 : :
87 : 1 : return value_array->values + index;
88 : : }
89 : :
90 : : static inline void
91 : 253 : value_array_grow (GValueArray *value_array,
92 : : guint n_values,
93 : : gboolean zero_init)
94 : : {
95 : 253 : g_return_if_fail (n_values >= value_array->n_values);
96 : :
97 : 253 : value_array->n_values = n_values;
98 : 253 : if (value_array->n_values > value_array->n_prealloced)
99 : : {
100 : 27 : guint i = value_array->n_prealloced;
101 : :
102 : : /* round up to the next multiple of GROUP_N_VALUES */
103 : 27 : value_array->n_prealloced = (value_array->n_values + GROUP_N_VALUES - 1) & ~(GROUP_N_VALUES - 1);
104 : 27 : value_array->values = g_renew (GValue, value_array->values, value_array->n_prealloced);
105 : 27 : if (!zero_init)
106 : 24 : i = value_array->n_values;
107 : 27 : memset (value_array->values + i, 0,
108 : 27 : (value_array->n_prealloced - i) * sizeof (value_array->values[0]));
109 : : }
110 : : }
111 : :
112 : : /**
113 : : * g_value_array_new:
114 : : * @n_prealloced: number of values to preallocate space for
115 : : *
116 : : * Allocate and initialize a new #GValueArray, optionally preserve space
117 : : * for @n_prealloced elements. New arrays always contain 0 elements,
118 : : * regardless of the value of @n_prealloced.
119 : : *
120 : : * Returns: a newly allocated #GValueArray with 0 values
121 : : *
122 : : * Deprecated: 2.32: Use #GArray and g_array_sized_new() instead.
123 : : */
124 : : GValueArray*
125 : 2 : g_value_array_new (guint n_prealloced)
126 : : {
127 : 2 : GValueArray *value_array = g_slice_new (GValueArray);
128 : :
129 : 2 : value_array->n_values = 0;
130 : 2 : value_array->n_prealloced = 0;
131 : 2 : value_array->values = NULL;
132 : 2 : value_array_grow (value_array, n_prealloced, TRUE);
133 : 2 : value_array->n_values = 0;
134 : :
135 : 2 : return value_array;
136 : : }
137 : :
138 : : /**
139 : : * g_value_array_free: (skip)
140 : : * @value_array: #GValueArray to free
141 : : *
142 : : * Free a #GValueArray including its contents.
143 : : *
144 : : * Deprecated: 2.32: Use #GArray and g_array_unref() instead.
145 : : */
146 : : void
147 : 3 : g_value_array_free (GValueArray *value_array)
148 : : {
149 : : guint i;
150 : :
151 : 3 : g_return_if_fail (value_array != NULL);
152 : :
153 : 371 : for (i = 0; i < value_array->n_values; i++)
154 : : {
155 : 368 : GValue *value = value_array->values + i;
156 : :
157 : 368 : if (G_VALUE_TYPE (value) != 0) /* we allow unset values in the array */
158 : 368 : g_value_unset (value);
159 : : }
160 : 3 : g_free (value_array->values);
161 : 3 : g_slice_free (GValueArray, value_array);
162 : : }
163 : :
164 : : /**
165 : : * g_value_array_copy:
166 : : * @value_array: #GValueArray to copy
167 : : *
168 : : * Construct an exact copy of a #GValueArray by duplicating all its
169 : : * contents.
170 : : *
171 : : * Returns: (transfer full): Newly allocated copy of #GValueArray
172 : : *
173 : : * Deprecated: 2.32: Use #GArray and g_array_ref() instead.
174 : : */
175 : : GValueArray*
176 : 1 : g_value_array_copy (const GValueArray *value_array)
177 : : {
178 : : GValueArray *new_array;
179 : : guint i;
180 : :
181 : 1 : g_return_val_if_fail (value_array != NULL, NULL);
182 : :
183 : 1 : new_array = g_slice_new (GValueArray);
184 : 1 : new_array->n_values = 0;
185 : 1 : new_array->values = NULL;
186 : 1 : new_array->n_prealloced = 0;
187 : 1 : value_array_grow (new_array, value_array->n_values, TRUE);
188 : 135 : for (i = 0; i < new_array->n_values; i++)
189 : 134 : if (G_VALUE_TYPE (value_array->values + i) != 0)
190 : : {
191 : 134 : GValue *value = new_array->values + i;
192 : :
193 : 134 : g_value_init (value, G_VALUE_TYPE (value_array->values + i));
194 : 134 : g_value_copy (value_array->values + i, value);
195 : : }
196 : 1 : return new_array;
197 : : }
198 : :
199 : : /**
200 : : * g_value_array_prepend:
201 : : * @value_array: #GValueArray to add an element to
202 : : * @value: (nullable): #GValue to copy into #GValueArray, or %NULL
203 : : *
204 : : * Insert a copy of @value as first element of @value_array. If @value is
205 : : * %NULL, an uninitialized value is prepended.
206 : : *
207 : : *
208 : : * Returns: (transfer none): the #GValueArray passed in as @value_array
209 : : *
210 : : * Deprecated: 2.32: Use #GArray and g_array_prepend_val() instead.
211 : : */
212 : : GValueArray*
213 : 50 : g_value_array_prepend (GValueArray *value_array,
214 : : const GValue *value)
215 : : {
216 : 50 : g_return_val_if_fail (value_array != NULL, NULL);
217 : :
218 : : G_GNUC_BEGIN_IGNORE_DEPRECATIONS
219 : 50 : return g_value_array_insert (value_array, 0, value);
220 : : G_GNUC_END_IGNORE_DEPRECATIONS
221 : : }
222 : :
223 : : /**
224 : : * g_value_array_append:
225 : : * @value_array: #GValueArray to add an element to
226 : : * @value: (nullable): #GValue to copy into #GValueArray, or %NULL
227 : : *
228 : : * Insert a copy of @value as last element of @value_array. If @value is
229 : : * %NULL, an uninitialized value is appended.
230 : : *
231 : : * Returns: (transfer none): the #GValueArray passed in as @value_array
232 : : *
233 : : * Deprecated: 2.32: Use #GArray and g_array_append_val() instead.
234 : : */
235 : : GValueArray*
236 : 200 : g_value_array_append (GValueArray *value_array,
237 : : const GValue *value)
238 : : {
239 : 200 : g_return_val_if_fail (value_array != NULL, NULL);
240 : :
241 : : G_GNUC_BEGIN_IGNORE_DEPRECATIONS
242 : 200 : return g_value_array_insert (value_array, value_array->n_values, value);
243 : : G_GNUC_END_IGNORE_DEPRECATIONS
244 : : }
245 : :
246 : : /**
247 : : * g_value_array_insert:
248 : : * @value_array: #GValueArray to add an element to
249 : : * @index_: insertion position, must be <= value_array->;n_values
250 : : * @value: (nullable): #GValue to copy into #GValueArray, or %NULL
251 : : *
252 : : * Insert a copy of @value at specified position into @value_array. If @value
253 : : * is %NULL, an uninitialized value is inserted.
254 : : *
255 : : * Returns: (transfer none): the #GValueArray passed in as @value_array
256 : : *
257 : : * Deprecated: 2.32: Use #GArray and g_array_insert_val() instead.
258 : : */
259 : : GValueArray*
260 : 250 : g_value_array_insert (GValueArray *value_array,
261 : : guint index,
262 : : const GValue *value)
263 : : {
264 : : guint i;
265 : :
266 : 250 : g_return_val_if_fail (value_array != NULL, NULL);
267 : 250 : g_return_val_if_fail (index <= value_array->n_values, value_array);
268 : :
269 : 250 : i = value_array->n_values;
270 : 250 : value_array_grow (value_array, value_array->n_values + 1, FALSE);
271 : 250 : if (index + 1 < value_array->n_values)
272 : 50 : memmove (value_array->values + index + 1, value_array->values + index,
273 : 50 : (i - index) * sizeof (value_array->values[0]));
274 : 250 : memset (value_array->values + index, 0, sizeof (value_array->values[0]));
275 : 250 : if (value)
276 : : {
277 : 250 : g_value_init (value_array->values + index, G_VALUE_TYPE (value));
278 : 250 : g_value_copy (value, value_array->values + index);
279 : : }
280 : 250 : return value_array;
281 : : }
282 : :
283 : : /**
284 : : * g_value_array_remove:
285 : : * @value_array: #GValueArray to remove an element from
286 : : * @index_: position of value to remove, which must be less than
287 : : * @value_array->n_values
288 : : *
289 : : * Remove the value at position @index_ from @value_array.
290 : : *
291 : : * Returns: (transfer none): the #GValueArray passed in as @value_array
292 : : *
293 : : * Deprecated: 2.32: Use #GArray and g_array_remove_index() instead.
294 : : */
295 : : GValueArray*
296 : 16 : g_value_array_remove (GValueArray *value_array,
297 : : guint index)
298 : : {
299 : 16 : g_return_val_if_fail (value_array != NULL, NULL);
300 : 16 : g_return_val_if_fail (index < value_array->n_values, value_array);
301 : :
302 : 16 : if (G_VALUE_TYPE (value_array->values + index) != 0)
303 : 16 : g_value_unset (value_array->values + index);
304 : 16 : value_array->n_values--;
305 : 16 : if (index < value_array->n_values)
306 : 16 : memmove (value_array->values + index, value_array->values + index + 1,
307 : 16 : (value_array->n_values - index) * sizeof (value_array->values[0]));
308 : 16 : if (value_array->n_prealloced > value_array->n_values)
309 : 16 : memset (value_array->values + value_array->n_values, 0, sizeof (value_array->values[0]));
310 : :
311 : 16 : return value_array;
312 : : }
313 : :
314 : : /**
315 : : * g_value_array_sort:
316 : : * @value_array: #GValueArray to sort
317 : : * @compare_func: (scope call): function to compare elements
318 : : *
319 : : * Sort @value_array using @compare_func to compare the elements according to
320 : : * the semantics of #GCompareFunc.
321 : : *
322 : : * The current implementation uses the same sorting algorithm as standard
323 : : * C qsort() function.
324 : : *
325 : : * Returns: (transfer none): the #GValueArray passed in as @value_array
326 : : *
327 : : * Deprecated: 2.32: Use #GArray and g_array_sort().
328 : : */
329 : : GValueArray*
330 : 1 : g_value_array_sort (GValueArray *value_array,
331 : : GCompareFunc compare_func)
332 : : {
333 : 1 : g_return_val_if_fail (compare_func != NULL, NULL);
334 : :
335 : 1 : if (value_array->n_values)
336 : 1 : qsort (value_array->values,
337 : 1 : value_array->n_values,
338 : : sizeof (value_array->values[0]),
339 : : compare_func);
340 : 1 : return value_array;
341 : : }
342 : :
343 : : /**
344 : : * g_value_array_sort_with_data: (rename-to g_value_array_sort)
345 : : * @value_array: #GValueArray to sort
346 : : * @compare_func: (scope call): function to compare elements
347 : : * @user_data: (closure): extra data argument provided for @compare_func
348 : : *
349 : : * Sort @value_array using @compare_func to compare the elements according
350 : : * to the semantics of #GCompareDataFunc.
351 : : *
352 : : * The current implementation uses the same sorting algorithm as standard
353 : : * C qsort() function.
354 : : *
355 : : * Returns: (transfer none): the #GValueArray passed in as @value_array
356 : : *
357 : : * Deprecated: 2.32: Use #GArray and g_array_sort_with_data().
358 : : */
359 : : GValueArray*
360 : 2 : g_value_array_sort_with_data (GValueArray *value_array,
361 : : GCompareDataFunc compare_func,
362 : : gpointer user_data)
363 : : {
364 : 2 : g_return_val_if_fail (value_array != NULL, NULL);
365 : 2 : g_return_val_if_fail (compare_func != NULL, NULL);
366 : :
367 : 2 : if (value_array->n_values)
368 : 1 : g_sort_array (value_array->values,
369 : 1 : value_array->n_values,
370 : : sizeof (value_array->values[0]),
371 : : compare_func, user_data);
372 : 2 : return value_array;
373 : : }
|