From 3780f207f924f14734cb839fd015bd883fe52ff1 Mon Sep 17 00:00:00 2001 From: Preston Pan Date: Thu, 30 Jan 2025 21:02:42 -0800 Subject: restructure project --- src/common/hash_table.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'src/common/hash_table.c') 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; -- cgit