diff options
author | Timothee Cour <timothee.cour2@gmail.com> | 2021-03-08 01:39:39 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-08 10:39:39 +0100 |
commit | 93cb5d68962155c833b7ec9611c9a6953dcdaf8c (patch) | |
tree | 69fdacc453f2727d02813e2c263dea0f13b6df5f /lib/std | |
parent | 0234b479ab921e410a275db667be2bb913376690 (diff) | |
download | Nim-93cb5d68962155c833b7ec9611c9a6953dcdaf8c.tar.gz |
add setutils.[]= (#17272)
* add setutils.[]= * address comments * proc => func (for other symbols too)
Diffstat (limited to 'lib/std')
-rw-r--r-- | lib/std/setutils.nim | 20 |
1 files changed, 17 insertions, 3 deletions
diff --git a/lib/std/setutils.nim b/lib/std/setutils.nim index d5d60d4ab..c7fac0a54 100644 --- a/lib/std/setutils.nim +++ b/lib/std/setutils.nim @@ -38,8 +38,8 @@ template toSet*(iter: untyped): untyped = macro enmRange(enm: typed): untyped = result = newNimNode(nnkCurly).add(enm.getType[1][1..^1]) -# proc fullSet*(T: typedesc): set[T] {.inline.} = # xxx would give: Error: ordinal type expected -proc fullSet*[T](U: typedesc[T]): set[T] {.inline.} = +# func fullSet*(T: typedesc): set[T] {.inline.} = # xxx would give: Error: ordinal type expected +func fullSet*[T](U: typedesc[T]): set[T] {.inline.} = ## Returns a set containing all elements in `U`. runnableExamples: assert bool.fullSet == {true, false} @@ -51,7 +51,7 @@ proc fullSet*[T](U: typedesc[T]): set[T] {.inline.} = else: # Hole filled enum enmRange(T) -proc complement*[T](s: set[T]): set[T] {.inline.} = +func complement*[T](s: set[T]): set[T] {.inline.} = ## Returns the set complement of `a`. runnableExamples: type Colors = enum @@ -61,3 +61,17 @@ proc complement*[T](s: set[T]): set[T] {.inline.} = 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 + +func `[]=`*[T](t: var set[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 |