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 non-negative integer.
- Size must be non-zero integer.
- Can be nested by any collection types (e.g.,
array,list,maporset).
Declarations
Array has 2 declaration formats:
| Format | Syntax |
|---|---|
Template |
array<data_type, size> identifier |
Square |
data_type identifier [size] |
Note
Array is a fixed-size collection, which's size must be assigned by a non-zero integer, and cannot be changed after declared.
Declare array by rand keyword
Initialization Assignment
PSSGen: Not support bool, enum, float32, float64, chandle, and struct as element in initialization assignment.
Array can be assigned at declaration; otherwise, each elements will be initialized to default initial value.
Array Operators
| Operator | Description |
|---|---|
[] |
Used to access a specific element of an array by 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 whether both sizes are not equal or if any element with corresponding index is not equal. |
in |
Evaluates to true if element on LHS of in is 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 data_type of element is bit or int. |
float64 sum()Not support yet v2.1 |
Returns the sum of all elements in the array, when data_type of element is float32 or float64. |
list<data_type> to_list() |
Returns all elements to a list-type. |
set<data_type> to_set(): |
Returns all elements to a set-type. |
Index operator []
PSSGen: Not support bool, enum, float32, float64, chandle, and struct as element for index operator [].
Used to access a specific element of an array by given index, which must be a non-negative integer.
Assignment operator =
PSSGen: 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
Operator that modify contents can only be used within exec block or native function.
Equality operator ==
Evaluates to true if both sizes are equal and all elements with corresponding indexes are equal.
- Equalize size and all elements with corresponding indexes.
- Inequalize size.
- Different data_type between arrays are incomparable.
- Equalize size and all elements with corresponding indexes.
- Inequalize size.
- Different data_type between arrays are incomparable.
Warning
Different data_type of element of two arrays should NOT be compared.
Inequality operator !=
Evaluates to true whether both sizes are not equal or if any element with corresponding index is not equal.
- Equalize size and all elements with corresponding indexes.
- Inequalize size.
- Different data_type between arrays are incomparable.
- Equalize size and all elements with corresponding indexes.
- Inequalize size.
- Different data_type between arrays are incomparable.
Warning
Different data_type of element of two arrays should NOT be compared.
Set membership operator in
Evaluates to true if element on LHS of in is exists in the array.
- Different data_type between LHS of
inand the array's element on RHS ofin.
- Different data_type between LHS of
inand the array's element on RHS ofin.
Warning
Data_type of element on LHS of in should be SAME as the array's element on RHS of in.
foreach statement
Iterates over the array's elements.
Look at Procedural/foreach for more information.
Usage: Constraint random array by using foreach()
For a random array, the foreach() method can be used to constraint each element's possible value.
function int size()
Returns the number of elements in the array.
Note
Considered as a constant expression.
function int sum()
Returns the sum of all elements in the array, when data_type of element is bit or int.
stringcan't be calculated
stringcan't be calculated
Warning
The data-type of element should be integer types (e.g., int or bit).
Usage: use sum() to constrain a random array
function float64 sum()
Returns the sum of all elements in the array, when data_type of element is float32 or float64.
stringcan't be calculated
stringcan't be calculated
Warning
The data-type of element should be floating-point types (e.g., float32 or float64).
function list<data_type> to_list()
Returns all elements to a list-type.
function set<data_type> to_set()
Returns all elements to a set-type.
Created: 2023-10-04