Rebus.Hash
- Package
- purescript-rebus
- Repository
- afcondon/purescript-rebus
Rebus.Hash — the one string hash everything in Rebus is made from.
It is a module of its own only so that Rebus.Assign can order keys and
shuffle a deck with the same arithmetic the glyphs are dealt with. It is not
a general-purpose hash and is exported for Rebus's own modules.
#hashWith Source
hashWith :: Int -> Int -> String -> IntA bounded, deterministic string hash of the djb2 family, reduced modulo a
prime at every step so it stays inside the safe integer range no matter how
long the text is — which is also what makes it reproducible on any runtime
that can multiply two small ints. seed and mult vary to get weakly
independent hashes out of one text.
Written as an explicit loop rather than a fold to keep the library's dependency surface at the five packages every PureScript program already has.
#multiplyHashes Source
multiplyHashes :: Int -> Int -> IntThe product of two hashes, modulo the prime hashWith reduces by.
hashWith is linear in the last characters of its text, so texts that
differ only there ("node 1", "node 2") get hashes that are neighbours. Where
that would show (an ORDER over keys, in Rebus.Assign) two differently
seeded hashes are multiplied, which neighbours do not survive.
The multiplication is done in two halves so that no intermediate leaves the 32-bit range: both arguments are below 1000003, the first is split at a thousand, and every product and sum stays under two thousand million.