sets_fragment.txt 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. These operations are supported by sets:
  31. ================== ========================================================
  32. operation meaning
  33. ================== ========================================================
  34. `A + B` union of two sets
  35. `A * B` intersection of two sets
  36. `A - B` difference of two sets (A without B's elements)
  37. `A == B` set equality
  38. `A <= B` subset relation (A is subset of B or equal to B)
  39. `A < B` strict subset relation (A is a proper subset of B)
  40. `e in A` set membership (A contains element e)
  41. `e notin A` A does not contain element e
  42. `contains(A, e)` A contains element e
  43. `card(A)` the cardinality of A (number of elements in A)
  44. `incl(A, elem)` same as `A = A + {elem}`
  45. `excl(A, elem)` same as `A = A - {elem}`
  46. ================== ========================================================
  47. ### Bit fields
  48. Sets are often used to define a type for the *flags* of a procedure.
  49. This is a cleaner (and type safe) solution than defining integer
  50. constants that have to be `or`'ed together.
  51. Enum, sets and casting can be used together as in:
  52. ```nim
  53. type
  54. MyFlag* {.size: sizeof(cint).} = enum
  55. A
  56. B
  57. C
  58. D
  59. MyFlags = set[MyFlag]
  60. proc toNum(f: MyFlags): int = cast[cint](f)
  61. proc toFlags(v: int): MyFlags = cast[MyFlags](v)
  62. assert toNum({}) == 0
  63. assert toNum({A}) == 1
  64. assert toNum({D}) == 8
  65. assert toNum({A, C}) == 5
  66. assert toFlags(0) == {}
  67. assert toFlags(7) == {A, B, C}
  68. ```
  69. Note how the set turns enum values into powers of 2.
  70. If using enums and sets with C, use distinct cint.
  71. For interoperability with C see also the
  72. [bitsize pragma](manual.html#implementation-specific-pragmas-bitsize-pragma).