Array
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.
- Size must be a non-zero integer.
- Can be nested within any collection type (e.g.,
array,list,maporset).
Declarations
An array has 2 declaration formats:
| Format | Syntax |
|---|---|
Template |
array<data_type, size> identifier |
Square |
data_type identifier [size] |
Note
An array is a fixed-size collection whose size must be a non-zero integer and cannot be changed after declaration.
Declare array by rand keyword
Initialization Assignment
PSSGen: Does not support bool, enum, float32, float64, chandle, and struct as element in initialization assignment.
An array can be assigned at declaration; otherwise, each element will be initialized to the default initial value.
Declaration form used below
The rest of this page uses the template form (array<data_type, size>).
Operators and methods behave the same on arrays declared in square form.
Array Operators
| Operator | Description |
|---|---|
[] |
Used to access a specific element of an array by a given index, which must be a non-negative integer. |
= |
Creates a copy of the array-type expression on the RHS and assigns it to the array 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 array. |
foreach |
Iterates over the array's elements. |
Array Methods
| Method | Description |
|---|---|
int size() |
Returns the number of elements in the array. |
int sum() |
Returns the sum of all elements in the array, when the data_type of the element is bit or int. |
float64 sum()Not supported yet v2.1 |
Returns the sum of all elements in the array, when the data_type of the element is float32 or float64. |
list<data_type> to_list() |
Returns all elements as a list-type value. |
set<data_type> to_set() |
Returns all elements as a set-type value. |
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 an array by a given index, which must be a non-negative integer.
Assignment operator =
PSSGen: Does not support bool, enum, float32, float64, chandle, and struct as element for assignment operator =.
Creates a copy of the array-type expression on the RHS and assigns it to the array 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.
- Arrays with different data_type are incomparable.
Warning
Two arrays with different data_type of element 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.
- Arrays with different data_type are incomparable.
Warning
Two arrays with different data_type of element should NOT be compared.
Set membership operator in
Evaluates to true if the element on the LHS of in exists in the array.
- The data_type of the LHS of
indiffers from that of the array'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 array's element on the RHS of in.
Within a slice
in can test a slice of the array instead of the whole array: [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 array,
e.g., constraint v in intArray[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 array's elements.
See Procedural/foreach for more information.
Recipe: Constrain every array element
function int size()
Returns the number of elements in the array.
Note
Considered a constant expression.
function int sum()
Returns the sum of all elements in the array, when the data_type of the element is bit or int.
- A
stringcannot be summed.
Warning
The data_type of the element should be an integer type (e.g., int or bit).
Recipe: Constrain the sum of an array
function float64 sum()
Returns the sum of all elements in the array, when the data_type of the element is float32 or float64.
- A
stringcannot be summed.
Warning
The data_type of the element should be a floating-point type (e.g., float32 or float64).
function list<data_type> to_list()
Returns all elements as a list-type value.
function set<data_type> to_set()
Returns all elements as a set-type value.