#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;
}
}
-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;
},
}
-// 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
//
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;
// 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.
}
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)
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;
}