misc cleanup

This commit is contained in:
Brendan Hansknecht 2022-10-12 20:07:03 -07:00
parent bb26844cf1
commit d87a750f7c
No known key found for this signature in database
GPG key ID: 0EA784685083E75B
3 changed files with 9 additions and 22 deletions

View file

@ -211,8 +211,6 @@ insertIfVacant = \dict, key, value ->
# We have decided not to expose the standard roc hashing algorithm.
# This is to avoid external dependence and the need for versioning.
# For now, it will just live within the Dict file.
# If we add non-exposed roc standard library files, this should be moved to it's own file.
# The current implementation is a form of [Wyhash final3](https://github.com/wangyi-fudan/wyhash/blob/a5995b98ebfa7bd38bfadc0919326d2e7aabb805/wyhash.h).
# It is 64bit and little endian specific currently.
LowLevelHasher := { originalSeed : U64, state : U64 } has [
@ -229,7 +227,6 @@ LowLevelHasher := { originalSeed : U64, state : U64 } has [
addI64,
addI128,
complete,
reset,
},
]
@ -246,9 +243,6 @@ combineState = \@LowLevelHasher { originalSeed, state }, { a, b, seed, length }
complete = \@LowLevelHasher { state } -> state
reset = \@LowLevelHasher { originalSeed } ->
@LowLevelHasher { originalSeed, state: originalSeed }
addI8 = \hasher, i8 ->
addU8 hasher (Num.toU8 i8)
addI16 = \hasher, i16 ->
@ -291,11 +285,9 @@ addU16 = \@LowLevelHasher { originalSeed, state }, u16 ->
addU32 = \@LowLevelHasher { originalSeed, state }, u32 ->
seed = Num.bitwiseXor originalSeed wyp0
p0 = Num.toU64 u32
p1 = Num.toU64 u32
a = Num.shiftLeftBy p0 32 |> Num.bitwiseOr p1
b = Num.shiftLeftBy p1 32 |> Num.bitwiseOr p0
a = Num.shiftLeftBy p0 32 |> Num.bitwiseOr p0
combineState (@LowLevelHasher { originalSeed, state }) { a, b, seed, length: 4 }
combineState (@LowLevelHasher { originalSeed, state }) { a, b: a, seed, length: 4 }
addU64 = \@LowLevelHasher { originalSeed, state }, u64 ->
seed = Num.bitwiseXor originalSeed wyp0

View file

@ -15,7 +15,6 @@ interface Hash
addI64,
addI128,
complete,
reset,
hashStrBytes,
hashList,
hashUnordered,
@ -75,10 +74,6 @@ Hasher has
## accumulated hash state.
complete : a -> U64 | a has Hasher
## Resets the internal state of a hasher
## The hasher should still have all the same parameters and seeds.
reset : a -> a | a has Hasher
## Adds a string into a [Hasher] by hashing its UTF-8 bytes.
hashStrBytes = \hasher, s ->
addBytes hasher (Str.toUtf8 s)
@ -90,21 +85,22 @@ hashList = \hasher, lst ->
## 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 effect the final hash.
## The order of the elements does not affect the final hash.
hashUnordered = \hasher, container, walk ->
walk
container
0
(\accum, elem ->
x =
# Note, we intentionally copy the hasher in every iteration.
# Having the same base state is required for unordered hashing.
hasher
|> reset
|> hash elem
|> complete
nextAccum = Num.addWrap accum x
if nextAccum < accum then
# we dont want to lose a bit of entropy on overflow, so add it back in.
# we don't want to lose a bit of entropy on overflow, so add it back in.
Num.addWrap nextAccum 1
else
nextAccum

View file

@ -1530,10 +1530,9 @@ define_builtins! {
12 HASH_ADD_I64: "addI64"
13 HASH_ADD_I128: "addI128"
14 HASH_COMPLETE: "complete"
15 HASH_RESET: "reset"
16 HASH_HASH_STR_BYTES: "hashStrBytes"
17 HASH_HASH_LIST: "hashList"
18 HASH_HASH_UNORDERED: "hashUnordered"
15 HASH_HASH_STR_BYTES: "hashStrBytes"
16 HASH_HASH_LIST: "hashList"
17 HASH_HASH_UNORDERED: "hashUnordered"
}
14 JSON: "Json" => {
0 JSON_JSON: "Json"