add hash function
This commit is contained in:
parent
c55fc02b24
commit
d6b0dad73e
|
@ -1,4 +1,4 @@
|
|||
import ./ptr_arith, typetraits
|
||||
import ./ptr_arith, typetraits, hashes
|
||||
|
||||
const rangesGCHoldEnabled = not defined(rangesDisableGCHold)
|
||||
const unsafeAPIEnabled = defined(rangesEnableUnsafeAPI)
|
||||
|
@ -50,6 +50,14 @@ 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.start)
|
||||
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
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import
|
||||
unittest,
|
||||
unittest, sets,
|
||||
../ranges/typedranges
|
||||
|
||||
suite "Typed ranges":
|
||||
|
@ -71,3 +71,11 @@ suite "Typed ranges":
|
|||
# check(r[0] == 5) # XXX: Uncomment once nim bug #8044 is fixed
|
||||
r[1] = 10
|
||||
check(r2[1] == 10)
|
||||
|
||||
test "hash function":
|
||||
var a = toRange(@[1,2,3])
|
||||
var b = toRange(@[4,5,6])
|
||||
var c = toRange(@[7,8,9])
|
||||
var x = toSet([a, b, c, a, b])
|
||||
check x.len == 3
|
||||
check a in x
|
||||
|
|
Loading…
Reference in New Issue