sets_fragment.txt 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. The set type models the mathematical notion of a set. The set's basetype can
  2. only be an ordinal type of a certain size, namely:
  3. * `int8`-`int16`
  4. * `uint8`/`byte`-`uint16`
  5. * `char`
  6. * `enum`
  7. * Ordinal subrange types, i.e. `range[-10..10]`
  8. or equivalent. When constructing a set with signed integer literals, the set's
  9. base type is defined to be in the range `0 .. DefaultSetElements-1` where
  10. `DefaultSetElements` is currently always 2^8. The maximum range length for the
  11. base type of a set is `MaxSetElements` which is currently always 2^16. Types
  12. with a bigger range length are coerced into the range `0 .. MaxSetElements-1`.
  13. The reason is that sets are implemented as high performance bit vectors.
  14. Attempting to declare a set with a larger type will result in an error:
  15. ```nim
  16. var s: set[int64] # Error: set is too large; use `std/sets` for ordinal types
  17. # with more than 2^16 elements
  18. ```
  19. **Note:** Nim also offers [hash sets](sets.html) (which you need to import
  20. with `import std/sets`), which have no such restrictions.
  21. Sets can be constructed via the set constructor: `{}` is the empty set. The
  22. empty set is type compatible with any concrete set type. The constructor
  23. can also be used to include elements (and ranges of elements):
  24. ```nim
  25. type
  26. CharSet = set[char]
  27. var
  28. x: CharSet
  29. x = {'a'..'z', '0'..'9'} # This constructs a set that contains the
  30. # letters from 'a' to 'z' and the digits
  31. # from '0' to '9'
  32. ```
  33. The module [`std/setutils`](setutils.html) provides a way to initialize a set from an iterable:
  34. ```nim
  35. import std/setutils
  36. let uniqueChars = myString.toSet
  37. ```
  38. These operations are supported by sets:
  39. ================== ========================================================
  40. operation meaning
  41. ================== ========================================================
  42. `A + B` union of two sets
  43. `A * B` intersection of two sets
  44. `A - B` difference of two sets (A without B's elements)
  45. `A == B` set equality
  46. `A <= B` subset relation (A is subset of B or equal to B)
  47. `A < B` strict subset relation (A is a proper subset of B)
  48. `e in A` set membership (A contains element e)
  49. `e notin A` A does not contain element e
  50. `contains(A, e)` A contains element e
  51. `card(A)` the cardinality of A (number of elements in A)
  52. `incl(A, elem)` same as `A = A + {elem}`
  53. `excl(A, elem)` same as `A = A - {elem}`
  54. ================== ========================================================
  55. ### Bit fields
  56. Sets are often used to define a type for the *flags* of a procedure.
  57. This is a cleaner (and type safe) solution than defining integer
  58. constants that have to be `or`'ed together.
  59. Enum, sets and casting can be used together as in:
  60. ```nim
  61. type
  62. MyFlag* {.size: sizeof(cint).} = enum
  63. A
  64. B
  65. C
  66. D
  67. MyFlags = set[MyFlag]
  68. proc toNum(f: MyFlags): int = cast[cint](f)
  69. proc toFlags(v: int): MyFlags = cast[MyFlags](v)
  70. assert toNum({}) == 0
  71. assert toNum({A}) == 1
  72. assert toNum({D}) == 8
  73. assert toNum({A, C}) == 5
  74. assert toFlags(0) == {}
  75. assert toFlags(7) == {A, B, C}
  76. ```
  77. Note how the set turns enum values into powers of 2.
  78. If using enums and sets with C, use distinct cint.
  79. For interoperability with C see also the
  80. [bitsize pragma](manual.html#implementation-specific-pragmas-bitsize-pragma).