含队列与关联数组的3D动态数组可行性技术问询
实现包含队列/关联数组的3D动态数组(SystemVerilog)
一、实现int c[][$][$](动态数组+队列+队列)
这种结构的层级为:最外层是动态数组,每个元素是元素为队列的队列。实现时需逐层初始化内部结构:
initial begin int c[][$][$]; // 初始化最外层动态数组,长度设为4 c = new[4]; foreach(c[i]) begin // 给当前动态数组元素(队列的队列)添加第一个空队列 c[i].push_front($queue()); // 往这个内部队列中添加数据 c[i][0].push_front(i * 2); c[i][0].push_front(i * 3); c[i][0].push_front(i * 4); // 可继续添加第二个内部队列并赋值 c[i].push_back($queue()); c[i][1].push_front(i * 10); c[i][1].push_front(i * 20); end $display("3D 动态数组+队列结构:%0p", c); end
也可以用临时队列简化赋值逻辑:
foreach(c[i]) begin int temp_q[$]; temp_q.push_front(i*2); temp_q.push_front(i*3); // 将临时队列加入外层队列 c[i].push_front(temp_q); end
二、实现int c[][$][int](动态数组+队列+关联数组)
这种结构的层级为:最外层是动态数组,每个元素是元素为关联数组的队列。实现时需先为队列添加关联数组实例,再进行键值赋值:
initial begin int c[][$][int]; // 初始化最外层动态数组,长度设为4 c = new[4]; foreach(c[i]) begin // 给当前动态数组元素(关联数组的队列)添加第一个空关联数组 c[i].push_front($assoc_array()); // 给关联数组赋值键值对 c[i][0][5] = i * 5; c[i][0][10] = i * 10; // 可继续添加第二个关联数组并赋值 int temp_assoc[int]; temp_assoc[1] = i; temp_assoc[2] = i * 2; c[i].push_back(temp_assoc); end $display("3D 动态数组+队列+关联数组结构:%0p", c); end
你尝试代码中的问题
- 变量名错误:遍历
c[i]时你写了a[i].push_front(...),应该操作c[i]相关的内部结构; - 内部层级未初始化:3D结构中,
c[i]本身是队列类型(元素为队列/关联数组),直接访问c[i][0]会触发越界,必须先往c[i]中添加队列/关联数组实例(如用$queue()/$assoc_array()创建空实例,或用临时变量传递),才能对内部结构赋值。
内容的提问来源于stack exchange,提问作者xxxl_eet
相关产品推荐
相关产品推荐

