4 * Efficient dictionary hash table class.
14 #define HASH_MAX_LOAD 2 /* Higher = more memory-efficient, slower */
16 static struct hash_tbl_node
*alloc_table(size_t newsize
)
18 size_t bytes
= newsize
*sizeof(struct hash_tbl_node
);
19 struct hash_tbl_node
*newtbl
= nasm_zalloc(bytes
);
24 struct hash_table
*hash_init(size_t size
)
26 struct hash_table
*head
= nasm_malloc(sizeof(struct hash_table
));
28 head
->table
= alloc_table(size
);
31 head
->max_load
= size
*(HASH_MAX_LOAD
-1)/HASH_MAX_LOAD
;
37 * Find an entry in a hash table.
39 * On failure, if "insert" is non-NULL, store data in that structure
40 * which can be used to insert that node using hash_add().
42 * WARNING: this data is only valid until the very next call of
43 * hash_add(); it cannot be "saved" to a later date.
45 * On success, return a pointer to the "data" element of the hash
48 void **hash_find(struct hash_table
*head
, const char *key
,
49 struct hash_insert
*insert
)
51 struct hash_tbl_node
*np
;
52 uint64_t hash
= crc64(CRC64_INIT
, key
);
53 struct hash_tbl_node
*tbl
= head
->table
;
54 size_t mask
= head
->size
-1;
55 size_t pos
= hash
& mask
;
56 size_t inc
= ((hash
>> 32) & mask
) | 1; /* Always odd */
58 while ((np
= &tbl
[pos
])->key
) {
59 if (hash
== np
->hash
&& !strcmp(key
, np
->key
))
61 pos
= (pos
+inc
) & mask
;
64 /* Not found. Store info for insert if requested. */
74 * Same as hash_find, but for case-insensitive hashing.
76 void **hash_findi(struct hash_table
*head
, const char *key
,
77 struct hash_insert
*insert
)
79 struct hash_tbl_node
*np
;
80 uint64_t hash
= crc64i(CRC64_INIT
, key
);
81 struct hash_tbl_node
*tbl
= head
->table
;
82 size_t mask
= head
->size
-1;
83 size_t pos
= hash
& mask
;
84 size_t inc
= ((hash
>> 32) & mask
) | 1; /* Always odd */
86 while ((np
= &tbl
[pos
])->key
) {
87 if (hash
== np
->hash
&& !nasm_stricmp(key
, np
->key
))
89 pos
= (pos
+inc
) & mask
;
92 /* Not found. Store info for insert if requested. */
102 * Insert node. Return a pointer to the "data" element of the newly
105 void **hash_add(struct hash_insert
*insert
, const char *key
, void *data
)
107 struct hash_table
*head
= insert
->head
;
108 struct hash_tbl_node
*np
= insert
->where
;
110 /* Insert node. We can always do this, even if we need to
111 rebalance immediately after. */
112 np
->hash
= insert
->hash
;
116 if (++head
->load
> head
->max_load
) {
117 /* Need to expand the table */
118 size_t newsize
= head
->size
<< 1;
119 struct hash_tbl_node
*newtbl
= alloc_table(newsize
);
120 size_t mask
= newsize
-1;
123 struct hash_tbl_node
*op
, *xp
;
126 /* Rebalance all the entries */
127 for (i
= 0, op
= head
->table
; i
< head
->size
; i
++, op
++) {
129 size_t pos
= op
->hash
& mask
;
130 size_t inc
= ((op
->hash
>> 32) & mask
) | 1;
132 while ((xp
= &newtbl
[pos
])->key
)
133 pos
= (pos
+inc
) & mask
;
140 nasm_free(head
->table
);
143 head
->table
= newtbl
;
144 head
->size
= newsize
;
145 head
->max_load
= newsize
*(HASH_MAX_LOAD
-1)/HASH_MAX_LOAD
;
152 * Iterate over all members of a hash set. For the first call,
153 * iterator should be initialized to NULL. Returns the data pointer,
154 * or NULL on failure.
156 void *hash_iterate(const struct hash_table
*head
,
157 struct hash_tbl_node
**iterator
,
160 struct hash_tbl_node
*np
= *iterator
;
161 struct hash_tbl_node
*ep
= head
->table
+ head
->size
;
183 * Free the hash itself. Doesn't free the data elements; use
184 * hash_iterate() to do that first, if needed.
186 void hash_free(struct hash_table
*head
)
188 nasm_free(head
->table
);