commit 394a3bd08aea889b6e5bf15e4a3141356dc39810
parent ea1f13579150e3f377cccbe32ca770036901f677
Author: Louis Burda <quent.burda@gmail.com>
Date: Thu, 13 Jul 2023 03:07:17 +0200
Update libs and src to new API
Diffstat:
16 files changed, 136 insertions(+), 141 deletions(-)
diff --git a/.gitmodules b/.gitmodules
@@ -1,15 +1,15 @@
[submodule "lib/libdvec"]
path = src/lib/libdvec
- url = git@sinitax.com:snx/libdvec
+ url = git@sinitax.com:sinitax/libdvec
[submodule "lib/libmaxint"]
path = src/lib/libmaxint
- url = git@sinitax.com:snx/libmaxint
+ url = git@sinitax.com:sinitax/libmaxint
[submodule "lib/liballoc"]
path = src/lib/liballoc
- url = git@sinitax.com:snx/liballoc
+ url = git@sinitax.com:sinitax/liballoc
[submodule "lib/libhmap"]
path = src/lib/libhmap
- url = git@sinitax.com:snx/libhmap
+ url = git@sinitax.com:sinitax/libhmap
[submodule "lib/liblist"]
path = src/lib/liblist
- url = git@sinitax.com:snx/liblist
+ url = git@sinitax.com:sinitax/liblist
diff --git a/src/06/main.c b/src/06/main.c
@@ -88,7 +88,7 @@ planets_deinit()
struct hmap_iter iter;
struct planet *p;
- for (HMAP_ITER(&planets, &iter)) {
+ for (HMAP_ITER(&planets, iter)) {
free(iter.link->key._p);
p = iter.link->value._p;
dvec_deinit(&p->children);
@@ -109,7 +109,7 @@ part1(void)
planets_init();
orbits = 0;
- for (HMAP_ITER(&planets, &iter)) {
+ for (HMAP_ITER(&planets, iter)) {
p = iter.link->value.p;
while (p->parent) {
orbits += 1;
diff --git a/src/07/main.c b/src/07/main.c
@@ -153,7 +153,7 @@ part1(void)
icc_init(&icc);
icc_parse_inst(&icc, aoc.input, aoc.input_size);
- dvec_init(&inst, 1, 0, &stdlib_heap_allocator);
+ dvec_init(&inst, sizeof(int), 0, &stdlib_heap_allocator);
dvec_copy(&inst, &icc.instructions);
max = 0;
@@ -180,7 +180,7 @@ part2(void)
icc_init(&iccs[0]);
icc_parse_inst(&iccs[0], aoc.input, aoc.input_size);
- dvec_init(&inst, 1, 0, &stdlib_heap_allocator);
+ dvec_init(&inst, sizeof(int), 0, &stdlib_heap_allocator);
dvec_copy(&inst, &iccs[0].instructions);
for (i = 1; i < 5; i++) {
diff --git a/src/10/main.c b/src/10/main.c
@@ -179,7 +179,7 @@ find_base(const struct asteroid **a_out, int *vis_out)
get_vis(&dirmap, a1);
vis = 0;
- for (HMAP_ITER(&dirmap, &iter))
+ for (HMAP_ITER(&dirmap, iter))
vis++;
aoc_debug("%i vis\n", vis);
@@ -188,7 +188,7 @@ find_base(const struct asteroid **a_out, int *vis_out)
cand = a1;
}
- for (HMAP_ITER(&dirmap, &iter))
+ for (HMAP_ITER(&dirmap, iter))
free(iter.link->key._p);
hmap_clear(&dirmap);
}
@@ -234,12 +234,12 @@ part2(void)
get_vis(&dirmap, base);
len = 0;
- for (HMAP_ITER(&dirmap, &iter))
+ for (HMAP_ITER(&dirmap, iter))
len++;
list = malloc(sizeof(struct asteroid *) * (size_t) len);
assert(list != NULL);
i = 0;
- for (HMAP_ITER(&dirmap, &iter))
+ for (HMAP_ITER(&dirmap, iter))
list[i++] = iter.link->value.p;
for (i = 0; i < len; i++) {
@@ -263,7 +263,7 @@ part2(void)
free(list);
- for (HMAP_ITER(&dirmap, &iter))
+ for (HMAP_ITER(&dirmap, iter))
free(iter.link->key._p);
asteroids_deinit();
diff --git a/src/11/main.c b/src/11/main.c
@@ -71,7 +71,7 @@ gen_map(struct hmap *panels, struct vec2 *pos, int rot)
min.x = min.y = -2;
max.x = max.y = 2;
- for (HMAP_ITER(panels, &iter)) {
+ for (HMAP_ITER(panels, iter)) {
ppos = iter.link->key.p;
min.x = MIN(min.x, ppos->x);
min.y = MIN(min.y, ppos->y);
@@ -93,7 +93,7 @@ gen_map(struct hmap *panels, struct vec2 *pos, int rot)
assert(map != NULL);
memset(map, '.', ((size_t) dims.x + 1) * (size_t) dims.y);
- for (HMAP_ITER(panels, &iter)) {
+ 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 ? '#' : '.';
@@ -119,7 +119,6 @@ panels_init(struct hmap *panels, int start_color)
struct icc icc;
void *key;
char *map;
- int rc;
enum {
STATE_COLOR,
STATE_ROTATE
@@ -150,10 +149,10 @@ panels_init(struct hmap *panels, int start_color)
(*link)->value.i = color;
} else {
key = memdup(&pos, sizeof(pos));
- rc = hmap_link_alloc(panels, link,
+ *link = hmap_link_alloc(panels,
(struct hmap_key) {.p = key},
- (struct hmap_val) {.i = color});
- assert(!rc);
+ (struct hmap_val) {.i = color},
+ NULL);
}
state = STATE_ROTATE;
} else if (state == STATE_ROTATE) {
@@ -202,12 +201,12 @@ part1(void)
panels_init(&panels, black);
count = 0;
- for (HMAP_ITER(&panels, &iter))
+ for (HMAP_ITER(&panels, iter))
count += 1;
aoc.answer = aprintf("%i", count);
aoc.solution = "2088";
- for (HMAP_ITER(&panels, &iter))
+ for (HMAP_ITER(&panels, iter))
free(iter.link->key._p);
hmap_deinit(&panels);
}
@@ -227,7 +226,7 @@ part2(void)
aoc.answer = map;
aoc.solution = solmap;
- for (HMAP_ITER(&panels, &iter))
+ for (HMAP_ITER(&panels, iter))
free(iter.link->key._p);
hmap_deinit(&panels);
}
diff --git a/src/13/main.c b/src/13/main.c
@@ -57,7 +57,7 @@ print_map(struct hmap *map)
int x, y;
size.x = size.y = 1;
- for (HMAP_ITER(map, &iter)) {
+ 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);
@@ -114,10 +114,11 @@ part1(void)
(*link)->value.i = outvec[2];
} else {
key = memdup(&pos, sizeof(pos));
- rc = hmap_link_alloc(&map, link,
+ *link = hmap_link_alloc(&map,
(struct hmap_key) {.p = key},
- (struct hmap_val) {.i = outvec[2]});
- assert(!rc);
+ (struct hmap_val) {.i = outvec[2]},
+ &rc);
+ assert(*link);
}
}
break;
@@ -125,7 +126,7 @@ part1(void)
}
count = 0;
- for (HMAP_ITER(&map, &iter)) {
+ for (HMAP_ITER(&map, iter)) {
if (iter.link->value.i == BLOCK)
count += 1;
}
@@ -133,7 +134,7 @@ part1(void)
aoc.answer = aprintf("%i", count);
aoc.solution = "329";
- for (HMAP_ITER(&map, &iter))
+ for (HMAP_ITER(&map, iter))
free(iter.link->key._p);
hmap_deinit(&map);
icc_deinit(&icc);
@@ -200,10 +201,11 @@ part2(void)
(*link)->value.i = outvec[2];
} else {
key = memdup(&pos, sizeof(pos));
- rc = hmap_link_alloc(&map, link,
+ *link = hmap_link_alloc(&map,
(struct hmap_key) {.p = key},
- (struct hmap_val) {.i = outvec[2]});
- assert(!rc);
+ (struct hmap_val) {.i = outvec[2]},
+ &rc);
+ assert(*link);
}
if (outvec[2] == BALL) {
ball.x = pos.x;
@@ -221,7 +223,7 @@ part2(void)
aoc.answer = aprintf("%i", score);
aoc.solution = "15973";
- for (HMAP_ITER(&map, &iter))
+ for (HMAP_ITER(&map, iter))
free(iter.link->key._p);
hmap_deinit(&map);
icc_deinit(&icc);
diff --git a/src/14/main.c b/src/14/main.c
@@ -178,7 +178,7 @@ part1(void)
aoc.answer = aprintf("%lu", ore);
aoc.solution = "1582325";
- for (HMAP_ITER(&reactions, &iter)) {
+ for (HMAP_ITER(&reactions, iter)) {
reaction = iter.link->value._p;
dvec_deinit(&reaction->ingredients);
free(iter.link->value._p);
@@ -224,7 +224,7 @@ part2(void)
aoc.answer = aprintf("%lu", fuel);
aoc.solution = "2267486";
- for (HMAP_ITER(&reactions, &iter)) {
+ for (HMAP_ITER(&reactions, iter)) {
reaction = iter.link->value._p;
dvec_deinit(&reaction->ingredients);
free(iter.link->value._p);
diff --git a/src/15/main.c b/src/15/main.c
@@ -94,7 +94,7 @@ print_map(struct hmap *map, struct vec2i droid)
struct vec2i pos;
const struct vec2i *vec;
- for (HMAP_ITER(map, &iter)) {
+ for (HMAP_ITER(map, iter)) {
vec = iter.link->key.p;
min.x = MIN(min.x, vec->x);
min.y = MIN(min.y, vec->y);
@@ -184,14 +184,14 @@ dijkstra_dist(struct hmap *map, struct vec2i start, const struct vec2i *end)
vec = dvec_add_slot(&outer);
vec2i_set(vec, &npos);
key = memdup(&npos, sizeof(struct vec2i));
- hmap_link_alloc(&seen, linkp,
+ *linkp = hmap_link_alloc(&seen,
(struct hmap_key) {.p = key},
- (struct hmap_val) {.s = dist});
+ (struct hmap_val) {.s = dist}, NULL);
}
}
exit:
- for (HMAP_ITER(&seen, &iter))
+ for (HMAP_ITER(&seen, iter))
free(iter.link->key._p);
dvec_deinit(&outer);
hmap_deinit(&seen);
@@ -261,9 +261,10 @@ explore_map(struct icc *icc, struct hmap *map,
(*linkp)->value.c = block_lut[move_result];
} else {
key = memdup(&npos, sizeof(struct vec2i));
- hmap_link_alloc(map, linkp,
+ *linkp = hmap_link_alloc(map,
(struct hmap_key) {.p = key},
- (struct hmap_val) {.c = block_lut[move_result]});
+ (struct hmap_val) {.c = block_lut[move_result]},
+ NULL);
}
switch (move_result) {
@@ -315,7 +316,7 @@ part1(void)
aoc.answer = aprintf("%lu", dist);
aoc.solution = "234";
- for (HMAP_ITER(&map, &iter))
+ for (HMAP_ITER(&map, iter))
free(iter.link->key._p);
hmap_deinit(&map);
icc_deinit(&icc);
@@ -344,7 +345,7 @@ part2(void)
aoc.answer = aprintf("%lu", dist);
aoc.solution = "292";
- for (HMAP_ITER(&map, &iter))
+ for (HMAP_ITER(&map, iter))
free(iter.link->key._p);
hmap_deinit(&map);
icc_deinit(&icc);
diff --git a/src/17/main.c b/src/17/main.c
@@ -87,7 +87,7 @@ receive_map(struct icc *icc, struct hmap *map,
void *key;
char c;
- for (HMAP_ITER(map, &iter))
+ for (HMAP_ITER(map, iter))
free(iter.link->key._p);
hmap_clear(map);
@@ -195,8 +195,8 @@ add_path(struct hmap *paths, struct path *path)
memcpy(dvec_add_slot(list), path, sizeof(struct path));
} else {
key = memdup(&path->start, sizeof(struct vec2i));
- dvec_alloc(&list, sizeof(struct path),
- 1, &stdlib_strict_heap_allocator);
+ list = dvec_alloc(sizeof(struct path),
+ 1, &stdlib_strict_heap_allocator, NULL);
memcpy(dvec_add_slot(list), path, sizeof(struct path));
hmap_add(paths,
(struct hmap_key) {.p = key},
@@ -495,7 +495,7 @@ part1(void)
aoc.answer = aprintf("%lu", answer);
aoc.solution = "4112";
- for (HMAP_ITER(&map, &iter))
+ for (HMAP_ITER(&map, iter))
free(iter.link->key._p);
hmap_deinit(&map);
icc_deinit(&icc);
@@ -593,13 +593,13 @@ exit:
dvec_deinit(&main);
dvec_deinit(&insts);
- for (HMAP_ITER(&paths, &iter)) {
+ for (HMAP_ITER(&paths, iter)) {
free(iter.link->key._p);
dvec_free(iter.link->value._p);
}
hmap_deinit(&paths);
- for (HMAP_ITER(&map, &iter))
+ for (HMAP_ITER(&map, iter))
free(iter.link->key._p);
hmap_deinit(&map);
diff --git a/src/18/main.c b/src/18/main.c
@@ -137,7 +137,7 @@ print_map(const struct hmap *map, struct hmap *distmap,
vec2i_set(&min, start);
vec2i_set(&max, start);
- for (HMAP_ITER(map, &iter)) {
+ for (HMAP_ITER(map, iter)) {
vec2i_set(&pos, iter.link->key._p);
if (pos.x < min.x) min.x = pos.x;
if (pos.y < min.y) min.y = pos.y;
@@ -291,8 +291,8 @@ add_node(struct hmap *graph, const struct hmap *map,
cstate->dist = 0;
pos = memdup(start, sizeof(struct vec2i));
- dvec_alloc(&paths, sizeof(struct path), 1,
- &stdlib_strict_heap_allocator);
+ paths = dvec_alloc(sizeof(struct path), 1,
+ &stdlib_strict_heap_allocator, NULL);
path = dvec_add_slot(paths);
path->dist = 0;
path->required = 0;
@@ -324,11 +324,13 @@ add_node(struct hmap *graph, const struct hmap *map,
paths = (*linkp)->value._p;
} else {
pos = memdup(&state.pos, sizeof(struct vec2i));
- dvec_alloc(&paths, sizeof(struct path),
- 1, &stdlib_strict_heap_allocator);
- hmap_link_alloc(&distmap, linkp,
+ paths = dvec_alloc(sizeof(struct path),
+ 1, &stdlib_strict_heap_allocator,
+ NULL);
+ *linkp = hmap_link_alloc(&distmap,
(struct hmap_key) {.p = pos},
- (struct hmap_val) {.p = paths});
+ (struct hmap_val) {.p = paths},
+ NULL);
}
if (update_paths(paths, state.dist, state.acquired)) {
@@ -343,7 +345,7 @@ add_node(struct hmap *graph, const struct hmap *map,
node->key = key;
dvec_init(&node->edges, sizeof(struct keypath), 1,
&stdlib_strict_heap_allocator);
- for (HMAP_ITER(map, &iter)) {
+ for (HMAP_ITER(map, iter)) {
c = iter.link->value.c;
if (vec2i_eql(iter.link->key.p, start))
continue;
@@ -375,7 +377,7 @@ add_node(struct hmap *graph, const struct hmap *map,
aoc_debug("\n");
}
- for (HMAP_ITER(&distmap, &iter)) {
+ for (HMAP_ITER(&distmap, iter)) {
free(iter.link->key._p);
dvec_free(iter.link->value._p);
}
@@ -391,7 +393,7 @@ gen_graph(struct hmap *graph, const struct hmap *map,
struct hmap_iter iter;
add_node(graph, map, start, -1);
- for (HMAP_ITER(map, &iter)) {
+ for (HMAP_ITER(map, iter)) {
if (islower(iter.link->value.c)) {
add_node(graph, map, iter.link->key.p,
iter.link->value.c - 'a');
@@ -463,14 +465,11 @@ state_alloc(const struct vec2i *pos, size_t dist, uint32_t acquired)
}
bool
-state_dist_ordering(const struct list_link *_a, const struct list_link *_b)
+state_dist_ordering(const void *p1, const void *p2, void *u)
{
- struct state *a, *b;
-
- a = LIST_UPCAST(_a, struct state, link);
- b = LIST_UPCAST(_b, struct state, link);
+ const struct state *s1 = p1, *s2 = p2;
- return a->dist < b->dist;
+ return s1->dist < s2->dist;
}
void
@@ -505,19 +504,20 @@ add_keys(struct list *active, struct hmap *graph,
progs = (*linkp)->value._p;
} else {
pos = memdup(&state.pos, sizeof(struct vec2i));
- dvec_alloc(&progs, sizeof(struct prog), 1,
- &stdlib_strict_heap_allocator);
- hmap_link_alloc(distmap, linkp,
+ progs = dvec_alloc(sizeof(struct prog), 1,
+ &stdlib_strict_heap_allocator, NULL);
+ *linkp = hmap_link_alloc(distmap,
(struct hmap_key) {.p = pos},
- (struct hmap_val) {.p = progs});
+ (struct hmap_val) {.p = progs}, NULL);
}
if ((prog = update_progs(progs, state.dist, state.acquired))) {
aoc_debug("updated prog %i %i %032b %032b\n",
prog->dist, state.dist, prog->acquired, state.acquired);
nstate = state_alloc(&state.pos, state.dist, state.acquired);
- list_insert_sorted(active, &nstate->link, false,
- state_dist_ordering);
+ list_insert_sorted(active, &nstate->link,
+ false, state_dist_ordering,
+ LIST_OFFSET(struct state, link), NULL);
}
}
}
@@ -554,15 +554,15 @@ min_dist_all_keys(struct hmap *graph, const struct vec2i *start, size_t keycnt)
list_insert_back(&active, &cstate->link);
pos = memdup(start, sizeof(struct vec2i));
- dvec_alloc(&progs, sizeof(struct prog), 1,
- &stdlib_strict_heap_allocator);
+ progs = dvec_alloc(sizeof(struct prog), 1,
+ &stdlib_strict_heap_allocator, NULL);
prog = dvec_add_slot(progs);
prog->acquired = 0;
prog->dist = 0;
hmap_add(&distmap, (struct hmap_key) {.p = pos},
(struct hmap_val) {.p = progs});
- for (HMAP_ITER(graph, &iter)) {
+ for (HMAP_ITER(graph, iter)) {
node = iter.link->value._p;
sort_edges(&node->edges);
}
@@ -606,7 +606,7 @@ min_dist_all_keys(struct hmap *graph, const struct vec2i *start, size_t keycnt)
}
exit:
- for (HMAP_ITER(&distmap, &iter)) {
+ for (HMAP_ITER(&distmap, iter)) {
free(iter.link->key._p);
dvec_free(iter.link->value._p);
}
@@ -643,14 +643,11 @@ state_multi_alloc(const struct vec2i *pos, size_t dist, uint32_t acquired)
}
bool
-state_multi_dist_ordering(const struct list_link *_a, const struct list_link *_b)
+state_multi_dist_ordering(const void *p1, const void *p2, void *u)
{
- struct state_multi *a, *b;
+ const struct state_multi *s1 = p1, *s2 = p2;
- a = LIST_UPCAST(_a, struct state_multi, link);
- b = LIST_UPCAST(_b, struct state_multi, link);
-
- return a->dist < b->dist;
+ return s1->dist < s2->dist;
}
void
@@ -689,11 +686,11 @@ add_keys_multi(struct list *active, struct hmap *graphs,
progs = (*linkp)->value._p;
} else {
pos = memdup(state.pos, sizeof(struct vec2i) * 4);
- dvec_alloc(&progs, sizeof(struct prog), 1,
- &stdlib_strict_heap_allocator);
- hmap_link_alloc(distmap, linkp,
+ progs = dvec_alloc(sizeof(struct prog), 1,
+ &stdlib_strict_heap_allocator, NULL);
+ *linkp = hmap_link_alloc(distmap,
(struct hmap_key) {.p = pos},
- (struct hmap_val) {.p = progs});
+ (struct hmap_val) {.p = progs}, NULL);
}
if ((prog = update_progs(progs, state.dist, state.acquired))) {
@@ -707,7 +704,9 @@ add_keys_multi(struct list *active, struct hmap *graphs,
prog->acquired, state.acquired);
nstate = state_multi_alloc(state.pos, state.dist, state.acquired);
list_insert_sorted(active, &nstate->link,
- false, state_multi_dist_ordering);
+ false, state_multi_dist_ordering,
+ LIST_OFFSET(struct state_multi, link),
+ NULL);
}
}
}
@@ -746,8 +745,8 @@ min_dist_all_keys_multi(struct hmap *graphs, const struct vec2i *starts, size_t
list_insert_back(&active, &cstate->link);
pos = memdup(starts, sizeof(struct vec2i) * 4);
- dvec_alloc(&progs, sizeof(struct prog), 1,
- &stdlib_strict_heap_allocator);
+ progs = dvec_alloc(sizeof(struct prog), 1,
+ &stdlib_strict_heap_allocator, NULL);
prog = dvec_add_slot(progs);
prog->acquired = 0;
prog->dist = 0;
@@ -755,7 +754,7 @@ min_dist_all_keys_multi(struct hmap *graphs, const struct vec2i *starts, size_t
(struct hmap_val) {.p = progs});
for (i = 0; i < 4; i++) {
- for (HMAP_ITER(&graphs[i], &hmap_iter)) {
+ for (HMAP_ITER(&graphs[i], hmap_iter)) {
node = hmap_iter.link->value._p;
sort_edges(&node->edges);
}
@@ -803,7 +802,7 @@ min_dist_all_keys_multi(struct hmap *graphs, const struct vec2i *starts, size_t
}
exit:
- for (HMAP_ITER(&distmap, &hmap_iter)) {
+ for (HMAP_ITER(&distmap, hmap_iter)) {
free(hmap_iter.link->key._p);
dvec_free(hmap_iter.link->value._p);
}
@@ -839,7 +838,7 @@ part1(void)
aoc.answer = aprintf("%lu", answer);
aoc.solution = "5402";
- for (HMAP_ITER(&graph, &iter)) {
+ for (HMAP_ITER(&graph, iter)) {
free(iter.link->key._p);
node = iter.link->value._p;
dvec_deinit(&node->edges);
@@ -847,7 +846,7 @@ part1(void)
}
hmap_deinit(&graph);
- for (HMAP_ITER(&map, &iter))
+ for (HMAP_ITER(&map, iter))
free(iter.link->key._p);
hmap_deinit(&map);
}
@@ -891,7 +890,7 @@ part2(void)
aoc.solution = "2138";
for (i = 0; i < 4; i++) {
- for (HMAP_ITER(&graphs[i], &hmap_iter)) {
+ for (HMAP_ITER(&graphs[i], hmap_iter)) {
free(hmap_iter.link->key._p);
node = hmap_iter.link->value._p;
dvec_deinit(&node->edges);
@@ -900,7 +899,7 @@ part2(void)
hmap_deinit(&graphs[i]);
}
- for (HMAP_ITER(&map, &hmap_iter))
+ for (HMAP_ITER(&map, hmap_iter))
free(hmap_iter.link->key._p);
hmap_deinit(&map);
}
diff --git a/src/20/part1.c b/src/20/part1.c
@@ -22,12 +22,9 @@ struct djk_state {
};
static bool
-djk_dist_ordering(const struct list_link *l1, const struct list_link *l2)
+djk_dist_ordering(const void *p1, const void *p2, void *u)
{
- const struct djk_state *s1, *s2;
-
- s1 = LIST_UPCAST(l1, struct djk_state, link);
- s2 = LIST_UPCAST(l2, struct djk_state, link);
+ const struct djk_state *s1 = p1, *s2 = p2;
return s1->dist < s2->dist;
}
@@ -66,7 +63,7 @@ load_portals(struct hmap *portals, struct hmap *map)
struct vec2i *key;
size_t i, id;
- for (HMAP_ITER(map, &iter)) {
+ for (HMAP_ITER(map, iter)) {
if (!isupper(iter.link->value.c))
continue;
@@ -110,7 +107,7 @@ get_portal(struct hmap *portals, struct vec2i *pos, struct vec2i *skip, size_t i
{
struct hmap_iter iter;
- for (HMAP_ITER(portals, &iter)) {
+ for (HMAP_ITER(portals, iter)) {
if (skip && vec2i_eql(skip, iter.link->key.p))
continue;
if (iter.link->value.s == id) {
@@ -134,9 +131,9 @@ add_djk_state(struct list *active, struct hmap *distmap,
hmap_linkp = hmap_link_pos(distmap, (struct hmap_key) {.p = pos});
if (!*hmap_linkp) {
key = memdup(pos, sizeof(struct vec2i));
- hmap_link_alloc(distmap, hmap_linkp,
+ *hmap_linkp = hmap_link_alloc(distmap,
(struct hmap_key) {.p = key},
- (struct hmap_val) {.s = dist});
+ (struct hmap_val) {.s = dist}, NULL);
new = true;
} else {
if (dist < (*hmap_linkp)->value.s) {
@@ -153,7 +150,8 @@ add_djk_state(struct list *active, struct hmap *distmap,
vec2i_set(&nstate->pos, pos);
nstate->link = LIST_LINK_INIT;
list_insert_sorted(active, &nstate->link,
- false, djk_dist_ordering);
+ false, djk_dist_ordering,
+ LIST_OFFSET(struct djk_state, link), NULL);
}
}
@@ -220,7 +218,7 @@ min_dist(struct hmap *map, struct hmap *portals,
}
exit:
- for (HMAP_ITER(&distmap, &hmap_iter))
+ for (HMAP_ITER(&distmap, hmap_iter))
free(hmap_iter.link->key._p);
hmap_deinit(&distmap);
list_free_items(&active, free, LIST_OFFSET(struct djk_state, link));
@@ -252,10 +250,10 @@ part1(void)
aoc.answer = aprintf("%lu", answer);
aoc.solution = "656";
- for (HMAP_ITER(&portals, &iter))
+ for (HMAP_ITER(&portals, iter))
free(iter.link->key._p);
hmap_deinit(&portals);
- for (HMAP_ITER(&map, &iter))
+ for (HMAP_ITER(&map, iter))
free(iter.link->key._p);
hmap_deinit(&map);
}
diff --git a/src/20/part2.c b/src/20/part2.c
@@ -33,12 +33,9 @@ vec3i_add_2d(struct vec3i *dst, const struct vec3i *src, const struct vec2i *off
}
static bool
-djk_dist_ordering(const struct list_link *l1, const struct list_link *l2)
+djk_dist_ordering(const void *p1, const void *p2, void *u)
{
- const struct djk_state *s1, *s2;
-
- s1 = LIST_UPCAST(l1, struct djk_state, link);
- s2 = LIST_UPCAST(l2, struct djk_state, link);
+ const struct djk_state *s1 = p1, *s2 = p2;
return s1->dist < s2->dist;
}
@@ -86,7 +83,7 @@ load_portals(struct hmap *portals, struct hmap *map,
size_t i, id;
bool inner;
- for (HMAP_ITER(map, &iter)) {
+ for (HMAP_ITER(map, iter)) {
if (!isupper(iter.link->value.c))
continue;
@@ -135,7 +132,7 @@ get_portal(struct hmap *portals, struct vec3i *pos, size_t id)
struct hmap_iter iter;
const struct vec3i *tmp;
- for (HMAP_ITER(portals, &iter)) {
+ for (HMAP_ITER(portals, iter)) {
tmp = iter.link->key.p;
if (iter.link->value.s == id) {
pos->x = tmp->x;
@@ -160,9 +157,9 @@ add_djk_state(struct list *active, struct hmap *distmap,
hmap_linkp = hmap_link_pos(distmap, (struct hmap_key) {.p = pos});
if (!*hmap_linkp) {
key = memdup(pos, sizeof(struct vec3i));
- hmap_link_alloc(distmap, hmap_linkp,
+ *hmap_linkp = hmap_link_alloc(distmap,
(struct hmap_key) {.p = key},
- (struct hmap_val) {.s = dist});
+ (struct hmap_val) {.s = dist}, NULL);
new = true;
} else {
if (dist < (*hmap_linkp)->value.s) {
@@ -180,7 +177,8 @@ add_djk_state(struct list *active, struct hmap *distmap,
vec3i_set(&nstate->pos, pos);
nstate->link = LIST_LINK_INIT;
list_insert_sorted(active, &nstate->link,
- false, djk_dist_ordering);
+ false, djk_dist_ordering,
+ LIST_OFFSET(struct djk_state, link), NULL);
}
}
@@ -271,7 +269,7 @@ min_dist(struct hmap *map, struct hmap *portals,
}
exit:
- for (HMAP_ITER(&distmap, &hmap_iter))
+ for (HMAP_ITER(&distmap, hmap_iter))
free(hmap_iter.link->key._p);
hmap_deinit(&distmap);
list_free_items(&active, free, LIST_OFFSET(struct djk_state, link));
@@ -304,10 +302,10 @@ part2(void)
aoc.answer = aprintf("%lu", answer);
aoc.solution = "7114";
- for (HMAP_ITER(&portals, &iter))
+ for (HMAP_ITER(&portals, iter))
free(iter.link->key._p);
hmap_deinit(&portals);
- for (HMAP_ITER(&map, &iter))
+ for (HMAP_ITER(&map, iter))
free(iter.link->key._p);
hmap_deinit(&map);
}
diff --git a/src/24/part2.c b/src/24/part2.c
@@ -87,7 +87,7 @@ map_count(struct hmap *map)
size_t x, y, count;
count = 0;
- for (HMAP_ITER(map, &iter)) {
+ for (HMAP_ITER(map, iter)) {
layer = iter.link->value.p;
for (y = 0; y < 5; y++) {
for (x = 0; x < 5; x++) {
@@ -207,7 +207,7 @@ step(struct hmap *map)
char *cell;
minz = maxz = 0;
- for (HMAP_ITER(map, &iter)) {
+ for (HMAP_ITER(map, iter)) {
layer = iter.link->value._p;
if (memchr(layer->cells, '#', 5 * 5)) {
minz = MIN(minz, iter.link->key.ss);
@@ -223,9 +223,9 @@ step(struct hmap *map)
if (!*linkp) {
layer = malloc(sizeof(struct layer));
memset(layer->cells, '.', 5 * 5);
- hmap_link_alloc(map, linkp,
+ *linkp = hmap_link_alloc(map,
(struct hmap_key) {.ss = z},
- (struct hmap_val) {.p = layer});
+ (struct hmap_val) {.p = layer}, NULL);
} else {
layer = (*linkp)->value._p;
}
@@ -283,7 +283,7 @@ part2(void)
aoc.answer = aprintf("%lu", answer);
aoc.solution = "1985";
- for (HMAP_ITER(&map, &iter))
+ for (HMAP_ITER(&map, iter))
free(iter.link->value._p);
hmap_deinit(&map);
}
diff --git a/src/25/main.c b/src/25/main.c
@@ -208,9 +208,9 @@ explore(struct icc *icc, struct dvec *in, struct dvec *out, struct hmap *states,
parse_output(out, &room->name, &room->doors, &room->items);
assert(room->name && room->doors);
- hmap_link_alloc(map, linkp,
+ *linkp = hmap_link_alloc(map,
(struct hmap_key) {.p = key},
- (struct hmap_val) {.p = room});
+ (struct hmap_val) {.p = room}, NULL);
if (!strcmp(room->name, "Pressure-Sensitive Floor")) {
assert(path->dircnt);
@@ -269,9 +269,9 @@ explore(struct icc *icc, struct dvec *in, struct dvec *out, struct hmap *states,
state = malloc(sizeof(struct state));
state->next = 0;
state->back = (dir + 2) % 4;
- hmap_link_alloc(states, linkp,
+ *linkp = hmap_link_alloc(states,
(struct hmap_key) {.p = key},
- (struct hmap_val) {.p = state});
+ (struct hmap_val) {.p = state}, NULL);
}
}
@@ -308,7 +308,7 @@ get_path(struct hmap *map, struct path *path, const char *name)
struct hmap_iter iter;
struct room *room;
- for (HMAP_ITER(map, &iter)) {
+ for (HMAP_ITER(map, iter)) {
room = iter.link->value._p;
if (!strcmp(room->name, name)) {
memcpy(path, &room->path, sizeof(struct path));
@@ -504,7 +504,7 @@ part1(void)
aoc.answer = aprintf("%lu", answer);
aoc.solution = "278664";
- for (HMAP_ITER(&map, &iter)) {
+ for (HMAP_ITER(&map, iter)) {
room = iter.link->value._p;
aoc_debug("-- ROOM --\n");
aoc_debug("path: %s\n", path_str(&room->path));
@@ -516,7 +516,7 @@ part1(void)
}
exit:
- for (HMAP_ITER(&map, &iter)) {
+ for (HMAP_ITER(&map, iter)) {
free(iter.link->key._p);
room = iter.link->value._p;
free(room->name);
@@ -527,7 +527,7 @@ exit:
}
hmap_deinit(&map);
- for (HMAP_ITER(&states, &iter)) {
+ for (HMAP_ITER(&states, iter)) {
free(iter.link->key._p);
free(iter.link->value._p);
}
diff --git a/src/common/dvec_s.h b/src/common/dvec_s.h
@@ -21,7 +21,7 @@ dvec_add_slot(struct dvec *dvec)
static inline void
dvec_rm_slots(struct dvec *dvec, void *slot, size_t count)
{
- dvec_remove(dvec, ((size_t) (slot - dvec->data)) / dvec->dsize, count);
+ dvec_rm(dvec, ((size_t) (slot - dvec->data)) / dvec->dsize, count);
}
static inline void
diff --git a/src/common/iccmp.c b/src/common/iccmp.c
@@ -92,7 +92,7 @@ icc_deinit(struct icc *icc)
mi_deinit(&icc->r4);
mi_deinit(&icc->tmp);
- for (HMAP_ITER(&icc->instructions, &iter)) {
+ for (HMAP_ITER(&icc->instructions, iter)) {
mi_deinit(iter.link->key._p);
free(iter.link->key._p);
mi_deinit(iter.link->value._p);
@@ -114,7 +114,7 @@ icc_copy(struct icc *dst, struct icc *src)
mi_set(&dst->in, &src->in);
mi_set(&dst->out, &src->out);
- for (HMAP_ITER(&dst->instructions, &hmap_iter)) {
+ for (HMAP_ITER(&dst->instructions, hmap_iter)) {
mi_deinit(hmap_iter.link->key._p);
free(hmap_iter.link->key._p);
mi_deinit(hmap_iter.link->value._p);
@@ -122,7 +122,7 @@ icc_copy(struct icc *dst, struct icc *src)
}
hmap_clear(&dst->instructions);
- for (HMAP_ITER(&src->instructions, &hmap_iter)) {
+ for (HMAP_ITER(&src->instructions, hmap_iter)) {
key = malloc(sizeof(struct maxint));
mi_init(key);
mi_set(key, hmap_iter.link->key.p);
@@ -581,7 +581,6 @@ icc_write_any(struct icc *icc, struct maxint *addr, struct maxint *value)
{
struct hmap_link **link;
struct maxint *key, *val;
- int rc;
link = hmap_link_pos(&icc->instructions, (struct hmap_key) {.p = addr});
if (*link) {
@@ -597,10 +596,9 @@ icc_write_any(struct icc *icc, struct maxint *addr, struct maxint *value)
mi_init(val);
mi_set(val, value);
- rc = hmap_link_alloc(&icc->instructions, link,
+ *link = hmap_link_alloc(&icc->instructions,
(struct hmap_key) {.p = key},
- (struct hmap_val) {.p = val});
- assert(!rc);
+ (struct hmap_val) {.p = val}, NULL);
}
return *link;