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
{
48 array_dealloc(heim_object_t ptr
)
50 heim_array_t array
= ptr
;
52 for (n
= 0; n
< array
->len
; n
++)
53 heim_release(array
->val
[n
]);
57 struct heim_type_data array_object
= {
70 * @return A new allocated array, free with heim_release()
74 heim_array_create(void)
78 array
= _heim_alloc_object(&array_object
, sizeof(*array
));
89 * Get type id of an dict
95 heim_array_get_type_id(void)
97 return HEIM_TID_ARRAY
;
101 * Append object to array
103 * @param array array to add too
104 * @param object the object to add
106 * @return zero if added, errno otherwise
110 heim_array_append_value(heim_array_t array
, heim_object_t object
)
114 ptr
= realloc(array
->val
, (array
->len
+ 1) * sizeof(array
->val
[0]));
118 array
->val
[array
->len
++] = heim_retain(object
);
124 * Iterate over all objects in array
126 * @param array array to iterate over
127 * @param fn function to call on each object
128 * @param ctx context passed to fn
132 heim_array_iterate_f(heim_array_t array
, heim_array_iterator_f_t fn
, void *ctx
)
135 for (n
= 0; n
< array
->len
; n
++)
136 fn(array
->val
[n
], ctx
);
141 * Iterate over all objects in array
143 * @param array array to iterate over
144 * @param fn block to call on each object
148 heim_array_iterate(heim_array_t array
, void (^fn
)(heim_object_t
))
151 for (n
= 0; n
< array
->len
; n
++)
157 * Get length of array
159 * @param array array to get length of
161 * @return length of array
165 heim_array_get_length(heim_array_t array
)
171 * Copy value of array
173 * @param array array copy object from
174 * @param idx index of object, 0 based, must be smaller then
175 * heim_array_get_length()
177 * @return a retained copy of the object
181 heim_array_copy_value(heim_array_t array
, size_t idx
)
183 if (idx
>= array
->len
)
184 heim_abort("index too large");
185 return heim_retain(array
->val
[idx
]);
189 * Delete value at idx
191 * @param array the array to modify
192 * @param idx the key to delete
196 heim_array_delete_value(heim_array_t array
, size_t idx
)
199 if (idx
>= array
->len
)
200 heim_abort("index too large");
201 obj
= array
->val
[idx
];
205 if (idx
< array
->len
)
206 memmove(&array
->val
[idx
], &array
->val
[idx
+ 1],
207 (array
->len
- idx
) * sizeof(array
->val
[0]));
216 * @param array the array to modify
217 * @param idx the key to delete
221 heim_array_filter(heim_array_t array
, bool (^block
)(heim_object_t
))
225 while (n
< array
->len
) {
226 if (block(array
->val
[n
])) {
227 heim_array_delete_value(array
, n
);
234 #endif /* __BLOCKS__ */