- Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathsetutils.nim
105 lines (93 loc) · 3.34 KB
/
setutils.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#
#
# Nim's Runtime Library
# (c) Copyright 2020 Nim Contributors
#
# See the file "copying.txt", included in this
# distribution, for details about the copyright.
#
## This module adds functionality for the built-in `set` type.
##
## See also
## ========
## * `std/packedsets <packedsets.html>`_
## * `std/sets <sets.html>`_
import std/[typetraits, macros]
#[
type SetElement* = char|byte|bool|int16|uint16|enum|uint8|int8
## The allowed types of a built-in set.
]#
templatetoSet*(iter: untyped): untyped=
## Returns a built-in set from the elements of the iterable `iter`.
runnableExamples:
assert"helloWorld".toSet == {'W', 'd', 'e', 'h', 'l', 'o', 'r'}
asserttoSet([10u16, 20, 30]) == {10u16, 20, 30}
assert [30u8, 100, 10].toSet == {10u8, 30, 100}
asserttoSet(@[1321i16, 321, 90]) == {90i16, 321, 1321}
asserttoSet([false]) == {false}
asserttoSet(0u8..10) == {0u8..10}
varresult: set[elementType(iter)]
for x in iter:
incl(result, x)
result
macroenumElementsAsSet(enm: typed): untyped=result=newNimNode(nnkCurly).add(enm.getType[1][1..^1])
# func fullSet*(T: typedesc): set[T] {.inline.} = # xxx would give: Error: ordinal type expected
funcfullSet*[T](U: typedesc[T]): set[T] {.inline.} =
## Returns a set containing all elements in `U`.
runnableExamples:
assertbool.fullSet == {true, false}
type A =range[1..3]
assert A.fullSet == {1.A, 2, 3}
assertint8.fullSet.len ==256
when T isOrdinal:
{T.low..T.high}
else: # Hole filled enum
enumElementsAsSet(T)
funccomplement*[T](s: set[T]): set[T] {.inline.} =
## Returns the set complement of `a`.
runnableExamples:
typeColors=enum
red, green =3, blue
assertcomplement({red, blue}) == {green}
assertcomplement({red, green, blue}).card ==0
assertcomplement({range[0..10](0), 1, 2, 3}) == {range[0..10](4), 5, 6, 7, 8, 9, 10}
assertcomplement({'0'..'9'}) == {0.char..255.char} - {'0'..'9'}
fullSet(T) - s
func`[]=`*[T](t: varset[T], key: T, val: bool) {.inline.} =
## Syntax sugar for `if val: t.incl key else: t.excl key`
runnableExamples:
type A =enum
a0, a1, a2, a3
var s = {a0, a3}
s[a0] =false
s[a1] =false
assert s == {a3}
s[a2] =true
s[a3] =true
assert s == {a2, a3}
if val: t.incl key else: t.excl key
whendefined(nimHasXorSet):
funcsymmetricDifference*[T](x, y: set[T]): set[T] {.magic: "XorSet".} =
## This operator computes the symmetric difference of two sets,
## equivalent to but more efficient than `x + y - x * y` or
## `(x - y) + (y - x)`.
runnableExamples:
assertsymmetricDifference({1, 2, 3}, {2, 3, 4}) == {1, 4}
else:
funcsymmetricDifference*[T](x, y: set[T]): set[T] {.inline.} =
result= x + y - (x * y)
proc`-+-`*[T](x, y: set[T]): set[T] {.inline.} =
## Operator alias for `symmetricDifference`.
runnableExamples:
assert {1, 2, 3} -+- {2, 3, 4} == {1, 4}
result=symmetricDifference(x, y)
proctoggle*[T](x: varset[T], y: set[T]) {.inline.} =
## Toggles the existence of each value of `y` in `x`.
## If any element in `y` is also in `x`, it is excluded from `x`;
## otherwise it is included.
## Equivalent to `x = symmetricDifference(x, y)`.
runnableExamples:
var x = {1, 2, 3}
x.toggle({2, 3, 4})
assert x == {1, 4}
x =symmetricDifference(x, y)