sets_fragment.txt 3.2 KB

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