summaryrefslogtreecommitdiff
path: root/src/hashtable.c
diff options
context:
space:
mode:
authorKai Stevenson <kai@kaistevenson.com>2023-02-17 21:07:35 -0800
committerKai Stevenson <kai@kaistevenson.com>2023-02-17 21:12:26 -0800
commit42696cdb053d994eacfc66bb348b0ddf0d80046c (patch)
tree105c9e6a746e25cc64df6aef61bf93f6f8713d28 /src/hashtable.c
parentdab73c05a0c4529bd26410beae0868ec2ff570c2 (diff)
added update() function, added multiply() function to stdlib
Diffstat (limited to 'src/hashtable.c')
-rw-r--r--src/hashtable.c14
1 files changed, 12 insertions, 2 deletions
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) {