aboutsummaryrefslogtreecommitdiff
path: root/src/hashmap.c
diff options
context:
space:
mode:
authorDanny van Kooten <dannyvankooten@users.noreply.github.com>2020-03-18 19:21:48 +0100
committerDanny van Kooten <dannyvankooten@users.noreply.github.com>2020-03-18 19:21:48 +0100
commit8d74fd5b0e5fc9f319a5b7d2e0716afcc312fd75 (patch)
treea6c195d183694304fbe238a68486aceea01d404a /src/hashmap.c
parent82de777afb99aa4129abcb01da76bf62e7b45263 (diff)
downloadunja-8d74fd5b0e5fc9f319a5b7d2e0716afcc312fd75.tar.gz
unja-8d74fd5b0e5fc9f319a5b7d2e0716afcc312fd75.zip
clean-up environment in env_free(). add hashmap_walk function for visiting all values in a hashmap.
Diffstat (limited to 'src/hashmap.c')
-rw-r--r--src/hashmap.c16
1 files changed, 15 insertions, 1 deletions
diff --git a/src/hashmap.c b/src/hashmap.c
index f99ba4c..600a005 100644
--- a/src/hashmap.c
+++ b/src/hashmap.c
@@ -51,7 +51,7 @@ void *hashmap_get(struct hashmap *hm, char *key) {
int pos = HASH(key);
struct node *node = hm->buckets[pos];
while (node) {
- if (strcmp(node->key, key) == 0) {
+ if (node->key && strcmp(node->key, key) == 0) {
return node->value;
}
@@ -112,6 +112,20 @@ void *hashmap_remove(struct hashmap *hm, char *key) {
return NULL;
}
+void hashmap_walk(struct hashmap *hm, void (*fn)(void *value)) {
+ struct node *node;
+ struct node *next;
+
+ for (int i=0; i < HASHMAP_CAP; i++) {
+ node = hm->buckets[i];
+ while (node) {
+ next = node->next;
+ fn(node->value);
+ node = next;
+ }
+ }
+}
+
/* free hashmap related memory */
void hashmap_free(struct hashmap *hm) {
struct node *node;