Associative Arrays :
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 :
mem[address]; // where address is a integer
str_arr["str"]; // string index is used
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 :
module assoc;
bit [31:0] mem[*];
int assoc[string];
bit [31:0] i;
initial
begin
mem[ 0 ] = 32'hFFAAFFAA;
mem[ 3 ] = 32'hFFAAFFAA;
mem[ 5 ] = 32'hFFAAFFAA;
mem[ 7 ] = 32'hFFAAFFAA;
$display("size of array: %d\n", mem.num());
// find first element
mem.first(i);
$display ("the first index of the array is %d", i);
// find the next element, which is now BASE_ADDR +3:
mem.next(i);
$display ("the next index used of the array is %d", i);
// find the last element
mem.last(i);
$display ("the last index used of the array is %d", i);
mem.prev(i);
$display ("the previous index used of the array is %d", i);
mem.delete();
$display("size of array: %d\n", mem.num());
$display(" Existence of array: %d\n", mem.exists(i));
end
endmodule
Output :
# size of array: 4
#
# the first index of the array is 0
# the next index used of the array is 3
# the last index used of the array is 7
# the previous index used of the array is 5
# size of array: 0
#
# Existence of array: 0
#
|
Posted By : sharath - Sept. 22, 2009, 10:43 p.m.
hii ,, how to pass the class for associative array...tell me one EX of assco(class_obj) and assoc("string");