Skip to content

Map

v2.2.0

Properties

  • Unordered.
  • Non-randomizable.
  • Key, element and data_type can be any scalar or aggregate of scalars.
  • Each key 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 map can be declared with the following syntax:

map<data_type, data_type> identifier

1
2
3
4
5
6
map<bit [4] , int    > nibble2int  ;    //  nibble2int  : {}
map<int     , bool   > int2bool    ;    //  int2bool    : {}
map<bool    , string > bool2string ;    //  bool2string : {}
map<string  , float32> string2float;    //  string2float: {}
map<float32 , int    > float2int   ;    //  float2int   : {}
map<eSTR2NUM, int    > enum2int    ;    //  enum2int    : {} (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 key and element in initialization assignment.

Not supported yet

PSSGen: Does not support bool, enum, float32, float64, chandle, and struct as key or element in initialization assignment.

A map can be assigned at declaration; otherwise, it will be initialized to an empty aggregate literal ({}).

1
2
3
4
5
6
map<bit [4] , int    > nibble2int   = {4'b1110:3      , 4'b0110:2     };
map<int     , bool   > int2bool     = {      4:true   ,       5:false };
map<bool    , string > bool2string  = {  false:"FALSE",    true:"TRUE"};
map<string  , float32> string2float = {  "2.1":2.1    ,   "2.2":2.2   };
map<float32 , int    > float2int    = {    2.4:2      ,     2.6:3     };
map<eSTR2NUM, int    > enum2int     = {    ONE:1      ,     TWO:2     }; // (1)!

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

Map Operators

Operator Description
[] Used to access a specific element of a map by a given key.
= Creates a copy of the map-type expression on the RHS and assigns it to the map on the LHS.
== Evaluates to true if both sizes are equal and all elements with corresponding keys are equal.
!= Evaluates to true if the sizes are not equal or if any element with corresponding key is not equal.
foreach Iterates over the map's elements.

Map Methods

Method Description
int size() Returns the number of elements in the map.
clear() Removes all elements from the map.
<data_type> delete(key) Removes the element with the specified key and returns it; the key must exist in the map.
insert(key, element) Adds the element with the specified key.
set<data_type> keys() Returns all keys as a set-type value.
list<data_type> values() Returns all elements as a list-type value.

Index operator []

v2.2.0

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

Not supported yet

PSSGen: Does not support bool, enum, float32, float64, chandle, and struct as key or element for index operator [].

Used to access a specific element of a map by a given key.

1
2
3
4
5
map<string, int> string2int = {"1":1, "2":2};

int intVal = string2int["2"];   //  intVal: 0 -> 2;
int intVal = string2int["3"];   //  ILLEGAL (1)
int intVal = string2int[ 1 ];   //  ILLEGAL (2)

  1. The key "3" does not exist.
  2. The data_types of the index and the key are not the same.

Warning

When used to reference an element in the map, the key should exist.

Note

When used on the LHS of an assignment, the key will be added if it does not exist.

1
2
3
map<string, int> string2int = {"1":1, "2":2};

string2int["3"] = 3;    //  string2int: {"1":1, "2":2} -> {"1":1, "2":2, "3":3}

Note

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

Assignment operator =

v2.2.0

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

Not supported yet

PSSGen: Does not support bool, enum, float32, float64, chandle, and struct as key or element for assignment operator =.

Creates a copy of the map-type expression on the RHS and assigns it to the map on the LHS. The last element will be used if there are multiple elements with the same key in the expression.

1
2
3
4
map<string, int> string2int = {"1":1, "2":2};

string2int = {"3":3, "4":4};    //  string2int: {"1":1, "2":2} -> {"3":3, "4":4}
string2int = {3:"3", 4:"4"};    //  ILLEGAL (1)

  1. The data_types are not the same as in the declaration of the map.
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 keys are equal.

map<string, int   > map_0 = {"1":1  , "2":2  };
map<string, int   > map_1 = {"1":0  , "2":2  };
map<string, int   > map_2 = {"0":1  , "2":2  };
map<string, int   > map_3 = {"2":2  , "1":1  };
map<string, int   > map_4 = {"1":1           };
map<int   , string> map_5 = {  1:"1",   2:"2"};

bit bitVal_0, bitVal_1, bitVal_2, bitVal_3, bitVal_4, bitVal_5;
if (map_0 == map_0) bitVal_0 = 1;   //  bitVal_0: 0 -> 1 (1)
if (map_0 == map_1) bitVal_1 = 1;   //  bitVal_1: 0 -> 0 (2)
if (map_0 == map_2) bitVal_2 = 1;   //  bitVal_2: 0 -> 0 (3)
if (map_0 == map_3) bitVal_3 = 1;   //  bitVal_3: 0 -> 1 (4)
if (map_0 == map_4) bitVal_4 = 1;   //  bitVal_4: 0 -> 0 (5)
if (map_0 == map_5) bitVal_5 = 1;   //  ILLEGAL (6)

  1. Same size, and all elements with corresponding keys are equal.
  2. An element with a corresponding key differs.
  3. An element with a corresponding key differs.
  4. Same size, and all elements with corresponding keys are equal (order does not matter).
  5. Different sizes.
  6. Maps with different data_type of key or element are incomparable.

Warning

Two maps with different data_type of key or element should NOT be compared.

Inequality operator !=

v2.2.0

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

map<string, int   > map_0 = {"1":1  , "2":2  };
map<string, int   > map_1 = {"1":0  , "2":2  };
map<string, int   > map_2 = {"0":1  , "2":2  };
map<string, int   > map_3 = {"2":2  , "1":1  };
map<string, int   > map_4 = {"1":1           };
map<int   , string> map_5 = {  1:"1",   2:"2"};

bit bitVal_0, bitVal_1, bitVal_2, bitVal_3, bitVal_4, bitVal_5;
if (map_0 != map_0) bitVal_0 = 1;   //  bitVal_0: 0 -> 0 (1)
if (map_0 != map_1) bitVal_1 = 1;   //  bitVal_1: 0 -> 1 (2)
if (map_0 != map_2) bitVal_2 = 1;   //  bitVal_2: 0 -> 1 (3)
if (map_0 != map_3) bitVal_3 = 1;   //  bitVal_3: 0 -> 0 (4)
if (map_0 != map_4) bitVal_4 = 1;   //  bitVal_4: 0 -> 1 (5)
if (map_0 != map_5) bitVal_5 = 1;   //  ILLEGAL (6)

  1. Same size, and all elements with corresponding keys are equal.
  2. An element with a corresponding key differs.
  3. An element with a corresponding key differs.
  4. Same size, and all elements with corresponding keys are equal (order does not matter).
  5. Different sizes.
  6. Maps with different data_type of key or element are incomparable.

Warning

Two maps with different data_type of key or element should NOT be compared.

foreach statement

v2.2.0

Iterates over the map's elements.

See Procedural/foreach for more information.

map<string, int> string2int = {"1":1, "2":2};
int    intVal_0 = 0;
string intVal_1 = "";
int    intVal_2 = 0;

foreach (i : string2int[j]) {
    intVal_0 = i;               //  intVal_0: 0  ->  1  ->  2
    intVal_1 = j;               //  intVal_1: "" -> "1" -> "2"
    intVal_2 = string2int[j];   //  intVal_2: 0  ->  1  ->  2
}


function int size()

v2.2.0

Returns the number of elements in the map.

1
2
3
map<string, int> string2int = {"1":1, "2":2};

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

function void clear()

v2.2.0

Removes all elements from the map.

1
2
3
map<string, int> string2int = {"1":1, "2":2};

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

Note

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

function <data_type> delete(data_type key)

v2.2.0

Removes the element with the specified key and returns it; the key must exist in the map.

1
2
3
4
map<string, int> string2int = {"1":1, "2":2};

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

  1. The key "3" does not exist.

Warning

The key should exist in the map.

Note

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

function void insert(data_type key, data_type element)

v2.2.0

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

Not supported yet

PSSGen: Does not support bool, enum, float32, float64, chandle, and struct as key or element of insert().

Adds the element with the specified key. The element will be replaced if the key already exists.

1
2
3
4
5
6
map<string, int> string2int = {"1":1, "2":2};

string2int.insert("1", 0  );    //  string2int: {"1":1, "2":2} -> {"1":0, "2":2}
string2int.insert("3", 3  );    //  string2int: {"1":0, "2":2} -> {"1":0, "2":2, "3":3}
string2int.insert("4", 4.0);    //  ILLEGAL (1)
string2int.insert(5  , 5  );    //  ILLEGAL (2)

  1. The data_type of the element is not the same.
  2. The data_type of the key is not the same.

Warning

The data_type of the key or element should be the same as in the map's declaration.

Note

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

function set<data_type> keys()

v2.2.0

Not supported yet

PSSGen: Does not support float32, float64, and chandle as key for keys() method.

Returns all keys as a set-type value.

1
2
3
map<string, int> string2int = {"1":1, "2":2, "2.5":2};

set<string> stringSet = string2int.keys();  //  stringSet: {} -> {"1", "2", "2.5"}

function list<data_type> values()

v2.2.0

Not supported yet

PSSGen: Does not support float32, float64, and chandle as element for values() method.

Returns all elements as a list-type value.

1
2
3
map<string, int> string2int = {"1":1, "2":2, "2.5":2};

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