xref: /minix3/crypto/external/bsd/heimdal/dist/base/array.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: array.c,v 1.1.1.2 2014/04/24 12:45:26 pettai Exp $	*/
2ebfedea0SLionel Sambuc 
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc  * Copyright (c) 2010 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc  * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc  * All rights reserved.
7ebfedea0SLionel Sambuc  *
8ebfedea0SLionel Sambuc  * Portions Copyright (c) 2010 Apple Inc. All rights reserved.
9ebfedea0SLionel Sambuc  *
10ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
11ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
12ebfedea0SLionel Sambuc  * are met:
13ebfedea0SLionel Sambuc  *
14ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
15ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
16ebfedea0SLionel Sambuc  *
17ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
18ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
19ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
20ebfedea0SLionel Sambuc  *
21ebfedea0SLionel Sambuc  * 3. Neither the name of the Institute nor the names of its contributors
22ebfedea0SLionel Sambuc  *    may be used to endorse or promote products derived from this software
23ebfedea0SLionel Sambuc  *    without specific prior written permission.
24ebfedea0SLionel Sambuc  *
25ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
26ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
29ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35ebfedea0SLionel Sambuc  * SUCH DAMAGE.
36ebfedea0SLionel Sambuc  */
37ebfedea0SLionel Sambuc 
38ebfedea0SLionel Sambuc #include "baselocl.h"
39ebfedea0SLionel Sambuc 
40ebfedea0SLionel Sambuc /*
41ebfedea0SLionel Sambuc  *
42ebfedea0SLionel Sambuc  */
43ebfedea0SLionel Sambuc 
44ebfedea0SLionel Sambuc struct heim_array_data {
45ebfedea0SLionel Sambuc     size_t len;
46ebfedea0SLionel Sambuc     heim_object_t *val;
47ebfedea0SLionel Sambuc };
48ebfedea0SLionel Sambuc 
49ebfedea0SLionel Sambuc static void
array_dealloc(heim_object_t ptr)50ebfedea0SLionel Sambuc array_dealloc(heim_object_t ptr)
51ebfedea0SLionel Sambuc {
52ebfedea0SLionel Sambuc     heim_array_t array = ptr;
53ebfedea0SLionel Sambuc     size_t n;
54ebfedea0SLionel Sambuc     for (n = 0; n < array->len; n++)
55ebfedea0SLionel Sambuc 	heim_release(array->val[n]);
56ebfedea0SLionel Sambuc     free(array->val);
57ebfedea0SLionel Sambuc }
58ebfedea0SLionel Sambuc 
59ebfedea0SLionel Sambuc struct heim_type_data array_object = {
60ebfedea0SLionel Sambuc     HEIM_TID_ARRAY,
61ebfedea0SLionel Sambuc     "dict-object",
62ebfedea0SLionel Sambuc     NULL,
63ebfedea0SLionel Sambuc     array_dealloc,
64ebfedea0SLionel Sambuc     NULL,
65ebfedea0SLionel Sambuc     NULL,
66ebfedea0SLionel Sambuc     NULL
67ebfedea0SLionel Sambuc };
68ebfedea0SLionel Sambuc 
69ebfedea0SLionel Sambuc /**
70ebfedea0SLionel Sambuc  * Allocate an array
71ebfedea0SLionel Sambuc  *
72ebfedea0SLionel Sambuc  * @return A new allocated array, free with heim_release()
73ebfedea0SLionel Sambuc  */
74ebfedea0SLionel Sambuc 
75ebfedea0SLionel Sambuc heim_array_t
heim_array_create(void)76ebfedea0SLionel Sambuc heim_array_create(void)
77ebfedea0SLionel Sambuc {
78ebfedea0SLionel Sambuc     heim_array_t array;
79ebfedea0SLionel Sambuc 
80ebfedea0SLionel Sambuc     array = _heim_alloc_object(&array_object, sizeof(*array));
81ebfedea0SLionel Sambuc     if (array == NULL)
82ebfedea0SLionel Sambuc 	return NULL;
83ebfedea0SLionel Sambuc 
84ebfedea0SLionel Sambuc     array->val = NULL;
85ebfedea0SLionel Sambuc     array->len = 0;
86ebfedea0SLionel Sambuc 
87ebfedea0SLionel Sambuc     return array;
88ebfedea0SLionel Sambuc }
89ebfedea0SLionel Sambuc 
90ebfedea0SLionel Sambuc /**
91ebfedea0SLionel Sambuc  * Get type id of an dict
92ebfedea0SLionel Sambuc  *
93ebfedea0SLionel Sambuc  * @return the type id
94ebfedea0SLionel Sambuc  */
95ebfedea0SLionel Sambuc 
96ebfedea0SLionel Sambuc heim_tid_t
heim_array_get_type_id(void)97ebfedea0SLionel Sambuc heim_array_get_type_id(void)
98ebfedea0SLionel Sambuc {
99ebfedea0SLionel Sambuc     return HEIM_TID_ARRAY;
100ebfedea0SLionel Sambuc }
101ebfedea0SLionel Sambuc 
102ebfedea0SLionel Sambuc /**
103ebfedea0SLionel Sambuc  * Append object to array
104ebfedea0SLionel Sambuc  *
105ebfedea0SLionel Sambuc  * @param array array to add too
106ebfedea0SLionel Sambuc  * @param object the object to add
107ebfedea0SLionel Sambuc  *
108ebfedea0SLionel Sambuc  * @return zero if added, errno otherwise
109ebfedea0SLionel Sambuc  */
110ebfedea0SLionel Sambuc 
111ebfedea0SLionel Sambuc int
heim_array_append_value(heim_array_t array,heim_object_t object)112ebfedea0SLionel Sambuc heim_array_append_value(heim_array_t array, heim_object_t object)
113ebfedea0SLionel Sambuc {
114ebfedea0SLionel Sambuc     heim_object_t *ptr;
115ebfedea0SLionel Sambuc 
116ebfedea0SLionel Sambuc     ptr = realloc(array->val, (array->len + 1) * sizeof(array->val[0]));
117ebfedea0SLionel Sambuc     if (ptr == NULL)
118ebfedea0SLionel Sambuc 	return ENOMEM;
119ebfedea0SLionel Sambuc     array->val = ptr;
120ebfedea0SLionel Sambuc     array->val[array->len++] = heim_retain(object);
121ebfedea0SLionel Sambuc 
122ebfedea0SLionel Sambuc     return 0;
123ebfedea0SLionel Sambuc }
124ebfedea0SLionel Sambuc 
125ebfedea0SLionel Sambuc /**
126ebfedea0SLionel Sambuc  * Iterate over all objects in array
127ebfedea0SLionel Sambuc  *
128ebfedea0SLionel Sambuc  * @param array array to iterate over
129ebfedea0SLionel Sambuc  * @param fn function to call on each object
130ebfedea0SLionel Sambuc  * @param ctx context passed to fn
131ebfedea0SLionel Sambuc  */
132ebfedea0SLionel Sambuc 
133ebfedea0SLionel Sambuc void
heim_array_iterate_f(heim_array_t array,heim_array_iterator_f_t fn,void * ctx)134ebfedea0SLionel Sambuc heim_array_iterate_f(heim_array_t array, heim_array_iterator_f_t fn, void *ctx)
135ebfedea0SLionel Sambuc {
136ebfedea0SLionel Sambuc     size_t n;
137ebfedea0SLionel Sambuc     for (n = 0; n < array->len; n++)
138ebfedea0SLionel Sambuc 	fn(array->val[n], ctx);
139ebfedea0SLionel Sambuc }
140ebfedea0SLionel Sambuc 
141ebfedea0SLionel Sambuc #ifdef __BLOCKS__
142ebfedea0SLionel Sambuc /**
143ebfedea0SLionel Sambuc  * Iterate over all objects in array
144ebfedea0SLionel Sambuc  *
145ebfedea0SLionel Sambuc  * @param array array to iterate over
146ebfedea0SLionel Sambuc  * @param fn block to call on each object
147ebfedea0SLionel Sambuc  */
148ebfedea0SLionel Sambuc 
149ebfedea0SLionel Sambuc void
150ebfedea0SLionel Sambuc heim_array_iterate(heim_array_t array, void (^fn)(heim_object_t))
151ebfedea0SLionel Sambuc {
152ebfedea0SLionel Sambuc     size_t n;
153ebfedea0SLionel Sambuc     for (n = 0; n < array->len; n++)
154ebfedea0SLionel Sambuc 	fn(array->val[n]);
155ebfedea0SLionel Sambuc }
156ebfedea0SLionel Sambuc #endif
157ebfedea0SLionel Sambuc 
158ebfedea0SLionel Sambuc /**
159ebfedea0SLionel Sambuc  * Get length of array
160ebfedea0SLionel Sambuc  *
161ebfedea0SLionel Sambuc  * @param array array to get length of
162ebfedea0SLionel Sambuc  *
163ebfedea0SLionel Sambuc  * @return length of array
164ebfedea0SLionel Sambuc  */
165ebfedea0SLionel Sambuc 
166ebfedea0SLionel Sambuc size_t
heim_array_get_length(heim_array_t array)167ebfedea0SLionel Sambuc heim_array_get_length(heim_array_t array)
168ebfedea0SLionel Sambuc {
169ebfedea0SLionel Sambuc     return array->len;
170ebfedea0SLionel Sambuc }
171ebfedea0SLionel Sambuc 
172ebfedea0SLionel Sambuc /**
173ebfedea0SLionel Sambuc  * Copy value of array
174ebfedea0SLionel Sambuc  *
175ebfedea0SLionel Sambuc  * @param array array copy object from
176ebfedea0SLionel Sambuc  * @param idx index of object, 0 based, must be smaller then
177ebfedea0SLionel Sambuc  *        heim_array_get_length()
178ebfedea0SLionel Sambuc  *
179ebfedea0SLionel Sambuc  * @return a retained copy of the object
180ebfedea0SLionel Sambuc  */
181ebfedea0SLionel Sambuc 
182ebfedea0SLionel Sambuc heim_object_t
heim_array_copy_value(heim_array_t array,size_t idx)183ebfedea0SLionel Sambuc heim_array_copy_value(heim_array_t array, size_t idx)
184ebfedea0SLionel Sambuc {
185ebfedea0SLionel Sambuc     if (idx >= array->len)
186ebfedea0SLionel Sambuc 	heim_abort("index too large");
187ebfedea0SLionel Sambuc     return heim_retain(array->val[idx]);
188ebfedea0SLionel Sambuc }
189ebfedea0SLionel Sambuc 
190ebfedea0SLionel Sambuc /**
191ebfedea0SLionel Sambuc  * Delete value at idx
192ebfedea0SLionel Sambuc  *
193ebfedea0SLionel Sambuc  * @param array the array to modify
194ebfedea0SLionel Sambuc  * @param idx the key to delete
195ebfedea0SLionel Sambuc  */
196ebfedea0SLionel Sambuc 
197ebfedea0SLionel Sambuc void
heim_array_delete_value(heim_array_t array,size_t idx)198ebfedea0SLionel Sambuc heim_array_delete_value(heim_array_t array, size_t idx)
199ebfedea0SLionel Sambuc {
200ebfedea0SLionel Sambuc     heim_object_t obj;
201ebfedea0SLionel Sambuc     if (idx >= array->len)
202ebfedea0SLionel Sambuc 	heim_abort("index too large");
203ebfedea0SLionel Sambuc     obj = array->val[idx];
204ebfedea0SLionel Sambuc 
205ebfedea0SLionel Sambuc     array->len--;
206ebfedea0SLionel Sambuc 
207ebfedea0SLionel Sambuc     if (idx < array->len)
208ebfedea0SLionel Sambuc 	memmove(&array->val[idx], &array->val[idx + 1],
209ebfedea0SLionel Sambuc 		(array->len - idx) * sizeof(array->val[0]));
210ebfedea0SLionel Sambuc 
211ebfedea0SLionel Sambuc     heim_release(obj);
212ebfedea0SLionel Sambuc }
213ebfedea0SLionel Sambuc 
214ebfedea0SLionel Sambuc #ifdef __BLOCKS__
215ebfedea0SLionel Sambuc /**
216ebfedea0SLionel Sambuc  * Get value at idx
217ebfedea0SLionel Sambuc  *
218ebfedea0SLionel Sambuc  * @param array the array to modify
219ebfedea0SLionel Sambuc  * @param idx the key to delete
220ebfedea0SLionel Sambuc  */
221ebfedea0SLionel Sambuc 
222ebfedea0SLionel Sambuc void
223*0a6a1f1dSLionel Sambuc heim_array_filter(heim_array_t array, int (^block)(heim_object_t))
224ebfedea0SLionel Sambuc {
225ebfedea0SLionel Sambuc     size_t n = 0;
226ebfedea0SLionel Sambuc 
227ebfedea0SLionel Sambuc     while (n < array->len) {
228ebfedea0SLionel Sambuc 	if (block(array->val[n])) {
229ebfedea0SLionel Sambuc 	    heim_array_delete_value(array, n);
230ebfedea0SLionel Sambuc 	} else {
231ebfedea0SLionel Sambuc 	    n++;
232ebfedea0SLionel Sambuc 	}
233ebfedea0SLionel Sambuc     }
234ebfedea0SLionel Sambuc }
235ebfedea0SLionel Sambuc 
236ebfedea0SLionel Sambuc #endif /* __BLOCKS__ */
237