fixup symlink
[heimdal.git] / base / dict.c
blob3893dfb9dbbaf03424f724e01dc3400b7bd47d8c
1 /*
2 * Copyright (c) 2002, 1997 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
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
10 * are met:
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
33 * SUCH DAMAGE.
36 #include "baselocl.h"
38 struct hashentry {
39 struct hashentry **prev;
40 struct hashentry *next;
41 heim_object_t key;
42 heim_object_t value;
45 struct heim_dict_data {
46 size_t size;
47 struct hashentry **tab;
50 static void
51 dict_dealloc(void *ptr)
53 heim_dict_t dict = ptr;
54 struct hashentry **h, *g, *i;
56 for (h = dict->tab; h < &dict->tab[dict->size]; ++h) {
57 for (g = h[0]; g; g = i) {
58 i = g->next;
59 heim_release(g->key);
60 heim_release(g->value);
61 free(g);
64 free(dict->tab);
67 struct heim_type_data dict_object = {
68 HEIM_TID_DICT,
69 "dict-object",
70 NULL,
71 dict_dealloc,
72 NULL,
73 NULL,
74 NULL
77 static size_t
78 isprime(size_t p)
80 size_t q, i;
82 for(i = 2 ; i < p; i++) {
83 q = p / i;
85 if (i * q == p)
86 return 0;
87 if (i * i > p)
88 return 1;
90 return 1;
93 static size_t
94 findprime(size_t p)
96 if (p % 2 == 0)
97 p++;
99 while (isprime(p) == 0)
100 p += 2;
102 return p;
106 * Allocate an array
108 * @return A new allocated array, free with heim_release()
111 heim_dict_t
112 heim_dict_create(size_t size)
114 heim_dict_t dict;
116 dict = _heim_alloc_object(&dict_object, sizeof(*dict));
118 dict->size = findprime(size);
119 if (dict->size == 0) {
120 heim_release(dict);
121 return NULL;
124 dict->tab = calloc(dict->size, sizeof(dict->tab[0]));
125 if (dict->tab == NULL) {
126 dict->size = 0;
127 heim_release(dict);
128 return NULL;
131 return dict;
135 * Get type id of an dict
137 * @return the type id
140 heim_tid_t
141 heim_dict_get_type_id(void)
143 return HEIM_TID_DICT;
146 /* Intern search function */
148 static struct hashentry *
149 _search(heim_dict_t dict, heim_object_t ptr)
151 unsigned long v = heim_get_hash(ptr);
152 struct hashentry *p;
154 for (p = dict->tab[v % dict->size]; p != NULL; p = p->next)
155 if (heim_cmp(ptr, p->key) == 0)
156 return p;
158 return NULL;
162 * Search for element in hash table
164 * @value dict the dict to search in
165 * @value key the key to search for
167 * @return a not-retained copy of the value for key or NULL if not found
170 heim_object_t
171 heim_dict_get_value(heim_dict_t dict, heim_object_t key)
173 struct hashentry *p;
174 p = _search(dict, key);
175 if (p == NULL)
176 return NULL;
178 return p->value;
182 * Search for element in hash table
184 * @value dict the dict to search in
185 * @value key the key to search for
187 * @return a retained copy of the value for key or NULL if not found
190 heim_object_t
191 heim_dict_copy_value(heim_dict_t dict, heim_object_t key)
193 struct hashentry *p;
194 p = _search(dict, key);
195 if (p == NULL)
196 return NULL;
198 return heim_retain(p->value);
202 * Add key and value to dict
204 * @value dict the dict to add too
205 * @value key the key to add
206 * @value value the value to add
208 * @return 0 if added, errno if not
212 heim_dict_set_value(heim_dict_t dict, heim_object_t key, heim_object_t value)
214 struct hashentry **tabptr, *h;
216 h = _search(dict, key);
217 if (h) {
218 heim_release(h->value);
219 h->value = heim_retain(value);
220 } else {
221 unsigned long v;
223 h = malloc(sizeof(*h));
224 if (h == NULL)
225 return ENOMEM;
227 h->key = heim_retain(key);
228 h->value = heim_retain(value);
230 v = heim_get_hash(key);
232 tabptr = &dict->tab[v % dict->size];
233 h->next = *tabptr;
234 *tabptr = h;
235 h->prev = tabptr;
236 if (h->next)
237 h->next->prev = &h->next;
240 return 0;
244 * Delete element with key key
246 * @value dict the dict to delete from
247 * @value key the key to delete
250 void
251 heim_dict_delete_key(heim_dict_t dict, heim_object_t key)
253 struct hashentry *h = _search(dict, key);
255 if (h == NULL)
256 return;
258 heim_release(h->key);
259 heim_release(h->value);
261 if ((*(h->prev) = h->next) != NULL)
262 h->next->prev = h->prev;
264 free(h);
268 * Do something for each element
270 * @value dict the dict to interate over
271 * @value func the function to search for
272 * @value arg argument to func
275 void
276 heim_dict_iterate_f(heim_dict_t dict, void *arg, heim_dict_iterator_f_t func)
278 struct hashentry **h, *g;
280 for (h = dict->tab; h < &dict->tab[dict->size]; ++h)
281 for (g = *h; g; g = g->next)
282 func(g->key, g->value, arg);
285 #ifdef __BLOCKS__
287 * Do something for each element
289 * @value dict the dict to interate over
290 * @value func the function to search for
293 void
294 heim_dict_iterate(heim_dict_t dict, void (^func)(heim_object_t, heim_object_t))
296 struct hashentry **h, *g;
298 for (h = dict->tab; h < &dict->tab[dict->size]; ++h)
299 for (g = *h; g; g = g->next)
300 func(g->key, g->value);
302 #endif