aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMosder <xmosder12@gmail.com>2026-07-10 12:55:39 +0200
committerMosder <xmosder12@gmail.com>2026-07-10 12:55:39 +0200
commit26d8457a5da5d70226fdbba5ed92fda8cac735f6 (patch)
tree0545c0e39a7e33a23c34fe26c55db801d7e7e451
parent54fdf207352a0ee44a8264972118f6cc9d30cad1 (diff)
feat: added functions for iterating over a hashmap
-rw-r--r--analyzer/utils/hashmap.c21
-rw-r--r--analyzer/utils/hashmap.h15
2 files changed, 36 insertions, 0 deletions
diff --git a/analyzer/utils/hashmap.c b/analyzer/utils/hashmap.c
index 8af7630..2ab8bab 100644
--- a/analyzer/utils/hashmap.c
+++ b/analyzer/utils/hashmap.c
@@ -96,6 +96,27 @@ void hashmap_delete(HashMap *map, void *key) {
}
}
+HashMapNode *hashmap_first(HashMap *map) {
+ for (size_t i = 0; i < HASHMAP_TABLE_SIZE; i++) {
+ HashMapNode *node = map->table[i];
+ if (node)
+ return node;
+ }
+ return NULL;
+}
+
+HashMapNode *hashmap_next(HashMap *map, HashMapNode *node) {
+ if (node->next)
+ return node->next;
+
+ for (size_t i = map->hash(node->key) + 1; i < HASHMAP_TABLE_SIZE; i++) {
+ HashMapNode *node = map->table[i];
+ if (node)
+ return node;
+ }
+ return NULL;
+}
+
void hashmap_free(HashMap *map) {
for (size_t i = 0; i < HASHMAP_TABLE_SIZE; i++) {
HashMapNode *curr = map->table[i];
diff --git a/analyzer/utils/hashmap.h b/analyzer/utils/hashmap.h
index 96e8b58..22c71c6 100644
--- a/analyzer/utils/hashmap.h
+++ b/analyzer/utils/hashmap.h
@@ -72,6 +72,21 @@ extern void *hashmap_get(HashMap *map, void *key);
// - key - pointer to the key of the entry to delete
extern void hashmap_delete(HashMap *map, void *key);
+// get first node in the hashmap, use with hashmap_next to iterate over hashmap
+// params:
+// - map - hash map to get the first node from
+// returns:
+// pointer to the first node in the hashmap or NULL if there's no entries in it
+extern HashMapNode *hashmap_first(HashMap *map);
+
+// get next node in the hashmap, use with hashmap_first to iterate over hashmap
+// params:
+// - map - hash map to get the next node from
+// - node - the node preceeding the one we search for
+// returns:
+// pointer to the next node in the hashmap or NULL if there's no next
+extern HashMapNode *hashmap_next(HashMap *map, HashMapNode *node);
+
// free the hash map
// - map - hash map to free
extern void hashmap_free(HashMap *map);