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.
32 typedef struct _GPtrArrayPriv {
39 g_ptr_array_grow(GPtrArrayPriv *array, guint length)
41 guint new_length = array->len + length;
43 g_return_if_fail(array != NULL);
45 if(new_length <= array->size) {
51 while(array->size < new_length) {
55 array->size = MAX(array->size, 16);
56 array->pdata = g_realloc(array->pdata, array->size * sizeof(gpointer));
62 return g_ptr_array_sized_new(0);
66 g_ptr_array_sized_new(guint reserved_size)
68 GPtrArrayPriv *array = g_new0(GPtrArrayPriv, 1);
74 if(reserved_size > 0) {
75 g_ptr_array_grow(array, reserved_size);
78 return (GPtrArray *)array;
82 g_ptr_array_free(GPtrArray *array, gboolean free_seg)
84 gpointer *data = NULL;
86 g_return_val_if_fail(array != NULL, NULL);
100 g_ptr_array_set_size(GPtrArray *array, gint length)
102 g_return_if_fail(array != NULL);
104 if((size_t)length > array->len) {
105 g_ptr_array_grow((GPtrArrayPriv *)array, length);
106 memset(array->pdata + array->len, 0, (length - array->len)
114 g_ptr_array_add(GPtrArray *array, gpointer data)
116 g_return_if_fail(array != NULL);
117 g_ptr_array_grow((GPtrArrayPriv *)array, 1);
118 array->pdata[array->len++] = data;
122 g_ptr_array_remove_index(GPtrArray *array, guint index)
124 gpointer removed_node;
126 g_return_val_if_fail(array != NULL, NULL);
127 g_return_val_if_fail(index >= 0 || index < array->len, NULL);
129 removed_node = array->pdata[index];
131 if(index != array->len - 1) {
132 g_memmove(array->pdata + index, array->pdata + index + 1,
133 (array->len - index - 1) * sizeof(gpointer));
137 array->pdata[array->len] = NULL;
143 g_ptr_array_remove_index_fast(GPtrArray *array, guint index)
145 gpointer removed_node;
147 g_return_val_if_fail(array != NULL, NULL);
148 g_return_val_if_fail(index >= 0 || index < array->len, NULL);
150 removed_node = array->pdata[index];
152 if(index != array->len - 1) {
153 g_memmove(array->pdata + index, array->pdata + array->len - 1,
158 array->pdata[array->len] = NULL;
164 g_ptr_array_remove(GPtrArray *array, gpointer data)
168 g_return_val_if_fail(array != NULL, FALSE);
170 for(i = 0; i < array->len; i++) {
171 if(array->pdata[i] == data) {
172 g_ptr_array_remove_index(array, i);
181 g_ptr_array_remove_fast(GPtrArray *array, gpointer data)
185 g_return_val_if_fail(array != NULL, FALSE);
187 for(i = 0; i < array->len; i++) {
188 if(array->pdata[i] == data) {
191 array->pdata [i] = array->pdata [array->len];
193 array->pdata [i] = NULL;
202 g_ptr_array_foreach(GPtrArray *array, GFunc func, gpointer user_data)
206 for(i = 0; i < array->len; i++) {
207 func(g_ptr_array_index(array, i), user_data);
212 g_ptr_array_sort(GPtrArray *array, GCompareFunc compare_func)
214 g_return_if_fail(array != NULL);
215 qsort(array->pdata, array->len, sizeof(gpointer), compare_func);
219 g_ptr_array_sort_with_data(GPtrArray *array, GCompareDataFunc compare_func,