From d59f0cc8c1b9584e71f186b5fa01346bc36dc2cf Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Tue, 14 Mar 2023 20:24:50 -0500 Subject: [PATCH] cleanup: removed old references to `to_u32` --- core/container/map.onyx | 10 +++++----- core/container/optional.onyx | 2 +- core/container/pair.onyx | 6 +++--- core/container/result.onyx | 7 +++++++ core/container/set.onyx | 4 ++-- core/hash/hash.onyx | 2 +- 6 files changed, 19 insertions(+), 12 deletions(-) diff --git a/core/container/map.onyx b/core/container/map.onyx index b8d4c029..d0425540 100644 --- a/core/container/map.onyx +++ b/core/container/map.onyx @@ -6,7 +6,7 @@ use core.intrinsics.onyx { __initialize } // // Map is a generic hash-map implementation that uses chaining. // Values can be of any type. Keys must of a type that supports -// the core.hash.to_u32, and the '==' operator. +// the core.hash.hash, and the '==' operator. // @conv.Custom_Format.{ #solidify format_map {K=Key_Type, V=Value_Type} } Map :: struct (Key_Type: type_expr, Value_Type: type_expr) where ValidKey(Key_Type) { @@ -28,11 +28,11 @@ Map :: struct (Key_Type: type_expr, Value_Type: type_expr) where ValidKey(Key_Ty #local ValidKey :: interface (t: $T) { // In order to use a certain type as a key in a Map, you must - // provide an implementation of core.hash.to_u32() for that type, + // provide an implementation of core.hash.hash() for that type, // and you must provide an operator overload for ==. - { hash.to_u32(t) } -> u32; - { t == t } -> bool; + { hash.hash(t) } -> u32; + { t == t } -> bool; } #inject Map { @@ -312,7 +312,7 @@ as_iter :: (m: &Map) => if hashes.data == null do init(map); lr := MapLookupResult.{}; - hash_value: u32 = hash.to_u32(key); + hash_value: u32 = hash.hash(key); lr.hash = hash_value; lr.hash_index = hash_value % hashes.count; diff --git a/core/container/optional.onyx b/core/container/optional.onyx index e4d961a3..d6d773e3 100644 --- a/core/container/optional.onyx +++ b/core/container/optional.onyx @@ -97,7 +97,7 @@ package core hash :: (o: ?$T/core.hash.Hashable) -> u32 { if !o.has_value do return 0; - return core.hash.to_u32(o.value); + return core.hash.hash(o.value); } } diff --git a/core/container/pair.onyx b/core/container/pair.onyx index 3a464535..d91bc18a 100644 --- a/core/container/pair.onyx +++ b/core/container/pair.onyx @@ -24,10 +24,10 @@ Pair :: struct (First_Type: type_expr, Second_Type: type_expr) { } #overload -core.hash.to_u32 :: (p: Pair($First_Type/hash.Hashable, $Second_Type/hash.Hashable)) => { +core.hash.hash :: (p: Pair($First_Type/hash.Hashable, $Second_Type/hash.Hashable)) => { h := 7; - h += h << 5 + hash.to_u32(p.first); - h += h << 5 + hash.to_u32(p.second); + h += h << 5 + hash.hash(p.first); + h += h << 5 + hash.hash(p.second); return h; } diff --git a/core/container/result.onyx b/core/container/result.onyx index 4e0ce7cc..23fc812a 100644 --- a/core/container/result.onyx +++ b/core/container/result.onyx @@ -182,3 +182,10 @@ Result_Data :: struct (T: type_expr, E: type_expr) { #overload __implicit_bool_cast :: macro (r: Result($O, $E)) => r.status == .Ok; + +#operator ? macro (r: Result($T, $E)) -> T { + res := r; + if r.status == .Ok do return r.__data.value; + + return return .{ .Err, .{ error = res.__data.error } }; +} diff --git a/core/container/set.onyx b/core/container/set.onyx index e38ef891..29bc7aa2 100644 --- a/core/container/set.onyx +++ b/core/container/set.onyx @@ -3,7 +3,7 @@ package core.set use core {array, hash, memory, math, Optional} #local SetValue :: interface (t: $T) { - { hash.to_u32(t) } -> u32; + { hash.hash(t) } -> u32; { t == t } -> bool; } @@ -155,7 +155,7 @@ as_iter :: (s: &Set) => lookup :: (use set: &Set, value: set.Elem_Type) -> SetLookupResult { lr := SetLookupResult.{}; - hash_value: u32 = hash.to_u32(value); // You cannot have a set of this type without defining a to_u32 hash. + hash_value: u32 = hash.hash(value); // You cannot have a set of this type without defining a hash function. lr.hash = hash_value; lr.hash_index = hash_value % hashes.count; diff --git a/core/hash/hash.onyx b/core/hash/hash.onyx index 739a79b2..b3345d50 100644 --- a/core/hash/hash.onyx +++ b/core/hash/hash.onyx @@ -37,7 +37,7 @@ hash :: #match -> u32 { for ch: key do hash += (hash << 5) + ~~ch; return hash; }, - (key: type_expr) -> u32 { return to_u32(cast(u32) key); }, + (key: type_expr) -> u32 { return hash(cast(u32) key); }, (key: bool) -> u32 { return 1 if key else 0; }, #order 10000 -- 2.25.1