From 12767f799ece97c68267c26d5873283cfe1f61b8 Mon Sep 17 00:00:00 2001 From: Charles Blake Date: Wed, 18 Mar 2020 04:06:34 -0400 Subject: [PATCH] Update as per https://github.com/nim-lang/RFCs/issues/201#issuecomment-600252318 Can now either `import althash; proc hash(h,salt: Hash): Hash = ...` to change how hashes are rehashed (when that is even necessary) or compile with `-d:unstableHashHash` to do so with the built-in hash rehasher. --- althash.nim | 7 +++++++ ilset.nim | 7 +++---- lpset.nim | 9 +++------ olset.nim | 7 +++---- otset.nim | 7 +++---- tsset.nim | 7 +++---- 6 files changed, 22 insertions(+), 22 deletions(-) diff --git a/althash.nim b/althash.nim index 781280e..941d25d 100644 --- a/althash.nim +++ b/althash.nim @@ -70,3 +70,10 @@ when int.sizeof == int64.sizeof: else: proc hashRoMu1*(x: int|uint): Hash {.inline.} = hashRoMu1(uint32(x)) proc hashRevFib*(x: int|uint): Hash {.inline.} = hashRevFib(uint32(x)) + +when defined(unstableHashHash): + proc hash*(hashValue, salt: Hash): Hash {.inline.} = + hashRoMu1(hashValue) xor Hash(salt) +else: + proc hash*(hashValue, salt: Hash): Hash {.inline.} = + hashRoMu1(hashValue) diff --git a/ilset.nim b/ilset.nim index a0fa7b0..d3a1f20 100644 --- a/ilset.nim +++ b/ilset.nim @@ -41,11 +41,10 @@ proc hash0[A; z: static[int]](s: ILSet[A,z], item: A): Hash {.inline.} = if result == z: # Rarely taken branch very predictable result = 314159265 # Value matters little; More bits spread while enlarging -# s.salt here is just a hash of the VM address of data[] giving distinct tabs -# distinct home addr locations that are at least as independent as `hashAddr`. +# s.salt here is just a hash of the VM address of data[] that can give distinct +# tabs distinct home addr locations at least as independent as `hashAddr`. proc hashHc[A; z: static[int]](s: ILSet[A,z], hc: Hash): Hash {.inline.} = -# if s.rehash: Hash(rotateLeftBits(hc * s.salt, 32)) else: hc - if s.rehash: hashRoMu1(hc) xor Hash(s.salt) else: hc + if s.rehash: hash(hc, s.salt) else: hc proc hash[A; z: static[int]](s: ILSet[A,z], i: int): Hash {.inline.} = s.hashHc s.hash0(s.data[i]) diff --git a/lpset.nim b/lpset.nim index fe4b4b8..2557f76 100644 --- a/lpset.nim +++ b/lpset.nim @@ -49,13 +49,10 @@ when defined(lpWarn) or not defined(danger): var lpMaxWarn* = 10 ## Most warnings per program invocation var lpWarnCnt = 0 # Running counter of warnings issued -# s.salt here is just a hash of the VM address of data[] giving distinct tabs -# distinct home addr locations that are at least as independent as `hashAddr`. +# s.salt here is just a hash of the VM address of data[] that can give distinct +# tabs distinct home addr locations at least as independent as `hashAddr`. proc hashHc[A](s: LPSet[A], hc: Hash): Hash {.inline.} = -# if s.rehash: Hash(rotateLeftBits(hc * s.salt, 32)) else: hc -#XXX May be better to call this hash2, allow importing environment to override& -# just default it to hashRoMu1, maybe providing salt as an extra argument? - if s.rehash: hashRoMu1(hc) xor Hash(s.salt) else: hc + if s.rehash: hash(hc, s.salt) else: hc proc hash[A](s: LPSet[A], i: int): Hash {.inline.} = s.hashHc s.data[i].hcode diff --git a/olset.nim b/olset.nim index 457eaa1..9245ab4 100644 --- a/olset.nim +++ b/olset.nim @@ -47,11 +47,10 @@ when defined(olWarn) or not defined(danger): var olMaxWarn* = 10 ## Most warnings per program invocation var olWarnCnt = 0 # Running counter of warnings issued -# s.salt here is just a hash of the VM address of data[] giving distinct tabs -# distinct home addr locations that are at least as independent as `hashAddr`. +# s.salt here is just a hash of the VM address of data[] that can give distinct +# tabs distinct home addr locations at least as independent as `hashAddr`. proc hashHc[A](s: OLSet[A], hc: Hash): Hash {.inline.} = -# if s.rehash: Hash(rotateLeftBits(hc * s.salt, 32)) else: hc - if s.rehash: hashRoMu1(hc) xor Hash(s.salt) else: hc + if s.rehash: hash(hc, s.salt) else: hc proc hash[A](s: OLSet[A], i: int): Hash {.inline.} = s.hashHc s.data[s.idx[i] - 1].hcode diff --git a/otset.nim b/otset.nim index e459ec9..aca0e4d 100644 --- a/otset.nim +++ b/otset.nim @@ -27,11 +27,10 @@ when defined(otWarn) or not defined(danger): var otMaxWarn* = 10 ## Most warnings per program invocation var otWarnCnt = 0 # Running counter of warnings issued -# s.salt here is just a hash of the VM address of data[] giving distinct tabs -# distinct home addr locations that are at least as independent as `hashAddr`. +# s.salt here is just a hash of the VM address of data[] that can give distinct +# tabs distinct home addr locations at least as independent as `hashAddr`. proc hashHc[A](s: OTSet[A], hc: Hash): Hash {.inline.} = -# if s.rehash: Hash(rotateLeftBits(hc * s.salt, 32)) else: hc - if s.rehash: hashRoMu1(hc) xor Hash(s.salt) else: hc + if s.rehash: hash(hc, s.salt) else: hc proc hash0[A](item: A): Hash {.inline.} = result = hash(item) # Account for hash() returning zero, our FREE sentinel. diff --git a/tsset.nim b/tsset.nim index 9b2268d..405e46c 100644 --- a/tsset.nim +++ b/tsset.nim @@ -52,11 +52,10 @@ when defined(tsWarn) or not defined(danger): var tsMaxWarn* = 10 ## Most warnings per program invocation var tsWarnCnt = 0 # Running counter of warnings issued -# s.salt here is just a hash of the VM address of data[] giving distinct tabs -# distinct home addr locations that are at least as independent as `hashAddr`. +# s.salt here is just a hash of the VM address of data[] that can give distinct +# tabs distinct home addr locations at least as independent as `hashAddr`. proc hashHc[A](s: TSSet[A], hc: Hash): Hash {.inline.} = -# if s.rehash: Hash(rotateLeftBits(hc * s.salt, 32)) else: hc - if s.rehash: hashRoMu1(hc) xor Hash(s.salt) else: hc + if s.rehash: hash(hc, s.salt) else: hc proc hash0[A](item: A): Hash {.inline.} = result = hash(item) # Account for hash() returning zero, our FREE sentinel.