Merge pull request #2596 from Nycto/devel
Fix floats in tuples in HashSets
This commit is contained in:
commit
d3f69e14f7
2 changed files with 71 additions and 43 deletions
|
|
@ -143,16 +143,23 @@ proc hashIgnoreCase*(x: string): THash =
|
||||||
h = h !& ord(c)
|
h = h !& ord(c)
|
||||||
result = !$h
|
result = !$h
|
||||||
|
|
||||||
|
proc hash*(x: float): THash {.inline.} =
|
||||||
|
var y = x + 1.0
|
||||||
|
result = cast[ptr THash](addr(y))[]
|
||||||
|
|
||||||
|
|
||||||
|
# Forward declarations before methods that hash containers. This allows
|
||||||
|
# containers to contain other containers
|
||||||
|
proc hash*[A](x: openArray[A]): THash
|
||||||
|
proc hash*[A](x: set[A]): THash
|
||||||
|
|
||||||
|
|
||||||
proc hash*[T: tuple](x: T): THash =
|
proc hash*[T: tuple](x: T): THash =
|
||||||
## efficient hashing of tuples.
|
## efficient hashing of tuples.
|
||||||
for f in fields(x):
|
for f in fields(x):
|
||||||
result = result !& hash(f)
|
result = result !& hash(f)
|
||||||
result = !$result
|
result = !$result
|
||||||
|
|
||||||
proc hash*(x: float): THash {.inline.} =
|
|
||||||
var y = x + 1.0
|
|
||||||
result = cast[ptr THash](addr(y))[]
|
|
||||||
|
|
||||||
proc hash*[A](x: openArray[A]): THash =
|
proc hash*[A](x: openArray[A]): THash =
|
||||||
for it in items(x): result = result !& hash(it)
|
for it in items(x): result = result !& hash(it)
|
||||||
result = !$result
|
result = !$result
|
||||||
|
|
@ -160,3 +167,4 @@ proc hash*[A](x: openArray[A]): THash =
|
||||||
proc hash*[A](x: set[A]): THash =
|
proc hash*[A](x: set[A]): THash =
|
||||||
for it in items(x): result = result !& hash(it)
|
for it in items(x): result = result !& hash(it)
|
||||||
result = !$result
|
result = !$result
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,37 @@
|
||||||
discard """
|
|
||||||
output: '''true
|
|
||||||
true'''
|
|
||||||
"""
|
|
||||||
|
|
||||||
import sets
|
import sets
|
||||||
var
|
|
||||||
a = initSet[int]()
|
|
||||||
b = initSet[int]()
|
|
||||||
c = initSet[string]()
|
|
||||||
|
|
||||||
for i in 0..5: a.incl(i)
|
block setEquality:
|
||||||
for i in 1..6: b.incl(i)
|
var
|
||||||
for i in 0..5: c.incl($i)
|
a = initSet[int]()
|
||||||
|
b = initSet[int]()
|
||||||
|
c = initSet[string]()
|
||||||
|
|
||||||
|
for i in 0..5: a.incl(i)
|
||||||
|
for i in 1..6: b.incl(i)
|
||||||
|
for i in 0..5: c.incl($i)
|
||||||
|
|
||||||
|
doAssert map(a, proc(x: int): int = x + 1) == b
|
||||||
|
doAssert map(a, proc(x: int): string = $x) == c
|
||||||
|
|
||||||
|
|
||||||
|
block setsContainingTuples:
|
||||||
|
var set = initSet[tuple[i: int, i64: int64, f: float]]()
|
||||||
|
set.incl( (i: 123, i64: 123'i64, f: 3.14) )
|
||||||
|
doAssert set.contains( (i: 123, i64: 123'i64, f: 3.14) )
|
||||||
|
doAssert( not set.contains( (i: 456, i64: 789'i64, f: 2.78) ) )
|
||||||
|
|
||||||
|
|
||||||
|
block setWithTuplesWithSeqs:
|
||||||
|
var s = initSet[tuple[s: seq[int]]]()
|
||||||
|
s.incl( (s: @[1, 2, 3]) )
|
||||||
|
doAssert s.contains( (s: @[1, 2, 3]) )
|
||||||
|
doAssert( not s.contains((s: @[4, 5, 6])) )
|
||||||
|
|
||||||
|
|
||||||
|
block setWithSequences:
|
||||||
|
var s = initSet[seq[int]]()
|
||||||
|
s.incl( @[1, 2, 3] )
|
||||||
|
doAssert s.contains(@[1, 2, 3])
|
||||||
|
doAssert( not s.contains(@[4, 5, 6]) )
|
||||||
|
|
||||||
|
|
||||||
echo map(a, proc(x: int): int = x + 1) == b
|
|
||||||
echo map(a, proc(x: int): string = $x) == c
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue