Friday, March 02, 2012

Assosiative arrays

Associative Arrays

Dynamic arrays are useful for dealing with contiguous collections of variables whose number changes dynamically.

When the size of the 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 lookup 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.

The syntax to declare an associative array is:

data_type array_id [ index_type ];

Where:

data_type is the data type of the array elements. Can be any type allowed for fixed-size arrays.

array_id is the name of the array being declared.

index_type is the data-type to be used as an index, or *. If * is specified, then the array is indexed by any integral expression of arbitrary size. An index type restricts the indexing expressions to a particular type.

bit [20:0] array_b[string]; // associative array of 21-bit vector, indexed by string.

No comments:

Post a Comment

Popular Posts