From a76c093c9f1a6578a0f38e9fd183a279dd1f1fa5 Mon Sep 17 00:00:00 2001 From: Lennart ten Wolde Date: Fri, 8 Dec 2023 04:24:47 +0100 Subject: [PATCH] fix: dont copy unset map nodes --- day_5.c | 2 +- map.c | 19 ++++++++++--------- map.h | 4 ++-- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/day_5.c b/day_5.c index df4a6a0..45b98c2 100644 --- a/day_5.c +++ b/day_5.c @@ -12,7 +12,7 @@ int main() { for(int i = 0; i < 10000; i++) { map_put(map, i, i + 1); } - for(int i = 1; i < 10000; i++) { + for(int i = 0; i < 10000; i++) { assert(map_get(map, i) == i + 1); } diff --git a/map.c b/map.c index bffa4bc..c0964c6 100644 --- a/map.c +++ b/map.c @@ -19,7 +19,7 @@ typedef struct MapNode_struct { int value; } MapNode; -struct _Map { +struct Map_struct { MapNode *nodes; uint32_t size; uint32_t capacity; @@ -126,15 +126,16 @@ __inline void map_ensure_capacity(Map *map) { for(int i = 0; i < capacity; i++) { MapNode node = nodes[i]; MapNode *next = node.next; + if(next == NULL) { + continue; + } map_set_node(map, node); - if(next != NULL) { - while((size_t) next != UINTPTR_MAX) { - map_set_node(map, *next); - MapNode *after = next->next; - assert(after != NULL); - free(next); - next = after; - } + while((size_t) next != UINTPTR_MAX) { + map_set_node(map, *next); + MapNode *after = next->next; + assert(after != NULL); + free(next); + next = after; } } free(nodes); diff --git a/map.h b/map.h index 1cddc9d..4c04ced 100644 --- a/map.h +++ b/map.h @@ -6,8 +6,8 @@ #ifndef MAP_H -struct _Map; -typedef struct _Map Map; +struct Map_struct; +typedef struct Map_struct Map; Map* map_create();