Queue :
In queues size is flexible. It can change easily
- Variable size array with automatic sizing, single dimension
- Many searching, sorting, and insertion methods
- Constant time to read, write, and insert at front & back
- Out of bounds access causes run-time error
A Queue is a variable size ordered collection of homogeneous objects. There are two main aspects of a queue that makes it attractive for verification purposes. First, a queue can have variable length, including a length of zero. This makes a queue an ideal candidate as a storage element that can shrink or grow as elements are deleted or added to it without fixing an artificial upper limit on its size as a regular fixed size array. The other advantage of having a queue is that, it provides a way to emulate both Last In First Out (LIFO) and First In First Out (FIFO) behavior that are required in so many ordered transactions. At the same time a queue still allows you to access any element randomly within the queue without any overhead just as a regular array. A Queue is analogous to one dimensional unpacked array that grows and shrinks automatically. Queues are declared using the same syntax as unpacked arrays, but specifying '$' as the array size. Eg:
byte q1 [$];
integer my_q[$] = {1, 3, 5};
string names [$] = {“Ram, Sham, Laxman”};
bit q [$:255];
// A queue whose maximum size is 256 bits.
Example :
module example ;
int int_queue[$] = { 1, 2, 3 };
string string_queue [$] = {"first","second","third","forth"};
string s;
initial
begin
// example of the use of size
$display("\n Use of size()");
for (int i = 0 ; i < int_queue.size(); i++ )
$display(int_queue[i]);
$display("\n\n Elements of string_queue[$]");
for (int i = 0; i < string_queue.size; i++)
$write(string_queue[i]," ");
// example of the use of insert
string_queue.insert(1,"next"); // former element 1 is now element 2.
string_queue.insert(2,"somewhere");
$display("\n\n Use of insert()");
for (int i = 0; i < string_queue.size; i++)
$write(string_queue[i]," ");
// example of the use of delete
string_queue.delete(1); // delete the element
string_queue.delete(3);
$display("\n\n Use of delete()");
for (int i = 0; i < string_queue.size; i++)
$write(string_queue[i]," ");
// example of the use of pop_front
// deletes the front of the queue
s = string_queue.pop_front();
$display("\n\n Use of pop_front()");
$display(" %s",s);
for (int i = 0; i < string_queue.size; i++)
$write(string_queue[i]," ");
// example of the use of pop_back
// deletes the back of the queue
s = string_queue.pop_back();
$display("\n\n Use of pop_back()");
$display(" %s",s);
for (int i = 0; i < string_queue.size; i++)
$write(string_queue[i]," ");
// example of the use of push_front and push_back
// grows the queue
string_queue.push_front("in-front");
string_queue.push_back("in-back");
$display("\n\n Use of push_front() and push_back()");
for (int i = 0; i < string_queue.size; i++)
$write(string_queue[i]," \n ");
end
endmodule
Output :
#
# Use of size()
# 1
# 2
# 3
#
#
# Elements of string_queue[$]
# first second third forth
#
# Use of insert()
# first next somewhere second third forth
#
# Use of delete()
# first somewhere second forth
#
# Use of pop_front()
# first
# somewhere second forth
#
# Use of pop_back()
# forth
# somewhere second
#
# Use of push_front() and push_back()
# in-front
# somewhere
# second
# in-back
#
|