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