Source Edit

This module adds functionality for the built-in set type.

See also

Imports

typetraits, macros

Procs

  1. func `[]=`[T](t: var set[T]; key: T; val: bool) {.inline.}

Syntax sugar for if val: t.incl key else: t.excl key

Example:

  1. type A = enum
  2. a0, a1, a2, a3
  3. var s = {a0, a3}
  4. s[a0] = false
  5. s[a1] = false
  6. assert s == {a3}
  7. s[a2] = true
  8. s[a3] = true
  9. assert s == {a2, a3}

Source Edit

  1. func complement[T](s: set[T]): set[T] {.inline.}

Returns the set complement of a.

Example:

  1. type Colors = enum
  2. red, green = 3, blue
  3. assert complement({red, blue}) == {green}
  4. assert complement({red, green, blue}).card == 0
  5. assert complement({range[0..10](0), 1, 2, 3}) == {range[0..10](4), 5, 6, 7, 8, 9, 10}
  6. assert complement({'0'..'9'}) == {0.char..255.char} - {'0'..'9'}

Source Edit

  1. func fullSet[T](U: typedesc[T]): set[T] {.inline.}

Returns a set containing all elements in U.

Example:

  1. assert bool.fullSet == {true, false}
  2. type A = range[1..3]
  3. assert A.fullSet == {1.A, 2, 3}
  4. assert int8.fullSet.len == 256

Source Edit

Templates

  1. template toSet(iter: untyped): untyped

Returns a built-in set from the elements of the iterable iter.

Example:

  1. assert "helloWorld".toSet == {'W', 'd', 'e', 'h', 'l', 'o', 'r'}
  2. assert toSet([10u16, 20, 30]) == {10u16, 20, 30}
  3. assert [30u8, 100, 10].toSet == {10u8, 30, 100}
  4. assert toSet(@[1321i16, 321, 90]) == {90i16, 321, 1321}
  5. assert toSet([false]) == {false}
  6. assert toSet(0u8..10) == {0u8..10}

Source Edit