User Tools

Site Tools


Sidebar

GitHub

CoreLib

Sponsors

This website is sponsored by

Notes

This wiki uses icons from icons8.com licensed under CC BY-ND 3.0.

proposals:tbv:tbv2

TBV2 - Associative arrays

Summary Associative arrays
Related issues
Current status Proposal submitted
Date submitted 04/17/03
Author submission Bhasker Jayaram
Author email jbhasker@esilicon.com

Enhancement

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.

  • function delete (arg: type_name; i1: index_subtype {; i2: index_subtype} ) return boolean;
  • function exists (arg: type_name; i1:index_subtype {; i2: index_subtype} ) return boolean;
  • function size (arg: type_name) return NATURAL;
  • function first (arg: type_name; variable i1:index_subtype {; variable i2: index_subtype} ) return boolean;
  • function last (arg: type_name; variable i1:index_subtype {; variable i2: index_subtype} ) return boolean;
  • function next (arg: type_name; variable i1:index_subtype {; variable i2: index_subtype} ) return boolean;
  • function prev (arg: type_name; variable i1:index_subtype {; variable i2: index_subtype} ) return boolean;
  • function dump (arg: type_name ; file: file_type) return Boolean;
  • function load (arg: type_name ; file: file_type) retirn Boolean;

The following actions can be performed on an associative array object.

  1. Insert an element – creates an element in the associative array by assigning a value.
        Mem_aa(2) :=0;
        Matrix (Blue, Red) <= 5;
     
  2. Read an element – reads the specified indexed element from the associative array. If the specified index in not within the index’s range, a range constraint error occurs.
        := mem_aa(5) ….
        := matrix (Blue, Red)<= matrix (blue, green) .. – Error as element not yet assigned.
     
  3. Count of elements – Function size returns the total number of elements in the array. IF array is empty, returns 0.
        If (size(mem_aa) > 5) then …
     
  4. Does element exist – Function exists returns true if the element exists, else it returns false.
        If (exists (matrix, blue, red)) then . . .
     
  5. Get first element – Function first returns the index of the first element in the array in the index variable. Function returns false if array is empty.
        Variable var_q: integer;
        If (first (mem_aa, var_q)) then-- index of first is returned in var_q.
     
  6. Get last element – Function last returns the index of the last element in the array in the index variable. Function returns false if array is empty.
        Variable var_m, var_n: COLOR;
        If (last (matrix, var_m, var_n)) then-- index of last is returned in var_q.
     
  7. Get next element – Function next returns the index of the next element in the array based on the value of the index variable. On return, the index variable contains the index of the next variable. Function returns false if array is empty or trying to access beyond last.
        If (next (mem_aa, var_q)) then-- index of next is returned in var_q based on current value of var_q.
     
  8. Get previous element – Function previous returns the index of the previous element in the array based on the index of the index variable. On return, the index variable contains the index of the previous element. Function returns false if array is empty or trying to access beyond first.
        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.
     
  9. Dump associative array – The functions dumps the contents of the associative array to the specified file. If no file is specified, it dumps the info to STDOUT. The order of elements dumped is from the smallest index to the largest. Functions returns false if some error occurred during the writing of the information. The file if it already exists is deleted of its contents before the new info is added. The format of the dump is one entry per line in pairs (index, value) form.
  10. Read associative array – An associative array can be loaded values directly from a file. The file contains pairs of values such as (index, value) which are either comma-separated or space-separated or are on separate lines. A line that starts with – is interpreted as a comment and is ignored. The file should not contain any other form of text. The function returns false if some error occurs during the read process.

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

  • One other way to implement would be using attributes. However there is no clean way I could think of to implement the traversal

operations.

  • Instead of having many implicit functions, another alternative would be to have only one implicit function “assoc_op” that takes

in an operation argument in addition to the other args.

proposals/tbv/tbv2.txt · Last modified: 29.08.2016 08:58 by paebbels