#
An Associative array is a better option when the size of the collection is unknown or the data space is sparse. So the associative arrays are mainly used to model the sparse memories. In the associative arrays the storage is allocated only when we use it not initially like in dynamic arrays. Associative arrays can be assigned only to another Associative array of a compatible
type and with the same index type.
Another main difference between Associative array and normal arrays is in that in assoc arrays the arrays subscript can be any scalar value.
So to summarize :
- Dynamically allocated, non-contiguous elements
- Accessed with integer, or string index, single dimension
- Great for sparse arrays with wide ranging index
- Array functions: exists, first, last, next, prev
eg :
1.
mem[address];
// where address is a integer
2.
3.
str_arr[
"str"
];
// string index is used
4.
5.
class_arr[class_obj]
// index is class object
* Another important thing about assoc arrays is that they are stored in random order. Associative arrays are unpacked arrays.
Build in methods : #
- num() — returns the number of entries in the Associative array Eg:sram_model.num()
- first() — assigns the value of the first index in the Associative array to the given index variable Eg:sram_model.first(i);
- last() — assigns the value of the last index in the Associative array to the given index variable Eg:sram_model.last(i);
- next() — assigns the value of the next index in the Associative array to the given index variable Eg:sram_model.next(i);
- prev() — assigns the value of the previous index in the Associative array to the given index variable Eg:sram_model.prev(i);
- delete() — removes all the elements in the Associative array.Eg:sram_model.delete(); If the index is specified, then the element at the specified index is deleted
- exists() — checks if element exists at the specified index in the Associative array Eg:sram_model.delete();
Example : #
01.
module
assoc;
02.
bit
[31:0] mem[*];
03.
int
assoc[string];
04.
bit
[31:0] i;
05.
initial
06.
begin
07.
mem[ 0 ] = 32'hFFAAFFAA;
08.
mem[ 3 ] = 32'hFFAAFFAA;
09.
mem[ 5 ] = 32'hFFAAFFAA;
10.
mem[ 7 ] = 32'hFFAAFFAA;
11.
$display(
"size of array: %d\n"
, mem.
num
());
12.
13.
// find first element
14.
mem.
first
(i);
15.
$display (
"the first index of the array is %d"
, i);
16.
17.
// find the next element, which is now BASE_ADDR +3:
18.
mem.
next
(i);
19.
$display (
"the next index used of the array is %d"
, i);
20.
21.
// find the last element
22.
mem.
last
(i);
23.
$display (
"the last index used of the array is %d"
, i);
24.
25.
mem.
prev
(i);
26.
$display (
"the previous index used of the array is %d"
, i);
27.
28.
mem.
delete
();
29.
$display(
"size of array: %d\n"
, mem.
num
());
30.
$display(
" Existence of array: %d\n"
, mem.exists(i));
31.
end
32.
endmodule
Output : #
01.
# size of array: 4
02.
#
03.
# the first index of the array is 0
04.
# the next index used of the array is 3
05.
# the last index used of the array is 7
06.
# the previous index used of the array is 5
07.
# size of array: 0
08.
#
09.
# Existence of array: 0
10.
#