summary refs log tree commit diff stats
path: root/lib/std
diff options
context:
space:
mode:
authorJason Beetham <beefers331@gmail.com>2021-02-18 18:33:28 -0700
committerGitHub <noreply@github.com>2021-02-18 17:33:28 -0800
commit35ded020748641379f68adf68c23d1f0aa167c2a (patch)
treea21af43dd37152fcb57ec33eb7e720af4c0d997c /lib/std
parent148e5ba2a5e51f19c51e2c11150e6986c04990ba (diff)
downloadNim-35ded020748641379f68adf68c23d1f0aa167c2a.tar.gz
Add setutils.complement, setutils.fullSet (#17066)
Diffstat (limited to 'lib/std')
-rw-r--r--lib/std/setutils.nim25
1 files changed, 24 insertions, 1 deletions
diff --git a/lib/std/setutils.nim b/lib/std/setutils.nim
index 215c7a76a..74f0dc819 100644
--- a/lib/std/setutils.nim
+++ b/lib/std/setutils.nim
@@ -14,7 +14,7 @@
 ## * `std/packedsets <packedsets.html>`_
 ## * `std/sets <sets.html>`_
 
-import std/typetraits
+import std/[typetraits, macros]
 
 #[
   type SetElement* = char|byte|bool|int16|uint16|enum|uint8|int8
@@ -35,3 +35,26 @@ template toSet*(iter: untyped): untyped =
   for x in iter:
     incl(result, x)
   result
+
+macro enmRange(enm: typed): untyped = result = newNimNode(nnkCurly).add(enm.getType[1][1..^1])
+
+proc fullSet*(T: typedesc): auto {.inline.} =
+  ## Returns a full set of all valid elements.
+  runnableExamples:
+    assert {true, false} == fullSet(bool)
+  when T is Ordinal:
+    {T.low..T.high}
+  else: # Hole filled enum
+    enmRange(T)
+
+proc complement*[T](s: set[T]): set[T] {.inline.} =
+  ## Returns the complement of the set.
+  ## Can also be thought of as inverting the set.
+  runnableExamples:
+    type Colors = enum
+      red, green = 3, blue
+    assert complement({red, blue}) == {green}
+    assert complement({red, green, blue}).card == 0
+    assert complement({range[0..10](0), 1, 2, 3}) == {range[0..10](4), 5, 6, 7, 8, 9, 10}
+    assert complement({'0'..'9'}) == {0.char..255.char} - {'0'..'9'}
+  fullSet(T) - s