Skip to content

Set

v2.2.0

Properties

  • Unordered.
  • Non-randomizable.
  • Element can be any scalar or aggregate of scalars.
  • Each element must be unique.
  • 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 set can be declared with the following syntax:

set<data_type> identifier

1
2
3
4
5
6
set<bit [4] > nibbleSet;    //  nibbleSet: {}
set<int     > intSet   ;    //  intSet   : {}
set<bool    > boolSet  ;    //  boolSet  : {}
set<string  > stringSet;    //  stringSet: {}
set<float32 > floatSet ;    //  floatSet : {}
set<eSTR2NUM> enumSet  ;    //  enumSet  : {} (1)
  1. Assume the enum type is defined before
    1
    2
    3
    enum eSTR2NUM {
        ZERO, ONE, TWO
    };
    

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 set can be assigned at declaration; otherwise, it will be initialized to an empty aggregate literal ({}).

1
2
3
4
5
6
set<bit [4] > nibbleSet = {4'b0001, 4'b0010};
set<int     > intSet    = {      1, 2      };
set<bool    > boolSet   = {  false, true   };
set<string  > stringSet = {    "1", "2"    };
set<float32 > floatSet  = {    1.0, 2.0    };
set<eSTR2NUM> enumSet   = {    ONE, TWO    };   // (1)!

  1. Assume the enum type is defined before
    1
    2
    3
    enum eSTR2NUM {
        ZERO, ONE, TWO
    };
    

Set Operators

Operator Description
= Creates a copy of the set-type expression on the RHS and assigns it to the set on the LHS.
== Evaluates to true if both sizes are equal and have exactly the same elements.
!= Evaluates to true if the sizes are not equal or the sets do not have exactly the same elements.
in Evaluates to true if the element on the LHS of in exists in the set.
foreach Iterates over the set's elements.

Set Methods

Method Description
int size() Returns the number of elements in the set.
clear() Removes all elements from the set.
delete(data_type element) Removes the element from the set.
insert(data_type element) Adds the element to the set.
list<data_type> to_list() Returns all elements as a list-type value.

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 set-type expression on the RHS and assigns it to the set on the LHS. Duplicate elements in the RHS will be merged automatically and appear only once in the set.

1
2
3
4
set<int> intSet;

intSet = {1, 2};    //  intSet: {}     -> {1, 2}
intSet = {3, 3, 4}; //  intSet: {1, 2} -> {3, 4} (1)

  1. A duplicate element will appear only once.
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 have exactly the same elements.

set<int   > set_0 = { 1 ,  2      };
set<int   > set_1 = { 2 ,  1      };
set<int   > set_2 = { 1 ,  2 ,  3 };
set<string> set_3 = {"1", "2"     };

bit bitVal_0, bitVal_1, bitVal_2, bitVal_3;
if (set_0 == set_0) bitVal_0 = 1;   //  bitVal_0: 0 -> 1 (1)
if (set_0 == set_1) bitVal_1 = 1;   //  bitVal_1: 0 -> 1 (2)
if (set_0 == set_2) bitVal_2 = 1;   //  bitVal_2: 0 -> 0 (3)
if (set_0 == set_3) bitVal_3 = 1;   //  ILLEGAL (4)

  1. Same size and the same elements.
  2. Same size and the same elements, in a different order.
  3. Different sizes.
  4. Sets with different data_type are incomparable.

Warning

Two sets with different data_type should NOT be compared.

Inequality operator !=

v2.2.0

Evaluates to true if the sizes are not equal or the sets do not have exactly the same elements.

set<int   > set_0 = { 1 ,  2      };
set<int   > set_1 = { 2 ,  1      };
set<int   > set_2 = { 1 ,  2 ,  3 };
set<string> set_3 = {"1", "2"     };

bit bitVal_0, bitVal_1, bitVal_2, bitVal_3;
if (set_0 != set_0) bitVal_0 = 1;   //  bitVal_0: 0 -> 0 (1)
if (set_0 != set_1) bitVal_1 = 1;   //  bitVal_1: 0 -> 0 (2)
if (set_0 != set_2) bitVal_2 = 1;   //  bitVal_2: 0 -> 1 (3)
if (set_0 != set_3) bitVal_3 = 1;   //  ILLEGAL (4)

  1. Same size and the same elements.
  2. Same size and the same elements, in a different order.
  3. Different sizes.
  4. Sets with different data_type are incomparable.

Warning

Two sets with different data_type should NOT be compared.

Set membership operator in

v2.2.0

Not supported yet

PSSGen: Does not support float32, float64, and chandle as element.

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

1
2
3
4
5
6
set<int> intSet = {1, 2};

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

  1. The data_type of the LHS of in differs from that of the set'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 set's element on the RHS of in.

foreach statement

v2.2.0

Iterates over the set's elements.

See Procedural/foreach for more information.

1
2
3
4
5
6
set<int> intSet = {3, 4};
int intVal = 0;

foreach (i : intSet) {
    intVal = i;     //  intVal: 0 -> 3 -> 4 or 0 -> 4 -> 3
}

Warning

For set-type, the index variable of a foreach statement should NOT be used to specify set elements.

foreach (intSet[i]) {}  //  ILLEGAL


function int size()

v2.2.0

Returns the number of elements in the set.

1
2
3
set<int> intSet = {1, 2};

int intVal = intSet.size(); //  intVal: 0 -> 2

function void clear()

v2.2.0

Removes all elements from the set.

1
2
3
set<int> intSet = {1, 2};

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

Note

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

function void delete(data_type element)

v2.2.0

Removes the element from the set.

1
2
3
4
5
set<int> intSet = {1, 2};

intSet.delete( 2 ); //  intSet: {1, 2} -> {1}
intSet.delete( 3 ); //  ILLEGAL (1)
intSet.delete("1"); //  ILLEGAL (2)

  1. The element does not exist in the set.
  2. The data_type of the element is not the same as that of the set.

Warning

The element to be deleted should exist in the set.

Note

Unlike list-type or map-type, delete(data_type element) of set-type will not return the element.

Note

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

function void insert(data_type 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 to the set.

1
2
3
4
5
set<int> intSet = {1, 2};

intSet.insert( 3 ); //  intSet: {1, 2}    -> {1, 2, 3}
intSet.insert( 2 ); //  intSet: {1, 2, 3} -> {1, 2, 3}
intSet.insert("4"); //  ILLEGAL (1)

  1. The data_type of the element is not the same as that of the set.
Note

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

function list<data_type> to_list()

v2.2.0

Returns all elements as a list-type value.

1
2
3
set<int> intSet = {1, 2};

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