Navigation
GitHub
CoreLib
Links
Sponsors
Notes
This wiki uses icons from icons8.com licensed under CC BY-ND 3.0.
This wiki uses icons from icons8.com licensed under CC BY-ND 3.0.
Summary | Associative arrays |
---|---|
Related issues | |
Current status | Proposal submitted |
Date submitted | 04/17/03 |
Author submission | Bhasker Jayaram |
Author email | jbhasker@esilicon.com |
An associative array is useful for holding sparse data. The indices are not restricted to a contiguous range. It is allocated storage as and when used.
To define an associative array, use the keyword associative.
type type_name is associative(assoc_type {, assoc_type}) of element_subtype_indication;
assoc_type
is either an array type or a discrete type. A range constraint can be specified for a discrete type.
An index constraint can be specified for an array type. These constraints only define a bound for the indices.
Some examples:
type myaaT is associative (INTEGER) of BIT; type COLOR is (Red, Blue, Green, Yellow, Orange); type my2aaT is associative (COLOR, COLOR) of INTEGER; type A1 is associative (STRING) of STRING(1 to 20); type A2 is associative (INTEGER range 0 to 20) of BIT_VECTOR(0 to 3); type A3 is associative (BIT_VECTOR(7 downto 0)) of STRING(1 to 20); -- Two associative arrays: variable mem_aa: myaaT: signal matrix: my2aaT;
An associative array type declaration implicitly defines the following subprograms.
The following actions can be performed on an associative array object.
Mem_aa(2) := ‘0’; Matrix (Blue, Red) <= 5;
:= mem_aa(5) …. := matrix (Blue, Red) … <= matrix (blue, green) .. – Error as element not yet assigned.
If (size(mem_aa) > 5) then …
If (exists (matrix, blue, red)) then . . .
Variable var_q: integer; If (first (mem_aa, var_q)) then … -- index of first is returned in var_q.
Variable var_m, var_n: COLOR; If (last (matrix, var_m, var_n)) then … -- index of last is returned in var_q.
If (next (mem_aa, var_q)) then … -- index of next is returned in var_q based on current value of var_q.
If (prev (matrix, var_m, var_n)) then … -- index of previous is returned in var_m, var_n based on the current value of var_m, var_n.
Ordering of elements
The first, next, prev, last functions are based on the premise that all elements of an
associative array are ordered from smallest to the largest based on the index values. The
ordering on the elements is based on rightmost dimension to the leftmost dimension. And
dimension ordering is based on the comparison operators as defined by the language.
For example, the elements of matrix are assumed to be ordered :
(red,red), (red, blue), (red, green) . . . (orange, yellow), (orange, orange)
Assignment
Nothing special here. Assignment between associative arrays of the same type are
allowed. Associative arrays can also be passed as parameters to subprograms.
Other alternatives
operations.
in an operation argument in addition to the other args.