2016-12-07 92 views
0

内部队列中的项目当我们需要删除队列里面的一些项目,我们可以很容易地编写代码象下面这样:删除foreach循环

foreach(queue[i]) begin 
    if(queue[i].value == 1) 
     queue.delete(i); 
end 

但在上面的代码中的bug,当队列[0] ==队列[1] == 1。因为queue.delete(0)将更改队列中项目的所有索引。

所以目前我使用如下代码:

foreach(queue[i]) begin 
    if(queue[i].value == 1) begin 
     queue.delete(i); 
     i--; 
    end 
end 

它的工作原理,但它看起来混乱乍一看。

所以我的问题是: 有没有更好的解决方案在这个问题在系统verilog?

+1

[这个问题]的解决方案(http://stackoverflow.com/questions/30494550/how-to-match-and-delete-an-element-from-a-queue)不适用于我。类似的解决方案可以使用'repeat'循环来实现:'repeat(q.size())begin if(q [i] == 1)q.delete(i); i ++; end'。请注意,即使是队列更改的大小,“repeat”循环中的迭代器也是不变的。 – sharvil111

+0

@ sharvil111我原来的问题有一个错字。我修改了它,现在它应该可以工作。 – awill

+0

我在之前的评论中指出了类似的问题。这是这个问题的简单链接:http://stackoverflow.com/questions/30494550/how-to-match-and-delete-an-element-from-a-queue – sharvil111

回答

0

我认为这应该工作(我无法立即对其进行测试。确保订单坚持下来,当你尝试一下)

queue = queue.find() with (item.value != 1); 

另一种方法是找到所有符合您的索引标准,在排序顺序取决于,然后通过索引环

int qi[$] = queue.find_index() with (item.value == 1); 
qi = qi.sort() with (-item); // sort highest to lowest 
foreach(qi[idx]) queue.delete(qi[idx]); 

参见IEEE1800-2012 § 7.12 阵列操作方法对于细节