From 6359b142d1379eb066e6248b97f58279ad907195 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Tue, 28 Sep 2021 23:06:11 -0500 Subject: [PATCH] changed map.update to be a macro --- core/container/iter.onyx | 2 +- core/container/map.onyx | 14 ++++++++++---- tests/aoc-2020/day24.onyx | 2 +- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/core/container/iter.onyx b/core/container/iter.onyx index 2605e782..639aec9a 100644 --- a/core/container/iter.onyx +++ b/core/container/iter.onyx @@ -82,7 +82,7 @@ take_one :: (it: Iterator($T)) -> (T, bool) { // value: i32; // iterator: Iterator(i32) = ...; // -// if (#code value) << iterator { +// if #(value) << iterator { // ...iterater closed... // } #operator << macro (dest: Code, it: Iterator($T)) -> bool { diff --git a/core/container/map.onyx b/core/container/map.onyx index 28be2766..30941b57 100644 --- a/core/container/map.onyx +++ b/core/container/map.onyx @@ -67,6 +67,8 @@ get :: (use map: ^Map($K, $V), key: K) -> V { return default_value; } +#operator [] macro (map: Map($K, $V), key: K) -> V do return (package core.map).get(^map, key); + get_ptr :: (use map: ^Map($K, $V), key: K) -> ^V { lr := lookup(map, key); if lr.entry_index >= 0 do return ^entries[lr.entry_index].value; @@ -92,11 +94,15 @@ delete :: (use map: ^Map($K, $V), key: K) { else do hashes[last.hash_index] = lr.entry_index; } -update :: (use map: ^Map($K, $V), key: K, f: (^V) -> void) { - lr := lookup(map, key); - if lr.entry_index < 0 do return; +update :: macro (map: ^Map($K, $V), key: K, body: Code) { + @Hack // Weird hack because 'lookup' exists at file scope. + lookup_ :: lookup - f(^entries[lr.entry_index].value); + lr := lookup_(map, key); + if lr.entry_index >= 0 { + it := ^map.entries[lr.entry_index].value; + #insert body; + } } clear :: (use map: ^Map($K, $V)) { diff --git a/tests/aoc-2020/day24.onyx b/tests/aoc-2020/day24.onyx index 0e52d1dd..79a53514 100644 --- a/tests/aoc-2020/day24.onyx +++ b/tests/aoc-2020/day24.onyx @@ -108,7 +108,7 @@ main :: (args: [] cstr) { } for ^cell: cells_to_consider { - map.update(^grid, *cell, (state: ^Cell) { state.alive = state.next; }); + map.update(^grid, *cell, #code { it.alive = it.next; }); } array.clear(^cells_to_consider); -- 2.25.1