Add more robust hashing based on a paper
The paper in question can be referenced [here](https://courses.cs.washington.edu/courses/cse521/15sp/refs/thorup1.pdf) This will compute a hash that is almost just as performant as the previous hashing, but handles the case where if lower numbered bits are all 0 and the highest bit of the length of the container is less than the lowest bit of the key, the hash will always be 0.
This commit is contained in:
parent
1e303100f8
commit
437461d809
2 changed files with 56 additions and 14 deletions
|
|
@ -50,8 +50,16 @@ template rawGetKnownHCImpl() {.dirty.} =
|
|||
proc rawGetKnownHC[X, A](t: X, key: A, hc: Hash): int {.inline.} =
|
||||
rawGetKnownHCImpl()
|
||||
|
||||
template bits(n): uint32 =
|
||||
var temp = n
|
||||
var bits = 0.uint32
|
||||
while temp != 0:
|
||||
temp = temp shr 1
|
||||
bits += 1
|
||||
bits
|
||||
|
||||
template genHashImpl(key, hc: typed) =
|
||||
hc = hash(key)
|
||||
hc = hash(key, targetBits=bits(maxHash(t)))
|
||||
if hc == 0: # This almost never taken branch should be very predictable.
|
||||
hc = 314159265 # Value doesn't matter; Any non-zero favorite is fine.
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue