List
Properties
- Ordered by index.
- Randomizable when its data_type is randomizable (e.g., randomizable scalar or aggregate of randomizable scalar).
- Element and data_type can be any scalar or aggregate of scalar.
- Index must be a non-negative integer.
- Can be nested within any collection type (e.g.,
array,list,maporset).
Declarations
A list can be declared with the following syntax:
list<data_type> identifier
-
Assume the enum type is defined before
-
Assume the struct type is defined before
Declare list by rand keyword
Initialization Assignment
PSSGen: Does not support bool, enum, float32, float64, chandle, and struct as element in initialization assignment.
A list can be assigned at declaration; otherwise, it will be initialized to an empty aggregate literal ({}).
List Operators
| Operator | Description |
|---|---|
[] |
Used to access a specific element of a list by a given index, which must be a non-negative integer. |
= |
Creates a copy of the list-type expression on the RHS and assigns it to the list on the LHS. |
== |
Evaluates to true if both sizes are equal and all elements with corresponding indexes are equal. |
!= |
Evaluates to true if the sizes are not equal or if any element with corresponding index is not equal. |
in |
Evaluates to true if the element on the LHS of in exists in the list. |
foreach |
Iterates over the list's elements. |
List Methods
| Method | Description |
|---|---|
int size() |
Returns the number of elements in the list. |
clear() |
Removes all elements from the list. |
<data_type> delete(int index) |
Removes the element at the specified index and returns it; the index must be a non-negative integer. |
insert(int index, data_type element) |
Adds the element at the specified index, and all elements at and beyond the index are moved by one. |
<data_type> pop_front() |
Removes the first element from the list and returns it. Same as delete(0). |
push_front(data_type element) |
Adds the element to the beginning of the list. Same as insert(0, element). |
<data_type> pop_back() |
Removes the last element from the list and returns it. Same as delete(size()-1). |
push_back(data_type element) |
Adds the element to the end of the list. Same as insert(size(), element). |
set<data_type> to_set() |
Returns all elements as a set-type value. |
shuffle()v2.3.0 v2.1 |
Randomizes the order of the elements. |
Index operator []
PSSGen: Does not support bool, enum, float32, float64, chandle, and struct as element for index operator [].
Used to access a specific element of a list by a given index, which must be a non-negative integer.
- The index is out of bounds.
Warning
Index should be smaller than size() of the list.
Assignment operator =
PSSGen: Does not support bool, enum, float32, float64, chandle, and struct as element for assignment operator =.
Creates a copy of the list-type expression on the RHS and assigns it to the list on the LHS.
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 indexes are equal.
- Same size, and all elements at corresponding indexes are equal.
- Different sizes.
- Lists with different data_type are incomparable.
Warning
Two lists with different data_type should NOT be compared.
Inequality operator !=
Evaluates to true if the sizes are not equal or if any element with corresponding index is not equal.
- Same size, and all elements at corresponding indexes are equal.
- Different sizes.
- Lists with different data_type are incomparable.
Warning
Two lists 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 list.
- The data_type of the LHS of
indiffers from that of the list'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 list's element on the RHS of in.
Within a slice
in can test a slice of the list instead of the whole list: [left..right] (both
indexes inclusive), [left..] (to the last element) or [..right] (from the first
element).
- The slice is empty when the left index is greater than the right index, the left index is
not less than the size, or the right index is negative;
inthen evaluates to false.
The slice indexes may be random, so a constraint can restrict a value to part of the list,
e.g., constraint v in intList[a..b];. A slice applies only to array and list (not to
set, and not to a value list literal such as {1, 2, 3}).
foreach statement
Iterates over the list's elements.
See Procedural/foreach for more information.
function int size()
Returns the number of elements in the list.
function void clear()
Removes all elements from the list.
Note
Methods that modify contents can only be used within an exec block or a native function.
function <data_type> delete(index)
Removes the element at the specified index and returns it; the index must be a non-negative integer.
- The index is out of bounds.
Warning
The index must be smaller than size() of the list.
Note
Methods that modify contents can only be used within an exec block or a native function.
function void insert(index, element)
PSSGen: Does not support bool, enum, float32, float64, chandle, and struct as element of insert().
Adds the element at the specified index, and all elements at and beyond the index are moved by one.
- The index is larger than
size()of the list. - The data_type of the element is not the same.
Warning
The index should NOT be larger than size() of the list.
Note
Methods that modify contents can only be used within an exec block or a native function.
function <data_type> pop_front()
Removes the first element from the list and returns it. Same as delete(0).
Note
Methods that modify contents can only be used within an exec block or a native function.
function void push_front(element)
PSSGen: Does not support bool, enum, float32, float64, chandle, and struct as element of push_front().
Adds the element to the beginning of the list. Same as insert(0, element).
Note
Methods that modify contents can only be used within an exec block or a native function.
function <data_type> pop_back()
Removes the last element from the list and returns it. Same as delete(size()-1).
Note
Methods that modify contents can only be used within an exec block or a native function.
function void push_back(element)
PSSGen: Does not support bool, enum, float32, float64, chandle, and struct as element of push_back().
Adds the element to the end of the list. Same as insert(size(), element).
Note
Methods that modify contents can only be used within an exec block or a native function.
function set<data_type> to_set()
Returns all elements as a set-type value.
function void shuffle()
Randomizes the order of the elements.
Note
Methods that modify contents can only be used within an exec block or a native function.