summaryrefslogtreecommitdiff
path: root/src/hashtable.c
diff options
context:
space:
mode:
authorKai Stevenson <kai@kaistevenson.com>2023-02-17 15:18:28 -0800
committerKai Stevenson <kai@kaistevenson.com>2023-02-17 15:18:28 -0800
commit5bb8f72816e7bbcffdc5053f9e7dd3d819fcd840 (patch)
treebeda48e0fba4e4d2b3134b393f990f6719ddf46c /src/hashtable.c
parentcb4abc221844aa22f11447914d6d5dedd71a054c (diff)
added more frees I think
Diffstat (limited to 'src/hashtable.c')
-rw-r--r--src/hashtable.c21
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) {