4 * Efficient dictionary hash table class.
12 #define HASH_INITIAL_SIZE 64
13 #define HASH_MAX_LOAD 2 /* Higher = more memory-efficient, slower */
15 static struct hash_tbl_node *alloc_table(size_t newsize)
17 size_t bytes = newsize*sizeof(struct hash_tbl_node);
18 struct hash_tbl_node *newtbl = nasm_zalloc(bytes);
23 struct hash_table *hash_init(void)
25 struct hash_table *head = nasm_malloc(sizeof(struct hash_table));
27 head->table = alloc_table(HASH_INITIAL_SIZE);
29 head->size = HASH_INITIAL_SIZE;
30 head->max_load = HASH_INITIAL_SIZE*(HASH_MAX_LOAD-1)/HASH_MAX_LOAD;
36 * Find an entry in a hash table.
38 * On failure, if "insert" is non-NULL, store data in that structure
39 * which can be used to insert that node using hash_add().
41 * WARNING: this data is only valid until the very next call of
42 * hash_add(); it cannot be "saved" to a later date.
44 * On success, return a pointer to the "data" element of the hash
47 void **hash_find(struct hash_table *head, const char *key,
48 struct hash_insert *insert)
50 struct hash_tbl_node *np;
51 uint64_t hash = crc64(key);
52 struct hash_tbl_node *tbl = head->table;
53 size_t mask = head->size-1;
54 size_t pos = hash & mask;
55 size_t inc = ((hash >> 32) & mask) | 1; /* Always odd */
57 while ((np = &tbl[pos])->key) {
58 if (hash == np->hash && !strcmp(key, np->key))
60 pos = (pos+inc) & mask;
63 /* Not found. Store info for insert if requested. */
73 * Same as hash_find, but for case-insensitive hashing.
75 void **hash_findi(struct hash_table *head, const char *key,
76 struct hash_insert *insert)
78 struct hash_tbl_node *np;
79 uint64_t hash = crc64i(key);
80 struct hash_tbl_node *tbl = head->table;
81 size_t mask = head->size-1;
82 size_t pos = hash & mask;
83 size_t inc = ((hash >> 32) & mask) | 1; /* Always odd */
85 while ((np = &tbl[pos])->key) {
86 if (hash == np->hash && !nasm_stricmp(key, np->key))
88 pos = (pos+inc) & mask;
91 /* Not found. Store info for insert if requested. */
101 * Insert node. Return a pointer to the "data" element of the newly
104 void **hash_add(struct hash_insert *insert, const char *key, void *data)
106 struct hash_table *head = insert->head;
107 struct hash_tbl_node *np = insert->where;
109 /* Insert node. We can always do this, even if we need to
110 rebalance immediately after. */
111 np->hash = insert->hash;
115 if (++head->load > head->max_load) {
116 /* Need to expand the table */
117 size_t newsize = head->size << 1;
118 struct hash_tbl_node *newtbl = alloc_table(newsize);
119 size_t mask = newsize-1;
122 struct hash_tbl_node *op, *xp;
125 /* Rebalance all the entries */
126 for (i = 0, op = head->table; i < head->size; i++, op++) {
128 size_t pos = op->hash & mask;
129 size_t inc = ((op->hash >> 32) & mask) | 1;
131 while ((xp = &newtbl[pos])->key)
132 pos = (pos+inc) & mask;
139 nasm_free(head->table);
142 head->table = newtbl;
143 head->size = newsize;
144 head->max_load = newsize*(HASH_MAX_LOAD-1)/HASH_MAX_LOAD;
151 * Iterate over all members of a hash set. For the first call,
152 * iterator should be initialized to NULL. Returns the data pointer,
153 * or NULL on failure.
155 void *hash_iterate(const struct hash_table *head,
156 struct hash_tbl_node **iterator,
159 struct hash_tbl_node *np = *iterator;
160 struct hash_tbl_node *ep = head->table + head->size;
182 * Free the hash itself. Doesn't free the data elements; use
183 * hash_iterate() to do that first, if needed.
185 void hash_free(struct hash_table *head)
187 nasm_free(head->table);