Skip to content

List

v2.2.0

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, map or set).

Declarations

v2.2.0

Not supported yet

PSSGen: Does not support chandle as data_type.

A list can be declared with the following syntax:

list<data_type> identifier

1
2
3
4
5
6
list<int     > intList   ;  //  intList   : {}
list<bit [8] > byteList  ;  //  byteList  : {}
list<bool    > boolList  ;  //  boolList  : {}
list<string  > stringList;  //  stringList: {}
list<eSTR2NUM> enumList  ;  //  enumList  : {} (1)
list<sSTR2NUM> structList;  //  structList: {} (2)
  1. Assume the enum type is defined before

    1
    2
    3
    enum eSTR2NUM {
        ZERO, ONE, TWO
    };
    

  2. Assume the struct type is defined before

    1
    2
    3
    4
    struct sSTR2NUM {
        string stringVal;
        int intVal;
    };
    

Declare list by rand keyword

Not supported yet v2.1

1
2
3
rand list<int    > intList   ;
rand list<bit [8]> byteList  ;
rand list<string > stringList;

Initialization Assignment

v2.2.0

PSSGen: Supports bit, int, and string as element in initialization assignment.

Not supported yet

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 ({}).

1
2
3
4
list<int    > intList    = {1    , 2    };  //  intList   : {1    , 2    }
list<bit [8]> byteList   = {8'b01, 8'b10};  //  byteList  : {8'b01, 8'b10}
list<bool   > boolList   = {false, true };  //  boolList  : {false, true }
list<string > stringList = {"1"  , "2"  };  //  stringList: {"1"  , "2"  }

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 []

v2.2.0

PSSGen: Supports bit, int, and string as element for index operator [].

Not supported yet

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.

1
2
3
4
list<int> intList = {1, 2, 3};

int intVal = intList[1];    //  intVal: 0 -> 2
int intVal = intList[9];    //  ILLEGAL (1)

  1. The index is out of bounds.

Warning

Index should be smaller than size() of the list.

Assignment operator =

v2.2.0

PSSGen: Supports bit, int, and string as element for assignment operator =.

Not supported yet

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.

1
2
3
list<int> intList = {1, 2, 3};

intList = {2, 3, 4};        //  intList: {1, 2, 3} -> {2, 3, 4}

Note

Operators that modify contents can only be used within an exec block or a native function.

Equality operator ==

v2.2.0

Evaluates to true if both sizes are equal and all elements with corresponding indexes are equal.

1
2
3
4
5
6
7
8
list<int   > intList_0  = { 1 ,  2 ,  3      };
list<int   > intList_1  = { 1 ,  2 ,  3 ,  4 };
list<string> stringList = {"1", "2", "3"};

bit bitVal_0, bitVal_1, bitVal_2;
if (intList_0 == intList_0 ) bitVal_0 = 1;  //  bitVal_0: 0 -> 1; (1)
if (intList_0 == intList_1 ) bitVal_1 = 1;  //  bitVal_1: 0 -> 0; (2)
if (intList_0 == stringList) bitVal_2 = 1;  //  ILLEGAL (3)

  1. Same size, and all elements at corresponding indexes are equal.
  2. Different sizes.
  3. Lists with different data_type are incomparable.

Warning

Two lists with different data_type should NOT be compared.

Inequality operator !=

v2.2.0

Evaluates to true if the sizes are not equal or if any element with corresponding index is not equal.

1
2
3
4
5
6
7
8
list<int   > intList_0  = { 1 ,  2 ,  3      };
list<int   > intList_1  = { 1 ,  2 ,  3 ,  4 };
list<string> stringList = {"1", "2", "3"};

bit bitVal_0, bitVal_1, bitVal_2;
if (intList_0 != intList_0 ) bitVal_0 = 1;  //  bitVal_0: 0 -> 0 (1)
if (intList_0 != intList_1 ) bitVal_1 = 1;  //  bitVal_1: 0 -> 1 (2)
if (intList_0 != stringList) bitVal_2 = 1;  //  ILLEGAL (3)

  1. Same size, and all elements at corresponding indexes are equal.
  2. Different sizes.
  3. Lists with different data_type are incomparable.

Warning

Two lists with different data_type should NOT be compared.

Set membership operator in

v2.2.0

Evaluates to true if the element on the LHS of in exists in the list.

1
2
3
4
5
6
list<int> intList = {1, 2, 3};

bit bitVal_0, bitVal_1, bitVal_2;
if ( 1  in intList) bitVal_0 = 1;           //  bitVal_0: 0 -> 1
if ( 0  in intList) bitVal_1 = 1;           //  bitVal_1: 0 -> 0
if ("1" in intList) bitVal_2 = 1;           //  ILLEGAL (1)

  1. The data_type of the LHS of in differs from that of the list's element on the RHS of in.

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

v3.1 Not supported yet

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).

1
2
3
4
5
6
7
list<int> intList = {1, 2, 3, 4};

bit bitVal_0, bitVal_1, bitVal_2, bitVal_3;
if (2 in intList[1..2]) bitVal_0 = 1;     //  bitVal_0: 0 -> 1 (slice: {2, 3})
if (1 in intList[1..] ) bitVal_1 = 1;     //  bitVal_1: 0 -> 0 (slice: {2, 3, 4})
if (1 in intList[..0] ) bitVal_2 = 1;     //  bitVal_2: 0 -> 1 (slice: {1})
if (1 in intList[2..1]) bitVal_3 = 1;     //  bitVal_3: 0 -> 0 (empty slice) (1)

  1. 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; in then 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

v2.2.0

Iterates over the list's elements.

See Procedural/foreach for more information.

list<int> intList = {4, 5, 6};
int intVal_0 = 0;
int intVal_1 = 0;
int intVal_2 = 0;

foreach (i : intList[j]) {
    intVal_0 = i;           //  intVal_0: 0 -> 4 -> 5 -> 6
    intVal_1 = j;           //  intVal_1: 0 -> 0 -> 1 -> 2
    intVal_2 = intList[j];  //  intVal_2: 0 -> 4 -> 5 -> 6
}


function int size()

v2.2.0

Returns the number of elements in the list.

1
2
3
list<int> intList = {1, 2, 3};

int intVal = intList.size();        //  intVal: 0 -> 3

function void clear()

v2.2.0

Removes all elements from the list.

1
2
3
list<int> intList = {1, 2, 3};

intList.clear();                    //  intList: {1, 2, 3} -> {}

Note

Methods that modify contents can only be used within an exec block or a native function.

function <data_type> delete(index)

v2.2.0

Removes the element at the specified index and returns it; the index must be a non-negative integer.

1
2
3
4
list<int> intList = {1, 2, 3};

int intVal = intList.delete(1);     //  intVal: 0 -> 2; intList: {1, 2, 3} -> {1, 3}
int intVal = intList.delete(5);     //  ILLEGAL (1)

  1. 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)

v2.2.0

PSSGen: Supports bit, int, and string as element of insert().

Not supported yet

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.

1
2
3
4
5
6
list<int> intList = {1, 2, 3};

intList.insert(1, 6);               //  intList: {1, 2, 3} -> {1, 6, 2, 3}
intList.insert(4, 7);               //  intList: {1, 6, 2, 3} -> {1, 6, 2, 3, 7}
intList.insert(9, 8);               //  ILLEGAL (1)
intList.insert(0, "1");             //  ILLEGAL (2)

  1. The index is larger than size() of the list.
  2. 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()

v2.2.0

Removes the first element from the list and returns it. Same as delete(0).

1
2
3
list<int> intList = {1, 2, 3};

int intVal = intList.pop_front();   //  intVal: 0 -> 1; intList: {1, 2, 3} -> {2, 3}

Note

Methods that modify contents can only be used within an exec block or a native function.

function void push_front(element)

v2.2.0

PSSGen: Supports bit, int, and string as element of push_front().

Not supported yet

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).

1
2
3
list<int> intList = {1, 2, 3};

intList.push_front(4);              //  intList: {1, 2, 3} -> {4, 1, 2, 3}

Note

Methods that modify contents can only be used within an exec block or a native function.

function <data_type> pop_back()

v2.2.0

Removes the last element from the list and returns it. Same as delete(size()-1).

1
2
3
list<int> intList = {1, 2, 3};

int intVal = intList.pop_back();    //  intVal: 0 -> 3; intList: {1, 2, 3} -> {1, 2}

Note

Methods that modify contents can only be used within an exec block or a native function.

function void push_back(element)

v2.2.0

PSSGen: Supports bit, int, and string as element of push_back().

Not supported yet

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).

1
2
3
list<int> intList = {1, 2, 3};

intList.push_back(4);               //  intList: {1, 2, 3} -> {1, 2, 3, 4}

Note

Methods that modify contents can only be used within an exec block or a native function.

function set<data_type> to_set()

v2.2.0

Returns all elements as a set-type value.

1
2
3
list<int> intList = {1, 2, 1};

set<int> intSet = intList.to_set(); //  intSet: {} -> {1, 2}

function void shuffle()

v2.3.0 v2.1

Randomizes the order of the elements.

1
2
3
list<int> intList = {1, 2};

intList.shuffle();                  //  intList: {1, 2} -> {1, 2} or {2, 1}

Note

Methods that modify contents can only be used within an exec block or a native function.