sets_fragment.txt 2.8 KB

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