Check for queue Empty

I have a queue like this -
int egress_port_pid_pri[3][*][*][$];

At the end of test case, I want to check that this array of queues to have size 0 . Can I do that without using 3 for loops.

In reply to ashish_banga:
If you change your associative array index from a wildcard ‘*’ to any explicit data type like ‘int’, you can use a single foreach loop:

int egress_port_pid_pri[3][int][int][$];
foreach (egress_port_pid_pr[i,j,k]) if (egress_port_pid_pr[i][j][k].size() == 0) ...


I recommend avoiding the wildcard index type altogether and always use an explicit datatype.

In reply to dave_59:

Do i need to take fourth dimension also , I think egress…[i][j][k] should work

In reply to ashish_banga:
Yes, you are correct. I fixed my example.

Hi @dave_59 ,

I found your response helpful but have found myself in a situation where I am still not getting results that I would hope for.

In my current situation I have a section of code where I have an array of queues. The array is bound to 4 elements.

In a forever block I can implement the if statement below and get results showing that none of the queues will ever be empty.

if ((arr_of_que[0].size() != '0) &&
(arr_of_que[1].size() != '0) &&
(arr_of_que[2].size() != '0) &&
(arr_of_que[3].size() != '0))
begin

However, when I attempt to do achieve the same functionality in the forever loop with with…

foreach(arr_of_que[f]) if (arr_of_que[f].size() != '0) begin

I unfortunately reach a condition where I am able to reach a queue size of zero on the indexes other than the first one.

Are you aware of a way to achieve the same functionality as the initial if statement without explicitly identifying each element in the array of queues?

I should mention that I am doing push_front() in the if statement so if I have a situation where a queue is empty, I am not able to do the push_front() and my system does not work as it needs to.

You probably want the array and() reduction method, not a foreachloop.

if (arr_of_que.and() with (item.size != 0) ) begin
1 Like

Thank you very much that is very clean looking and works well.
I was able to achieve the functionality a different way, but I think my way was much less efficient.

This was my arguably overly complex way…

bit [3:0] queues_not_empty;
...
foreach (arry_of_que[f]) if (arry_of_que[f].size() != '0) begin
  queues_not_empty[f] = 1;
end
if (&queues_not_empty == '1) begin
...

Thanks again!