Merge pull request #859 from reactormonk/sets_equivalence
sets equivalence
This commit is contained in:
commit
925d19a8fb
2 changed files with 32 additions and 0 deletions
|
|
@ -224,3 +224,20 @@ proc toOrderedSet*[A](keys: openArray[A]): TOrderedSet[A] =
|
||||||
proc `$`*[A](s: TOrderedSet[A]): string =
|
proc `$`*[A](s: TOrderedSet[A]): string =
|
||||||
## The `$` operator for ordered hash sets.
|
## The `$` operator for ordered hash sets.
|
||||||
dollarImpl()
|
dollarImpl()
|
||||||
|
|
||||||
|
proc `<`*[A](s, t: TSet[A]): bool =
|
||||||
|
## Is s a strict subset of t?
|
||||||
|
s.counter != t.counter and s <= t
|
||||||
|
|
||||||
|
proc `<=`*[A](s, t: TSet[A]): bool =
|
||||||
|
## Is s a subset of t?
|
||||||
|
result = false
|
||||||
|
if s.counter > t.counter: return
|
||||||
|
result = true
|
||||||
|
for item in s:
|
||||||
|
if not(t.contains(item)):
|
||||||
|
result = false
|
||||||
|
return
|
||||||
|
|
||||||
|
proc `==`*[A](s, t: TSet[A]): bool =
|
||||||
|
s.counter == t.counter and s <= t
|
||||||
|
|
|
||||||
15
tests/sets/testequivalence.nim
Normal file
15
tests/sets/testequivalence.nim
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
discard """
|
||||||
|
output: ''''''
|
||||||
|
"""
|
||||||
|
import unittest
|
||||||
|
import sets
|
||||||
|
|
||||||
|
doAssert(toSet(@[1,2,3]) <= toSet(@[1,2,3,4]), "equivalent or subset")
|
||||||
|
doAssert(toSet(@[1,2,3]) <= toSet(@[1,2,3]), "equivalent or subset")
|
||||||
|
doAssert((not(toSet(@[1,2,3]) <= toSet(@[1,2]))), "equivalent or subset")
|
||||||
|
doAssert(toSet(@[1,2,3]) <= toSet(@[1,2,3,4]), "strict subset")
|
||||||
|
doAssert((not(toSet(@[1,2,3]) < toSet(@[1,2,3]))), "strict subset")
|
||||||
|
doAssert((not(toSet(@[1,2,3]) < toSet(@[1,2]))), "strict subset")
|
||||||
|
doAssert((not(toSet(@[1,2,3]) == toSet(@[1,2,3,4]))), "==")
|
||||||
|
doAssert(toSet(@[1,2,3]) == toSet(@[1,2,3]), "==")
|
||||||
|
doAssert((not(toSet(@[1,2,3]) == toSet(@[1,2]))), "==")
|
||||||
Loading…
Add table
Add a link
Reference in a new issue