fix: dont copy unset map nodes

This commit is contained in:
2023-12-08 04:24:47 +01:00
parent b203c93f47
commit a76c093c9f
3 changed files with 13 additions and 12 deletions

View File

@@ -12,7 +12,7 @@ int main() {
for(int i = 0; i < 10000; i++) { for(int i = 0; i < 10000; i++) {
map_put(map, i, i + 1); 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); assert(map_get(map, i) == i + 1);
} }

7
map.c
View File

@@ -19,7 +19,7 @@ typedef struct MapNode_struct {
int value; int value;
} MapNode; } MapNode;
struct _Map { struct Map_struct {
MapNode *nodes; MapNode *nodes;
uint32_t size; uint32_t size;
uint32_t capacity; uint32_t capacity;
@@ -126,8 +126,10 @@ __inline void map_ensure_capacity(Map *map) {
for(int i = 0; i < capacity; i++) { for(int i = 0; i < capacity; i++) {
MapNode node = nodes[i]; MapNode node = nodes[i];
MapNode *next = node.next; MapNode *next = node.next;
if(next == NULL) {
continue;
}
map_set_node(map, node); map_set_node(map, node);
if(next != NULL) {
while((size_t) next != UINTPTR_MAX) { while((size_t) next != UINTPTR_MAX) {
map_set_node(map, *next); map_set_node(map, *next);
MapNode *after = next->next; MapNode *after = next->next;
@@ -136,7 +138,6 @@ __inline void map_ensure_capacity(Map *map) {
next = after; next = after;
} }
} }
}
free(nodes); free(nodes);
} }
} }

4
map.h
View File

@@ -6,8 +6,8 @@
#ifndef MAP_H #ifndef MAP_H
struct _Map; struct Map_struct;
typedef struct _Map Map; typedef struct Map_struct Map;
Map* map_create(); Map* map_create();