Add setutils.complement, setutils.fullSet (#17066)
This commit is contained in:
parent
148e5ba2a5
commit
35ded02074
3 changed files with 55 additions and 9 deletions
|
|
@ -75,8 +75,11 @@
|
||||||
- `strscans.scanf` now supports parsing single characters.
|
- `strscans.scanf` now supports parsing single characters.
|
||||||
- `strscans.scanTuple` added which uses `strscans.scanf` internally, returning a tuple which can be unpacked for easier usage of `scanf`.
|
- `strscans.scanTuple` added which uses `strscans.scanf` internally, returning a tuple which can be unpacked for easier usage of `scanf`.
|
||||||
|
|
||||||
- Added `setutils.toSet` that can take any iterable and convert it to a built-in set,
|
- Added `setutils.toSet` that can take any iterable and convert it to a built-in `set`,
|
||||||
if the iterable yields a built-in settable type.
|
if the iterable yields a built-in settable type.
|
||||||
|
- Added `setutils.fullSet` which returns a full built-in `set` for a valid type.
|
||||||
|
- Added `setutils.complement` which returns the complement of a built-in `set`.
|
||||||
|
|
||||||
|
|
||||||
- Added `math.isNaN`.
|
- Added `math.isNaN`.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@
|
||||||
## * `std/packedsets <packedsets.html>`_
|
## * `std/packedsets <packedsets.html>`_
|
||||||
## * `std/sets <sets.html>`_
|
## * `std/sets <sets.html>`_
|
||||||
|
|
||||||
import std/typetraits
|
import std/[typetraits, macros]
|
||||||
|
|
||||||
#[
|
#[
|
||||||
type SetElement* = char|byte|bool|int16|uint16|enum|uint8|int8
|
type SetElement* = char|byte|bool|int16|uint16|enum|uint8|int8
|
||||||
|
|
@ -35,3 +35,26 @@ template toSet*(iter: untyped): untyped =
|
||||||
for x in iter:
|
for x in iter:
|
||||||
incl(result, x)
|
incl(result, x)
|
||||||
result
|
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
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,34 @@
|
||||||
discard """
|
discard """
|
||||||
targets: "c js"
|
targets: "c js"
|
||||||
|
disabled: "bsd" # pending #17093
|
||||||
"""
|
"""
|
||||||
import std/setutils
|
import std/setutils
|
||||||
|
type
|
||||||
|
Colors = enum
|
||||||
|
red, green = 5, blue = 10
|
||||||
|
Bar = enum
|
||||||
|
bar0 = -1, bar1, bar2
|
||||||
template main =
|
template main =
|
||||||
doAssert "abcbb".toSet == {'a', 'b', 'c'}
|
block: # toSet
|
||||||
doAssert toSet([10u8, 12, 13]) == {10u8, 12, 13}
|
doAssert "abcbb".toSet == {'a', 'b', 'c'}
|
||||||
doAssert toSet(0u16..30) == {0u16..30}
|
doAssert toSet([10u8, 12, 13]) == {10u8, 12, 13}
|
||||||
type A = distinct char
|
doAssert toSet(0u16..30) == {0u16..30}
|
||||||
doAssert [A('x')].toSet == {A('x')}
|
type A = distinct char
|
||||||
|
doAssert [A('x')].toSet == {A('x')}
|
||||||
|
|
||||||
|
block: # fullSet
|
||||||
|
doAssert fullSet(Colors) == {red, green, blue}
|
||||||
|
doAssert fullSet(char) == {0.chr..255.chr}
|
||||||
|
doAssert fullSet(Bar) == {bar0, bar1, bar2}
|
||||||
|
doAssert fullSet(bool) == {true, false}
|
||||||
|
|
||||||
|
block: # complement
|
||||||
|
doAssert {red, blue}.complement == {green}
|
||||||
|
doAssert (complement {red, green, blue}).card == 0
|
||||||
|
doAssert (complement {false}) == {true}
|
||||||
|
doAssert {bar0}.complement == {bar1, bar2}
|
||||||
|
doAssert {range[0..10](0), 1, 2, 3}.complement == {range[0..10](4), 5, 6, 7, 8, 9, 10}
|
||||||
|
doAssert {'0'..'9'}.complement == {0.char..255.char} - {'0'..'9'}
|
||||||
|
|
||||||
main()
|
main()
|
||||||
static: main()
|
static: main()
|
||||||
Loading…
Add table
Add a link
Reference in a new issue