From 5bb8f72816e7bbcffdc5053f9e7dd3d819fcd840 Mon Sep 17 00:00:00 2001 From: Kai Stevenson Date: Fri, 17 Feb 2023 15:18:28 -0800 Subject: added more frees I think --- src/hashtable.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'src/hashtable.c') 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) { -- cgit v1.3.1