aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/hashmap.c47
-rw-r--r--src/hashmap.h10
2 files changed, 32 insertions, 25 deletions
diff --git a/src/hashmap.c b/src/hashmap.c
index 2787435..6b51d3e 100644
--- a/src/hashmap.c
+++ b/src/hashmap.c
@@ -215,37 +215,38 @@ hashmap_remove(struct hashmap *hm, const char *key)
return NULL;
}
+#define HASHMAP_WALK(hm, ...) \
+ struct node *node; \
+ struct node *next; \
+ for (size_t i = 0; i < hm->cur_cap; i++) { \
+ node = hm->buckets[i]; \
+ while (node) { \
+ next = node->next; \
+ __VA_ARGS__; \
+ node = next; \
+ } \
+ }
+
void
-hashmap_walk(struct hashmap *hm, void (*fn)(const void *key, void *value))
+hashmap_walk(struct hashmap *hm, hashmap_cb cb)
{
- struct node *node;
- struct node *next;
+ HASHMAP_WALK(hm, cb(node->key, node->value));
+}
- for (int i=0; i < hm->cur_cap; i++) {
- node = hm->buckets[i];
- while (node) {
- next = node->next;
- fn(node->key, node->value);
- node = next;
- }
- }
+void
+hashmap_destroy(struct hashmap *hm, hashmap_cb cb)
+{
+ HASHMAP_WALK(hm, cb(node->key, node->value), free(node));
+
+ free(hm->buckets);
+ free(hm);
}
void
hashmap_free(struct hashmap *hm)
{
- struct node *node;
- struct node *next;
-
- for (int i=0; i < hm->cur_cap; i++) {
- node = hm->buckets[i];
- while (node) {
- next = node->next;
- free(node);
- node = next;
- }
- }
+ HASHMAP_WALK(hm, free(node));
free(hm->buckets);
- free(hm);
+ free(hm);
}
diff --git a/src/hashmap.h b/src/hashmap.h
index 981c21b..52df625 100644
--- a/src/hashmap.h
+++ b/src/hashmap.h
@@ -19,6 +19,8 @@
#define HASHMAP_GROW_RATE 2
#endif
+typedef void (hashmap_cb)(const void *key, void *value);
+
struct hashmap {
struct node **buckets;
size_t init_cap;
@@ -49,9 +51,13 @@ void *hashmap_resolve(struct hashmap *hm, const char *key);
*/
void *hashmap_remove(struct hashmap *hm, const char *key);
+/* Iterate over keys in the hashmap */
+void hashmap_walk(struct hashmap *hm, hashmap_cb);
+
+/* free hashmap related memory calling a function before freeing each node */
+void hashmap_destroy(struct hashmap *hm, hashmap_cb cb);
+
/* free hashmap related memory */
void hashmap_free(struct hashmap *hm);
-void hashmap_walk(struct hashmap *hm, void (*fn)(const void *key, void *value));
-
#endif