ch2-Associative Arrays
2019-10-24
SystemVerilog Associative Arrays
When size of a collection is unknown or the data space is sparse, an associative array is a better option. Associative arrays do not have any storage allocated until it is used, and the index expression is not restricted to integral expressions, but can be of any type.
An associative array implements a look-up table of the elements of its declared type. The data type to be used as an index serves as the lookup key and imposes an ordering.
Syntax
1 |
|
Initialization Example
1 |
|
Associative Array Methods
Function | Description |
---|---|
function int num (); | Returns the number of entries in the associative array |
function int size (); | Also returns the number of entries, if empty 0 is returned |
function void delete ( [input index] ); | index when specified deletes the entry at that index, else the whole array is deleted |
function int exists (input index); | Checks whether an element exists at specified index; returns 1 if it does, else 0 |
function int first (ref index); | Assigns to the given index variable the value of the first index; returns 0 for empty array |
function int last (ref index); | Assigns to given index variable the value of the last index; returns 0 for empty array |
function int next (ref index); | Finds the smallest index whose value is greater than the given index |
function int prev (ref index); | Finds the largest index whose value is smaller than the given index |
Creating a dynamic array of associative arrays
dynamic array的每一个元素都是associative array
1 |
|
Embedding a dynamic array within each index of an associative array
每个associative array的元素都是一个dynamic array
1 |
|
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!