fix: dont copy unset map nodes
This commit is contained in:
2
day_5.c
2
day_5.c
@@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
19
map.c
19
map.c
@@ -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,15 +126,16 @@ __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;
|
assert(after != NULL);
|
||||||
assert(after != NULL);
|
free(next);
|
||||||
free(next);
|
next = after;
|
||||||
next = after;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
free(nodes);
|
free(nodes);
|
||||||
|
|||||||
Reference in New Issue
Block a user