no more map.cmp_function. default is now always optional
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 13 May 2021 17:24:59 +0000 (12:24 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 13 May 2021 17:24:59 +0000 (12:24 -0500)
core/container/map.onyx
examples/11_map.onyx
tests/aoc-2020/day17.onyx
tests/aoc-2020/day24.onyx

index c6be5e4271712fb7dac3b1359fe6c6984e68b5f9..385062f759040f2a9a84e9ddd9daad7f30a397c3 100644 (file)
@@ -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;
index 86154af615ad577ffe7c9e9319e13dd2941c2546..e84676691662f8194ff05519fee5b1fc664357fa 100644 (file)
@@ -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.
 }
index e4f51bf026a5d99382143a436ed460a89108ca92..22cf20b60dba691964147b54292ad35850616ba6 100644 (file)
@@ -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)
index 3180fbf9bc50e3d405a119f518bb83acb64b0a94..ee1458de99f5f6bdff53adec5fd4d6138d5cb472 100644 (file)
@@ -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;
 }