aoc-2019-c

git clone https://git.sinitax.com/sinitax/aoc-2019-c
Log | Files | Refs | README | sfeed.txt

commit 7d77e0cc6ded255d08f27d9148bdc1efe0d602e1
parent 92624930e87e34891fd11c65a6ca07697aebe7ba
Author: Louis Burda <quent.burda@gmail.com>
Date:   Thu, 11 Nov 2021 02:34:12 +0100

Modify hashmap implementation

Diffstat:
Mlibs/src/hashmap.c | 7++++---
Msrc/10/main.c | 7++++---
Msrc/11/main.c | 7+++----
3 files changed, 11 insertions(+), 10 deletions(-)

diff --git a/libs/src/hashmap.c b/libs/src/hashmap.c @@ -83,7 +83,8 @@ hashmap_add(struct hashmap *hm, struct hashmap_key key, void *value) while (*iter) iter = &((*iter)->next); *iter = CHKP(malloc(sizeof(struct hashmap_link))); - (*iter)->key = key; + (*iter)->key.key = CHKP(memdup(key.key, key.len)); + (*iter)->key.len = key.len; (*iter)->value = value; (*iter)->next = NULL; } @@ -105,8 +106,8 @@ hashmap_set(struct hashmap *hm, struct hashmap_key key, void *value) } *iter = CHKP(malloc(sizeof(struct hashmap_link))); - // TODO memdup key here using its length instead of in user code!! - (*iter)->key = key; + (*iter)->key.key = CHKP(memdup(key.key, key.len)); + (*iter)->key.len = key.len; (*iter)->value = value; (*iter)->next = NULL; diff --git a/src/10/main.c b/src/10/main.c @@ -123,14 +123,14 @@ get_vis(struct hashmap *hm, struct asteroid *a1) dir.dy = a2->y - a1->y; reduce_frac(&dir); - if (hashmap_get(hm, HASHMAP_KEY(&dir, sizeof(dir)), &link)) { + key = HASHMAP_KEY(&dir, sizeof(dir)); + if (hashmap_get(hm, key, &link)) { tmp = link->value; odir = (struct dir) { tmp->x - a1->x, tmp->y - a1->y }; dir = (struct dir) { a2->x - a1->x, a2->y - a1->y }; if (ABS(odir.dx) + ABS(odir.dy) > ABS(dir.dx) + ABS(dir.dy)) link->value = a2; } else { - key = HASHMAP_KEY(CHKP(memdup(&dir, sizeof(dir))), sizeof(dir)); hashmap_set(hm, key, a2); } } @@ -237,7 +237,8 @@ part2(void) prev = angle(list[0], base); for (k = 0; k < len; k++) { - debug("%i: %i %i %f\n", k+1, list[k]->x, list[k]->y, angle(list[k], base)); + debug("%i: %i %i %f\n", k+1, list[k]->x, + list[k]->y, angle(list[k], base)); } aoc.answer = CHKP(aprintf("%i", list[199]->x * 100 + list[199]->y)); diff --git a/src/11/main.c b/src/11/main.c @@ -119,7 +119,7 @@ panels_init(struct hashmap *panels, int start_color) icc_set_debug(&icc, aoc.debug & 0b10); pos.x = pos.y = 0; - hashmap_set(panels, HASHMAP_KEY(memdup(&pos, sizeof(pos)), sizeof(pos)), &start_color); + hashmap_set(panels, HASHMAP_KEY(&pos, sizeof(pos)), &start_color); rot = 0; color = 0; @@ -131,10 +131,9 @@ panels_init(struct hashmap *panels, int start_color) if (state == STATE_COLOR) { color = mi_cast_ul(&icc.out); ASSERT(color == 0 || color == 1); - key = HASHMAP_KEY(CHKP(memdup(&pos, sizeof(pos))), sizeof(pos)); debug("OUTPUT %i %i: %i\n", pos.x, pos.y, color); - if (hashmap_set(panels, key, color ? &white : &black)) - free(key.key); + key = HASHMAP_KEY(&pos, sizeof(pos)); + hashmap_set(panels, key, color ? &white : &black); state = STATE_ROT; } else if (state == STATE_ROT) { brot = mi_cast_ul(&icc.out);