2 * Copyright (c) 2010 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
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
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
42 struct heim_array_data
{
46 heim_object_t
*allocated
;
50 array_dealloc(heim_object_t ptr
)
52 heim_array_t array
= ptr
;
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
= {
73 * @return A new allocated array, free with heim_release()
77 heim_array_create(void)
81 array
= _heim_alloc_object(&array_object
, sizeof(*array
));
85 array
->allocated
= NULL
;
86 array
->allocated_len
= 0;
94 * Get type id of an dict
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
)
118 size_t leading
= array
->val
- array
->allocated
; /* unused leading slots */
119 size_t trailing
= array
->allocated_len
- array
->len
- leading
;
123 /* We have pre-allocated space; use it */
124 array
->val
[array
->len
++] = heim_retain(object
);
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
);
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]));
147 array
->allocated
= ptr
;
148 array
->allocated_len
= new_len
;
149 array
->val
= &ptr
[leading
];
150 array
->val
[array
->len
++] = heim_retain(object
);
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.
161 heim_array_prepend_value(heim_array_t array
, heim_object_t object
)
164 size_t leading
= array
->val
- array
->allocated
; /* unused leading slots */
165 size_t trailing
= array
->allocated_len
- array
->len
- leading
;
169 /* We have pre-allocated space; use it */
171 array
->val
[0] = heim_retain(object
);
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
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 */
187 array
->val
[0] = heim_retain(object
);
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]));
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
);
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
)
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))
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
);
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
255 heim_array_iterate_f(heim_array_t array
, void *ctx
, heim_array_iterator_f_t fn
)
259 for (n
= 0; n
< array
->len
; n
++) {
260 fn(array
->val
[n
], ctx
, &stop
);
268 * Iterate over all objects in array
270 * @param array array to iterate over
271 * @param fn block to call on each object
275 heim_array_iterate(heim_array_t array
, void (^fn
)(heim_object_t
, int *))
279 for (n
= 0; n
< array
->len
; n
++) {
280 fn(array
->val
[n
], &stop
);
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
296 heim_array_iterate_reverse_f(heim_array_t array
, void *ctx
, heim_array_iterator_f_t fn
)
301 for (n
= array
->len
; n
> 0; n
--) {
302 fn(array
->val
[n
- 1], ctx
, &stop
);
310 * Iterate over all objects in array, backwards
312 * @param array array to iterate over
313 * @param fn block to call on each object
317 heim_array_iterate_reverse(heim_array_t array
, void (^fn
)(heim_object_t
, int *))
321 for (n
= array
->len
; n
> 0; n
--) {
322 fn(array
->val
[n
- 1], &stop
);
330 * Get length of array
332 * @param array array to get length of
334 * @return length of array
338 heim_array_get_length(heim_array_t array
)
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
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
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
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
406 heim_array_delete_value(heim_array_t array
, size_t idx
)
409 if (idx
>= array
->len
)
410 heim_abort("index too large");
411 obj
= array
->val
[idx
];
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.
427 else if (idx
< array
->len
)
428 (void) memmove(&array
->val
[idx
], &array
->val
[idx
+ 1],
429 (array
->len
- idx
) * sizeof(array
->val
[0]));
435 * Filter out entres of array when function return true
437 * @param array the array to modify
438 * @param fn filter function
442 heim_array_filter_f(heim_array_t array
, void *ctx
, heim_array_filter_f_t fn
)
446 while (n
< array
->len
) {
447 if (fn(array
->val
[n
], ctx
)) {
448 heim_array_delete_value(array
, n
);
458 * Filter out entres of array when block return true
460 * @param array the array to modify
461 * @param block filter block
465 heim_array_filter(heim_array_t array
, int (^block
)(heim_object_t
))
469 while (n
< array
->len
) {
470 if (block(array
->val
[n
])) {
471 heim_array_delete_value(array
, n
);
478 #endif /* __BLOCKS__ */