开发者

Is there anything like a expandable Queue in C#?

i have a set of IDs on which i do some operations:

Queue<string> queue = new Queue<string>();
queue.Enqueue("1");
q开发者_开发技巧ueue.Enqueue("2");
...
queue.Enqueue("10");

foreach (string id in queue)
{
    DoSomeWork(id);
}

static void DoSomeWork(string id)
{
   // Do some work and oooo there are new ids which should also be processed :)
   foreach(string newID in newIDs)
   {
       if(!queue.Contains(newID)) queue.Enqueue(newID);
   }
}

Is it possible to add some new items to queue in DoSomeWork() which will be also processed bei the main foreach-Loop?


What you're doing is to use an iterator over a changing collection. This is bad practice, since some collections will throw an exception when doing this (e.g. the collection should not change during the enumeration).

Use the following approach, which does use new items as well:

while (queue.Count > 0)
{
    DoSomeWork(queue.Dequeue());
}


Use Dequeue instead of a foreach-loop. Most enumerators become invalid whenever the underlying container is changed. And En-/Dequeue are the natural operations on a Queue. Else you could use List<T> or HashSet<T>

while(queue.Count>0)
{
  var value=queue.Dequeue();
  ...
}

To check if an item has already been processed a HashSet<T> is a fast solution. I typically use a combination of HashSet and Queue in those cases. The advantage of this solution is that it's O(n) because checking and adding to a HashSet is O(1). Your original code was O(n^2) since Contains on a Queue is O(n).

Queue<string> queue=new Queue<string>();
HashSet<string> allItems=new HashSet<string>();

void Add(string item)
{
  if(allItems.Add(item))
    queue.Enqueue(item);
}

void DoWork()
{
    while(queue.Count>0)
    {
      var value=queue.Dequeue();
      ...
    }  
}


It is common for loop iterations to add more work; just pass the queue into the method as an argument and adding to it should work fine.

The problem is that ou should be using Dequeue:

while(queue.Count>0) {
    DoSomeWork(queue.Dequeue());
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜