Skip to content

Commit 597ea44

Browse files
handle empty hash table case
1 parent e0d0cf6 commit 597ea44

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

src/linker/hash_table.c

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,21 @@ hash_table_push_path_raw(Arena *arena, HashTable *ht, String8 path, void *value)
162162

163163
////////////////////////////////
164164

165+
internal BucketNode *
166+
hash_table_bucket_from_hash(HashTable *ht, U64 hash)
167+
{
168+
BucketNode *bucket = 0;
169+
if (ht->cap > 0) {
170+
bucket = ht->buckets[hash % ht->cap].first;
171+
}
172+
return bucket;
173+
}
174+
165175
internal BucketNode *
166176
hash_table_search_string(HashTable *ht, String8 key_string)
167177
{
168-
U64 hash = hash_table_hasher(key_string);
169-
U64 ibucket = hash % ht->cap;
170-
BucketList *bucket = ht->buckets + ibucket;
171-
for (BucketNode *n = bucket->first; n != 0; n = n->next) {
178+
BucketNode *bucket = hash_table_bucket_from_hash(ht, hash_table_hasher(key_string));
179+
for EachNode(n, BucketNode, bucket) {
172180
if (str8_match(n->v.key_string, key_string, 0)) {
173181
return n;
174182
}
@@ -179,10 +187,8 @@ hash_table_search_string(HashTable *ht, String8 key_string)
179187
internal BucketNode *
180188
hash_table_search_u32(HashTable *ht, U32 key_u32)
181189
{
182-
U64 hash = hash_table_hasher(str8_struct(&key_u32));
183-
U64 ibucket = hash % ht->cap;
184-
BucketList *bucket = ht->buckets + ibucket;
185-
for (BucketNode *n = bucket->first; n != 0; n = n->next) {
190+
BucketNode *bucket = hash_table_bucket_from_hash(ht, hash_table_hasher(str8_struct(&key_u32)));
191+
for EachNode(n, BucketNode, bucket) {
186192
if (n->v.key_u32 == key_u32) {
187193
return n;
188194
}
@@ -193,10 +199,8 @@ hash_table_search_u32(HashTable *ht, U32 key_u32)
193199
internal BucketNode *
194200
hash_table_search_u64(HashTable *ht, U64 key_u64)
195201
{
196-
U64 hash = hash_table_hasher(str8_struct(&key_u64));
197-
U64 ibucket = hash % ht->cap;
198-
BucketList *bucket = ht->buckets + ibucket;
199-
for (BucketNode *n = bucket->first; n != 0; n = n->next) {
202+
BucketNode *bucket = hash_table_bucket_from_hash(ht, hash_table_hasher(str8_struct(&key_u64)));
203+
for EachNode(n, BucketNode, bucket) {
200204
if (n->v.key_u64 == key_u64) {
201205
return n;
202206
}
@@ -219,10 +223,8 @@ hash_table_search_path(HashTable *ht, String8 path)
219223
internal BucketNode *
220224
hash_table_search_raw(HashTable *ht, void *key)
221225
{
222-
U64 hash = hash_table_hasher(str8_struct(&key));
223-
U64 ibucket = hash % ht->cap;
224-
BucketList *bucket = ht->buckets + ibucket;
225-
for (BucketNode *n = bucket->first; n != 0; n = n->next) {
226+
BucketNode *bucket = hash_table_bucket_from_hash(ht, hash_table_hasher(str8_struct(&key)));
227+
for EachNode(n, BucketNode, bucket) {
226228
if (n->v.key_raw == key) {
227229
return n;
228230
}

0 commit comments

Comments
 (0)