mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-29 17:34:43 +00:00
50 lines
2.0 KiB
Plaintext
50 lines
2.0 KiB
Plaintext
The set type models the mathematical notion of a set. The set's basetype can
|
|
only be an ordinal type of a certain size, namely:
|
|
* ``int8``-``int16``
|
|
* ``uint8``/``byte``-``uint16``
|
|
* ``char``
|
|
* ``enum``
|
|
or equivalent. The reason is that sets are implemented as high
|
|
performance bit vectors. Attempting to declare a set with a larger type will
|
|
result in an error:
|
|
|
|
.. code-block:: nim
|
|
|
|
var s: set[int64] # Error: set is too large
|
|
|
|
Sets can be constructed via the set constructor: ``{}`` is the empty set. The
|
|
empty set is type compatible with any concrete set type. The constructor
|
|
can also be used to include elements (and ranges of elements):
|
|
|
|
.. code-block:: nim
|
|
type
|
|
CharSet = set[char]
|
|
var
|
|
x: CharSet
|
|
x = {'a'..'z', '0'..'9'} # This constructs a set that contains the
|
|
# letters from 'a' to 'z' and the digits
|
|
# from '0' to '9'
|
|
|
|
These operations are supported by sets:
|
|
|
|
================== ========================================================
|
|
operation meaning
|
|
================== ========================================================
|
|
``A + B`` union of two sets
|
|
``A * B`` intersection of two sets
|
|
``A - B`` difference of two sets (A without B's elements)
|
|
``A == B`` set equality
|
|
``A <= B`` subset relation (A is subset of B or equal to B)
|
|
``A < B`` strong subset relation (A is a real subset of B)
|
|
``e in A`` set membership (A contains element e)
|
|
``e notin A`` A does not contain element e
|
|
``contains(A, e)`` A contains element e
|
|
``card(A)`` the cardinality of A (number of elements in A)
|
|
``incl(A, elem)`` same as ``A = A + {elem}``
|
|
``excl(A, elem)`` same as ``A = A - {elem}``
|
|
================== ========================================================
|
|
|
|
Sets are often used to define a type for the *flags* of a procedure. This is
|
|
a much cleaner (and type safe) solution than just defining integer
|
|
constants that should be ``or``'ed together.
|