From af591544c59e9b05e539f4583f1143776bb8a9e4 Mon Sep 17 00:00:00 2001 From: Lolo Iccl Date: Tue, 8 May 2018 22:09:37 +0900 Subject: [PATCH] Modify hash for HashSet to use `xor` to mix hash of items. --- lib/pure/collections/sets.nim | 7 ++----- tests/collections/tsets.nim | 4 ---- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/lib/pure/collections/sets.nim b/lib/pure/collections/sets.nim index 94bdbb8609..59c90bc2ba 100644 --- a/lib/pure/collections/sets.nim +++ b/lib/pure/collections/sets.nim @@ -18,7 +18,7 @@ ## that ``=`` performs a copy of the set. import - hashes, math, algorithm + hashes, math {.pragma: myShallow.} when not defined(nimhygiene): @@ -123,11 +123,8 @@ iterator items*[A](s: HashSet[A]): A = proc hash*[A](s: HashSet[A]): Hash = ## hashing of HashSet assert s.isValid, "The set needs to be initialized." - var hcs: seq[Hash] for h in 0..high(s.data): - hcs.add(s.data[h].hcode) - for hc in sorted(hcs, cmp[int]): - result = result !& hc + result = result xor s.data[h].hcode result = !$result const diff --git a/tests/collections/tsets.nim b/tests/collections/tsets.nim index be624ffe87..61e14260af 100644 --- a/tests/collections/tsets.nim +++ b/tests/collections/tsets.nim @@ -85,10 +85,6 @@ block hashForHashedSet: var hashSeq: seq[Hash] = @[] doAssert s1 == 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: let