關聯數組實際上是一種查找表,內存空間直到被使用時才會分配,每個數據項都會有一個特定的“鍵(索引)”,索引的類型不局限于整型。
相對于一般的數組,關聯數組的內存空間利用會更加充分,因為它是稀疏的,無需一開始就分配很大的內存空間。
當變量集合的大小未知或數據空間稀疏時,關聯數組是比動態數組更好的選擇。例如只需要使用到地址0和地址FF,關聯數組可以只分配2個地址空間,而動態數組則需要分配256個地址空間。
打個比方,如果動態數組時內存,那么關聯數組更像是一個具有“tag”的cache。
關聯數組也是一種unpacked數組。
data_type array_id [index_type];
其中data_type是數組元素的數據類型。array_id是數組的名稱。index_type是索引(或鍵)的數據類型。各種數據類型的索引示例如下:
//wildcard index. Can be indexed by any integral type int myArray[ * ]; //Array that stores 'bit [31:0]' at string type index. bit [31:0] myArray[ string ]; //Array that stores 'string' at string type index. string myArray [ string ]; // Array that stores 'int' at Class type index int myArray [ class ]; //Array that stores 'logic' type at integer type index logic myArray[ integer ]; typedef bit signed [7:0] mByte; int myArray [mByte]; //'bit signed' index
比較特別的是以class作為索引類型的聯合數組。
module assoc_arr; class AB; int a; int b; endclass int arr[AB]; //Associative array 'arr' with class 'AB' as index AB obj, obj1; initial begin obj = new(); obj1= new(); arr[obj]=20; //Store 20 at the object handle index 'obj' $display("%0d",arr[obj]); arr[obj1]=10; //Store 10 at the object handle index 'obj1' $display("%0d",arr[obj1]); end endmodule
仿真log:
20 10 V C S S i m u l a t i o n R e p o r t
聲明類“AB”,并使用它作為關聯數組中的索引
int arr[AB]
聲明兩個AB類型的對象(obj和obj1),并實例化,賦值以這兩個對象為索引的聯合數組值。
arr[obj] = 20; arr[obj1] = 10;
String Index– Example
下面是一個以字符串為索引類型的聯合數組示例:
module assoc_arr; integer St [string] = '{"Peter":26, "Paul":24, "Mary":22}; integer data; initial begin $display("St=",St); $display("data stored at Peter = %0d",St["Peter"]); $display("data stored at Paul = %0d",St["Paul"]); $display("data stored at Mary = %0d",St["Mary"]); St["mike"] = 20; //new data stored at new index "mike" data = St["mike"]; //retrieve data stored at index "mike" $display("data stored at mike = %0d",data); $display("St=",St); end endmodule
仿真log:
run -all; # KERNEL: St='{"Mary":22, "Paul":24, "Peter":26} # KERNEL: data stored at Peter = 26 # KERNEL: data stored at Paul = 24 # KERNEL: data stored at Mary = 22 # KERNEL: data stored at mike = 20 # KERNEL: St='{"Mary":22, "Paul":24, "Peter":26, "mike":20} # KERNEL: Simulation has fnished.
聲明關聯數組“St”,并對其進行初始化:
integer St [string] = '{"Peter":26, "Paul":24, "Mary":22};
意味著將26存儲在索引“Peter”中,24存儲在索引中
“Paul”,22存儲在索引“Mary”中。
2. 分別打印數組。
3. 在新索引“mike”中添加新數據。注意,這個數據項的內存此時才開始分配。
Associative Array Methods
下面是關聯數組提供的一些方法。
module assoc_arr; int temp, imem[int]; integer St [string] = '{"Peter":20, "Paul":22, "Mary":23}; initial begin if(St.exists( "Peter") ) $display(" Index Peter exists "); //Assign data to imem[int] imem[ 2'd3 ] = 1; imem[ 16'hffff ] = 2; imem[ 4'b1000 ] = 3; $display( " imem has %0d entries", imem.num ); if(imem.exists( 4'b1000)) $display("Index 4b'1000 exist)"); imem.delete(4'b1000); if(imem.exists( 4'b1000)) $display("Index 4b'1000 exists)"); else $display(" Index 4b'1000 does not exist"); imem[ 4'b1000 ] = 3; if(imem.first(temp)) $display(" First entry is at index %0d ",temp); if(imem.next(temp)) $display(" Next entry is at index %0b ",temp); if(imem.last(temp)) $display(" Last entry is at index %0h",temp); imem.delete( ); //delete all entries $display(" imem has %0d entries", imem.num ); $display(" imem = %p", imem); end endmodule
仿真log:
Index Peter exists imem has 3 entries Index 4b'1000 exists Index 4b'1000 does not exist First entry is at index 3 Next entry is at index 1000 Last entry is at index ffff imem has 0 entries imem = '{} V C S Simulation Report
聲明了兩個關聯數組
int imem[int]; integer St [string] = '{"Peter":20, "Paul":22, "Mary":23};
使用.exists()判斷某個索引對應的數據元素是否存在。
使用.num(),獲取該聯合數組具有的元素個數
使用.delete()刪除特定索引對應的元素。如果不指定索引,則所有的元素都會被刪除。
使用方法first()、next()和last()訪問某個聯合數組元素
First entry is at index 3 Next entry is at index 1000 Last entry is at index ffff
Associative Array– Default Value
可以在關聯數組聲明時為其指定默認值。
module assoc_arr; string words [int] = '{default: "hello"}; initial begin $display("words = %p", words['hffff]); //default $display("words = %p", words[0]); //default $display("words = %p", words[1000]); //default words['hffff] = "goodbye"; $display("words = %p", words); $display("words = %p", words[100]); //default end endmodule
仿真log:
words = "hello" words = "hello" words = "hello" words = '{0xffff:"goodbye"} words = "hello" V C S S i m u l a t i o n R e p o r t
數組“words”的初始化默認值是“hello”,當我們訪問索引“hffff”、“0”和“1000”時,都會打印默認值“hello”。
我們為index ' hffff分配一個字符串值" goodbye "。
Creating aDynamic Array ofAssociative Arrays
關聯數組也可以是動態數組中的元素,如下示例:
module assoc_arr; //Create a dynamic array whose elements are associative arrays int games [ ] [string]; initial begin //Create a dynamic array with size of 3 elements games = new [3]; //Initialize the associative array inside each dynamic //array element games [0] = '{ "football" : 10,"baseball" : 20,"hututu":70 }; games [1] = '{ "cricket" : 30, "ice hockey" : 40 }; games [2] = '{ "soccer" : 50, "rugby" : 60 }; // Iterate through each element of dynamic array foreach (games[element]) // Iterate through each index of the current element in // dynamic array foreach (games[element][index]) $display ("games[%0d][%s] = %0d", element, index, games[element][index]); end endmodule
仿真log:
games[0][baseball] = 20 games[0][football] = 10 games[0][hututu] = 70 games[1][cricket] = 30 games[1][ice hockey] = 40 games[2][rugby] = 60 games[2][soccer] = 50 V C S S i m u l a t i o n R e p o r t
審核編輯:湯梓紅
-
Verilog
+關注
關注
28文章
1351瀏覽量
110075 -
System
+關注
關注
0文章
165瀏覽量
36929 -
數組
+關注
關注
1文章
417瀏覽量
25939
原文標題:SystemVerilog中的關聯數組
文章出處:【微信號:芯片驗證工程師,微信公眾號:芯片驗證工程師】歡迎添加關注!文章轉載請注明出處。
發布評論請先 登錄
相關推薦
評論