Modify previous commit and add tests
This commit is contained in:
parent
ee8313da3f
commit
5c7b66e07a
2 changed files with 55 additions and 4 deletions
|
|
@ -18,7 +18,7 @@
|
||||||
## that ``=`` performs a copy of the set.
|
## that ``=`` performs a copy of the set.
|
||||||
|
|
||||||
import
|
import
|
||||||
hashes, math
|
hashes, math, algorithm
|
||||||
|
|
||||||
{.pragma: myShallow.}
|
{.pragma: myShallow.}
|
||||||
when not defined(nimhygiene):
|
when not defined(nimhygiene):
|
||||||
|
|
@ -123,8 +123,11 @@ 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):
|
||||||
result = result !& s.data[h].hcode
|
hcs.add(s.data[h].hcode)
|
||||||
|
for hc in sorted(hcs, cmp[int]):
|
||||||
|
result = result !& hc
|
||||||
result = !$result
|
result = !$result
|
||||||
|
|
||||||
const
|
const
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,6 @@
|
||||||
import sets
|
import sets
|
||||||
|
import hashes
|
||||||
|
import algorithm
|
||||||
|
|
||||||
block setEquality:
|
block setEquality:
|
||||||
var
|
var
|
||||||
|
|
@ -35,7 +37,7 @@ block setWithSequences:
|
||||||
doAssert( not s.contains(@[4, 5, 6]) )
|
doAssert( not s.contains(@[4, 5, 6]) )
|
||||||
|
|
||||||
block setClearWorked:
|
block setClearWorked:
|
||||||
var s = initSet[char]()
|
var s = initSet[char]()
|
||||||
|
|
||||||
for c in "this is a test":
|
for c in "this is a test":
|
||||||
s.incl(c)
|
s.incl(c)
|
||||||
|
|
@ -68,12 +70,58 @@ block orderedSetClearWorked:
|
||||||
for c in "eat at joes":
|
for c in "eat at joes":
|
||||||
s.incl(c)
|
s.incl(c)
|
||||||
|
|
||||||
r = ""
|
r = ""
|
||||||
for c in items(s):
|
for c in items(s):
|
||||||
add(r, c)
|
add(r, c)
|
||||||
|
|
||||||
doAssert r == "zeat jos"
|
doAssert r == "zeat jos"
|
||||||
|
|
||||||
|
block hashForHashedSet:
|
||||||
|
let
|
||||||
|
seq1 = "This is the test."
|
||||||
|
seq2 = "the test is This."
|
||||||
|
s1 = seq1.toSet()
|
||||||
|
s2 = seq2.toSet()
|
||||||
|
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
|
||||||
|
str = "This is the test."
|
||||||
|
rstr = str.reversed
|
||||||
|
|
||||||
|
var
|
||||||
|
s1 = initOrderedSet[char]()
|
||||||
|
s2 = initOrderedSet[char]()
|
||||||
|
r = initOrderedSet[char]()
|
||||||
|
expected: Hash
|
||||||
|
added: seq[char] = @[]
|
||||||
|
reversed: Hash
|
||||||
|
radded: seq[char] = @[]
|
||||||
|
|
||||||
|
expected = 0
|
||||||
|
for c in str:
|
||||||
|
if (not (c in added)):
|
||||||
|
expected = expected !& hash(c)
|
||||||
|
added.add(c)
|
||||||
|
s1.incl(c)
|
||||||
|
s2.incl(c)
|
||||||
|
expected = !$expected
|
||||||
|
doAssert hash(s1) == expected
|
||||||
|
doAssert hash(s1) == hash(s2)
|
||||||
|
doAssert hash(s1) != hash(r)
|
||||||
|
|
||||||
|
reversed = 0
|
||||||
|
for c in rstr:
|
||||||
|
if (not (c in radded)):
|
||||||
|
reversed = reversed !& hash(c)
|
||||||
|
radded.add(c)
|
||||||
|
r.incl(c)
|
||||||
|
reversed = !$reversed
|
||||||
|
doAssert hash(r) == reversed
|
||||||
|
doAssert hash(s1) != reversed
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue