sets_fragment.txt 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. .. code-block:: nim
  13. var s: set[int64] # Error: set is too large
  14. **Note:** Nim also offers `hash sets <sets.html>`_ (which you need to import
  15. with `import sets`), which have no such restrictions.
  16. Sets can be constructed via the set constructor: `{}` is the empty set. The
  17. empty set is type compatible with any concrete set type. The constructor
  18. can also be used to include elements (and ranges of elements):
  19. .. code-block:: nim
  20. type
  21. CharSet = set[char]
  22. var
  23. x: CharSet
  24. x = {'a'..'z', '0'..'9'} # This constructs a set that contains the
  25. # letters from 'a' to 'z' and the digits
  26. # from '0' to '9'
  27. These operations are supported by sets:
  28. ================== ========================================================
  29. operation meaning
  30. ================== ========================================================
  31. `A + B` union of two sets
  32. `A * B` intersection of two sets
  33. `A - B` difference of two sets (A without B's elements)
  34. `A == B` set equality
  35. `A <= B` subset relation (A is subset of B or equal to B)
  36. `A < B` strict subset relation (A is a proper subset of B)
  37. `e in A` set membership (A contains element e)
  38. `e notin A` A does not contain element e
  39. `contains(A, e)` A contains element e
  40. `card(A)` the cardinality of A (number of elements in A)
  41. `incl(A, elem)` same as `A = A + {elem}`
  42. `excl(A, elem)` same as `A = A - {elem}`
  43. ================== ========================================================
  44. Bit fields
  45. ~~~~~~~~~~
  46. Sets are often used to define a type for the *flags* of a procedure.
  47. This is a cleaner (and type safe) solution than defining integer
  48. constants that have to be `or`'ed together.
  49. Enum, sets and casting can be used together as in:
  50. .. code-block:: nim
  51. type
  52. MyFlag* {.size: sizeof(cint).} = enum
  53. A
  54. B
  55. C
  56. D
  57. MyFlags = set[MyFlag]
  58. proc toNum(f: MyFlags): int = cast[cint](f)
  59. proc toFlags(v: int): MyFlags = cast[MyFlags](v)
  60. assert toNum({}) == 0
  61. assert toNum({A}) == 1
  62. assert toNum({D}) == 8
  63. assert toNum({A, C}) == 5
  64. assert toFlags(0) == {}
  65. assert toFlags(7) == {A, B, C}
  66. Note how the set turns enum values into powers of 2.
  67. If using enums and sets with C, use distinct cint.
  68. For interoperability with C see also the
  69. `bitsize pragma <manual.html#implementation-specific-pragmas-bitsize-pragma>`_.