Hash table now works.
This commit is contained in:
parent
b44d00ed33
commit
6101b8c43a
2 changed files with 18 additions and 6 deletions
|
|
@ -99,13 +99,20 @@ void h_slist_free(HSlist *slist) {
|
||||||
}
|
}
|
||||||
|
|
||||||
HHashTable* h_hashtable_new(HArena *arena, HEqualFunc equalFunc, HHashFunc hashFunc) {
|
HHashTable* h_hashtable_new(HArena *arena, HEqualFunc equalFunc, HHashFunc hashFunc) {
|
||||||
HHashTable *ht = h_arena_malloc(arena, sizeof(HHashTable*));
|
HHashTable *ht = h_arena_malloc(arena, sizeof(HHashTable));
|
||||||
ht->hashFunc = hashFunc;
|
ht->hashFunc = hashFunc;
|
||||||
ht->equalFunc = equalFunc;
|
ht->equalFunc = equalFunc;
|
||||||
ht->capacity = 64; // to start; should be tuned later...
|
ht->capacity = 64; // to start; should be tuned later...
|
||||||
ht->used = 0;
|
ht->used = 0;
|
||||||
|
ht->arena = arena;
|
||||||
ht->contents = h_arena_malloc(arena, sizeof(HHashTableEntry) * ht->capacity);
|
ht->contents = h_arena_malloc(arena, sizeof(HHashTableEntry) * ht->capacity);
|
||||||
memset(ht->contents, 0, sizeof(HHashTableEntry) * ht->capacity);
|
for (size_t i = 0; i < ht->capacity; i++) {
|
||||||
|
ht->contents[i].key = NULL;
|
||||||
|
ht->contents[i].value = NULL;
|
||||||
|
ht->contents[i].next = NULL;
|
||||||
|
ht->contents[i].hashval = 0;
|
||||||
|
}
|
||||||
|
//memset(ht->contents, 0, sizeof(HHashTableEntry) * ht->capacity);
|
||||||
return ht;
|
return ht;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -115,7 +122,8 @@ void* h_hashtable_get(HHashTable* ht, void* key) {
|
||||||
assert((ht->capacity & (ht->capacity - 1)) == 0); // capacity is a power of 2
|
assert((ht->capacity & (ht->capacity - 1)) == 0); // capacity is a power of 2
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
for (HHashTableEntry *hte = &ht->contents[hashval & (ht->capacity - 1)];
|
HHashTableEntry *hte = NULL;
|
||||||
|
for (hte = &ht->contents[hashval & (ht->capacity - 1)];
|
||||||
hte != NULL;
|
hte != NULL;
|
||||||
hte = hte->next) {
|
hte = hte->next) {
|
||||||
if (hte->hashval != hashval)
|
if (hte->hashval != hashval)
|
||||||
|
|
@ -140,13 +148,17 @@ void h_hashtable_put(HHashTable* ht, void* key, void* value) {
|
||||||
do {
|
do {
|
||||||
if (hte->hashval == hashval && ht->equalFunc(key, hte->key))
|
if (hte->hashval == hashval && ht->equalFunc(key, hte->key))
|
||||||
goto insert_here;
|
goto insert_here;
|
||||||
} while (hte->next);
|
if (hte->next != NULL)
|
||||||
|
hte = hte->next;
|
||||||
|
} while (hte->next != NULL);
|
||||||
// Add a new link...
|
// Add a new link...
|
||||||
assert (hte->next == NULL);
|
assert (hte->next == NULL);
|
||||||
hte->next = h_arena_malloc(ht->arena, sizeof(HHashTableEntry));
|
hte->next = h_arena_malloc(ht->arena, sizeof(HHashTableEntry));
|
||||||
hte = hte->next;
|
hte = hte->next;
|
||||||
hte->next = NULL;
|
hte->next = NULL;
|
||||||
}
|
ht->used++;
|
||||||
|
} else
|
||||||
|
ht->used++;
|
||||||
|
|
||||||
insert_here:
|
insert_here:
|
||||||
hte->key = key;
|
hte->key = key;
|
||||||
|
|
|
||||||
|
|
@ -201,7 +201,7 @@ void h_hashtable_free(HHashTable* ht);
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
#include <malloc.h>
|
#include <malloc.h>
|
||||||
#define arena_malloc(a, s) malloc(s)
|
#define h_arena_malloc(a, s) malloc(s)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif // #ifndef HAMMER_INTERNAL__H
|
#endif // #ifndef HAMMER_INTERNAL__H
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue