1 /* x-hash.c - basic hash tables
2 $Id: x-hash.c,v 1.7 2003/07/17 05:25:44 jharper Exp $
4 Copyright (c) 2002 Apple Computer, Inc. All rights reserved.
6 Permission is hereby granted, free of charge, to any person
7 obtaining a copy of this software and associated documentation files
8 (the "Software"), to deal in the Software without restriction,
9 including without limitation the rights to use, copy, modify, merge,
10 publish, distribute, sublicense, and/or sell copies of the Software,
11 and to permit persons to whom the Software is furnished to do so,
12 subject to the following conditions:
14 The above copyright notice and this permission notice shall be
15 included in all copies or substantial portions of the Software.
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 NONINFRINGEMENT. IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT
21 HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 DEALINGS IN THE SOFTWARE.
26 Except as contained in this notice, the name(s) of the above
27 copyright holders shall not be used in advertising or otherwise to
28 promote the sale, use or other dealings in this Software without
29 prior written authorization. */
36 struct x_hash_table_struct {
37 unsigned int bucket_index;
38 unsigned int total_keys;
42 x_compare_fun *compare_keys;
43 x_destroy_fun *destroy_key;
44 x_destroy_fun *destroy_value;
47 #define ITEM_NEW(k, v) X_PFX (list_prepend) ((x_list *) (k), v)
48 #define ITEM_FREE(i) X_PFX (list_free_1) (i)
49 #define ITEM_KEY(i) ((void *) (i)->next)
50 #define ITEM_VALUE(i) ((i)->data)
52 #define SPLIT_THRESHOLD_FACTOR 2
54 /* http://planetmath.org/?op=getobj&from=objects&name=GoodHashTablePrimes */
55 static const unsigned int bucket_sizes[] = {
56 29, 53, 97, 193, 389, 769, 1543, 3079, 6151, 12289, 24593, 49157,
57 98317, 196613, 393241, 786433, 1572869, 3145739, 6291469, 12582917,
58 25165843, 50331653, 100663319, 201326611, 402653189, 805306457,
62 #define N_BUCKET_SIZES (sizeof (bucket_sizes) / sizeof (bucket_sizes[0]))
64 static inline unsigned int
65 hash_table_total_buckets (x_hash_table *h)
67 return bucket_sizes[h->bucket_index];
71 hash_table_destroy_item (x_hash_table *h, void *k, void *v)
73 if (h->destroy_key != 0)
74 (*h->destroy_key) (k);
76 if (h->destroy_value != 0)
77 (*h->destroy_value) (v);
80 static inline unsigned int
81 hash_table_hash_key (x_hash_table *h, void *k)
84 return (*h->hash_key) (k);
86 return (unsigned int) k;
90 hash_table_compare_keys (x_hash_table *h, void *k1, void *k2)
92 if (h->compare_keys == 0)
95 return (*h->compare_keys) (k1, k2) == 0;
99 hash_table_split (x_hash_table *h)
102 x_list *node, *item, *next;
103 int new_size, old_size;
107 if (h->bucket_index == N_BUCKET_SIZES - 1)
110 old_size = hash_table_total_buckets (h);
115 new_size = hash_table_total_buckets (h);
116 new = calloc (new_size, sizeof (x_list *));
124 for (i = 0; i < old_size; i++)
126 for (node = old[i]; node != 0; node = next)
131 b = hash_table_hash_key (h, ITEM_KEY (item)) % new_size;
142 X_EXTERN x_hash_table *
143 X_PFX (hash_table_new) (x_hash_fun *hash,
144 x_compare_fun *compare,
145 x_destroy_fun *key_destroy,
146 x_destroy_fun *value_destroy)
150 h = calloc (1, sizeof (x_hash_table));
155 h->buckets = calloc (hash_table_total_buckets (h), sizeof (x_list *));
164 h->compare_keys = compare;
165 h->destroy_key = key_destroy;
166 h->destroy_value = value_destroy;
172 X_PFX (hash_table_free) (x_hash_table *h)
179 n = hash_table_total_buckets (h);
181 for (i = 0; i < n; i++)
183 for (node = h->buckets[i]; node != 0; node = node->next)
186 hash_table_destroy_item (h, ITEM_KEY (item), ITEM_VALUE (item));
189 X_PFX (list_free) (h->buckets[i]);
196 X_EXTERN unsigned int
197 X_PFX (hash_table_size) (x_hash_table *h)
201 return h->total_keys;
205 hash_table_modify (x_hash_table *h, void *k, void *v, int replace)
207 unsigned int hash_value;
212 hash_value = hash_table_hash_key (h, k);
214 for (node = h->buckets[hash_value % hash_table_total_buckets (h)];
215 node != 0; node = node->next)
219 if (hash_table_compare_keys (h, ITEM_KEY (item), k))
223 hash_table_destroy_item (h, ITEM_KEY (item),
226 ITEM_VALUE (item) = v;
230 hash_table_destroy_item (h, k, ITEM_VALUE (item));
231 ITEM_VALUE (item) = v;
237 /* Key isn't already in the table. Insert it. */
239 if (h->total_keys + 1
240 > hash_table_total_buckets (h) * SPLIT_THRESHOLD_FACTOR)
242 hash_table_split (h);
245 hash_value = hash_value % hash_table_total_buckets (h);
246 h->buckets[hash_value] = X_PFX (list_prepend) (h->buckets[hash_value],
252 X_PFX (hash_table_insert) (x_hash_table *h, void *k, void *v)
254 hash_table_modify (h, k, v, 0);
258 X_PFX (hash_table_replace) (x_hash_table *h, void *k, void *v)
260 hash_table_modify (h, k, v, 1);
264 X_PFX (hash_table_remove) (x_hash_table *h, void *k)
266 unsigned int hash_value;
271 hash_value = hash_table_hash_key (h, k);
273 for (ptr = &h->buckets[hash_value % hash_table_total_buckets (h)];
274 *ptr != 0; ptr = &((*ptr)->next))
278 if (hash_table_compare_keys (h, ITEM_KEY (item), k))
280 hash_table_destroy_item (h, ITEM_KEY (item), ITEM_VALUE (item));
284 X_PFX (list_free_1) (item);
292 X_PFX (hash_table_lookup) (x_hash_table *h, void *k, void **k_ret)
294 unsigned int hash_value;
299 hash_value = hash_table_hash_key (h, k);
301 for (node = h->buckets[hash_value % hash_table_total_buckets (h)];
302 node != 0; node = node->next)
306 if (hash_table_compare_keys (h, ITEM_KEY (item), k))
309 *k_ret = ITEM_KEY (item);
311 return ITEM_VALUE (item);
322 X_PFX (hash_table_foreach) (x_hash_table *h,
323 x_hash_foreach_fun *fun, void *data)
330 n = hash_table_total_buckets (h);
332 for (i = 0; i < n; i++)
334 for (node = h->buckets[i]; node != 0; node = node->next)
337 (*fun) (ITEM_KEY (item), ITEM_VALUE (item), data);