From bcc6ca4cf9b148450e671772945d655ad90863f4 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Thu, 22 Jun 2023 18:29:03 -0500 Subject: [PATCH] changed: `map.get` returns an Optional --- core/container/map.onyx | 4 ++-- core/conv/format.onyx | 8 +++++--- core/conv/parse.onyx | 6 +++--- core/threads/thread.onyx | 2 +- tests/aoc-2020/day15.onyx | 4 ++-- tests/aoc-2020/day17.onyx | 9 +++++---- tests/aoc-2020/day20.onyx | 4 ++-- tests/aoc-2020/day21.onyx | 24 +++++++++++------------- tests/aoc-2020/day24.onyx | 6 +++--- tests/aoc-2020/day25.onyx | 2 +- tests/aoc-2020/day7.onyx | 2 +- tests/aoc-2021/day05.onyx | 8 ++------ tests/aoc-2021/day08.onyx | 16 ++++++++-------- tests/aoc-2021/day09.onyx | 12 ++++++------ tests/aoc-2021/day10.onyx | 6 +++--- tests/aoc-2021/day12.onyx | 2 +- tests/aoc-2021/day14.onyx | 2 +- tests/baked_parameters.onyx | 2 +- tests/i32map.onyx | 2 +- tests/persist_locals.onyx | 2 +- 20 files changed, 60 insertions(+), 63 deletions(-) diff --git a/core/container/map.onyx b/core/container/map.onyx index 594aa3e5..e055f4b4 100644 --- a/core/container/map.onyx +++ b/core/container/map.onyx @@ -134,7 +134,7 @@ has :: (use map: &Map, key: map.Key_Type) -> bool { This is subject to change with the addition of Optional to the standard library. """ -get :: (use map: &Map, key: map.Key_Type) -> map.Value_Type { +get :: (use map: &Map, key: map.Key_Type) -> ? map.Value_Type { lr := lookup(map, key); if lr.entry_index >= 0 do return entries[lr.entry_index].value; @@ -301,7 +301,7 @@ as_iter :: (m: &Map) => // // Helper operator overloads for accessing values, accessing // values by pointer, and setting values. -#operator [] macro (map: Map($K, $V), key: K) -> V { return #this_package.get(&map, key); } +#operator [] macro (map: Map($K, $V), key: K) -> ?V { return #this_package.get(&map, key); } #operator &[] macro (map: Map($K, $V), key: K) -> &V { return #this_package.get_ptr(&map, key); } #operator []= macro (map: Map($K, $V), key: K, value: V) { #this_package.put(&map, key, value); } diff --git a/core/conv/format.onyx b/core/conv/format.onyx index 1f0eafd6..91a66fa1 100644 --- a/core/conv/format.onyx +++ b/core/conv/format.onyx @@ -433,9 +433,11 @@ format_any :: (output: &Format_Output, formatting: &Format, v: any) { // // Use a custom formatter, if one is registered for the type. - if formatting.custom_format && custom_formatters->has(v.type) { - custom_formatters[v.type](output, formatting, v.data); - return; + if formatting.custom_format { + custom_formatters->get(v.type)->with([formatter] { + formatter(output, formatting, v.data); + return; + }); } switch v.type { diff --git a/core/conv/parse.onyx b/core/conv/parse.onyx index 18dc22d0..c3e4b6f3 100644 --- a/core/conv/parse.onyx +++ b/core/conv/parse.onyx @@ -20,9 +20,9 @@ parse_any :: macro (v: &$T, to_parse: str, string_allocator := context.allocator #overload parse_any :: (target: rawptr, data_type: type_expr, to_parse: str, string_allocator := context.allocator) -> bool { - if custom_parsers->has(data_type) { - return custom_parsers[data_type](target, to_parse, string_allocator); - } + custom_parsers->get(data_type)->with([parser] { + return parser(target, to_parse, string_allocator); + }); use runtime.info {*}; info := get_type_info(data_type); diff --git a/core/threads/thread.onyx b/core/threads/thread.onyx index eaa90207..caefd3e5 100644 --- a/core/threads/thread.onyx +++ b/core/threads/thread.onyx @@ -91,7 +91,7 @@ __initialize :: () { __exited :: (id: i32) { sync.scoped_mutex(&thread_mutex); - thread := thread_map->get(id); + thread := thread_map->get(id)?; if thread != null { thread.alive = false; #if runtime.Wait_Notify_Available { diff --git a/tests/aoc-2020/day15.onyx b/tests/aoc-2020/day15.onyx index 6fa54b7f..1bc2335c 100644 --- a/tests/aoc-2020/day15.onyx +++ b/tests/aoc-2020/day15.onyx @@ -26,12 +26,12 @@ main :: (args: [] cstr) { } while turn != 2021 { - st := map.get(&nums, last_num); + st := map.get(&nums, last_num) ?? .{}; if st.previous == 0 do last_num = 0; else do last_num = st.recent - st.previous; - st = map.get(&nums, last_num); + st = map.get(&nums, last_num) ?? .{}; st.previous = st.recent; st.recent = turn; map.put(&nums, last_num, st); diff --git a/tests/aoc-2020/day17.onyx b/tests/aoc-2020/day17.onyx index ccf9b0d1..f1b5a87c 100644 --- a/tests/aoc-2020/day17.onyx +++ b/tests/aoc-2020/day17.onyx @@ -43,8 +43,9 @@ get_neighbor_count :: (cubes: &Map(CubePos, CubeState), pos: CubePos) -> u32 { for x: -1 .. 2 do for y: -1 .. 2 do for z: -1 .. 2 do for w: -1 .. 2 { if x == 0 && y == 0 && z == 0 && w == 0 do continue; key := CubePos.{ pos.x + x, pos.y + y, pos.z + z, pos.w + w }; - state := map.get(cubes, key); - if state.alive do count += 1; + map.get(cubes, key)->with([s] { + if s.alive do count += 1; + }); } return count; @@ -90,7 +91,7 @@ main :: (args: [] cstr) { } for &cube: cubes_to_consider { - state := map.get(&cubes, *cube); + state := map.get(&cubes, *cube) ?? .{}; ncount := get_neighbor_count(&cubes, *cube); if state.alive { @@ -103,7 +104,7 @@ main :: (args: [] cstr) { } for &cube: cubes_to_consider { - state := map.get(&cubes, *cube); + state := map.get(&cubes, *cube) ?? .{}; state.alive = state.next; map.put(&cubes, *cube, state); } diff --git a/tests/aoc-2020/day20.onyx b/tests/aoc-2020/day20.onyx index 99c983cc..9ae73e7c 100644 --- a/tests/aoc-2020/day20.onyx +++ b/tests/aoc-2020/day20.onyx @@ -332,7 +332,7 @@ main :: (args: [] cstr) { if grid[tid.pos_x + 12 * tid.pos_y] != 0 do continue; - tile_ptr := map.get(&tile_map, tid.match.tile); + tile_ptr := map.get(&tile_map, tid.match.tile)->unwrap(); tile_ptr.pos_x = tid.pos_x; tile_ptr.pos_y = tid.pos_y; grid[tid.pos_x + 12 * tid.pos_y] = tid.match.tile; @@ -348,7 +348,7 @@ main :: (args: [] cstr) { forest : [12 * 8 * 12 * 8] u8; for y: 0 .. 12 { for x: 0 .. 12 { - tile := map.get(&tile_map, grid[y * 12 + x]); + tile := map.get(&tile_map, grid[y * 12 + x])->unwrap(); for fy: 0 .. 8 { for fx: 0 .. 8 { diff --git a/tests/aoc-2020/day21.onyx b/tests/aoc-2020/day21.onyx index 6a6ef029..729bbdd5 100644 --- a/tests/aoc-2020/day21.onyx +++ b/tests/aoc-2020/day21.onyx @@ -57,11 +57,10 @@ main :: (args: [] cstr) { array.push(&food.ingredients, ingredient_name); - ingredient := map.get(&ingredient_map, ingredient_name); - if ingredient.name.data == null { - ingredient.name = ingredient_name; - array.init(&ingredient.appears_on, 4); - } + ingredient := map.get(&ingredient_map, ingredient_name) ?? .{ + name = ingredient_name, + appears_on = make([..] u32, 4) + }; array.push(&ingredient.appears_on, line_num); @@ -76,11 +75,10 @@ main :: (args: [] cstr) { array.push(&food.allergens, allergen_name); - allergen := map.get(&allergen_map, allergen_name); - if allergen.name.data == null { - allergen.name = allergen_name; - array.init(&allergen.appears_on, 4); - } + allergen := map.get(&allergen_map, allergen_name) ?? .{ + name = allergen_name, + appears_on = make([..] u32, 4) + }; array.push(&allergen.appears_on, line_num); @@ -109,7 +107,7 @@ main :: (args: [] cstr) { potential_allergen_count := 0; for &allergen_name: potential_allergens { c := array_count_contains(&potential_allergens, *allergen_name, string.equal); - allergen := map.get(&allergen_map, *allergen_name); + allergen := map.get(&allergen_map, *allergen_name)->unwrap(); if c == allergen.appears_on.count { potential_allergen_count += 1; } @@ -122,7 +120,7 @@ main :: (args: [] cstr) { total_safe := 0; for safe: definitely_safe { - ingredient := map.get(&ingredient_map, safe); + ingredient := map.get(&ingredient_map, safe)->unwrap(); total_safe += ingredient.appears_on.count; map.delete(&ingredient_map, safe); @@ -152,7 +150,7 @@ main :: (args: [] cstr) { } if match_count == 1 { - ingredient := map.get(&ingredient_map, matching_ingredient_name); + ingredient := map.get(&ingredient_map, matching_ingredient_name)->unwrap(); map.delete(&ingredient_map, matching_ingredient_name); ingredient.allergen = allergen_entry.key; diff --git a/tests/aoc-2020/day24.onyx b/tests/aoc-2020/day24.onyx index c7700030..d781376f 100644 --- a/tests/aoc-2020/day24.onyx +++ b/tests/aoc-2020/day24.onyx @@ -65,7 +65,7 @@ main :: (args: [] cstr) { } - curr := map.get(&grid, loc); + curr := map.get(&grid, loc) ?? .{}; map.put(&grid, loc, .{ alive = !curr.alive }); } @@ -95,7 +95,7 @@ main :: (args: [] cstr) { } for &cell: cells_to_consider { - state := map.get(&grid, *cell); + state := map.get(&grid, *cell) ?? .{}; ncount := get_neighbor_count(&grid, *cell); if state.alive { @@ -125,7 +125,7 @@ get_neighbor_count :: (grid: &map.Map(Vec2, Cell), pos: Vec2) -> u32 { count := 0; for &dir: Hex_Directions { - cell := map.get(grid, Vec2.{ x = pos.x + dir.x, y = pos.y + dir.y }); + cell := map.get(grid, Vec2.{ x = pos.x + dir.x, y = pos.y + dir.y }) ?? .{}; if cell.alive do count += 1; } diff --git a/tests/aoc-2020/day25.onyx b/tests/aoc-2020/day25.onyx index 2da20af9..9b6f80b6 100644 --- a/tests/aoc-2020/day25.onyx +++ b/tests/aoc-2020/day25.onyx @@ -35,7 +35,7 @@ dlp_bsgs :: (n: u32, a: u32, b: u32) -> u32 { tmp = ~~b; for i: 0 .. m { if map.has(&t, ~~tmp) { - v := map.get(&t, ~~tmp); + v := map.get(&t, ~~tmp) ?? 0; return i * m + v; } diff --git a/tests/aoc-2020/day7.onyx b/tests/aoc-2020/day7.onyx index cc03c7c9..5b030823 100644 --- a/tests/aoc-2020/day7.onyx +++ b/tests/aoc-2020/day7.onyx @@ -32,7 +32,7 @@ bg_free :: (use graph: &BagGraph) { } bg_get_node :: (use graph: &BagGraph, name: str) -> &BagNode { - node := map.get(&node_map, name); + node := map.get(&node_map, name) ?? null; if node == null { node = calloc(sizeof BagNode); diff --git a/tests/aoc-2021/day05.onyx b/tests/aoc-2021/day05.onyx index f87b0667..cf34dc22 100644 --- a/tests/aoc-2021/day05.onyx +++ b/tests/aoc-2021/day05.onyx @@ -66,11 +66,7 @@ main :: (args) => { for &line: lines { for p: line_points(*line) { - if point_count->has(p) { - point_count[p] = point_count[p] + 1; - } else { - point_count[p] = 1; - } + point_count[p] = (point_count[p] ?? 0) + 1; } } @@ -81,4 +77,4 @@ main :: (args) => { printf("Part 2: {}\n", count); } -} \ No newline at end of file +} diff --git a/tests/aoc-2021/day08.onyx b/tests/aoc-2021/day08.onyx index 2ea49697..ff258357 100644 --- a/tests/aoc-2021/day08.onyx +++ b/tests/aoc-2021/day08.onyx @@ -74,8 +74,8 @@ decode_line :: (left, right: str) -> u32 { // Solve for bottom segment for three_data { - if solved_segments[it] == 4 do continue; - if solved_segments[it] == 1 do continue; + if solved_segments[it] ?? 0 == 4 do continue; + if solved_segments[it] ?? 0 == 1 do continue; for o: one_data do if it == o do continue continue; solved_segments[it] = 7; @@ -88,7 +88,7 @@ decode_line :: (left, right: str) -> u32 { if seg.count != 5 do continue; for four_data { - if solved_segments[it] == 4 do continue; + if solved_segments[it] ?? 0 == 4 do continue; for o: one_data do if it == o do continue continue; if array.contains(seg, it) { @@ -104,17 +104,17 @@ decode_line :: (left, right: str) -> u32 { break; } - if solved_segments[it] == 0 { + if solved_segments[it] ?? 0 == 0 { solved_segments[it] = 2; } } for one_data { - if solved_segments[it] == 0 do solved_segments[it] = 3; + if solved_segments[it] ?? 0 == 0 do solved_segments[it] = 3; } for *array.first(left_segments, (x) => x.count == 7) { - if solved_segments[it] == 0 { + if solved_segments[it] ?? 0 == 0 { solved_segments[it] = 5; } } @@ -126,7 +126,7 @@ decode_line :: (left, right: str) -> u32 { string.strip_whitespace(it); num_segments : [7] bool; - for w: *it do num_segments[solved_segments[w] - 1] = true; + for w: *it do num_segments[solved_segments[w] ?? 0 - 1] = true; sum *= 10; for i: 10 { @@ -189,4 +189,4 @@ main :: (args) => { printf("Part {}: {}\n", 1 if PART == 1 else 2, answer); } -} \ No newline at end of file +} diff --git a/tests/aoc-2021/day09.onyx b/tests/aoc-2021/day09.onyx index 7a7eeed9..1eb4e92d 100644 --- a/tests/aoc-2021/day09.onyx +++ b/tests/aoc-2021/day09.onyx @@ -36,12 +36,12 @@ find_span :: macro (low: Pos) -> u32 { potential << Pos.{t.x,t.y+1}; } if t.x > 0 { - if heightmap[Pos.{t.x-1,t.y}].height > 0 { + if heightmap[Pos.{t.x-1,t.y}]->unwrap().height > 0 { potential << Pos.{t.x-1,t.y}; } } if t.y > 0 { - if heightmap[Pos.{t.x,t.y-1}].height > 0 { + if heightmap[Pos.{t.x,t.y-1}]->unwrap().height > 0 { potential << Pos.{t.x,t.y-1}; } } @@ -85,12 +85,12 @@ main :: (args) => { for y: height - 1 do for x: width { map.update(&heightmap, .{x,y}) { - it.dy = it.height - heightmap[Pos.{x,y+1}].height; + it.dy = it.height - heightmap[Pos.{x,y+1}]->unwrap().height; } } for x: width - 1 do for y: height { map.update(&heightmap, .{x,y}) { - it.dx = it.height - heightmap[Pos.{x+1,y}].height; + it.dx = it.height - heightmap[Pos.{x+1,y}]->unwrap().height; } } @@ -102,11 +102,11 @@ main :: (args) => { if y < height - 1 && h.dy >= 0 do continue; if x > 0 { - if heightmap[Pos.{x-1, y}].dx <= 0 do continue; + if heightmap[Pos.{x-1, y}]->unwrap().dx <= 0 do continue; } if y > 0 { - if heightmap[Pos.{x, y-1}].dy <= 0 do continue; + if heightmap[Pos.{x, y-1}]->unwrap().dy <= 0 do continue; } lowest << .{x, y}; diff --git a/tests/aoc-2021/day10.onyx b/tests/aoc-2021/day10.onyx index caa399cb..b64b9542 100644 --- a/tests/aoc-2021/day10.onyx +++ b/tests/aoc-2021/day10.onyx @@ -29,14 +29,14 @@ main :: (args) => { for ch: line { switch ch { case #char "(", #char "[", #char "<", #char "{" { - char_stack << bracket_map[ch]; + char_stack << bracket_map[ch]->unwrap(); } case #char ")", #char "]", #char ">", #char "}" { x := array.pop(&char_stack); if x != ch { // printf("Expected '{}', found '{}' instead.\n", x, ch); - corrupted_score += score_map[ch]; + corrupted_score += score_map[ch]->unwrap(); continue continue; } } @@ -70,4 +70,4 @@ main :: (args) => { println(completion_scores); printf("Part 2: {}\n", completion_scores[completion_scores.count / 2]); } -} \ No newline at end of file +} diff --git a/tests/aoc-2021/day12.onyx b/tests/aoc-2021/day12.onyx index d253b7d7..fe7d7a93 100644 --- a/tests/aoc-2021/day12.onyx +++ b/tests/aoc-2021/day12.onyx @@ -67,7 +67,7 @@ main :: (args) => { node_idx := node_stack.count - 1; defer node_stack[node_idx].child_idx += 1; - children := edge_map[node_stack[node_idx].name]; + children := edge_map[node_stack[node_idx].name] ?? .[]; valid := node_stack[node_idx].child_idx < children.count; if valid { diff --git a/tests/aoc-2021/day14.onyx b/tests/aoc-2021/day14.onyx index faf3452e..64362d38 100644 --- a/tests/aoc-2021/day14.onyx +++ b/tests/aoc-2021/day14.onyx @@ -78,7 +78,7 @@ main :: (args) => { mode: Map(u8, u64); for& polymer_state.entries { - mode[it.key.second] = mode[it.key.second] + it.value.now; + mode[it.key.second] = (mode[it.key.second] ?? .{}) + it.value.now; } maximum := array.fold(mode.entries, cast(u64) 0, (x, y) => math.max(x.value, y)); diff --git a/tests/baked_parameters.onyx b/tests/baked_parameters.onyx index d7b7bfc0..459f032e 100644 --- a/tests/baked_parameters.onyx +++ b/tests/baked_parameters.onyx @@ -39,7 +39,7 @@ main :: (args: [] cstr) { map.put(&ages, "Pam", 24); print_age :: (ages: &map.Map(str, u32), name: str) { - age := map.get(ages, name); + age := map.get(ages, name) ?? 0; printf("{}'s age is {}.\n", name, age); } diff --git a/tests/i32map.onyx b/tests/i32map.onyx index 1c2cb03e..fdc16368 100644 --- a/tests/i32map.onyx +++ b/tests/i32map.onyx @@ -17,7 +17,7 @@ main :: (args: [] cstr) { println(map.has(&imap, 50)); println(map.has(&imap, 51)); - printf("{}{}\n", map.get(&imap, 50), map.get(&imap, 1234)); + printf("{}{}\n", map.get(&imap, 50)->unwrap(), map.get(&imap, 1234)->unwrap()); printf("{*p}\n", &imap); diff --git a/tests/persist_locals.onyx b/tests/persist_locals.onyx index 27b134d2..2cf4da09 100644 --- a/tests/persist_locals.onyx +++ b/tests/persist_locals.onyx @@ -32,7 +32,7 @@ cached_fib :: (n: u64) -> u64 { if n <= 1 do return n; - if map.has(&cache, n) do return map.get(&cache, n); + map.get(&cache, n)->with([n] { return n; }); res := cached_fib(n - 1) + cached_fib(n - 2); map.put(&cache, n, res); -- 2.25.1