Skip to content

Commit

Permalink
try to make it bootstrap
Browse files Browse the repository at this point in the history
  • Loading branch information
narimiran committed May 12, 2019
1 parent 8c4ecc1 commit db04f15
Showing 1 changed file with 52 additions and 42 deletions.
94 changes: 52 additions & 42 deletions lib/pure/hashes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ proc hash*[T: Ordinal](x: T): Hash {.inline.} =
## Efficient hashing of other ordinal types (e.g. enums).
result = ord(x)

template singleByteHashImpl(result: Hash, x: typed, start, stop: int) =
proc singleByteHashImpl[T](h: var Hash, x: openArray[T], start, stop: int) {.inline.} =
for i in start .. stop:
result = result !& hash(x[i])
h = h !& hash(x[i])

template multiByteHashImpl(result: Hash, x: typed, start, stop: int) =
let stepSize = IntSize div sizeof(x[start])
Expand Down Expand Up @@ -184,7 +184,16 @@ proc hash*(x: cstring): Hash =
result = result !& ord(x[i])
inc i
else:
multiByteHashImpl(result, x, 0, high(x))
var
i = 0
l = len(x)
while i < l - IntSize:
let n = cast[ptr Hash](unsafeAddr x[i])[]
result = result !& n
i += IntSize
while i < l:
result = result !& ord(x[i])
inc i
result = !$result

proc hash*(sBuf: string, sPos, ePos: int): Hash =
Expand Down Expand Up @@ -339,42 +348,43 @@ proc hash*[A](x: set[A]): Hash =


when isMainModule:
block empty:
var
a = ""
b = newSeq[char]()
c = newSeq[int]()
doAssert hash(a) == 0
doAssert hash(b) == 0
doAssert hash(c) == 0
doAssert hashIgnoreCase(a) == 0
doAssert hashIgnoreStyle(a) == 0
block sameButDifferent:
doAssert hash("aa bb aaaa1234") == hash("aa bb aaaa1234", 0, 13)
doAssert hash("aa bb aaaa1234") == hash(cstring"aa bb aaaa1234")
doAssert hashIgnoreCase("aA bb aAAa1234") == hash("aa bb aaaa1234")
doAssert hashIgnoreStyle("aa_bb_AAaa1234") == hashIgnoreCase("aaBBAAAa1234")
block smallSize: # no multibyte hashing
let
xx = @['H','e','l','l','o']
ii = @[72, 101, 108, 108, 111]
ss = "Hello"
doAssert hash(xx) == hash(ii)
doAssert hash(xx) == hash(ss)
doAssert hash(xx) == hash(xx, 0, xx.high)
doAssert hash(ss) == hash(ss, 0, ss.high)
block largeSize: # longer than 8 characters, should trigger multibyte hashing
let
xx = @['H','e','l','l','o']
xxl = @['H','e','l','l','o','w','e','e','n','s']
ssl = "Helloweens"
doAssert hash(xxl) == hash(ssl)
doAssert hash(xxl) == hash(xxl, 0, xxl.high)
doAssert hash(ssl) == hash(ssl, 0, ssl.high)
doAssert hash(xx) == hash(xxl, 0, 4)
block misc:
let
a = [1'u8, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4]
b = [1'i8, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4]
doAssert hash(a) == hash(b)
doAssert hash(a, 2, 5) == hash(b, 2, 5)
static:
block empty:
var
a = ""
b = newSeq[char]()
c = newSeq[int]()
doAssert hash(a) == 0
doAssert hash(b) == 0
doAssert hash(c) == 0
doAssert hashIgnoreCase(a) == 0
doAssert hashIgnoreStyle(a) == 0
block sameButDifferent:
doAssert hash("aa bb aaaa1234") == hash("aa bb aaaa1234", 0, 13)
doAssert hash("aa bb aaaa1234") == hash(cstring"aa bb aaaa1234")
doAssert hashIgnoreCase("aA bb aAAa1234") == hash("aa bb aaaa1234")
doAssert hashIgnoreStyle("aa_bb_AAaa1234") == hashIgnoreCase("aaBBAAAa1234")
block smallSize: # no multibyte hashing
let
xx = @['H','e','l','l','o']
ii = @[72, 101, 108, 108, 111]
ss = "Hello"
doAssert hash(xx) == hash(ii)
doAssert hash(xx) == hash(ss)
doAssert hash(xx) == hash(xx, 0, xx.high)
doAssert hash(ss) == hash(ss, 0, ss.high)
block largeSize: # longer than 8 characters, should trigger multibyte hashing
let
xx = @['H','e','l','l','o']
xxl = @['H','e','l','l','o','w','e','e','n','s']
ssl = "Helloweens"
doAssert hash(xxl) == hash(ssl)
doAssert hash(xxl) == hash(xxl, 0, xxl.high)
doAssert hash(ssl) == hash(ssl, 0, ssl.high)
doAssert hash(xx) == hash(xxl, 0, 4)
block misc:
let
a = [1'u8, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4]
b = [1'i8, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4]
doAssert hash(a) == hash(b)
doAssert hash(a, 2, 5) == hash(b, 2, 5)

0 comments on commit db04f15

Please sign in to comment.