roken: qsort provide ANSI C prototype for swapfunc()
[heimdal.git] / lib / base / array.c
blob994fa7d38e4cd9c9122682c304648dc407566982
1 /*
2 * Copyright (c) 2010 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Portions Copyright (c) 2010 Apple Inc. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
36 #include "baselocl.h"
42 struct heim_array_data {
43 size_t len;
44 heim_object_t *val;
45 size_t allocated_len;
46 heim_object_t *allocated;
49 static void HEIM_CALLCONV
50 array_dealloc(heim_object_t ptr)
52 heim_array_t array = ptr;
53 size_t n;
54 for (n = 0; n < array->len; n++)
55 heim_release(array->val[n]);
56 free(array->allocated);
59 struct heim_type_data array_object = {
60 HEIM_TID_ARRAY,
61 "array-object",
62 NULL,
63 array_dealloc,
64 NULL,
65 NULL,
66 NULL,
67 NULL
70 /**
71 * Allocate an array
73 * @return A new allocated array, free with heim_release()
76 heim_array_t
77 heim_array_create(void)
79 heim_array_t array;
81 array = _heim_alloc_object(&array_object, sizeof(*array));
82 if (array == NULL)
83 return NULL;
85 array->allocated = NULL;
86 array->allocated_len = 0;
87 array->val = NULL;
88 array->len = 0;
90 return array;
93 /**
94 * Get type id of an dict
96 * @return the type id
99 heim_tid_t
100 heim_array_get_type_id(void)
102 return HEIM_TID_ARRAY;
106 * Append object to array
108 * @param array array to add too
109 * @param object the object to add
111 * @return zero if added, errno otherwise
115 heim_array_append_value(heim_array_t array, heim_object_t object)
117 heim_object_t *ptr;
118 size_t leading = array->val - array->allocated; /* unused leading slots */
119 size_t trailing = array->allocated_len - array->len - leading;
120 size_t new_len;
122 if (trailing > 0) {
123 /* We have pre-allocated space; use it */
124 array->val[array->len++] = heim_retain(object);
125 return 0;
128 if (leading > (array->len + 1)) {
130 * We must have appending to, and deleting at index 0 from this
131 * array a lot; don't want to grow forever!
133 (void) memmove(&array->allocated[0], &array->val[0],
134 array->len * sizeof(array->val[0]));
135 array->val = array->allocated;
137 /* We have pre-allocated space; use it */
138 array->val[array->len++] = heim_retain(object);
139 return 0;
142 /* Pre-allocate extra .5 times number of used slots */
143 new_len = leading + array->len + 1 + (array->len >> 1);
144 ptr = realloc(array->allocated, new_len * sizeof(array->val[0]));
145 if (ptr == NULL)
146 return ENOMEM;
147 array->allocated = ptr;
148 array->allocated_len = new_len;
149 array->val = &ptr[leading];
150 array->val[array->len++] = heim_retain(object);
152 return 0;
156 * Internal function to insert at index 0, taking care to optimize the
157 * case where we're always inserting at index 0, particularly the case
158 * where we insert at index 0 and delete from the right end.
160 static int
161 heim_array_prepend_value(heim_array_t array, heim_object_t object)
163 heim_object_t *ptr;
164 size_t leading = array->val - array->allocated; /* unused leading slots */
165 size_t trailing = array->allocated_len - array->len - leading;
166 size_t new_len;
168 if (leading > 0) {
169 /* We have pre-allocated space; use it */
170 array->val--;
171 array->val[0] = heim_retain(object);
172 array->len++;
173 return 0;
175 if (trailing > (array->len + 1)) {
177 * We must have prepending to, and deleting at index
178 * array->len - 1 from this array a lot; don't want to grow
179 * forever!
181 (void) memmove(&array->allocated[array->len], &array->val[0],
182 array->len * sizeof(array->val[0]));
183 array->val = &array->allocated[array->len];
185 /* We have pre-allocated space; use it */
186 array->val--;
187 array->val[0] = heim_retain(object);
188 array->len++;
189 return 0;
191 /* Pre-allocate extra .5 times number of used slots */
192 new_len = array->len + 1 + trailing + (array->len >> 1);
193 ptr = realloc(array->allocated, new_len * sizeof(array->val[0]));
194 if (ptr == NULL)
195 return ENOMEM;
196 (void) memmove(&ptr[1], &ptr[0], array->len * sizeof (array->val[0]));
197 array->allocated = ptr;
198 array->allocated_len = new_len;
199 array->val = &ptr[0];
200 array->val[0] = heim_retain(object);
201 array->len++;
203 return 0;
207 * Insert an object at a given index in an array
209 * @param array array to add too
210 * @param idx index where to add element (-1 == append, -2 next to last, ...)
211 * @param object the object to add
213 * @return zero if added, errno otherwise
217 heim_array_insert_value(heim_array_t array, size_t idx, heim_object_t object)
219 int ret;
221 if (idx == 0)
222 return heim_array_prepend_value(array, object);
223 else if (idx > array->len)
224 heim_abort("index too large");
227 * We cheat: append this element then rotate elements around so we
228 * have this new element at the desired location, unless we're truly
229 * appending the new element. This means reusing array growth in
230 * heim_array_append_value() instead of duplicating that here.
232 ret = heim_array_append_value(array, object);
233 if (ret != 0 || idx == (array->len - 1))
234 return ret;
236 * Shift to the right by one all the elements after idx, then set
237 * [idx] to the new object.
239 (void) memmove(&array->val[idx + 1], &array->val[idx],
240 (array->len - idx - 1) * sizeof(array->val[0]));
241 array->val[idx] = heim_retain(object);
243 return 0;
247 * Iterate over all objects in array
249 * @param array array to iterate over
250 * @param ctx context passed to fn
251 * @param fn function to call on each object
254 void
255 heim_array_iterate_f(heim_array_t array, void *ctx, heim_array_iterator_f_t fn)
257 size_t n;
258 int stop = 0;
259 for (n = 0; n < array->len; n++) {
260 fn(array->val[n], ctx, &stop);
261 if (stop)
262 return;
266 #ifdef __BLOCKS__
268 * Iterate over all objects in array
270 * @param array array to iterate over
271 * @param fn block to call on each object
274 void
275 heim_array_iterate(heim_array_t array, void (^fn)(heim_object_t, int *))
277 size_t n;
278 int stop = 0;
279 for (n = 0; n < array->len; n++) {
280 fn(array->val[n], &stop);
281 if (stop)
282 return;
285 #endif
288 * Iterate over all objects in array, backwards
290 * @param array array to iterate over
291 * @param ctx context passed to fn
292 * @param fn function to call on each object
295 void
296 heim_array_iterate_reverse_f(heim_array_t array, void *ctx, heim_array_iterator_f_t fn)
298 size_t n;
299 int stop = 0;
301 for (n = array->len; n > 0; n--) {
302 fn(array->val[n - 1], ctx, &stop);
303 if (stop)
304 return;
308 #ifdef __BLOCKS__
310 * Iterate over all objects in array, backwards
312 * @param array array to iterate over
313 * @param fn block to call on each object
316 void
317 heim_array_iterate_reverse(heim_array_t array, void (^fn)(heim_object_t, int *))
319 size_t n;
320 int stop = 0;
321 for (n = array->len; n > 0; n--) {
322 fn(array->val[n - 1], &stop);
323 if (stop)
324 return;
327 #endif
330 * Get length of array
332 * @param array array to get length of
334 * @return length of array
337 size_t
338 heim_array_get_length(heim_array_t array)
340 return array->len;
344 * Get value of element at array index
346 * @param array array copy object from
347 * @param idx index of object, 0 based, must be smaller then
348 * heim_array_get_length()
350 * @return a not-retained copy of the object
353 heim_object_t
354 heim_array_get_value(heim_array_t array, size_t idx)
356 if (idx >= array->len)
357 heim_abort("index too large");
358 return array->val[idx];
362 * Get value of element at array index
364 * @param array array copy object from
365 * @param idx index of object, 0 based, must be smaller then
366 * heim_array_get_length()
368 * @return a retained copy of the object
371 heim_object_t
372 heim_array_copy_value(heim_array_t array, size_t idx)
374 if (idx >= array->len)
375 heim_abort("index too large");
376 return heim_retain(array->val[idx]);
380 * Set value at array index
382 * @param array array copy object from
383 * @param idx index of object, 0 based, must be smaller then
384 * heim_array_get_length()
385 * @param value value to set
389 void
390 heim_array_set_value(heim_array_t array, size_t idx, heim_object_t value)
392 if (idx >= array->len)
393 heim_abort("index too large");
394 heim_release(array->val[idx]);
395 array->val[idx] = heim_retain(value);
399 * Delete value at idx
401 * @param array the array to modify
402 * @param idx the key to delete
405 void
406 heim_array_delete_value(heim_array_t array, size_t idx)
408 heim_object_t obj;
409 if (idx >= array->len)
410 heim_abort("index too large");
411 obj = array->val[idx];
413 array->len--;
416 * Deleting the first or last elements is cheap, as we leave
417 * allocated space for opportunistic reuse later; no realloc(), no
418 * memmove(). All others require a memmove().
420 * If we ever need to optimize deletion of non-last/ non-first
421 * element we can use a tagged object type to signify "deleted
422 * value" so we can leave holes in the array, avoid memmove()s on
423 * delete, and opportunistically re-use those holes on insert.
425 if (idx == 0)
426 array->val++;
427 else if (idx < array->len)
428 (void) memmove(&array->val[idx], &array->val[idx + 1],
429 (array->len - idx) * sizeof(array->val[0]));
431 heim_release(obj);
435 * Filter out entres of array when function return true
437 * @param array the array to modify
438 * @param fn filter function
441 void
442 heim_array_filter_f(heim_array_t array, void *ctx, heim_array_filter_f_t fn)
444 size_t n = 0;
446 while (n < array->len) {
447 if (fn(array->val[n], ctx)) {
448 heim_array_delete_value(array, n);
449 } else {
450 n++;
455 #ifdef __BLOCKS__
458 * Filter out entres of array when block return true
460 * @param array the array to modify
461 * @param block filter block
464 void
465 heim_array_filter(heim_array_t array, int (^block)(heim_object_t))
467 size_t n = 0;
469 while (n < array->len) {
470 if (block(array->val[n])) {
471 heim_array_delete_value(array, n);
472 } else {
473 n++;
478 #endif /* __BLOCKS__ */