5 * Aaron Bockover (abockover@novell.com)
7 * (C) 2006 Novell, Inc.
9 * Permission is hereby granted, free of charge, to any person obtaining
10 * a copy of this software and associated documentation files (the
11 * "Software"), to deal in the Software without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sublicense, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 typedef struct _GPtrArrayPriv {
40 g_ptr_array_grow(GPtrArrayPriv *array, guint length)
42 gint new_length = array->len + length;
44 g_return_if_fail(array != NULL);
46 if(new_length <= array->size) {
52 while(array->size < new_length) {
56 array->size = MAX(array->size, 16);
57 array->pdata = g_realloc(array->pdata, array->size * sizeof(gpointer));
63 return g_ptr_array_sized_new(0);
67 g_ptr_array_sized_new(guint reserved_size)
69 GPtrArrayPriv *array = g_new0(GPtrArrayPriv, 1);
75 if(reserved_size > 0) {
76 g_ptr_array_grow(array, reserved_size);
79 return (GPtrArray *)array;
83 g_ptr_array_free(GPtrArray *array, gboolean free_seg)
85 gpointer *data = NULL;
87 g_return_val_if_fail(array != NULL, NULL);
101 g_ptr_array_set_size(GPtrArray *array, gint length)
103 g_return_if_fail(array != NULL);
105 if(length > array->len) {
106 g_ptr_array_grow((GPtrArrayPriv *)array, length);
107 memset(array->pdata + array->len, 0, (length - array->len)
115 g_ptr_array_add(GPtrArray *array, gpointer data)
117 g_return_if_fail(array != NULL);
118 g_ptr_array_grow((GPtrArrayPriv *)array, 1);
119 array->pdata[array->len++] = data;
123 g_ptr_array_remove_index(GPtrArray *array, guint index)
125 gpointer removed_node;
127 g_return_val_if_fail(array != NULL, NULL);
128 g_return_val_if_fail(index >= 0 || index < array->len, NULL);
130 removed_node = array->pdata[index];
132 if(index != array->len - 1) {
133 g_memmove(array->pdata + index, array->pdata + index + 1,
134 (array->len - index - 1) * sizeof(gpointer));
138 array->pdata[array->len] = NULL;
144 g_ptr_array_remove(GPtrArray *array, gpointer data)
148 g_return_val_if_fail(array != NULL, FALSE);
150 for(i = 0; i < array->len; i++) {
151 if(array->pdata[i] == data) {
152 g_ptr_array_remove_index(array, i);
161 g_ptr_array_foreach(GPtrArray *array, GFunc func, gpointer user_data)
165 for(i = 0; i < array->len; i++) {
166 func(g_ptr_array_index(array, i), user_data);
171 g_ptr_array_sort(GPtrArray *array, GCompareFunc compare_func)
173 g_return_if_fail(array != NULL);
174 qsort(array->pdata, array->len, sizeof(gpointer), compare_func);
178 g_ptr_array_sort_with_data(GPtrArray *array, GCompareDataFunc compare_func,