summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--lib/pure/collections/sets.nim17
-rw-r--r--tests/sets/testequivalence.nim16
2 files changed, 33 insertions, 0 deletions
diff --git a/lib/pure/collections/sets.nim b/lib/pure/collections/sets.nim
index 7259772aa..e478a2ce1 100644
--- a/lib/pure/collections/sets.nim
+++ b/lib/pure/collections/sets.nim
@@ -224,3 +224,20 @@ proc toOrderedSet*[A](keys: openArray[A]): TOrderedSet[A] =
 proc `$`*[A](s: TOrderedSet[A]): string =
   ## The `$` operator for ordered hash sets.
   dollarImpl()
+
+proc `<`*[A](s, t: TSet[A]): bool =
+  ## Is a a strict subset of b?
+  s.counter != t.counter and s <= t
+
+proc `<=`*[A](s, t: TSet[A]): bool =
+  ## Is a a subset of b?
+  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
diff --git a/tests/sets/testequivalence.nim b/tests/sets/testequivalence.nim
new file mode 100644
index 000000000..a1e02fee7
--- /dev/null
+++ b/tests/sets/testequivalence.nim
@@ -0,0 +1,16 @@
+import unittest
+import sets
+
+suite "sets":
+  test "equivalent or subset":
+    check toSet(@[1,2,3]) <= toSet(@[1,2,3,4])
+    check toSet(@[1,2,3]) <= toSet(@[1,2,3])
+    check(not(toSet(@[1,2,3]) <= toSet(@[1,2])))
+  test "strict subset":
+    check toSet(@[1,2,3]) <= toSet(@[1,2,3,4])
+    check(not(toSet(@[1,2,3]) < toSet(@[1,2,3])))
+    check(not(toSet(@[1,2,3]) < toSet(@[1,2])))
+  test "==":
+    check(not(toSet(@[1,2,3]) == toSet(@[1,2,3,4])))
+    check toSet(@[1,2,3]) == toSet(@[1,2,3])
+    check(not(toSet(@[1,2,3]) == toSet(@[1,2])))