nserver

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | LICENSE

commit 1993bfc6cee9f87ee50137bfe9bae49d513d7cbb
parent fde085c87b6c30eeb6181921b3e9b13ee295700c
Author: rsiddharth <s@ricketyspace.net>
Date:   Sat,  9 Nov 2019 10:43:15 -0500

nserver: Update hashmap.

Add 'Hashmap_keys'. Taken from lcthw/hashmap.

Diffstat:
nserver/src/hashmap.c | 35+++++++++++++++++++++++++++++++++++
nserver/src/hashmap.h | 3+++
2 files changed, 38 insertions(+), 0 deletions(-)

diff --git a/nserver/src/hashmap.c b/nserver/src/hashmap.c @@ -268,6 +268,41 @@ int Hashmap_traverse(Hashmap *map, Hashmap_traverse_cb traverse_cb) return 0; } +/** + * Returns all keys of the hashmap as a DArray. + * + * Use DArray_destroy on the 'keys' returned by this function after + * use. + */ +DArray *Hashmap_keys(Hashmap *map) +{ + check(map != NULL, "map is NULL"); + + DArray *keys = DArray_create(sizeof(void *), + DEFAULT_NUMBER_OF_KEYS); + check(keys != NULL, "Unable to initialize keys"); + + int i = 0; + int j = 0; + + for (i = 0; i < DArray_count(map->buckets); i++) { + DArray *bucket = DArray_get(map->buckets, i); + if (bucket) { + for (j = 0; j < DArray_count(bucket); j++) { + HashmapNode *node = DArray_get(bucket, j); + + if (node) { + DArray_push(keys, node->key); + } + } + } + } + + return keys; + error: + return NULL; +} + void *Hashmap_delete(Hashmap *map, void *key) { uint32_t hash = 0; diff --git a/nserver/src/hashmap.h b/nserver/src/hashmap.h @@ -5,6 +5,7 @@ #include <darray.h> #define DEFAULT_NUMBER_OF_BUCKETS 100 +#define DEFAULT_NUMBER_OF_KEYS 50 typedef int (*Hashmap_compare) (void *a, void *b); typedef uint32_t(*Hashmap_hash) (void *key); @@ -36,5 +37,7 @@ int Hashmap_traverse(Hashmap *map, Hashmap_traverse_cb travers_cb); void *Hashmap_delete(Hashmap *map, void *key); +DArray *Hashmap_keys(Hashmap *map); + uint32_t fnv_hash(void *a); #endif