Set
Properties
- Unordered.
- Non-randomizable.
- Element can be any scalar or aggregate of scalars.
- Each element must be unique.
- Can be nested within any collection type (e.g.,
array,list,maporset).
Declarations
A set can be declared with the following syntax:
set<data_type> identifier
- Assume the enum type is defined before
Initialization Assignment
PSSGen: Does not support bool, enum, float32, float64, chandle, and struct as element in initialization assignment.
A set can be assigned at declaration; otherwise, it will be initialized to an empty aggregate literal ({}).
- Assume the enum type is defined before
Set Operators
| Operator | Description |
|---|---|
= |
Creates a copy of the set-type expression on the RHS and assigns it to the set on the LHS. |
== |
Evaluates to true if both sizes are equal and have exactly the same elements. |
!= |
Evaluates to true if the sizes are not equal or the sets do not have exactly the same elements. |
in |
Evaluates to true if the element on the LHS of in exists in the set. |
foreach |
Iterates over the set's elements. |
Set Methods
| Method | Description |
|---|---|
int size() |
Returns the number of elements in the set. |
clear() |
Removes all elements from the set. |
delete(data_type element) |
Removes the element from the set. |
insert(data_type element) |
Adds the element to the set. |
list<data_type> to_list() |
Returns all elements as a list-type value. |
Assignment operator =
PSSGen: Does not support bool, enum, float32, float64, chandle, and struct as element for assignment operator =.
Creates a copy of the set-type expression on the RHS and assigns it to the set on the LHS.
Duplicate elements in the RHS will be merged automatically and appear only once in the set.
- A duplicate element will appear only once.
Note
Operators that modify contents can only be used within an exec block or a native function.
Equality operator ==
Evaluates to true if both sizes are equal and have exactly the same elements.
- Same size and the same elements.
- Same size and the same elements, in a different order.
- Different sizes.
- Sets with different data_type are incomparable.
Warning
Two sets with different data_type should NOT be compared.
Inequality operator !=
Evaluates to true if the sizes are not equal or the sets do not have exactly the same elements.
- Same size and the same elements.
- Same size and the same elements, in a different order.
- Different sizes.
- Sets with different data_type are incomparable.
Warning
Two sets with different data_type should NOT be compared.
Set membership operator in
Evaluates to true if the element on the LHS of in exists in the set.
- The data_type of the LHS of
indiffers from that of the set's element on the RHS ofin.
Warning
The data_type of the element on the LHS of in should be the SAME as that of the set's element on the RHS of in.
foreach statement
Iterates over the set's elements.
See Procedural/foreach for more information.
Warning
For set-type, the index variable of a foreach statement should NOT be used to specify set elements.
function int size()
Returns the number of elements in the set.
function void clear()
Removes all elements from the set.
Note
Methods that modify contents can only be used within an exec block or a native function.
function void delete(data_type element)
Removes the element from the set.
- The element does not exist in the set.
- The data_type of the element is not the same as that of the set.
Warning
The element to be deleted should exist in the set.
Note
Unlike list-type or map-type, delete(data_type element) of set-type will not return the element.
Note
Methods that modify contents can only be used within an exec block or a native function.
function void insert(data_type element)
PSSGen: Does not support bool, enum, float32, float64, chandle, and struct as element of insert().
Adds the element to the set.
- The data_type of the element is not the same as that of the set.
Note
Methods that modify contents can only be used within an exec block or a native function.
function list<data_type> to_list()
Returns all elements as a list-type value.