1 /* $NetBSD: array.c,v 1.1.1.2 2014/04/24 12:45:26 pettai Exp $ */
4 * Copyright (c) 2010 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Portions Copyright (c) 2010 Apple Inc. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the Institute nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 struct heim_array_data
{
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
]);
59 struct heim_type_data array_object
= {
72 * @return A new allocated array, free with heim_release()
76 heim_array_create(void)
80 array
= _heim_alloc_object(&array_object
, sizeof(*array
));
91 * Get type id of an dict
97 heim_array_get_type_id(void)
99 return HEIM_TID_ARRAY
;
103 * Append object to array
105 * @param array array to add too
106 * @param object the object to add
108 * @return zero if added, errno otherwise
112 heim_array_append_value(heim_array_t array
, heim_object_t object
)
116 ptr
= realloc(array
->val
, (array
->len
+ 1) * sizeof(array
->val
[0]));
120 array
->val
[array
->len
++] = heim_retain(object
);
126 * Iterate over all objects in array
128 * @param array array to iterate over
129 * @param fn function to call on each object
130 * @param ctx context passed to fn
134 heim_array_iterate_f(heim_array_t array
, heim_array_iterator_f_t fn
, void *ctx
)
137 for (n
= 0; n
< array
->len
; n
++)
138 fn(array
->val
[n
], ctx
);
143 * Iterate over all objects in array
145 * @param array array to iterate over
146 * @param fn block to call on each object
150 heim_array_iterate(heim_array_t array
, void (^fn
)(heim_object_t
))
153 for (n
= 0; n
< array
->len
; n
++)
159 * Get length of array
161 * @param array array to get length of
163 * @return length of array
167 heim_array_get_length(heim_array_t array
)
173 * Copy value of array
175 * @param array array copy object from
176 * @param idx index of object, 0 based, must be smaller then
177 * heim_array_get_length()
179 * @return a retained copy of the object
183 heim_array_copy_value(heim_array_t array
, size_t idx
)
185 if (idx
>= array
->len
)
186 heim_abort("index too large");
187 return heim_retain(array
->val
[idx
]);
191 * Delete value at idx
193 * @param array the array to modify
194 * @param idx the key to delete
198 heim_array_delete_value(heim_array_t array
, size_t idx
)
201 if (idx
>= array
->len
)
202 heim_abort("index too large");
203 obj
= array
->val
[idx
];
207 if (idx
< array
->len
)
208 memmove(&array
->val
[idx
], &array
->val
[idx
+ 1],
209 (array
->len
- idx
) * sizeof(array
->val
[0]));
218 * @param array the array to modify
219 * @param idx the key to delete
223 heim_array_filter(heim_array_t array
, int (^block
)(heim_object_t
))
227 while (n
< array
->len
) {
228 if (block(array
->val
[n
])) {
229 heim_array_delete_value(array
, n
);
236 #endif /* __BLOCKS__ */