Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions lib/std/setutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,21 @@ template toSet*(iter: untyped): untyped =

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.
# proc fullSet*(T: typedesc): set[T] {.inline.} = # xxx would give: Error: ordinal type expected
proc fullSet*[T](U: typedesc[T]): set[T] {.inline.} =
## Returns a set containing all elements in `U`.
runnableExamples:
assert {true, false} == fullSet(bool)
assert bool.fullSet == {true, false}
type A = range[1..3]
assert A.fullSet == {1.A, 2, 3}
assert int8.fullSet.len == 256
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.
## Returns the set complement of `a`.
runnableExamples:
type Colors = enum
red, green = 3, blue
Expand Down
6 changes: 4 additions & 2 deletions tests/stdlib/tsetutils.nim
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
discard """
targets: "c js"
disabled: "bsd" # pending #17093
"""

import std/setutils

type
Colors = enum
red, green = 5, blue = 10
Bar = enum
bar0 = -1, bar1, bar2

template main =
block: # toSet
doAssert "abcbb".toSet == {'a', 'b', 'c'}
Expand All @@ -31,4 +33,4 @@ template main =
doAssert {'0'..'9'}.complement == {0.char..255.char} - {'0'..'9'}

main()
static: main()
static: main()