2 * Hash Array Mapped Trie (HAMT) implementation
4 * Copyright (C) 2001-2007 Peter Johnson
6 * Based on the paper "Ideal Hash Tries" by Phil Bagwell [2000].
7 * One algorithmic change from that described in the paper: we use the LSB's
8 * of the key to index the root table and move upward in the key rather than
9 * use the MSBs as described in the paper. The LSBs have more entropy.
11 * Redistribution and use in source and binary forms, with or without
12 * 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.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
36 #include "libyasm-stdint.h"
41 STAILQ_ENTRY(HAMTEntry) next; /* next hash table entry */
42 /*@dependent@*/ const char *str; /* string being hashed */
43 /*@owned@*/ void *data; /* data pointer being stored */
46 typedef struct HAMTNode {
47 unsigned long BitMapKey; /* 32 bits, bitmap or hash key */
48 uintptr_t BaseValue; /* Base of HAMTNode list or value */
52 STAILQ_HEAD(HAMTEntryHead, HAMTEntry) entries;
54 /*@exits@*/ void (*error_func) (const char *file, unsigned int line,
56 unsigned long (*HashKey) (const char *key);
57 unsigned long (*ReHashKey) (const char *key, int Level);
58 int (*CmpKey) (const char *s1, const char *s2);
61 /* XXX make a portable version of this. This depends on the pointer being
62 * 4 or 2-byte aligned (as it uses the LSB of the pointer variable to store
65 #define IsSubTrie(n) ((n)->BaseValue & 1)
66 #define SetSubTrie(h, n, v) do { \
67 if ((uintptr_t)(v) & 1) \
68 h->error_func(__FILE__, __LINE__, \
69 N_("Subtrie is seen as subtrie before flag is set (misaligned?)")); \
70 (n)->BaseValue = (uintptr_t)(v) | 1; \
72 #define SetValue(h, n, v) do { \
73 if ((uintptr_t)(v) & 1) \
74 h->error_func(__FILE__, __LINE__, \
75 N_("Value is seen as subtrie (misaligned?)")); \
76 (n)->BaseValue = (uintptr_t)(v); \
78 #define GetSubTrie(n) (HAMTNode *)(((n)->BaseValue | 1) ^ 1)
81 HashKey(const char *key)
83 unsigned long a=31415, b=27183, vHash;
84 for (vHash=0; *key; key++, a*=b)
85 vHash = a*vHash + *key;
90 ReHashKey(const char *key, int Level)
92 unsigned long a=31415, b=27183, vHash;
93 for (vHash=0; *key; key++, a*=b)
94 vHash = a*vHash*(unsigned long)Level + *key;
99 HashKey_nocase(const char *key)
101 unsigned long a=31415, b=27183, vHash;
102 for (vHash=0; *key; key++, a*=b)
103 vHash = a*vHash + tolower(*key);
108 ReHashKey_nocase(const char *key, int Level)
110 unsigned long a=31415, b=27183, vHash;
111 for (vHash=0; *key; key++, a*=b)
112 vHash = a*vHash*(unsigned long)Level + tolower(*key);
117 HAMT_create(int nocase, /*@exits@*/ void (*error_func)
118 (const char *file, unsigned int line, const char *message))
120 /*@out@*/ HAMT *hamt = yasm_xmalloc(sizeof(HAMT));
123 STAILQ_INIT(&hamt->entries);
124 hamt->root = yasm_xmalloc(32*sizeof(HAMTNode));
126 for (i=0; i<32; i++) {
127 hamt->root[i].BitMapKey = 0;
128 hamt->root[i].BaseValue = 0;
131 hamt->error_func = error_func;
133 hamt->HashKey = HashKey_nocase;
134 hamt->ReHashKey = ReHashKey_nocase;
135 hamt->CmpKey = yasm__strcasecmp;
137 hamt->HashKey = HashKey;
138 hamt->ReHashKey = ReHashKey;
139 hamt->CmpKey = strcmp;
146 HAMT_delete_trie(HAMTNode *node)
148 if (IsSubTrie(node)) {
149 unsigned long i, Size;
151 /* Count total number of bits in bitmap to determine size */
152 BitCount(Size, node->BitMapKey);
157 for (i=0; i<Size; i++)
158 HAMT_delete_trie(&(GetSubTrie(node))[i]);
159 yasm_xfree(GetSubTrie(node));
164 HAMT_destroy(HAMT *hamt, void (*deletefunc) (/*@only@*/ void *data))
169 while (!STAILQ_EMPTY(&hamt->entries)) {
171 entry = STAILQ_FIRST(&hamt->entries);
172 STAILQ_REMOVE_HEAD(&hamt->entries, next);
173 deletefunc(entry->data);
179 HAMT_delete_trie(&hamt->root[i]);
181 yasm_xfree(hamt->root);
186 HAMT_traverse(HAMT *hamt, void *d,
187 int (*func) (/*@dependent@*/ /*@null@*/ void *node,
191 STAILQ_FOREACH(entry, &hamt->entries, next) {
192 int retval = func(entry->data, d);
200 HAMT_first(const HAMT *hamt)
202 return STAILQ_FIRST(&hamt->entries);
206 HAMT_next(const HAMTEntry *prev)
208 return STAILQ_NEXT(prev, next);
212 HAMTEntry_get_data(const HAMTEntry *entry)
217 /*@-temptrans -kepttrans -mustfree@*/
219 HAMT_insert(HAMT *hamt, const char *str, void *data, int *replace,
220 void (*deletefunc) (/*@only@*/ void *data))
222 HAMTNode *node, *newnodes;
224 unsigned long key, keypart, Map;
228 key = hamt->HashKey(str);
229 keypart = key & 0x1F;
230 node = &hamt->root[keypart];
232 if (!node->BaseValue) {
233 node->BitMapKey = key;
234 entry = yasm_xmalloc(sizeof(HAMTEntry));
237 STAILQ_INSERT_TAIL(&hamt->entries, entry, next);
238 SetValue(hamt, node, entry);
240 hamt->error_func(__FILE__, __LINE__,
241 N_("Data is seen as subtrie (misaligned?)"));
247 if (!(IsSubTrie(node))) {
248 if (node->BitMapKey == key
249 && hamt->CmpKey(((HAMTEntry *)(node->BaseValue))->str,
253 deletefunc(((HAMTEntry *)(node->BaseValue))->data);
254 ((HAMTEntry *)(node->BaseValue))->str = str;
255 ((HAMTEntry *)(node->BaseValue))->data = data;
259 return ((HAMTEntry *)(node->BaseValue))->data;
261 unsigned long key2 = node->BitMapKey;
262 /* build tree downward until keys differ */
264 unsigned long keypart2;
266 /* replace node with subtrie */
268 if (keypartbits > 30) {
269 /* Exceeded 32 bits: rehash */
270 key = hamt->ReHashKey(str, level);
271 key2 = hamt->ReHashKey(
272 ((HAMTEntry *)(node->BaseValue))->str, level);
275 keypart = (key >> keypartbits) & 0x1F;
276 keypart2 = (key2 >> keypartbits) & 0x1F;
278 if (keypart == keypart2) {
279 /* Still equal, build one-node subtrie and continue
282 newnodes = yasm_xmalloc(sizeof(HAMTNode));
283 newnodes[0].BitMapKey = key2;
284 newnodes[0].BaseValue = node->BaseValue;
285 node->BitMapKey = 1<<keypart;
286 SetSubTrie(hamt, node, newnodes);
290 /* partitioned: allocate two-node subtrie */
291 newnodes = yasm_xmalloc(2*sizeof(HAMTNode));
293 entry = yasm_xmalloc(sizeof(HAMTEntry));
296 STAILQ_INSERT_TAIL(&hamt->entries, entry, next);
298 /* Copy nodes into subtrie based on order */
299 if (keypart2 < keypart) {
300 newnodes[0].BitMapKey = key2;
301 newnodes[0].BaseValue = node->BaseValue;
302 newnodes[1].BitMapKey = key;
303 SetValue(hamt, &newnodes[1], entry);
305 newnodes[0].BitMapKey = key;
306 SetValue(hamt, &newnodes[0], entry);
307 newnodes[1].BitMapKey = key2;
308 newnodes[1].BaseValue = node->BaseValue;
311 /* Set bits in bitmap corresponding to keys */
312 node->BitMapKey = (1UL<<keypart) | (1UL<<keypart2);
313 SetSubTrie(hamt, node, newnodes);
321 /* Subtrie: look up in bitmap */
323 if (keypartbits > 30) {
324 /* Exceeded 32 bits of current key: rehash */
325 key = hamt->ReHashKey(str, level);
328 keypart = (key >> keypartbits) & 0x1F;
329 if (!(node->BitMapKey & (1<<keypart))) {
330 /* bit is 0 in bitmap -> add node to table */
334 node->BitMapKey |= 1<<keypart;
336 /* Count total number of bits in bitmap to determine new size */
337 BitCount(Size, node->BitMapKey);
341 newnodes = yasm_xmalloc(Size*sizeof(HAMTNode));
343 /* Count bits below to find where to insert new node at */
344 BitCount(Map, node->BitMapKey & ~((~0UL)<<keypart));
345 Map &= 0x1F; /* Clamp to <32 */
346 /* Copy existing nodes leaving gap for new node */
347 memcpy(newnodes, GetSubTrie(node), Map*sizeof(HAMTNode));
348 memcpy(&newnodes[Map+1], &(GetSubTrie(node))[Map],
349 (Size-Map-1)*sizeof(HAMTNode));
350 /* Delete old subtrie */
351 yasm_xfree(GetSubTrie(node));
352 /* Set up new node */
353 newnodes[Map].BitMapKey = key;
354 entry = yasm_xmalloc(sizeof(HAMTEntry));
357 STAILQ_INSERT_TAIL(&hamt->entries, entry, next);
358 SetValue(hamt, &newnodes[Map], entry);
359 SetSubTrie(hamt, node, newnodes);
365 /* Count bits below */
366 BitCount(Map, node->BitMapKey & ~((~0UL)<<keypart));
367 Map &= 0x1F; /* Clamp to <32 */
369 /* Go down a level */
371 node = &(GetSubTrie(node))[Map];
374 /*@=temptrans =kepttrans =mustfree@*/
377 HAMT_search(HAMT *hamt, const char *str)
380 unsigned long key, keypart, Map;
384 key = hamt->HashKey(str);
385 keypart = key & 0x1F;
386 node = &hamt->root[keypart];
388 if (!node->BaseValue)
392 if (!(IsSubTrie(node))) {
393 if (node->BitMapKey == key
394 && hamt->CmpKey(((HAMTEntry *)(node->BaseValue))->str,
396 return ((HAMTEntry *)(node->BaseValue))->data;
401 /* Subtree: look up in bitmap */
403 if (keypartbits > 30) {
404 /* Exceeded 32 bits of current key: rehash */
405 key = hamt->ReHashKey(str, level);
408 keypart = (key >> keypartbits) & 0x1F;
409 if (!(node->BitMapKey & (1<<keypart)))
410 return NULL; /* bit is 0 in bitmap -> no match */
412 /* Count bits below */
413 BitCount(Map, node->BitMapKey & ~((~0UL)<<keypart));
414 Map &= 0x1F; /* Clamp to <32 */
416 /* Go down a level */
418 node = &(GetSubTrie(node))[Map];