remove trailing whitespace from newly added gtk-doc comments and
[platform/upstream/glib.git] / gobject / gvaluearray.c
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2  * Copyright (C) 2001 Red Hat, Inc.
3  *
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.
8  *
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.
13  *
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.
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 #include "gobjectalias.h"
31
32
33 /**
34  * SECTION:value_arrays
35  *
36  * @Short_description: A container structure to maintain an array of
37  * generic values
38  *
39  * @See_also:#GValue, #GParamSpecValueArray, g_param_spec_value_array()
40  *
41  * @Title: Value arrays
42  *
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.
47  */
48
49
50 #ifdef  DISABLE_MEM_POOLS
51 #  define       GROUP_N_VALUES  (1)     /* power of 2 !! */
52 #else
53 #  define       GROUP_N_VALUES  (8)     /* power of 2 !! */
54 #endif
55
56
57 /* --- functions --- */
58 /**
59  * g_value_array_get_nth:
60  * @value_array: #GValueArray to get a value from
61  * @index_: index of the value of interest
62  *
63  * Return a pointer to the value at @index_ containd in @value_array.
64  *
65  * Returns: pointer to a value at @index_ in @value_array
66  */
67 GValue*
68 g_value_array_get_nth (GValueArray *value_array,
69                        guint        index)
70 {
71   g_return_val_if_fail (value_array != NULL, NULL);
72   g_return_val_if_fail (index < value_array->n_values, NULL);
73
74   return value_array->values + index;
75 }
76
77 static inline void
78 value_array_grow (GValueArray *value_array,
79                   guint        n_values,
80                   gboolean     zero_init)
81 {
82   g_return_if_fail (n_values >= value_array->n_values);
83
84   value_array->n_values = n_values;
85   if (value_array->n_values > value_array->n_prealloced)
86     {
87       guint i = value_array->n_prealloced;
88
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);
91       if (!zero_init)
92         i = value_array->n_values;
93       memset (value_array->values + i, 0,
94               (value_array->n_prealloced - i) * sizeof (value_array->values[0]));
95     }
96 }
97
98 static inline void
99 value_array_shrink (GValueArray *value_array)
100 {
101 #ifdef  DISABLE_MEM_POOLS
102   if (value_array->n_prealloced >= value_array->n_values + GROUP_N_VALUES)
103     {
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);
106     }
107 #endif
108 }
109
110 /**
111  * g_value_array_new:
112  * @n_prealloced: number of values to preallocate space for
113  *
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.
117  *
118  * Returns: a newly allocated #GValueArray with 0 values
119  */
120 GValueArray*
121 g_value_array_new (guint n_prealloced)
122 {
123   GValueArray *value_array = g_slice_new (GValueArray);
124
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;
130
131   return value_array;
132 }
133
134 /**
135  * g_value_array_free:
136  * @value_array: #GValueArray to free
137  *
138  * Free a #GValueArray including its contents.
139  */
140 void
141 g_value_array_free (GValueArray *value_array)
142 {
143   guint i;
144
145   g_return_if_fail (value_array != NULL);
146
147   for (i = 0; i < value_array->n_values; i++)
148     {
149       GValue *value = value_array->values + i;
150
151       if (G_VALUE_TYPE (value) != 0) /* we allow unset values in the array */
152         g_value_unset (value);
153     }
154   g_free (value_array->values);
155   g_slice_free (GValueArray, value_array);
156 }
157
158 /**
159  * g_value_array_copy:
160  * @value_array: #GValueArray to copy
161  *
162  * Construct an exact copy of a #GValueArray by duplicating all its
163  * contents.
164  *
165  * Returns: Newly allocated copy of #GValueArray
166  */
167 GValueArray*
168 g_value_array_copy (const GValueArray *value_array)
169 {
170   GValueArray *new_array;
171   guint i;
172
173   g_return_val_if_fail (value_array != NULL, NULL);
174
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)
182       {
183         GValue *value = new_array->values + i;
184         
185         g_value_init (value, G_VALUE_TYPE (value_array->values + i));
186         g_value_copy (value_array->values + i, value);
187       }
188   return new_array;
189 }
190
191 /**
192  * g_value_array_prepend:
193  * @value_array: #GValueArray to add an element to
194  * @value: #GValue to copy into #GValueArray
195  *
196  * Insert a copy of @value as first element of @value_array.
197  *
198  * Returns: the #GValueArray passed in as @value_array
199  */
200 GValueArray*
201 g_value_array_prepend (GValueArray  *value_array,
202                        const GValue *value)
203 {
204   g_return_val_if_fail (value_array != NULL, NULL);
205
206   return g_value_array_insert (value_array, 0, value);
207 }
208
209 /**
210  * g_value_array_append:
211  * @value_array: #GValueArray to add an element to
212  * @value: #GValue to copy into #GValueArray
213  *
214  * Insert a copy of @value as last element of @value_array.
215  *
216  * Returns: the #GValueArray passed in as @value_array
217  */
218 GValueArray*
219 g_value_array_append (GValueArray  *value_array,
220                       const GValue *value)
221 {
222   g_return_val_if_fail (value_array != NULL, NULL);
223
224   return g_value_array_insert (value_array, value_array->n_values, value);
225 }
226
227 /**
228  * g_value_array_insert:
229  * @value_array: #GValueArray to add an element to
230  * @index_: insertion position, must be &lt;= value_array-&gt;n_values
231  * @value: #GValue to copy into #GValueArray
232  *
233  * Insert a copy of @value at specified position into @value_array.
234  *
235  * Returns: the #GValueArray passed in as @value_array
236  */
237 GValueArray*
238 g_value_array_insert (GValueArray  *value_array,
239                       guint         index,
240                       const GValue *value)
241 {
242   guint i;
243
244   g_return_val_if_fail (value_array != NULL, NULL);
245   g_return_val_if_fail (index <= value_array->n_values, value_array);
246
247   /* we support NULL for "value" as a shortcut for an unset value */
248
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]));
255   if (value)
256     {
257       g_value_init (value_array->values + index, G_VALUE_TYPE (value));
258       g_value_copy (value, value_array->values + index);
259     }
260   return value_array;
261 }
262
263 /**
264  * g_value_array_remove:
265  * @value_array: #GValueArray to remove an element from
266  * @index_: position of value to remove, must be &lt; value_array->n_values
267  *
268  * Remove the value at position @index_ from @value_array.
269  *
270  * Returns: the #GValueArray passed in as @value_array
271  */
272 GValueArray*
273 g_value_array_remove (GValueArray *value_array,
274                       guint        index)
275 {
276   g_return_val_if_fail (value_array != NULL, NULL);
277   g_return_val_if_fail (index < value_array->n_values, value_array);
278
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]));
288
289   return value_array;
290 }
291
292 /**
293  * g_value_array_sort:
294  * @value_array: #GValueArray to sort
295  * @compare_func: function to compare elements
296  *
297  * Sort @value_array using @compare_func to compare the elements accoring to
298  * the semantics of #GCompareFunc.
299  *
300  * The current implementation uses Quick-Sort as sorting algorithm.
301  *
302  * Returns: the #GValueArray passed in as @value_array
303  */
304 GValueArray*
305 g_value_array_sort (GValueArray *value_array,
306                     GCompareFunc compare_func)
307 {
308   g_return_val_if_fail (compare_func != NULL, NULL);
309
310   if (value_array->n_values)
311     qsort (value_array->values,
312            value_array->n_values,
313            sizeof (value_array->values[0]),
314            compare_func);
315   return value_array;
316 }
317
318 /**
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
323  *
324  * Sort @value_array using @compare_func to compare the elements accoring
325  * to the semantics of #GCompareDataFunc.
326  *
327  * The current implementation uses Quick-Sort as sorting algorithm.
328  *
329  * Returns: the #GValueArray passed in as @value_array
330  */
331 GValueArray*
332 g_value_array_sort_with_data (GValueArray     *value_array,
333                               GCompareDataFunc compare_func,
334                               gpointer         user_data)
335 {
336   g_return_val_if_fail (value_array != NULL, NULL);
337   g_return_val_if_fail (compare_func != NULL, NULL);
338
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);
344   return value_array;
345 }
346
347 #define __G_VALUE_ARRAY_C__
348 #include "gobjectaliasdef.c"