systemverilog的关联数组

关联数组定义

在 SystemVerilog 中,关联数组(Associative Arrays)是一种非常灵活的数据结构,它可以使用任意类型的键(key)来索引数组中的元素。这使得关联数组特别适合于实现类似哈希表(hash tables)或字典(dictionaries)的功能,其中键可以是字符串、数字或其他复杂类型。

data_type array_name [index_type];

//data_type:数组元素的数据类型。

//array_name:关联数组的名称。

//index_type:关联数组索引的数据类型,可以是任何类型,包括整型、字符串、结构体、类等。如果索引类型为*,则表示索引可以是任何integral类型。

关联数组可以在声明时通过花括号{}进行初始化,初始化时列出所有的键值对,键值对之间用逗号分隔。例如:

int my_array[string] = '{"apple": 1, "banana": 2, "orange": 3};

// 访问  
int value = my_array["banana"];  
  
// 修改  
my_array["apple"] = 5;

//使用foreach循环遍历:
foreach (my_array[key]) begin  
  $display("my_array[%s] = %0d", key, my_array[key]);  
end
//使用first、next方法遍历:
string key;  
if (my_array.first(key)) begin    
    $display("my_array[%s] = %0d", key, my_array[key]);   
end
//num():返回数组中元素的数量。
//exists(index):检查数组中是否存在指定索引的元素。
//delete(index):删除数组中指定索引的元素。如果不带索引号调用,则会清空整个数组。

举例

module associative_array_example;

    // 声明一个关联数组,键为字符串类型,值为整数类型
    int my_assoc[string];

    // 初始化数组
    initial begin
        // 添加键值对
        my_assoc["one"] = 1;
        my_assoc["two"] = 2;
        my_assoc["three"] = 3;

        // 检查键是否存在
        if (my_assoc.exists("one")) begin
            $display("Key 'one' exists.");
        end

        // 访问数组中的值
        $display("Value for 'two': %0d", my_assoc["two"]);

        // 删除一个键值对
        my_assoc.delete("three");

        // 遍历数组中的所有键值对
        foreach (my_assoc[key]) begin
            $display("Key: %s, Value: %0d", key, my_assoc[key]);
        end
    end

endmodule
module test;
    initial begin
        bit (63:0) assoc(bit(63:0)), idx = 1;  // 初始化关联数组,索引为 63 位的位向量
        // 初始化一些零散的值
        repeat(64) begin
            assoc(idx) = idx;
            idx = idx << 1;  // 每次左移一位
        end
        // 使用 foreach遍历数组
        $display("this is 1:");
        foreach(assoc(i)) 
            $display("assoc(%h) = %h", i, assoc(i));  // 这里使用16进制打印,每4位代替16二进制
        // 使用函数遍历数组,first和next函数会返回1或0
        $display("this is 2:");
        if(assoc.first(idx))begin 
            do 
                $display("assoc(%d) = %d", idx, assoc(idx));  // 这里按10进制打印
            while(assoc.next(idx));  // 得到下一个索引
        end
        // 找到第一个元素
        assoc.first(idx);
        // 删除第一个元素
        assoc.delete(idx);
        $display("The array now has %0d elements", assoc.num);
    end
endmodule
module associative_array_example;

    // 声明一个关联数组,键为字符串类型,值为整数类型
    int my_assoc[string];

    // 初始化数组
    initial begin
        // 添加键值对
        my_assoc["apple"] = 5;
        my_assoc["banana"] = 3;

        string key = "apple";

        // 检查键是否存在
        if (my_assoc.exists(key)) begin
            $display("The key '%s' exists in the associative array.", key);
            // 如果键存在,可以继续读取或修改值
            $display("Value for '%s': %0d", key, my_assoc[key]);
        end else begin
            $display("The key '%s' does not exist in the associative array.", key);
        end
    end

endmodule

相关推荐

  1. systemverilog关联数组

    2024-07-14 07:30:01       28 阅读
  2. SystemVerilog基本语法和流水实现

    2024-07-14 07:30:01       28 阅读
  3. SystemVerilog Constants、Processes

    2024-07-14 07:30:01       38 阅读
  4. SystemVerilog测试框架示例

    2024-07-14 07:30:01       25 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-14 07:30:01       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-14 07:30:01       71 阅读
  3. 在Django里面运行非项目文件

    2024-07-14 07:30:01       58 阅读
  4. Python语言-面向对象

    2024-07-14 07:30:01       69 阅读

热门阅读

  1. 最新得物data参数加密分析与响应数据解密

    2024-07-14 07:30:01       20 阅读
  2. JVM OutOfMemoryError异常模拟

    2024-07-14 07:30:01       17 阅读
  3. 2024.7.13刷题记录-牛客小白月赛98(未完)

    2024-07-14 07:30:01       23 阅读
  4. 代码随想录第五十五天打卡

    2024-07-14 07:30:01       25 阅读
  5. 《HarmonyOS应用开发者基础认证》考试题目

    2024-07-14 07:30:01       28 阅读
  6. 每天一个数据分析题(四百二十六)- 总体方差

    2024-07-14 07:30:01       24 阅读
  7. [C++]类与对象

    2024-07-14 07:30:01       20 阅读
  8. 大模型日报 2024-07-13

    2024-07-14 07:30:01       20 阅读
  9. 家校管理系统

    2024-07-14 07:30:01       18 阅读
  10. 使用vllIm部署大语言模型

    2024-07-14 07:30:01       23 阅读