Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
c-blake committed Mar 18, 2020
1 parent 508e3b9 commit 12767f7
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 22 deletions.
7 changes: 7 additions & 0 deletions althash.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)
7 changes: 3 additions & 4 deletions ilset.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
9 changes: 3 additions & 6 deletions lpset.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions olset.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions otset.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 3 additions & 4 deletions tsset.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 12767f7

Please sign in to comment.