parent
5f5ac8ce16
commit
734da9e1df
4 changed files with 14 additions and 11 deletions
|
|
@ -1019,9 +1019,9 @@ when isMainModule and not defined(release):
|
||||||
# --> {1, 3, 5}
|
# --> {1, 3, 5}
|
||||||
|
|
||||||
block toSeqAndString:
|
block toSeqAndString:
|
||||||
var a = toHashSet([2, 4, 5])
|
var a = toHashSet([2, 7, 5])
|
||||||
var b = initHashSet[int]()
|
var b = initHashSet[int]()
|
||||||
for x in [2, 4, 5]: b.incl(x)
|
for x in [2, 7, 5]: b.incl(x)
|
||||||
assert($a == $b)
|
assert($a == $b)
|
||||||
#echo a
|
#echo a
|
||||||
#echo toHashSet(["no", "esc'aping", "is \" provided"])
|
#echo toHashSet(["no", "esc'aping", "is \" provided"])
|
||||||
|
|
|
||||||
|
|
@ -112,29 +112,32 @@ proc hash*[T: proc](x: T): Hash {.inline.} =
|
||||||
else:
|
else:
|
||||||
result = hash(pointer(x))
|
result = hash(pointer(x))
|
||||||
|
|
||||||
|
const
|
||||||
|
prime = uint(11)
|
||||||
|
|
||||||
proc hash*(x: int): Hash {.inline.} =
|
proc hash*(x: int): Hash {.inline.} =
|
||||||
## Efficient hashing of integers.
|
## Efficient hashing of integers.
|
||||||
result = x
|
result = cast[Hash](cast[uint](x) * prime)
|
||||||
|
|
||||||
proc hash*(x: int64): Hash {.inline.} =
|
proc hash*(x: int64): Hash {.inline.} =
|
||||||
## Efficient hashing of `int64` integers.
|
## Efficient hashing of `int64` integers.
|
||||||
result = cast[int](x)
|
result = cast[Hash](cast[uint](x) * prime)
|
||||||
|
|
||||||
proc hash*(x: uint): Hash {.inline.} =
|
proc hash*(x: uint): Hash {.inline.} =
|
||||||
## Efficient hashing of unsigned integers.
|
## Efficient hashing of unsigned integers.
|
||||||
result = cast[int](x)
|
result = cast[Hash](x * prime)
|
||||||
|
|
||||||
proc hash*(x: uint64): Hash {.inline.} =
|
proc hash*(x: uint64): Hash {.inline.} =
|
||||||
## Efficient hashing of `uint64` integers.
|
## Efficient hashing of `uint64` integers.
|
||||||
result = cast[int](x)
|
result = cast[Hash](cast[uint](x) * prime)
|
||||||
|
|
||||||
proc hash*(x: char): Hash {.inline.} =
|
proc hash*(x: char): Hash {.inline.} =
|
||||||
## Efficient hashing of characters.
|
## Efficient hashing of characters.
|
||||||
result = ord(x)
|
result = cast[Hash](cast[uint](ord(x)) * prime)
|
||||||
|
|
||||||
proc hash*[T: Ordinal](x: T): Hash {.inline.} =
|
proc hash*[T: Ordinal](x: T): Hash {.inline.} =
|
||||||
## Efficient hashing of other ordinal types (e.g. enums).
|
## Efficient hashing of other ordinal types (e.g. enums).
|
||||||
result = ord(x)
|
result = cast[Hash](cast[uint](ord(x)) * prime)
|
||||||
|
|
||||||
proc hash*(x: float): Hash {.inline.} =
|
proc hash*(x: float): Hash {.inline.} =
|
||||||
## Efficient hashing of floats.
|
## Efficient hashing of floats.
|
||||||
|
|
|
||||||
|
|
@ -233,7 +233,7 @@ block tablesref:
|
||||||
for y in 0..1:
|
for y in 0..1:
|
||||||
assert t[(x,y)] == $x & $y
|
assert t[(x,y)] == $x & $y
|
||||||
assert($t ==
|
assert($t ==
|
||||||
"{(x: 0, y: 1): \"01\", (x: 0, y: 0): \"00\", (x: 1, y: 0): \"10\", (x: 1, y: 1): \"11\"}")
|
"{(x: 1, y: 1): \"11\", (x: 0, y: 0): \"00\", (x: 0, y: 1): \"01\", (x: 1, y: 0): \"10\"}")
|
||||||
|
|
||||||
block tableTest2:
|
block tableTest2:
|
||||||
var t = newTable[string, float]()
|
var t = newTable[string, float]()
|
||||||
|
|
@ -340,7 +340,7 @@ block tablesref:
|
||||||
block anonZipTest:
|
block anonZipTest:
|
||||||
let keys = @['a','b','c']
|
let keys = @['a','b','c']
|
||||||
let values = @[1, 2, 3]
|
let values = @[1, 2, 3]
|
||||||
doAssert "{'a': 1, 'b': 2, 'c': 3}" == $ toTable zip(keys, values)
|
doAssert "{'c': 3, 'a': 1, 'b': 2}" == $ toTable zip(keys, values)
|
||||||
|
|
||||||
block clearTableTest:
|
block clearTableTest:
|
||||||
var t = newTable[string, float]()
|
var t = newTable[string, float]()
|
||||||
|
|
|
||||||
|
|
@ -48,7 +48,7 @@ block tableTest1:
|
||||||
for y in 0..1:
|
for y in 0..1:
|
||||||
assert t[(x,y)] == $x & $y
|
assert t[(x,y)] == $x & $y
|
||||||
assert($t ==
|
assert($t ==
|
||||||
"{(x: 0, y: 1): \"01\", (x: 0, y: 0): \"00\", (x: 1, y: 0): \"10\", (x: 1, y: 1): \"11\"}")
|
"{(x: 1, y: 1): \"11\", (x: 0, y: 0): \"00\", (x: 0, y: 1): \"01\", (x: 1, y: 0): \"10\"}")
|
||||||
|
|
||||||
block tableTest2:
|
block tableTest2:
|
||||||
var t = initTable[string, float]()
|
var t = initTable[string, float]()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue