aoc-2019-c

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

commit 44eac1222be04c60f60ef0309b44f4c3145e12e2
parent ce61311b9842de5c9be9c73b75a01455e9c18073
Author: Louis Burda <quent.burda@gmail.com>
Date:   Tue, 21 Mar 2023 02:21:53 +0100

Update libhashmap

Diffstat:
M.gitmodules | 4+++-
M06/info.mk | 2+-
M06/main.c | 48+++++++++++++++++++++++++-----------------------
M09/info.mk | 2+-
M10/info.mk | 2+-
M10/main.c | 66+++++++++++++++++++++++++++++++++++++-----------------------------
M11/info.mk | 2+-
M11/main.c | 86++++++++++++++++++++++++++++++++++++++++++++-----------------------------------
M13/info.mk | 2+-
M13/main.c | 79++++++++++++++++++++++++++++++++++++++++++++++---------------------------------
MMakefile | 19+++++++++++++------
Mcommon/iccmp.c | 73++++++++++++++++++++++++++++++++++++++-----------------------------------
Mcommon/iccmp.h | 8++++----
Dlib/libhashmap | 1-
Alib/libhmap | 1+
15 files changed, 220 insertions(+), 175 deletions(-)

diff --git a/.gitmodules b/.gitmodules @@ -5,8 +5,10 @@ path = lib/libmaxint url = git@sinitax.com:snx/libmaxint [submodule "lib/libhashmap"] - path = lib/libhashmap + path = lib/libhmap url = git@sinitax.com:snx/libhashmap [submodule "lib/liballoc"] path = lib/liballoc url = git@sinitax.com:snx/liballoc +[submodule "lib/libhmap"] + url = git@sinitax.com:snx/libhmap diff --git a/06/info.mk b/06/info.mk @@ -1,4 +1,4 @@ 06_SRC = 06/main.c common/main.c common/aoc.c common/util.c -06_SRC += lib/libdvec/build/libdvec.a lib/libhashmap/build/libhashmap.a +06_SRC += lib/libdvec/build/libdvec.a lib/libhmap/build/libhmap.a 06_SRC += lib/liballoc/build/liballoc.a 06_HDR = common/aoc.h common/util.h diff --git a/06/main.c b/06/main.c @@ -1,6 +1,6 @@ #include "aoc.h" #include "dvec.h" -#include "hashmap.h" +#include "hmap.h" #include "allocator.h" #include "util.h" @@ -17,36 +17,38 @@ struct planet { struct dvec children; }; -struct hashmap planets; +struct hmap planets; struct planet * planets_get(const char *name) { - struct hashmap_link *link; + struct hmap_link *link; + struct hmap_key key; - link = hashmap_get(&planets, name, strlen(name)); + key.p = name; + link = hmap_get(&planets, key); return link ? link->value.p : NULL; } struct planet * planets_add(const char *name) { - struct planet *p; - int rc; + struct planet *planet; + char *key; - p = malloc(sizeof(struct planet)); - if (!p) die("main: planets_add: malloc"); + planet = malloc(sizeof(struct planet)); + if (!planet) die("main: planets_add: malloc"); - p->name = aprintf("%s", name); - p->parent = NULL; - dvec_init(&p->children, sizeof(struct planet *), + planet->name = aprintf("%s", name); + planet->parent = NULL; + dvec_init(&planet->children, sizeof(struct planet *), 3, &stdlib_heap_allocator); - rc = hashmap_add(&planets, aprintf("%s", name), strlen(name), - (struct hashmap_val) {.p = p}); - assert(!rc); + key = aprintf("%s", name); + assert(!hmap_add(&planets, (struct hmap_key) {.p = key}, + (struct hmap_val) {.p = planet})); - return p; + return planet; } void @@ -57,8 +59,8 @@ planets_init() char buf[256], *sep; int rc; - hashmap_init(&planets, 100, hashmap_str_hasher, - hashmap_default_keycmp, &stdlib_heap_allocator); + hmap_init(&planets, 100, hmap_str_hash, + hmap_str_keycmp, &stdlib_heap_allocator); pos = aoc.input; end = aoc.input + aoc.input_size; @@ -85,31 +87,31 @@ planets_init() void planets_deinit() { - struct hashmap_iter iter; + struct hmap_iter iter; struct planet *p; - for (HASHMAP_ITER(&planets, &iter)) { - free(iter.link->key); + for (HMAP_ITER(&planets, &iter)) { + free(iter.link->key._p); p = iter.link->value.p; dvec_deinit(&p->children); free(p->name); free(p); } - hashmap_deinit(&planets); + hmap_deinit(&planets); } void part1(void) { - struct hashmap_iter iter; + struct hmap_iter iter; struct planet *p; int orbits; planets_init(); orbits = 0; - for (HASHMAP_ITER(&planets, &iter)) { + for (HMAP_ITER(&planets, &iter)) { p = iter.link->value.p; while (p->parent) { orbits += 1; diff --git a/09/info.mk b/09/info.mk @@ -1,4 +1,4 @@ 09_SRC = 09/main.c common/main.c common/iccmp.c common/aoc.c common/util.c 09_SRC += lib/libdvec/build/libdvec.a lib/liballoc/build/liballoc.a -09_SRC += lib/libmaxint/build/libmaxint.a lib/libhashmap/build/libhashmap.a +09_SRC += lib/libmaxint/build/libmaxint.a lib/libhmap/build/libhmap.a 09_HDR = common/aoc.h common/iccmp.h common/util.h diff --git a/10/info.mk b/10/info.mk @@ -1,4 +1,4 @@ 10_SRC = 10/main.c common/main.c common/aoc.c common/util.c -10_SRC += lib/libhashmap/build/libhashmap.a lib/liballoc/build/liballoc.a +10_SRC += lib/libhmap/build/libhmap.a lib/liballoc/build/liballoc.a 10_HDR = common/aoc.h common/util.h 10_LDLIBS = -lm diff --git a/10/main.c b/10/main.c @@ -1,10 +1,11 @@ #include "allocator.h" #include "aoc.h" #include "util.h" -#include "hashmap.h" +#include "hmap.h" #include <math.h> #include <assert.h> +#include <stdbool.h> #include <stdio.h> #include <stdlib.h> @@ -21,15 +22,22 @@ struct dir { struct asteroid *asteroids; int width, height; +bool +hmap_dir_keycmp(struct hmap_key k1, struct hmap_key k2) +{ + const struct dir *d1 = k1.p, *d2 = k2.p; + + return d1->dx == d2->dx && d1->dy == d2->dy; +} + uint32_t -dir_hasher(const void *p, size_t len) +hmap_dir_hash(struct hmap_key key) { - const struct dir *dir; + const struct dir *dir = key.p; uint32_t hash; - dir = p; - hash = ((uint16_t) dir->dx & 0xFFFF); - hash |= ((uint16_t) dir->dy & 0xFFFF) << 16; + hash = ((uint32_t) dir->dx & 0xFFFF); + hash |= ((uint32_t) dir->dy & 0xFFFF) << 16; return hash; } @@ -113,9 +121,9 @@ reduce_frac(struct dir *d) } void -get_vis(struct hashmap *hm, struct asteroid *a1) +get_vis(struct hmap *hm, struct asteroid *a1) { - struct hashmap_link *link; + struct hmap_link *link; struct asteroid *a2, *tmp; struct dir dir, odir; void *key; @@ -128,7 +136,7 @@ get_vis(struct hashmap *hm, struct asteroid *a1) dir.dy = a2->y - a1->y; reduce_frac(&dir); - link = hashmap_get(hm, &dir, sizeof(dir)); + link = hmap_get(hm, (struct hmap_key) {.p = &dir}); if (link) { tmp = link->value.p; odir = (struct dir) { tmp->x - a1->x, tmp->y - a1->y }; @@ -137,8 +145,8 @@ get_vis(struct hashmap *hm, struct asteroid *a1) link->value.p = a2; } else { key = memdup(&dir, sizeof(struct dir)); - rc = hashmap_add(hm, key, sizeof(struct dir), - (struct hashmap_val) {.p = a2}); + rc = hmap_add(hm, (struct hmap_key) {.p = key}, + (struct hmap_val) {.p = a2}); assert(!rc); } } @@ -157,12 +165,12 @@ void find_base(struct asteroid **a_out, int *vis_out) { struct asteroid *a1, *cand; - struct hashmap_iter iter; - struct hashmap dirmap; + struct hmap_iter iter; + struct hmap dirmap; int vis, maxvis; - hashmap_init(&dirmap, 100, dir_hasher, - hashmap_default_keycmp, &stdlib_strict_heap_allocator); + hmap_init(&dirmap, 100, hmap_dir_hash, hmap_dir_keycmp, + &stdlib_strict_heap_allocator); maxvis = 0; cand = NULL; @@ -171,7 +179,7 @@ find_base(struct asteroid **a_out, int *vis_out) get_vis(&dirmap, a1); vis = 0; - for (HASHMAP_ITER(&dirmap, &iter)) + for (HMAP_ITER(&dirmap, &iter)) vis++; aoc_debug("%i vis\n", vis); @@ -180,13 +188,13 @@ find_base(struct asteroid **a_out, int *vis_out) cand = a1; } - for (HASHMAP_ITER(&dirmap, &iter)) - free(iter.link->key); - hashmap_clear(&dirmap); + for (HMAP_ITER(&dirmap, &iter)) + free(iter.link->key._p); + hmap_clear(&dirmap); } assert(cand != NULL); - hashmap_deinit(&dirmap); + hmap_deinit(&dirmap); if (vis_out) *vis_out = maxvis; if (a_out) *a_out = cand; @@ -212,26 +220,26 @@ void part2(void) { struct asteroid *base, *tmp; - struct hashmap_iter iter; + struct hmap_iter iter; struct asteroid **list; - struct hashmap dirmap; + struct hmap dirmap; int i, k, len; float prev; asteroids_init(); find_base(&base, NULL); - hashmap_init(&dirmap, 100, dir_hasher, - hashmap_default_keycmp, &stdlib_strict_heap_allocator); + hmap_init(&dirmap, 100, hmap_dir_hash, hmap_dir_keycmp, + &stdlib_strict_heap_allocator); get_vis(&dirmap, base); len = 0; - for (HASHMAP_ITER(&dirmap, &iter)) + for (HMAP_ITER(&dirmap, &iter)) len++; list = malloc(sizeof(struct asteroid *) * (size_t) len); assert(list != NULL); i = 0; - for (HASHMAP_ITER(&dirmap, &iter)) + for (HMAP_ITER(&dirmap, &iter)) list[i++] = iter.link->value.p; for (i = 0; i < len; i++) { @@ -255,9 +263,9 @@ part2(void) free(list); - for (HASHMAP_ITER(&dirmap, &iter)) - free(iter.link->key); + for (HMAP_ITER(&dirmap, &iter)) + free(iter.link->key._p); asteroids_deinit(); - hashmap_deinit(&dirmap); + hmap_deinit(&dirmap); } diff --git a/11/info.mk b/11/info.mk @@ -1,4 +1,4 @@ 11_SRC = 11/main.c common/main.c common/iccmp.c common/aoc.c common/util.c 11_SRC += lib/libdvec/build/libdvec.a lib/liballoc/build/liballoc.a -11_SRC += lib/libmaxint/build/libmaxint.a lib/libhashmap/build/libhashmap.a +11_SRC += lib/libmaxint/build/libmaxint.a lib/libhmap/build/libhmap.a 11_HDR = common/aoc.h common/iccmp.h common/util.h diff --git a/11/main.c b/11/main.c @@ -1,10 +1,11 @@ #include "allocator.h" #include "aoc.h" #include "iccmp.h" -#include "hashmap.h" +#include "hmap.h" #include "util.h" #include <assert.h> +#include <stdbool.h> #include <stdio.h> #include <stdlib.h> @@ -39,32 +40,39 @@ const char *solmap = "\ "; uint32_t -pos_hasher(const void *p, size_t len) +hmap_pos_hash(struct hmap_key key) { - const struct vec2 *vec; + const struct vec2 *vec = key.p; uint32_t hash; - vec = p; - hash = (uint16_t) vec->x; - hash |= ((uint16_t) vec->y) << 16; + hash = (uint32_t) vec->x; + hash |= ((uint32_t) vec->y) << 16; return hash; } +bool +hmap_pos_keycmp(struct hmap_key k1, struct hmap_key k2) +{ + const struct vec2 *v1 = k1.p, *v2 = k2.p; + + return v1->x == v2->x && v1->y == v2->y; +} + char * -gen_map(struct hashmap *panels, struct vec2 *pos, int rot) +gen_map(struct hmap *panels, struct vec2 *pos, int rot) { const char symbol[4] = "^>v<"; struct vec2 min, max, dims; - struct hashmap_iter iter; - struct vec2 *ppos; + struct hmap_iter iter; + const struct vec2 *ppos; int i, color; char *map; min.x = min.y = -2; max.x = max.y = 2; - for (HASHMAP_ITER(panels, &iter)) { - ppos = iter.link->key; + for (HMAP_ITER(panels, &iter)) { + ppos = iter.link->key.p; min.x = MIN(min.x, ppos->x); min.y = MIN(min.y, ppos->y); max.x = MAX(max.x, ppos->x); @@ -85,8 +93,8 @@ gen_map(struct hashmap *panels, struct vec2 *pos, int rot) assert(map != NULL); memset(map, '.', ((size_t) dims.x + 1) * (size_t) dims.y); - for (HASHMAP_ITER(panels, &iter)) { - ppos = iter.link->key; + for (HMAP_ITER(panels, &iter)) { + ppos = iter.link->key.p; color = iter.link->value.i; map[(ppos->y - min.y) * (dims.x + 1) + (ppos->x - min.x)] = color ? '#' : '.'; } @@ -103,13 +111,13 @@ gen_map(struct hashmap *panels, struct vec2 *pos, int rot) } void -panels_init(struct hashmap *panels, int start_color) +panels_init(struct hmap *panels, int start_color) { - struct hashmap_link **link; + struct hmap_link **link; int color, rot, brot; struct vec2 pos; struct icc icc; - void *key, *value; + void *key; char *map; int rc; enum { @@ -122,8 +130,8 @@ panels_init(struct hashmap *panels, int start_color) pos.x = pos.y = 0; key = memdup(&pos, sizeof(pos)); - hashmap_add(panels, key, sizeof(pos), - (struct hashmap_val) {.i = start_color}); + hmap_add(panels, (struct hmap_key) {.p = key }, + (struct hmap_val) {.i = start_color}); rot = 0; color = 0; @@ -136,14 +144,15 @@ panels_init(struct hashmap *panels, int start_color) color = (int) mi_cast_ul(&icc.out); assert(color == 0 || color == 1); aoc_debug("OUTPUT %i %i: %i\n", pos.x, pos.y, color); - link = hashmap_link_pos(panels, &pos, sizeof(pos)); + link = hmap_link_pos(panels, + (struct hmap_key) {.p = &pos}); if (*link) { (*link)->value.i = color; } else { key = memdup(&pos, sizeof(pos)); - rc = hashmap_link_alloc(panels, link, - key, sizeof(pos), - (struct hashmap_val) {.i = color}); + rc = hmap_link_alloc(panels, link, + (struct hmap_key) {.p = key}, + (struct hmap_val) {.i = color}); assert(!rc); } state = STATE_ROTATE; @@ -163,7 +172,8 @@ panels_init(struct hashmap *panels, int start_color) } break; case ICC_INPUT: - link = hashmap_link_get(panels, &pos, sizeof(pos)); + link = hmap_link_get(panels, + (struct hmap_key) {.p = &pos}); if (link) { aoc_debug("INPUT %i %i: %i\n", pos.x, pos.y, (*link)->value.i); @@ -183,41 +193,41 @@ panels_init(struct hashmap *panels, int start_color) void part1(void) { - struct hashmap panels; - struct hashmap_iter iter; + struct hmap panels; + struct hmap_iter iter; int count; - hashmap_init(&panels, 100, pos_hasher, - hashmap_default_keycmp, &stdlib_strict_heap_allocator); + hmap_init(&panels, 100, hmap_pos_hash, hmap_pos_keycmp, + &stdlib_strict_heap_allocator); panels_init(&panels, black); count = 0; - for (HASHMAP_ITER(&panels, &iter)) + for (HMAP_ITER(&panels, &iter)) count += 1; aoc.answer = aprintf("%i", count); aoc.solution = "2088"; - for (HASHMAP_ITER(&panels, &iter)) - free(iter.link->key); - hashmap_deinit(&panels); + for (HMAP_ITER(&panels, &iter)) + free(iter.link->key._p); + hmap_deinit(&panels); } void part2(void) { - struct hashmap panels; - struct hashmap_iter iter; + struct hmap panels; + struct hmap_iter iter; char *map; - hashmap_init(&panels, 100, pos_hasher, - hashmap_default_keycmp, &stdlib_strict_heap_allocator); + hmap_init(&panels, 100, hmap_pos_hash, hmap_pos_keycmp, + &stdlib_strict_heap_allocator); panels_init(&panels, white); map = gen_map(&panels, NULL, 0); aoc.answer = map; aoc.solution = solmap; - for (HASHMAP_ITER(&panels, &iter)) - free(iter.link->key); - hashmap_deinit(&panels); + for (HMAP_ITER(&panels, &iter)) + free(iter.link->key._p); + hmap_deinit(&panels); } diff --git a/13/info.mk b/13/info.mk @@ -1,4 +1,4 @@ 13_SRC = 13/main.c common/main.c common/iccmp.c common/aoc.c common/util.c 13_SRC += lib/libdvec/build/libdvec.a lib/liballoc/build/liballoc.a -13_SRC += lib/libmaxint/build/libmaxint.a lib/libhashmap/build/libhashmap.a +13_SRC += lib/libmaxint/build/libmaxint.a lib/libhmap/build/libhmap.a 13_HDR = common/aoc.h common/iccmp.h common/util.h diff --git a/13/main.c b/13/main.c @@ -1,11 +1,12 @@ #include "allocator.h" #include "aoc.h" #include "iccmp.h" -#include "hashmap.h" +#include "hmap.h" #include "maxint.h" #include "util.h" #include <assert.h> +#include <stdbool.h> #include <stddef.h> #include <stdint.h> #include <stdlib.h> @@ -26,30 +27,38 @@ enum { }; uint32_t -pos_hasher(const void *key, size_t len) +hmap_pos_hash(struct hmap_key key) { - const struct vec2 *pos; + const struct vec2 *pos = key.p; uint32_t hash; - pos = key; hash = ((uint32_t) pos->x & 0xffffffff); - hash = (hash << 4) | ((uint32_t) pos->y & 0xffffffff); + hash = (hash << 8) ^ ((uint32_t) pos->y & 0xffffffff); return hash; } +bool +hmap_pos_keycmp(struct hmap_key k1, struct hmap_key k2) +{ + const struct vec2 *v1 = k1.p, *v2 = k2.p; + + return v1->x == v2->x && v1->y == v2->y; +} + void -print_map(struct hashmap *map) +print_map(struct hmap *map) { - struct hashmap_iter iter; - struct vec2 size, pos, *ppos; - struct hashmap_link *link; + struct hmap_iter iter; + const struct vec2 *ppos; + struct vec2 size, pos; + struct hmap_link *link; char tile; int x, y; size.x = size.y = 1; - for (HASHMAP_ITER(map, &iter)) { - ppos = iter.link->key; + for (HMAP_ITER(map, &iter)) { + ppos = iter.link->key.p; size.x = MAX(size.x, ppos->x + 1); size.y = MAX(size.y, ppos->y + 1); } @@ -58,7 +67,7 @@ print_map(struct hashmap *map) for (x = 0; x < size.x; x++) { pos.x = x; pos.y = y; - link = hashmap_get(map, &pos, sizeof(pos)); + link = hmap_get(map, (struct hmap_key) {.p = &pos}); if (link) tile = tiles[link->value.i]; else @@ -74,16 +83,16 @@ print_map(struct hashmap *map) void part1(void) { - struct hashmap_link **link; - struct hashmap_iter iter; - struct hashmap map; + struct hmap_link **link; + struct hmap_iter iter; + struct hmap map; struct icc icc; struct vec2 pos; int outind, outvec[3]; int rc, count; void *key; - hashmap_init(&map, 100, pos_hasher, hashmap_default_keycmp, + hmap_init(&map, 100, hmap_pos_hash, hmap_pos_keycmp, &stdlib_strict_heap_allocator); icc_init(&icc); @@ -99,13 +108,15 @@ part1(void) if (!outind) { pos.x = outvec[0]; pos.y = outvec[1]; - link = hashmap_link_pos(&map, &pos, sizeof(pos)); + link = hmap_link_pos(&map, + (struct hmap_key) {.p = &pos}); if (*link) { (*link)->value.i = outvec[2]; } else { key = memdup(&pos, sizeof(pos)); - rc = hashmap_link_alloc(&map, link, key, sizeof(pos), - (struct hashmap_val) {.i = outvec[2]}); + rc = hmap_link_alloc(&map, link, + (struct hmap_key) {.p = key}, + (struct hmap_val) {.i = outvec[2]}); assert(!rc); } } @@ -114,7 +125,7 @@ part1(void) } count = 0; - for (HASHMAP_ITER(&map, &iter)) { + for (HMAP_ITER(&map, &iter)) { if (iter.link->value.i == BLOCK) count += 1; } @@ -122,18 +133,18 @@ part1(void) aoc.answer = aprintf("%i", count); aoc.solution = "329"; - for (HASHMAP_ITER(&map, &iter)) - free(iter.link->key); - hashmap_deinit(&map); + for (HMAP_ITER(&map, &iter)) + free(iter.link->key._p); + hmap_deinit(&map); icc_deinit(&icc); } void part2(void) { - struct hashmap_link **link; - struct hashmap_iter iter; - struct hashmap map; + struct hmap_link **link; + struct hmap_iter iter; + struct hmap map; struct maxint addr = MI_INIT; struct maxint val = MI_INIT; mi_ul addr_ul, val_ul; @@ -143,7 +154,7 @@ part2(void) int rc, score, dir; void *key; - hashmap_init(&map, 100, pos_hasher, hashmap_default_keycmp, + hmap_init(&map, 100, hmap_pos_hash, hmap_pos_keycmp, &stdlib_strict_heap_allocator); icc_init(&icc); @@ -183,13 +194,15 @@ part2(void) if (pos.x == -1 && pos.y == 0) { score = outvec[2]; } else { - link = hashmap_link_pos(&map, &pos, sizeof(pos)); + link = hmap_link_pos(&map, + (struct hmap_key) {.p = &pos}); if (*link) { (*link)->value.i = outvec[2]; } else { key = memdup(&pos, sizeof(pos)); - rc = hashmap_link_alloc(&map, link, key, sizeof(pos), - (struct hashmap_val) {.i = outvec[2]}); + rc = hmap_link_alloc(&map, link, + (struct hmap_key) {.p = key}, + (struct hmap_val) {.i = outvec[2]}); assert(!rc); } if (outvec[2] == BALL) { @@ -208,8 +221,8 @@ part2(void) aoc.answer = aprintf("%i", score); aoc.solution = "15973"; - for (HASHMAP_ITER(&map, &iter)) - free(iter.link->key); - hashmap_deinit(&map); + for (HMAP_ITER(&map, &iter)) + free(iter.link->key._p); + hmap_deinit(&map); icc_deinit(&icc); } diff --git a/Makefile b/Makefile @@ -1,8 +1,15 @@ -CFLAGS = -I lib/libdvec/include -I lib/libhashmap/include +CFLAGS = -I lib/libdvec/include -I lib/libhmap/include CFLAGS += -I lib/libmaxint/include -I lib/liballoc/include CFLAGS += -Wunused-variable -Wunused-function -Wformat -Wconversion CFLAGS += -I common -g -Werror +ifeq "$(DEBUG)" "1" +LIBDVEC_ENV = DEBUG=1 ASSERT_ALLOC=1 ASSERT_ARGS=1 +LIBHMAP_ENV = DEBUG=1 +LIBMAXINT_ENV = DEBUG=1 +LIBALLOC_ENV = DEBUG=1 +endif + DAYS = $(shell seq 1 24 | xargs printf "%02i\n") all:: @@ -10,16 +17,16 @@ all:: include */info.mk lib/libdvec/build/libdvec.a: - make -C lib/libdvec build/libdvec.a DEBUG=1 + make -C lib/libdvec build/libdvec.a $(LIBDVEC_ENV) -lib/libhashmap/build/libhashmap.a: - make -C lib/libhashmap build/libhashmap.a DEBUG=1 +lib/libhmap/build/libhmap.a: + make -C lib/libhmap build/libhmap.a $(LIBHMAP_ENV) lib/libmaxint/build/libmaxint.a: - make -C lib/libmaxint build/libmaxint.a DEBUG=1 + make -C lib/libmaxint build/libmaxint.a $(LIBMAXINT_ENV) lib/liballoc/build/liballoc.a: - make -C lib/liballoc build/liballoc.a DEBUG=1 + make -C lib/liballoc build/liballoc.a $(LIBALLOC_ENV) define make-day all:: $1/main diff --git a/common/iccmp.c b/common/iccmp.c @@ -1,7 +1,7 @@ #include "iccmp.h" #include "allocator.h" #include "aoc.h" -#include "hashmap.h" +#include "hmap.h" #include "maxint.h" #include "util.h" @@ -29,15 +29,15 @@ mi_tmp(int64_t val) } static bool -icc_hashmap_cmp(const void *key1, size_t size1, const void *key2, size_t size2) +icc_hmap_keycmp(struct hmap_key k1, struct hmap_key k2) { - return mi_cmp(key1, key2) == 0; + return mi_cmp(k1.p, k2.p) == 0; } static uint32_t -icc_addr_hasher(const void *key, size_t size) +icc_hmap_hash(struct hmap_key key) { - return (uint32_t) mi_cast_ul(key); + return (uint32_t) mi_cast_ul(key.p); } void @@ -70,15 +70,15 @@ icc_init(struct icc *icc) icc->line_terminated = true; - rc = hashmap_init(&icc->instructions, 1024, icc_addr_hasher, - icc_hashmap_cmp, &stdlib_strict_heap_allocator); + rc = hmap_init(&icc->instructions, 1024, icc_hmap_hash, + icc_hmap_keycmp, &stdlib_strict_heap_allocator); assert(!rc); } void icc_deinit(struct icc *icc) { - struct hashmap_iter iter; + struct hmap_iter iter; mi_deinit(&icc->rip); mi_deinit(&icc->base); @@ -92,14 +92,14 @@ icc_deinit(struct icc *icc) mi_deinit(&icc->r4); mi_deinit(&icc->tmp); - for (HASHMAP_ITER(&icc->instructions, &iter)) { - mi_deinit(iter.link->key); - free(iter.link->key); + for (HMAP_ITER(&icc->instructions, &iter)) { + mi_deinit(iter.link->key._p); + free(iter.link->key._p); mi_deinit(iter.link->value.p); free(iter.link->value.p); } - hashmap_deinit(&icc->instructions); + hmap_deinit(&icc->instructions); } void @@ -110,11 +110,11 @@ icc_copy(struct icc *dst, struct icc *src) mi_set(&dst->base, &src->base); mi_set(&dst->in, &src->in); mi_set(&dst->out, &src->out); - hashmap_copy(&dst->instructions, &src->instructions); + hmap_copy(&dst->instructions, &src->instructions); } void -icc_reset(struct icc *icc, struct hashmap *inst) +icc_reset(struct icc *icc, struct hmap *inst) { int rc; @@ -122,7 +122,7 @@ icc_reset(struct icc *icc, struct hashmap *inst) mi_setv(&icc->rip, 0, MI_POS); mi_setv(&icc->base, 0, MI_POS); if (inst) { - rc = hashmap_copy(&icc->instructions, inst); + rc = hmap_copy(&icc->instructions, inst); assert(!rc); } } @@ -154,8 +154,8 @@ icc_parse_inst(struct icc *icc, const char *str, size_t len) mi_init(val); mi_setv(val, (mi_ul) ABS(v), v >= 0 ? MI_POS : MI_NEG); - rc = hashmap_add(&icc->instructions, key, - sizeof(struct maxint), (struct hashmap_val) {.p = val}); + rc = hmap_add(&icc->instructions, (struct hmap_key) {.p = key}, + (struct hmap_val) {.p = val}); assert(!rc); rip += 1; @@ -556,27 +556,30 @@ icc_step_inst(struct icc *icc) return rc; } -struct hashmap_link * +struct hmap_link * icc_write_any(struct icc *icc, struct maxint *addr, struct maxint *value) { - struct hashmap_link **link; + struct hmap_link **link; struct maxint *key, *val; int rc; - key = malloc(sizeof(struct maxint)); - assert(key != NULL); - mi_init(key); - mi_set(key, addr); + link = hmap_link_pos(&icc->instructions, (struct hmap_key) {.p = addr}); + if (*link) { + mi_set((*link)->value.p, value); + } else { + key = malloc(sizeof(struct maxint)); + assert(key != NULL); + mi_init(key); + mi_set(key, addr); - val = malloc(sizeof(struct maxint)); - assert(val != NULL); - mi_init(val); - mi_set(val, value); + val = malloc(sizeof(struct maxint)); + assert(val != NULL); + mi_init(val); + mi_set(val, value); - link = hashmap_link_pos(&icc->instructions, key, sizeof(struct maxint)); - if (!*link) { - rc = hashmap_link_alloc(&icc->instructions, link, key, - sizeof(struct maxint), (struct hashmap_val) {.p = val}); + rc = hmap_link_alloc(&icc->instructions, link, + (struct hmap_key) {.p = key}, + (struct hmap_val) {.p = val}); assert(!rc); } @@ -586,9 +589,9 @@ icc_write_any(struct icc *icc, struct maxint *addr, struct maxint *value) int icc_write(struct icc *icc, struct maxint *addr, struct maxint *val) { - struct hashmap_link *link; + struct hmap_link *link; - link = hashmap_get(&icc->instructions, addr, sizeof(struct maxint)); + link = hmap_get(&icc->instructions, (struct hmap_key) {.p = addr}); if (!link) { if (addr->sign != MI_POS) { mi_set(&icc->read_addr, addr); @@ -607,9 +610,9 @@ icc_write(struct icc *icc, struct maxint *addr, struct maxint *val) int icc_read(struct icc *icc, struct maxint *addr, struct maxint **out) { - struct hashmap_link *link; + struct hmap_link *link; - link = hashmap_get(&icc->instructions, addr, sizeof(struct maxint)); + link = hmap_get(&icc->instructions, (struct hmap_key) {.p = addr}); if (!link) { if (addr->sign != MI_POS) { mi_set(&icc->read_addr, addr); diff --git a/common/iccmp.h b/common/iccmp.h @@ -1,7 +1,7 @@ #pragma once #include "dvec.h" -#include "hashmap.h" +#include "hmap.h" #include "maxint.h" enum { @@ -54,7 +54,7 @@ struct icc { bool line_terminated; struct maxint rip, base; - struct hashmap instructions; + struct hmap instructions; }; extern const char *icc_err[]; @@ -62,12 +62,12 @@ extern const char *icc_err[]; void icc_init(struct icc *icc); void icc_deinit(struct icc *icc); void icc_copy(struct icc *dst, struct icc *src); -void icc_reset(struct icc *icc, struct hashmap *inst); +void icc_reset(struct icc *icc, struct hmap *inst); void icc_parse_inst(struct icc *icc, const char *str, size_t len); int icc_step_inst(struct icc *icc); -struct hashmap_link *icc_write_any(struct icc *icc, +struct hmap_link *icc_write_any(struct icc *icc, struct maxint *addr, struct maxint *in); int icc_write(struct icc *icc, struct maxint *addr, struct maxint *in); int icc_read(struct icc *icc, struct maxint *addr, struct maxint **out); diff --git a/lib/libhashmap b/lib/libhashmap @@ -1 +0,0 @@ -Subproject commit bd2e5f7bdd17d757a17301587206eb1cb3af3ad5 diff --git a/lib/libhmap b/lib/libhmap @@ -0,0 +1 @@ +Subproject commit 529e0034ea003cc5cfed97f4f756068c5f0172d3