#
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:
1.
byte q1 [$];
2.
integer my_q[$] = {1, 3, 5};
3.
string names [$] = {“Ram, Sham, Laxman”};
4.
5.
bit
q [$:255];
6.
// A queue whose maximum size is 256 bits.
Example :
01.
module
example ;
02.
03.
int
int_queue[$] = { 1, 2, 3 };
04.
string string_queue [$] = {
"first"
,
"second"
,
"third"
,
"forth"
};
05.
06.
string s;
07.
08.
initial
09.
begin
10.
// example of the use of size
11.
$display(
"\n Use of size()"
);
12.
13.
for
(
int
i = 0 ; i < int_queue.size(); i++ )
14.
$display(int_queue[i]);
15.
16.
$display(
"\n\n Elements of string_queue[$]"
);
17.
for
(
int
i = 0; i < string_queue.size; i++)
18.
$write(string_queue[i],
" "
);
19.
20.
// example of the use of insert
21.
string_queue.insert(1,
"next"
);
// former element 1 is now element 2.
22.
string_queue.insert(2,
"somewhere"
);
23.
24.
$display(
"\n\n Use of insert()"
);
25.
for
(
int
i = 0; i < string_queue.size; i++)
26.
$write(string_queue[i],
" "
);
27.
28.
// example of the use of delete
29.
string_queue.
delete
(1);
// delete the element
30.
string_queue.
delete
(3);
31.
32.
$display(
"\n\n Use of delete()"
);
33.
for
(
int
i = 0; i < string_queue.size; i++)
34.
$write(string_queue[i],
" "
);
35.
36.
// example of the use of pop_front
37.
// deletes the front of the queue
38.
s = string_queue.pop_front();
39.
$display(
"\n\n Use of pop_front()"
);
40.
$display(
" %s"
,s);
41.
42.
for
(
int
i = 0; i < string_queue.size; i++)
43.
$write(string_queue[i],
" "
);
44.
45.
// example of the use of pop_back
46.
// deletes the back of the queue
47.
s = string_queue.pop_back();
48.
$display(
"\n\n Use of pop_back()"
);
49.
$display(
" %s"
,s);
50.
for
(
int
i = 0; i < string_queue.size; i++)
51.
$write(string_queue[i],
" "
);
52.
53.
// example of the use of push_front and push_back
54.
// grows the queue
55.
string_queue.push_front(
"in-front"
);
56.
string_queue.push_back(
"in-back"
);
57.
58.
$display(
"\n\n Use of push_front() and push_back()"
);
59.
for
(
int
i = 0; i < string_queue.size; i++)
60.
$write(string_queue[i],
" \n "
);
61.
62.
end
63.
endmodule
Output : #
01.
#
02.
# Use of size()
03.
# 1
04.
# 2
05.
# 3
06.
#
07.
#
08.
# Elements of string_queue[$]
09.
# first second third forth
10.
#
11.
# Use of insert()
12.
# first next somewhere second third forth
13.
#
14.
# Use of delete()
15.
# first somewhere second forth
16.
#
17.
# Use of pop_front()
18.
# first
19.
# somewhere second forth
20.
#
21.
# Use of pop_back()
22.
# forth
23.
# somewhere second
24.
#
25.
# Use of push_front() and push_back()
26.
# in-front
27.
# somewhere
28.
# second
29.
# in-back
30.
#
31.
<p> </p>