fix hash function

This commit is contained in:
andri lim 2018-06-17 07:20:46 +07:00 committed by zah
parent 08478bc225
commit 8ea8a4ff27
2 changed files with 15 additions and 7 deletions

View File

@ -50,13 +50,6 @@ proc toRange*[T](a: seq[T]): Range[T] {.inline.} = toImmutableRange(a)
converter toImmutableRange*[T](a: MutRange[T]): Range[T] {.inline.} = Range[T](a)
proc hash*(x: Range): Hash =
var h: Hash = 0
when rangesGCHoldEnabled:
h = h !& hash(x.gcHold)
h = h !& hash(x.mLen)
result = !$h
proc len*(r: Range): int {.inline.} = int(r.mLen)
proc high*(r: Range): int {.inline.} = r.len - 1
@ -181,3 +174,6 @@ proc `&`*[T](a, b: Range[T]): seq[T] =
result = newSeq[T](a.len + b.len)
copyRange(T, result, 0, a)
copyRange(T, result, a.len, b)
proc hash*(x: Range): Hash =
result = hash(toOpenArray(x))

View File

@ -76,6 +76,8 @@ suite "Typed ranges":
var a = toRange(@[1,2,3])
var b = toRange(@[4,5,6])
var c = toRange(@[7,8,9])
var d = toRange(@[1,2,3,4,5,6,7,8,9])
var e = toRange(@[1,2,3,4,5,6,7,8,9])
var x = toSet([a, b, c, a, b])
check x.len == 3
check a in x
@ -84,3 +86,13 @@ suite "Typed ranges":
var y = toSet([z, b, c])
check z in y
check z in x
var u = d[0..2]
var v = d[3..5]
var uu = e[0..2]
var vv = e[3..5]
check hash(u) != hash(v)
check hash(uu) == hash(u)
check hash(v) == hash(vv)
check hash(uu) != hash(vv)