Modify hash for HashSet to use xor to mix hash of items.
This commit is contained in:
parent
5c7b66e07a
commit
af591544c5
2 changed files with 2 additions and 9 deletions
|
|
@ -18,7 +18,7 @@
|
||||||
## that ``=`` performs a copy of the set.
|
## that ``=`` performs a copy of the set.
|
||||||
|
|
||||||
import
|
import
|
||||||
hashes, math, algorithm
|
hashes, math
|
||||||
|
|
||||||
{.pragma: myShallow.}
|
{.pragma: myShallow.}
|
||||||
when not defined(nimhygiene):
|
when not defined(nimhygiene):
|
||||||
|
|
@ -123,11 +123,8 @@ iterator items*[A](s: HashSet[A]): A =
|
||||||
proc hash*[A](s: HashSet[A]): Hash =
|
proc hash*[A](s: HashSet[A]): Hash =
|
||||||
## hashing of HashSet
|
## hashing of HashSet
|
||||||
assert s.isValid, "The set needs to be initialized."
|
assert s.isValid, "The set needs to be initialized."
|
||||||
var hcs: seq[Hash]
|
|
||||||
for h in 0..high(s.data):
|
for h in 0..high(s.data):
|
||||||
hcs.add(s.data[h].hcode)
|
result = result xor s.data[h].hcode
|
||||||
for hc in sorted(hcs, cmp[int]):
|
|
||||||
result = result !& hc
|
|
||||||
result = !$result
|
result = !$result
|
||||||
|
|
||||||
const
|
const
|
||||||
|
|
|
||||||
|
|
@ -85,10 +85,6 @@ block hashForHashedSet:
|
||||||
var hashSeq: seq[Hash] = @[]
|
var hashSeq: seq[Hash] = @[]
|
||||||
doAssert s1 == s2
|
doAssert s1 == s2
|
||||||
doAssert hash(s1) == hash(s2)
|
doAssert hash(s1) == hash(s2)
|
||||||
for c in seq1:
|
|
||||||
if (not (hash(c) in hashSeq)):
|
|
||||||
hashSeq.add(hash(c))
|
|
||||||
doAssert hash(s1) == hash(sorted(hashSeq, cmp[Hash]))
|
|
||||||
|
|
||||||
block hashForOrderdSet:
|
block hashForOrderdSet:
|
||||||
let
|
let
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue