diff --git a/ranges/typedranges.nim b/ranges/typedranges.nim index af17402..4dd3e2e 100644 --- a/ranges/typedranges.nim +++ b/ranges/typedranges.nim @@ -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)) diff --git a/tests/ttypedranges.nim b/tests/ttypedranges.nim index bf15d25..b2ea9df 100644 --- a/tests/ttypedranges.nim +++ b/tests/ttypedranges.nim @@ -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) + \ No newline at end of file