diff options
author | Preston Pan <ret2pop@gmail.com> | 2025-01-30 21:02:42 -0800 |
---|---|---|
committer | Preston Pan <ret2pop@gmail.com> | 2025-01-30 21:02:42 -0800 |
commit | 3780f207f924f14734cb839fd015bd883fe52ff1 (patch) | |
tree | de13568dbf97ffe477943854bcb3ea6001b46cf8 /src/common/hash_table.c | |
parent | ef9ab1fd141f4057d41f2d6ed8ab8d67c44894d5 (diff) |
Diffstat (limited to 'src/common/hash_table.c')
-rw-r--r-- | src/common/hash_table.c | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/src/common/hash_table.c b/src/common/hash_table.c index 6b4c014..caefae6 100644 --- a/src/common/hash_table.c +++ b/src/common/hash_table.c @@ -111,6 +111,17 @@ void ht_insert(ht_t *ht, char *key, void *value) { list_push_back(bucket, init_pair(key, value)); } +void *ht_modify(ht_t *ht, char *key, void *newval) { + void *oldval = ht_pop(ht, key); + ht_insert(ht, key, newval); + return oldval; +} + +void ht_overwrite(ht_t *ht, char *key, void *newval, void (*freefunc)(void *)) { + void *oldval = ht_modify(ht, key, newval); + freefunc(oldval); +} + void *ht_pop(ht_t *ht, char *key) { unsigned long bn = hash(key) % ht->size; list_t *bucket = ht->buckets[bn]; @@ -136,6 +147,14 @@ void ht_free(void *x, void (*freefunc)(void *)) { free(ht); } +ht_t *init_map(size_t size) { + ht_t *ht = safe_calloc(1, sizeof(size)); + size_t realsize = size == 0 ? DEFAULT_HT_SIZE : size; + ht->buckets = safe_calloc(realsize, sizeof(list_t *)); + ht->is_map = true; + return ht; +} + /* DJB2 hash function */ unsigned long hash(char *str) { unsigned long hash = 5381; |