mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-26 13:29:12 +00:00
add the ability to hash Nats
This commit is contained in:
parent
3399fe10a0
commit
6aec9cf058
3 changed files with 26 additions and 5 deletions
|
@ -14,14 +14,16 @@ interface Hash
|
|||
hashI32,
|
||||
hashI64,
|
||||
hashI128,
|
||||
hashNat,
|
||||
complete,
|
||||
hashStrBytes,
|
||||
hashList,
|
||||
hashUnordered,
|
||||
] imports [
|
||||
Bool.{ isEq },
|
||||
List,
|
||||
Str,
|
||||
Num.{ U8, U16, U32, U64, U128, I8, I16, I32, I64, I128 },
|
||||
Num.{ U8, U16, U32, U64, U128, I8, I16, I32, I64, I128, Nat },
|
||||
]
|
||||
|
||||
## A value that can hashed.
|
||||
|
@ -88,6 +90,21 @@ hashI64 = \hasher, n -> addU64 hasher (Num.toU64 n)
|
|||
hashI128 : a, I128 -> a | a has Hasher
|
||||
hashI128 = \hasher, n -> addU128 hasher (Num.toU128 n)
|
||||
|
||||
## Adds a single Nat to a hasher.
|
||||
hashNat : a, Nat -> a | a has Hasher
|
||||
hashNat = \hasher, n ->
|
||||
isPlatform32bit =
|
||||
x : Nat
|
||||
x = 0xffff_ffff
|
||||
y = Num.addWrap x 1
|
||||
|
||||
y == 0
|
||||
|
||||
if isPlatform32bit then
|
||||
addU32 hasher (Num.toU32 n)
|
||||
else
|
||||
addU64 hasher (Num.toU64 n)
|
||||
|
||||
## Adds a container of [Hash]able elements to a [Hasher] by hashing each element.
|
||||
## The container is iterated using the walk method passed in.
|
||||
## The order of the elements does not affect the final hash.
|
||||
|
|
|
@ -174,6 +174,9 @@ const fn num_symbol_to_hash_lambda(symbol: Symbol) -> Option<FlatHash> {
|
|||
Symbol::NUM_I128 | Symbol::NUM_SIGNED128 => {
|
||||
Some(SingleLambdaSetImmediate(Symbol::HASH_HASH_I128))
|
||||
}
|
||||
Symbol::NUM_NAT | Symbol::NUM_NATURAL => {
|
||||
Some(SingleLambdaSetImmediate(Symbol::HASH_HASH_NAT))
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1522,10 +1522,11 @@ define_builtins! {
|
|||
11 HASH_HASH_I32: "hashI32"
|
||||
12 HASH_HASH_I64: "hashI64"
|
||||
13 HASH_HASH_I128: "hashI128"
|
||||
14 HASH_COMPLETE: "complete"
|
||||
15 HASH_HASH_STR_BYTES: "hashStrBytes"
|
||||
16 HASH_HASH_LIST: "hashList"
|
||||
17 HASH_HASH_UNORDERED: "hashUnordered"
|
||||
14 HASH_HASH_NAT: "hashNat"
|
||||
15 HASH_COMPLETE: "complete"
|
||||
16 HASH_HASH_STR_BYTES: "hashStrBytes"
|
||||
17 HASH_HASH_LIST: "hashList"
|
||||
18 HASH_HASH_UNORDERED: "hashUnordered"
|
||||
}
|
||||
14 JSON: "Json" => {
|
||||
0 JSON_JSON: "Json"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue