This commit is contained in:
flywind 2021-01-02 10:13:01 -06:00 • committed by GitHub
commit e869767aa7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 5 deletions

View file

@ -297,6 +297,9 @@ proc murmurHash(x: openArray[byte]): Hash =
h1 = h1 xor (h1 shr 16) h1 = h1 xor (h1 shr 16)
return cast[Hash](h1) return cast[Hash](h1)
proc hashVmImpl(x: cstring, sPos, ePos: int): Hash =
doAssert false, "implementation override in compiler/vmops.nim"
proc hashVmImpl(x: string, sPos, ePos: int): Hash = proc hashVmImpl(x: string, sPos, ePos: int): Hash =
doAssert false, "implementation override in compiler/vmops.nim" doAssert false, "implementation override in compiler/vmops.nim"
@ -341,11 +344,14 @@ proc hash*(x: cstring): Hash =
inc i inc i
result = !$result result = !$result
else: else:
when not defined(js) and defined(nimToOpenArrayCString): when nimvm:
murmurHash(toOpenArrayByte(x, 0, x.high)) hashVmImpl(x, 0, high(x))
else: else:
let xx = $x when not defined(js) and defined(nimToOpenArrayCString):
murmurHash(toOpenArrayByte(xx, 0, high(xx))) murmurHash(toOpenArrayByte(x, 0, x.high))
else:
let xx = $x
murmurHash(toOpenArrayByte(xx, 0, high(xx)))
proc hash*(sBuf: string, sPos, ePos: int): Hash = proc hash*(sBuf: string, sPos, ePos: int): Hash =
## Efficient hashing of a string buffer, from starting ## Efficient hashing of a string buffer, from starting

View file

@ -86,8 +86,13 @@ block largeSize: # longer than 4 characters
doAssert hash(xx, 0, 3) == hash(ssl, 0, 3) doAssert hash(xx, 0, 3) == hash(ssl, 0, 3)
proc main() = proc main() =
doAssert hash(0.0) == hash(0) doAssert hash(0.0) == hash(0)
when sizeof(int) == 8: doAssert hash(cstring"abracadabra") == 97309975
doAssert hash(cstring"abracadabra") == hash("abracadabra")
when sizeof(int) == 8 or defined(js):
block: block:
var s: seq[Hash] var s: seq[Hash]
for a in [0.0, 1.0, -1.0, 1000.0, -1000.0]: for a in [0.0, 1.0, -1.0, 1000.0, -1000.0]: