xref: /minix3/crypto/external/bsd/heimdal/dist/base/dict.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: dict.c,v 1.1.1.2 2014/04/24 12:45:26 pettai Exp $	*/
2ebfedea0SLionel Sambuc 
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc  * Copyright (c) 2002, 1997 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 struct hashentry {
41ebfedea0SLionel Sambuc     struct hashentry **prev;
42ebfedea0SLionel Sambuc     struct hashentry *next;
43ebfedea0SLionel Sambuc     heim_object_t key;
44ebfedea0SLionel Sambuc     heim_object_t value;
45ebfedea0SLionel Sambuc };
46ebfedea0SLionel Sambuc 
47ebfedea0SLionel Sambuc struct heim_dict_data {
48ebfedea0SLionel Sambuc     size_t size;
49ebfedea0SLionel Sambuc     struct hashentry **tab;
50ebfedea0SLionel Sambuc };
51ebfedea0SLionel Sambuc 
52ebfedea0SLionel Sambuc static void
dict_dealloc(void * ptr)53ebfedea0SLionel Sambuc dict_dealloc(void *ptr)
54ebfedea0SLionel Sambuc {
55ebfedea0SLionel Sambuc     heim_dict_t dict = ptr;
56ebfedea0SLionel Sambuc     struct hashentry **h, *g, *i;
57ebfedea0SLionel Sambuc 
58ebfedea0SLionel Sambuc     for (h = dict->tab; h < &dict->tab[dict->size]; ++h) {
59ebfedea0SLionel Sambuc 	for (g = h[0]; g; g = i) {
60ebfedea0SLionel Sambuc 	    i = g->next;
61ebfedea0SLionel Sambuc 	    heim_release(g->key);
62ebfedea0SLionel Sambuc 	    heim_release(g->value);
63ebfedea0SLionel Sambuc 	    free(g);
64ebfedea0SLionel Sambuc 	}
65ebfedea0SLionel Sambuc     }
66ebfedea0SLionel Sambuc     free(dict->tab);
67ebfedea0SLionel Sambuc }
68ebfedea0SLionel Sambuc 
69ebfedea0SLionel Sambuc struct heim_type_data dict_object = {
70ebfedea0SLionel Sambuc     HEIM_TID_DICT,
71ebfedea0SLionel Sambuc     "dict-object",
72ebfedea0SLionel Sambuc     NULL,
73ebfedea0SLionel Sambuc     dict_dealloc,
74ebfedea0SLionel Sambuc     NULL,
75ebfedea0SLionel Sambuc     NULL,
76ebfedea0SLionel Sambuc     NULL
77ebfedea0SLionel Sambuc };
78ebfedea0SLionel Sambuc 
79ebfedea0SLionel Sambuc static size_t
isprime(size_t p)80ebfedea0SLionel Sambuc isprime(size_t p)
81ebfedea0SLionel Sambuc {
82*0a6a1f1dSLionel Sambuc     size_t q, i;
83ebfedea0SLionel Sambuc 
84ebfedea0SLionel Sambuc     for(i = 2 ; i < p; i++) {
85ebfedea0SLionel Sambuc 	q = p / i;
86ebfedea0SLionel Sambuc 
87ebfedea0SLionel Sambuc 	if (i * q == p)
88ebfedea0SLionel Sambuc 	    return 0;
89ebfedea0SLionel Sambuc 	if (i * i > p)
90ebfedea0SLionel Sambuc 	    return 1;
91ebfedea0SLionel Sambuc     }
92ebfedea0SLionel Sambuc     return 1;
93ebfedea0SLionel Sambuc }
94ebfedea0SLionel Sambuc 
95ebfedea0SLionel Sambuc static size_t
findprime(size_t p)96ebfedea0SLionel Sambuc findprime(size_t p)
97ebfedea0SLionel Sambuc {
98ebfedea0SLionel Sambuc     if (p % 2 == 0)
99ebfedea0SLionel Sambuc 	p++;
100ebfedea0SLionel Sambuc 
101ebfedea0SLionel Sambuc     while (isprime(p) == 0)
102ebfedea0SLionel Sambuc 	p += 2;
103ebfedea0SLionel Sambuc 
104ebfedea0SLionel Sambuc     return p;
105ebfedea0SLionel Sambuc }
106ebfedea0SLionel Sambuc 
107ebfedea0SLionel Sambuc /**
108ebfedea0SLionel Sambuc  * Allocate an array
109ebfedea0SLionel Sambuc  *
110ebfedea0SLionel Sambuc  * @return A new allocated array, free with heim_release()
111ebfedea0SLionel Sambuc  */
112ebfedea0SLionel Sambuc 
113ebfedea0SLionel Sambuc heim_dict_t
heim_dict_create(size_t size)114ebfedea0SLionel Sambuc heim_dict_create(size_t size)
115ebfedea0SLionel Sambuc {
116ebfedea0SLionel Sambuc     heim_dict_t dict;
117ebfedea0SLionel Sambuc 
118ebfedea0SLionel Sambuc     dict = _heim_alloc_object(&dict_object, sizeof(*dict));
119ebfedea0SLionel Sambuc 
120ebfedea0SLionel Sambuc     dict->size = findprime(size);
121ebfedea0SLionel Sambuc     if (dict->size == 0) {
122ebfedea0SLionel Sambuc 	heim_release(dict);
123ebfedea0SLionel Sambuc 	return NULL;
124ebfedea0SLionel Sambuc     }
125ebfedea0SLionel Sambuc 
126ebfedea0SLionel Sambuc     dict->tab = calloc(dict->size, sizeof(dict->tab[0]));
127ebfedea0SLionel Sambuc     if (dict->tab == NULL) {
128ebfedea0SLionel Sambuc 	dict->size = 0;
129ebfedea0SLionel Sambuc 	heim_release(dict);
130ebfedea0SLionel Sambuc 	return NULL;
131ebfedea0SLionel Sambuc     }
132ebfedea0SLionel Sambuc 
133ebfedea0SLionel Sambuc     return dict;
134ebfedea0SLionel Sambuc }
135ebfedea0SLionel Sambuc 
136ebfedea0SLionel Sambuc /**
137ebfedea0SLionel Sambuc  * Get type id of an dict
138ebfedea0SLionel Sambuc  *
139ebfedea0SLionel Sambuc  * @return the type id
140ebfedea0SLionel Sambuc  */
141ebfedea0SLionel Sambuc 
142ebfedea0SLionel Sambuc heim_tid_t
heim_dict_get_type_id(void)143ebfedea0SLionel Sambuc heim_dict_get_type_id(void)
144ebfedea0SLionel Sambuc {
145ebfedea0SLionel Sambuc     return HEIM_TID_DICT;
146ebfedea0SLionel Sambuc }
147ebfedea0SLionel Sambuc 
148ebfedea0SLionel Sambuc /* Intern search function */
149ebfedea0SLionel Sambuc 
150ebfedea0SLionel Sambuc static struct hashentry *
_search(heim_dict_t dict,heim_object_t ptr)151ebfedea0SLionel Sambuc _search(heim_dict_t dict, heim_object_t ptr)
152ebfedea0SLionel Sambuc {
153ebfedea0SLionel Sambuc     unsigned long v = heim_get_hash(ptr);
154ebfedea0SLionel Sambuc     struct hashentry *p;
155ebfedea0SLionel Sambuc 
156ebfedea0SLionel Sambuc     for (p = dict->tab[v % dict->size]; p != NULL; p = p->next)
157ebfedea0SLionel Sambuc 	if (heim_cmp(ptr, p->key) == 0)
158ebfedea0SLionel Sambuc 	    return p;
159ebfedea0SLionel Sambuc 
160ebfedea0SLionel Sambuc     return NULL;
161ebfedea0SLionel Sambuc }
162ebfedea0SLionel Sambuc 
163ebfedea0SLionel Sambuc /**
164ebfedea0SLionel Sambuc  * Search for element in hash table
165ebfedea0SLionel Sambuc  *
166ebfedea0SLionel Sambuc  * @value dict the dict to search in
167ebfedea0SLionel Sambuc  * @value key the key to search for
168ebfedea0SLionel Sambuc  *
169ebfedea0SLionel Sambuc  * @return a retained copy of the value for key or NULL if not found
170ebfedea0SLionel Sambuc  */
171ebfedea0SLionel Sambuc 
172ebfedea0SLionel Sambuc heim_object_t
heim_dict_copy_value(heim_dict_t dict,heim_object_t key)173ebfedea0SLionel Sambuc heim_dict_copy_value(heim_dict_t dict, heim_object_t key)
174ebfedea0SLionel Sambuc {
175ebfedea0SLionel Sambuc     struct hashentry *p;
176ebfedea0SLionel Sambuc     p = _search(dict, key);
177ebfedea0SLionel Sambuc     if (p == NULL)
178ebfedea0SLionel Sambuc 	return NULL;
179ebfedea0SLionel Sambuc 
180ebfedea0SLionel Sambuc     return heim_retain(p->value);
181ebfedea0SLionel Sambuc }
182ebfedea0SLionel Sambuc 
183ebfedea0SLionel Sambuc /**
184ebfedea0SLionel Sambuc  * Add key and value to dict
185ebfedea0SLionel Sambuc  *
186ebfedea0SLionel Sambuc  * @value dict the dict to add too
187ebfedea0SLionel Sambuc  * @value key the key to add
188ebfedea0SLionel Sambuc  * @value value the value to add
189ebfedea0SLionel Sambuc  *
190ebfedea0SLionel Sambuc  * @return 0 if added, errno if not
191ebfedea0SLionel Sambuc  */
192ebfedea0SLionel Sambuc 
193ebfedea0SLionel Sambuc int
heim_dict_add_value(heim_dict_t dict,heim_object_t key,heim_object_t value)194ebfedea0SLionel Sambuc heim_dict_add_value(heim_dict_t dict, heim_object_t key, heim_object_t value)
195ebfedea0SLionel Sambuc {
196ebfedea0SLionel Sambuc     struct hashentry **tabptr, *h;
197ebfedea0SLionel Sambuc 
198ebfedea0SLionel Sambuc     h = _search(dict, key);
199ebfedea0SLionel Sambuc     if (h) {
200ebfedea0SLionel Sambuc 	heim_release(h->value);
201ebfedea0SLionel Sambuc 	h->value = heim_retain(value);
202ebfedea0SLionel Sambuc     } else {
203ebfedea0SLionel Sambuc 	unsigned long v;
204ebfedea0SLionel Sambuc 
205ebfedea0SLionel Sambuc 	h = malloc(sizeof(*h));
206ebfedea0SLionel Sambuc 	if (h == NULL)
207ebfedea0SLionel Sambuc 	    return ENOMEM;
208ebfedea0SLionel Sambuc 
209ebfedea0SLionel Sambuc 	h->key = heim_retain(key);
210ebfedea0SLionel Sambuc 	h->value = heim_retain(value);
211ebfedea0SLionel Sambuc 
212ebfedea0SLionel Sambuc 	v = heim_get_hash(key);
213ebfedea0SLionel Sambuc 
214ebfedea0SLionel Sambuc 	tabptr = &dict->tab[v % dict->size];
215ebfedea0SLionel Sambuc 	h->next = *tabptr;
216ebfedea0SLionel Sambuc 	*tabptr = h;
217ebfedea0SLionel Sambuc 	h->prev = tabptr;
218ebfedea0SLionel Sambuc 	if (h->next)
219ebfedea0SLionel Sambuc 	    h->next->prev = &h->next;
220ebfedea0SLionel Sambuc     }
221ebfedea0SLionel Sambuc 
222ebfedea0SLionel Sambuc     return 0;
223ebfedea0SLionel Sambuc }
224ebfedea0SLionel Sambuc 
225ebfedea0SLionel Sambuc /**
226ebfedea0SLionel Sambuc  * Delete element with key key
227ebfedea0SLionel Sambuc  *
228ebfedea0SLionel Sambuc  * @value dict the dict to delete from
229ebfedea0SLionel Sambuc  * @value key the key to delete
230ebfedea0SLionel Sambuc  */
231ebfedea0SLionel Sambuc 
232ebfedea0SLionel Sambuc void
heim_dict_delete_key(heim_dict_t dict,heim_object_t key)233ebfedea0SLionel Sambuc heim_dict_delete_key(heim_dict_t dict, heim_object_t key)
234ebfedea0SLionel Sambuc {
235ebfedea0SLionel Sambuc     struct hashentry *h = _search(dict, key);
236ebfedea0SLionel Sambuc 
237ebfedea0SLionel Sambuc     if (h == NULL)
238ebfedea0SLionel Sambuc 	return;
239ebfedea0SLionel Sambuc 
240ebfedea0SLionel Sambuc     heim_release(h->key);
241ebfedea0SLionel Sambuc     heim_release(h->value);
242ebfedea0SLionel Sambuc 
243ebfedea0SLionel Sambuc     if ((*(h->prev) = h->next) != NULL)
244ebfedea0SLionel Sambuc 	h->next->prev = h->prev;
245ebfedea0SLionel Sambuc 
246ebfedea0SLionel Sambuc     free(h);
247ebfedea0SLionel Sambuc }
248ebfedea0SLionel Sambuc 
249ebfedea0SLionel Sambuc /**
250ebfedea0SLionel Sambuc  * Do something for each element
251ebfedea0SLionel Sambuc  *
252ebfedea0SLionel Sambuc  * @value dict the dict to interate over
253ebfedea0SLionel Sambuc  * @value func the function to search for
254ebfedea0SLionel Sambuc  * @value arg argument to func
255ebfedea0SLionel Sambuc  */
256ebfedea0SLionel Sambuc 
257ebfedea0SLionel Sambuc void
heim_dict_iterate_f(heim_dict_t dict,heim_dict_iterator_f_t func,void * arg)258ebfedea0SLionel Sambuc heim_dict_iterate_f(heim_dict_t dict, heim_dict_iterator_f_t func, void *arg)
259ebfedea0SLionel Sambuc {
260ebfedea0SLionel Sambuc     struct hashentry **h, *g;
261ebfedea0SLionel Sambuc 
262ebfedea0SLionel Sambuc     for (h = dict->tab; h < &dict->tab[dict->size]; ++h)
263ebfedea0SLionel Sambuc 	for (g = *h; g; g = g->next)
264ebfedea0SLionel Sambuc 	    func(g->key, g->value, arg);
265ebfedea0SLionel Sambuc }
266ebfedea0SLionel Sambuc 
267ebfedea0SLionel Sambuc #ifdef __BLOCKS__
268ebfedea0SLionel Sambuc /**
269ebfedea0SLionel Sambuc  * Do something for each element
270ebfedea0SLionel Sambuc  *
271ebfedea0SLionel Sambuc  * @value dict the dict to interate over
272ebfedea0SLionel Sambuc  * @value func the function to search for
273ebfedea0SLionel Sambuc  */
274ebfedea0SLionel Sambuc 
275ebfedea0SLionel Sambuc void
276ebfedea0SLionel Sambuc heim_dict_iterate(heim_dict_t dict, void (^func)(heim_object_t, heim_object_t))
277ebfedea0SLionel Sambuc {
278ebfedea0SLionel Sambuc     struct hashentry **h, *g;
279ebfedea0SLionel Sambuc 
280ebfedea0SLionel Sambuc     for (h = dict->tab; h < &dict->tab[dict->size]; ++h)
281ebfedea0SLionel Sambuc 	for (g = *h; g; g = g->next)
282ebfedea0SLionel Sambuc 	    func(g->key, g->value);
283ebfedea0SLionel Sambuc }
284ebfedea0SLionel Sambuc #endif
285