summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarson Fleming <[email protected]>2026-02-01 04:09:19 -0500
committerCarson Fleming <[email protected]>2026-02-01 04:09:19 -0500
commit1ab4f55dfa898b0929bbb3c5ead76337b920d0bf (patch)
tree2396e550b2517e6bfa202961c0b4d749163634fa
parentcbcbbc4c25126fae22369286eef75b3b0973d6ec (diff)
downloadsafec-1ab4f55dfa898b0929bbb3c5ead76337b920d0bf.tar.gz
map_foreach + fix counter bug
-rw-r--r--map.c11
-rw-r--r--map.h2
2 files changed, 13 insertions, 0 deletions
diff --git a/map.c b/map.c
index ab7f922..a34def4 100644
--- a/map.c
+++ b/map.c
@@ -156,9 +156,20 @@ void* map_remove(map_t* map, const void* key) {
struct __map_entry* entry = fetch_entry(map, key);
if (entry->next == NULL) return NULL;
+ map->size--;
void* rv = entry->value;
struct __map_entry* steal = entry->next;
*entry = *steal;
free(steal);
return rv;
}
+
+void map_foreach(map_t* map, foreach_func_t foreach_func, void* data) {
+ for (size_t i = 0; i < map->__num_buckets; i++) {
+ struct __map_entry* entry = &map->__buckets[i];
+ while (entry->next != NULL) {
+ foreach_func(entry->key, entry->value, data);
+ entry = entry->next;
+ }
+ }
+}
diff --git a/map.h b/map.h
index bca8c1e..466ed25 100644
--- a/map.h
+++ b/map.h
@@ -5,6 +5,7 @@ size_t hash_bytes(const void* start, size_t size);
typedef size_t (*hash_func_t)(const void*);
typedef bool (*eq_func_t)(const void*, const void*);
+typedef void (*foreach_func_t)(const void*, void*, void*);
struct __map_entry {
void* key;
@@ -42,5 +43,6 @@ void* map_get_or_default(const map_t* map, const void* key, void* default_val);
void* map_compute_if_absent(map_t* map, void* key, void* default_val);
void map_put(map_t* map, void* key, void* val);
void* map_remove(map_t* map, const void* key);
+void map_foreach(map_t* map, foreach_func_t foreach_func, void* data);
#endif