enable bsd for tsetutils; improve setutils API (#17098)

This commit is contained in:
Timothee Cour 2021-02-20 09:02:57 -08:00 • committed by GitHub
commit 6b3bdd798d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 7 deletions

View file

@ -38,18 +38,21 @@ template toSet*(iter: untyped): untyped =
macro enmRange(enm: typed): untyped = result = newNimNode(nnkCurly).add(enm.getType[1][1..^1]) macro enmRange(enm: typed): untyped = result = newNimNode(nnkCurly).add(enm.getType[1][1..^1])
proc fullSet*(T: typedesc): auto {.inline.} = # proc fullSet*(T: typedesc): set[T] {.inline.} = # xxx would give: Error: ordinal type expected
## Returns a full set of all valid elements. proc fullSet*[T](U: typedesc[T]): set[T] {.inline.} =
## Returns a set containing all elements in `U`.
runnableExamples: 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: when T is Ordinal:
{T.low..T.high} {T.low..T.high}
else: # Hole filled enum else: # Hole filled enum
enmRange(T) enmRange(T)
proc complement*[T](s: set[T]): set[T] {.inline.} = proc complement*[T](s: set[T]): set[T] {.inline.} =
## Returns the complement of the set. ## Returns the set complement of `a`.
## Can also be thought of as inverting the set.
runnableExamples: runnableExamples:
type Colors = enum type Colors = enum
red, green = 3, blue red, green = 3, blue

View file

@ -1,13 +1,15 @@
discard """ discard """
targets: "c js" targets: "c js"
disabled: "bsd" # pending #17093
""" """
import std/setutils import std/setutils
type type
Colors = enum Colors = enum
red, green = 5, blue = 10 red, green = 5, blue = 10
Bar = enum Bar = enum
bar0 = -1, bar1, bar2 bar0 = -1, bar1, bar2
template main = template main =
block: # toSet block: # toSet
doAssert "abcbb".toSet == {'a', 'b', 'c'} doAssert "abcbb".toSet == {'a', 'b', 'c'}
@ -31,4 +33,4 @@ template main =
doAssert {'0'..'9'}.complement == {0.char..255.char} - {'0'..'9'} doAssert {'0'..'9'}.complement == {0.char..255.char} - {'0'..'9'}
main() main()
static: main() static: main()