From 42696cdb053d994eacfc66bb348b0ddf0d80046c Mon Sep 17 00:00:00 2001 From: Kai Stevenson Date: Fri, 17 Feb 2023 21:07:35 -0800 Subject: added update() function, added multiply() function to stdlib --- src/hashtable.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'src/hashtable.c') diff --git a/src/hashtable.c b/src/hashtable.c index 3a83861..2ec766d 100644 --- a/src/hashtable.c +++ b/src/hashtable.c @@ -49,18 +49,28 @@ unsigned int hashtable_add_pair(hashtable_t* hashtable, pair_t pair) { unsigned int hash = get_hash(pair.key); for (int i = 0; i < hashtable->capacity; i++) { pair_t p = hashtable->pairs[hash % hashtable->capacity]; - if (p.key == NULL || p.key == pair.key) { + if (p.key == NULL || strcmp(p.key, pair.key) == 0) { hashtable->pairs[hash % hashtable->capacity] = pair; return hash; } } return 0; } +unsigned int hashtable_update_pair_recurse(hashtable_t* origin, pair_t pair) { + pair_t p = hashtable_get_pair(origin, pair.key); + if (p.key != NULL) { + return hashtable_add_pair(origin, pair); + } + if (origin->parent != NULL) { + return hashtable_update_pair_recurse(origin->parent, pair); + } + 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) { + if (p.key != NULL && strcmp(p.key, key) == 0) { return p; } else if (p.key == NULL) { -- cgit v1.3.1