队列
队列是一头取另一头存的数据结构,符合先进先出的顺序。队列像一个动态数组一样,队列长度可以增加或者减小,同时也支持在任意位置增加和移除元素。
队列声明和unpacked 数组是类似的,只是需要使用$符号来确定数组的大小。在队列中,下标0表示第一个元素,$表示最后一个entry。可以指定队列的长度,也可以不指定队列的长度。
bit queue_1[$]; // queue of bits (unbound queue)
int queue_2[$]; // queue of int
byte queue_3[$:255]; // queue of byte (bounded queue with 256 entries)
string queue_4[$]; // queue of strings
队列的初始化不需要像数组那样在大括号前面加’
queue_1 = {0,1,2,3};
queue_4 = {“Red”,"Blue”,"Green”};
索引0表示front,最后索引表示back,push表示添加元素,pop表示移除元素。
方法 | 描述 |
---|---|
size() | 返回队列中元素的数目 |
insert | 在队列中某一个确定的位置插入元素 |
delete | 在队列中某一个确定的位置删除元素 |
push_front | 在队列的首部插入一个元素 |
push_back | 在队列的末尾插入一个元素 |
pop_front | 从队列的首部移除一个元素 |
pop_back | 从队列的末尾移除一个元素 |
队列方法例子
module queue;
//declaration
bit [31:0] queue_1[$] ; //unbouned queue
string queue_2[$] ;
initial begin
//queue initialization
queue_1 = {0, 1, 2, 3} ;
queue_2 = {"Red", "Blue", "Green"};
//Size Method
$display("----------Queue_1 size is %0d -----------", queue_1.size());
foreach(queue_1[i])
$display("\t queue_1[%0d] = %0d", i, queue_1[i]);
$display("----------Queue_2 size is %0d -----------", queue_2.size());
foreach(queue_2[i])
$display("\t queue_2[%0d] = %0d", i, queue_2[i]);
//insert method
queue_2.insert(1,"Orange");
$display("----------Queue_2 size after inserting Orange is %0d -----", queue_2.size());
foreach(queue_2[i])
$display("\t queue_2[%0d] = %0s", i, queue_2[i]);
//delete method
queue_2.delete(3);
$display("---------Queue_2 size after delete is %0d ------------", queue_2.size());
foreach(queue_2[i])
$display("\t queue_2[%0d] = %0s", i, queue_2[i]);
end
endmodule
输出结果是:
module queue;
//declaration
bit [31:0] queue_1[$] ;
int lvar ;
initial begin
//Queue initialization
queue_1 = {0, 1, 2, 3};
//Size-Method
$display("\t Queue_1 size is %0d", queue_1.size());
//Push_front Method
queue_1.push_front(22);
$display("\t Queue_1 size after push_front is %0d", queue_1.size());
//Push_back Method
queue_1.push_back(44);
$display("\t Queue_1 size after push_back is %0d", queue_1.size());
//pop_front Method
lvar = queue_1.pop_front();
$display("\t Queue_1 size after pop_front is %0d", queue_1.size());
//pop_back Method
lvar = queue_1.pop_back();
$display("\t Queue_1 pop_back value is %0d", lvar);
end
仿真结果如下:
Queue_1 size is 4
Queue_1 size after push_front is 5
Queue_1 size after push_back is 6
Queue_1 pop_front value is 22
Queue_1 pop_back value is 44
对于bounded queue的插入操作:
module queues_array;
//declaration
int queue[$:2];
int index;
int temp_var;
initial begin
//Queue Initialization:
queue = {7,3,1};
$display("Queue elements are,");
$display("\tqueue = %p",queue);
queue.push_back(10);
$display("After push_back Queue elements are,");
$display("\tqueue = %p",queue);
queue.push_front(10);
$display("After push_front Queue elements are,");
$display("\tqueue = %p",queue);
end
endmodule
输出结果是:
Queue elements are,
queue = '{7, 3, 1}
After push_back Queue elements are,
queue = '{7, 3, 1}
After push_front Queue elements are,
queue = '{10, 7, 3}
访问队列里随机一个元素
一般使用下标进行随机访问文章来源:https://www.toymoban.com/news/detail-595032.html
module queue;
//declaration
int queue[$] ;
int index ;
int temp_var ;
initial begin
//Queue Initialization
queue = {7, 3, 0, 1, 8} ;
$display("------- Queue elements with index ------");
foreach(queue[i])
$display("\t queue[%0d] = %0d", i, queue[i]);
$display("--------------------------------------\n");
$display("Before Queue size is %0d", queue.size());
repeat(2) begin
index = $urandom_range(0,4); // index of queue is from 0 to 4
temp_var = queue[index];
$display("Value od Index %0d in Queue is %0d", index, temp_var);
end
$display("After Queue size is %0d", queue.size());
end
endmodule
其输出结果为:
任意访问需要使用队列名[索引]的方式,区别于pop方法,用索引不会删除队列元素。如果你想删除,可以队列名.delete(index)方法,通过delete方法,提供索引值就能删除。更彻底的,不加索引,队列名.delete()将会删除整个队列。文章来源地址https://www.toymoban.com/news/detail-595032.html
到了这里,关于systemverilog学习 ----队列的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!