//
// 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) {
#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 {
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;
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);
}
}
}
#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;
}
#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 } };
+}
use core {array, hash, memory, math, Optional}
#local SetValue :: interface (t: $T) {
- { hash.to_u32(t) } -> u32;
+ { hash.hash(t) } -> u32;
{ t == t } -> bool;
}
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;
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