Map
Properties
- Unordered.
- Non-randomizable.
- Key, element and data_type can be any scalar or aggregate of scalars.
- Each key must be unique.
- Can be nested within any collection type (e.g.,
array,list,maporset).
Declarations
A map can be declared with the following syntax:
map<data_type, data_type> identifier
- Assume the enum type is defined before
Initialization Assignment
PSSGen: Does not support bool, enum, float32, float64, chandle, and struct as key or element in initialization assignment.
A map can be assigned at declaration; otherwise, it will be initialized to an empty aggregate literal ({}).
- Assume the enum type is defined before
Map Operators
| Operator | Description |
|---|---|
[] |
Used to access a specific element of a map by a given key. |
= |
Creates a copy of the map-type expression on the RHS and assigns it to the map on the LHS. |
== |
Evaluates to true if both sizes are equal and all elements with corresponding keys are equal. |
!= |
Evaluates to true if the sizes are not equal or if any element with corresponding key is not equal. |
foreach |
Iterates over the map's elements. |
Map Methods
| Method | Description |
|---|---|
int size() |
Returns the number of elements in the map. |
clear() |
Removes all elements from the map. |
<data_type> delete(key) |
Removes the element with the specified key and returns it; the key must exist in the map. |
insert(key, element) |
Adds the element with the specified key. |
set<data_type> keys() |
Returns all keys as a set-type value. |
list<data_type> values() |
Returns all elements as a list-type value. |
Index operator []
PSSGen: Does not support bool, enum, float32, float64, chandle, and struct as key or element for index operator [].
Used to access a specific element of a map by a given key.
- The key
"3"does not exist. - The data_types of the index and the key are not the same.
Warning
When used to reference an element in the map, the key should exist.
Note
When used on the LHS of an assignment, the key will be added if it does not exist.
Note
Operators that modify contents can only be used within an exec block or a native function.
Assignment operator =
PSSGen: Does not support bool, enum, float32, float64, chandle, and struct as key or element for assignment operator =.
Creates a copy of the map-type expression on the RHS and assigns it to the map on the LHS.
The last element will be used if there are multiple elements with the same key in the expression.
- The
data_types are not the same as in the declaration of the map.
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 all elements with corresponding keys are equal.
- Same size, and all elements with corresponding keys are equal.
- An element with a corresponding key differs.
- An element with a corresponding key differs.
- Same size, and all elements with corresponding keys are equal (order does not matter).
- Different sizes.
- Maps with different data_type of key or element are incomparable.
Warning
Two maps with different data_type of key or element should NOT be compared.
Inequality operator !=
Evaluates to true if the sizes are not equal or if any element with corresponding key is not equal.
- Same size, and all elements with corresponding keys are equal.
- An element with a corresponding key differs.
- An element with a corresponding key differs.
- Same size, and all elements with corresponding keys are equal (order does not matter).
- Different sizes.
- Maps with different data_type of key or element are incomparable.
Warning
Two maps with different data_type of key or element should NOT be compared.
foreach statement
Iterates over the map's elements.
See Procedural/foreach for more information.
function int size()
Returns the number of elements in the map.
function void clear()
Removes all elements from the map.
Note
Methods that modify contents can only be used within an exec block or a native function.
function <data_type> delete(data_type key)
Removes the element with the specified key and returns it; the key must exist in the map.
- The key
"3"does not exist.
Warning
The key should exist in the map.
Note
Methods that modify contents can only be used within an exec block or a native function.
function void insert(data_type key, data_type element)
PSSGen: Does not support bool, enum, float32, float64, chandle, and struct as key or element of insert().
Adds the element with the specified key. The element will be replaced if the key already exists.
- The data_type of the element is not the same.
- The data_type of the key is not the same.
Warning
The data_type of the key or element should be the same as in the map's declaration.
Note
Methods that modify contents can only be used within an exec block or a native function.
function set<data_type> keys()
Returns all keys as a set-type value.
function list<data_type> values()
PSSGen: Does not support float32, float64, and chandle as element for values() method.
Returns all elements as a list-type value.