diff options
| author | Kai Stevenson <kai@kaistevenson.com> | 2023-02-17 15:18:28 -0800 |
|---|---|---|
| committer | Kai Stevenson <kai@kaistevenson.com> | 2023-02-17 15:18:28 -0800 |
| commit | 5bb8f72816e7bbcffdc5053f9e7dd3d819fcd840 (patch) | |
| tree | beda48e0fba4e4d2b3134b393f990f6719ddf46c /src/hashtable.c | |
| parent | cb4abc221844aa22f11447914d6d5dedd71a054c (diff) | |
added more frees I think
Diffstat (limited to 'src/hashtable.c')
| -rw-r--r-- | src/hashtable.c | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/src/hashtable.c b/src/hashtable.c index 2506c5b..9a8484d 100644 --- a/src/hashtable.c +++ b/src/hashtable.c @@ -32,15 +32,28 @@ unsigned int get_hash(char* str) { } return hash; } -/* TODO: handle collision -TODO: return 0 if something goes wrong */ unsigned int hashtable_add_pair(hashtable_t* hashtable, pair_t pair) { unsigned int hash = get_hash(pair.key); - hashtable->pairs[hash % hashtable->capacity] = pair; - return hash; + for (int i = 0; i < hashtable->capacity; i++) { + pair_t p = hashtable->pairs[hash % hashtable->capacity]; + if (p.key == NULL || p.key == pair.key) { + hashtable->pairs[hash % hashtable->capacity] = pair; + return hash; + } + } + return 0; } pair_t hashtable_get_pair(hashtable_t* hashtable, char* key) { unsigned int hash = get_hash(key); + for (int i = 0; i < hashtable->capacity; i++) { + pair_t p = hashtable->pairs[hash % hashtable->capacity]; + if (p.key == key) { + return p; + } + else if (p.key == NULL) { + return init_pair(NULL, NULL); + } + } return hashtable->pairs[(hash) % hashtable->capacity]; } pair_t hashtable_get_pair_recurse(hashtable_t* origin, char* key) { |
