From: Brendan Hansen Date: Thu, 13 May 2021 17:24:59 +0000 (-0500) Subject: no more map.cmp_function. default is now always optional X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=cc766f7a38c6ac9037f48be6b29307dbfb9fdf3d;p=onyx.git no more map.cmp_function. default is now always optional --- diff --git a/core/container/map.onyx b/core/container/map.onyx index c6be5e42..385062f7 100644 --- a/core/container/map.onyx +++ b/core/container/map.onyx @@ -2,6 +2,7 @@ package core.map #private_file array :: package core.array #private_file string :: package core.string +use package core.intrinsics.onyx { __zero_value } Map :: struct (K: type_expr, V: type_expr) { hashes : [..] i32; @@ -17,7 +18,7 @@ Map :: struct (K: type_expr, V: type_expr) { } } -make :: ($Key: type_expr, $Value: type_expr, default: Value = 0, hash_count: i32 = 16) -> Map(Key, Value) { +make :: ($Key: type_expr, $Value: type_expr, default := __zero_value(Value), hash_count: i32 = 16) -> Map(Key, Value) { map : Map(Key, Value); init(^map, default = default, hash_count = hash_count); return map; @@ -118,15 +119,6 @@ hash_function :: proc { }, } -// CLEANUP: This could be replaced with operator overloading of '==' -cmp_function :: proc { - (a: rawptr, b: rawptr) -> bool { return a == b; }, - (a: i32, b: i32) -> bool { return a == b; }, - (a: i64, b: i64) -> bool { return a == b; }, - - string.equal, -} - // // Private symbols // @@ -148,7 +140,7 @@ lookup :: (use map: ^Map($K, $V), key: K) -> MapLookupResult { lr.entry_index = hashes[lr.hash_index]; while lr.entry_index >= 0 { - if cmp_function(entries[lr.entry_index].key, key) do return lr; + if entries[lr.entry_index].key == key do return lr; lr.entry_prev = lr.entry_index; lr.entry_index = entries[lr.entry_index].next; diff --git a/examples/11_map.onyx b/examples/11_map.onyx index 86154af6..e8467669 100644 --- a/examples/11_map.onyx +++ b/examples/11_map.onyx @@ -53,10 +53,10 @@ main :: (args: [] cstr) { // which types can do which things. One thing that Onyx does have // however is explicit overloaded procedures. They are talked about // more in 14_overloaded_procs.onyx. For our purposes right now, we - // just need to know that Map uses two overloaded procedures to - // achieve its implementation: map.hash_function, map.cmp_function. + // just need to know that Map uses an overloaded procedures to + // achieve its implementation, map.hash_function. // To make Map work with any new datatype you come up with, simply - // define an overload for map.hash_function and map.cmp_function. + // define an overload for map.hash_function and the '==' operator. // If you want to see what this looks like, take a look at // tests/aoc-2020/day17.onyx in this repository. } diff --git a/tests/aoc-2020/day17.onyx b/tests/aoc-2020/day17.onyx index e4f51bf0..22cf20b6 100644 --- a/tests/aoc-2020/day17.onyx +++ b/tests/aoc-2020/day17.onyx @@ -16,7 +16,7 @@ CubeState :: struct { return 17 * c.x + 13 * c.y + 11 * c.z + 19 * c.w; } -#add_overload map.cmp_function, (a: CubePos, b: CubePos) -> bool { +#operator == (a: CubePos, b: CubePos) -> bool { return (a.x == b.x) && (a.y == b.y) && (a.z == b.z) diff --git a/tests/aoc-2020/day24.onyx b/tests/aoc-2020/day24.onyx index 3180fbf9..ee1458de 100644 --- a/tests/aoc-2020/day24.onyx +++ b/tests/aoc-2020/day24.onyx @@ -11,7 +11,7 @@ Vec2 :: struct { return v.x * 11 + v.y * 17; } -#add_overload map.cmp_function, (v1: Vec2, v2: Vec2) -> bool { +#operator == (v1: Vec2, v2: Vec2) -> bool { return v1.x == v2.x && v1.y == v2.y; }