Updated on 2019-12-05
Fills a gap in Microsoft's Queue offering with an alternative allowing efficient indexed access
Microsoft provides a Queue
Note: I originally called this Queue
Using the code is exactly like using the code in Microsoft's Queue
The demo code is pretty self explanatory:
var q = new IndexedQueue<int>(1);
Console.WriteLine("Enqueuing 1..20");
for (var i = 0; i < 20; ++i)
q.Enqueue(i + 1);
Console.WriteLine("Dequeuing 1..10");
for (var i = 0; i < 10; ++i)
Console.Write("{0} ",q.Dequeue());
Console.WriteLine();
Console.WriteLine("Enqueuing 21..40 (force array wrap)");
for (var i = 0; i < 20; ++i)
q.Enqueue(i + 21);
Console.WriteLine("Enumerating");
foreach (var i in q)
Console.Write("{0} ", i);
Console.WriteLine();
Console.WriteLine("Removing 34 (middle of array)");
(q as System.Collections.Generic.ICollection<int>).Remove(34);
Console.WriteLine("Enumerating");
foreach (var i in q)
Console.Write("{0} ", i);
Console.WriteLine();
Console.WriteLine("Removing 32 (end of array)");
(q as System.Collections.Generic.ICollection<int>).Remove(32);
Console.WriteLine("Enumerating");
foreach (var i in q)
Console.Write("{0} ", i);
Console.WriteLine();
Implementing it was fairly straightforward, but removes from the middle (for ICollection
Anyway, if you need it, here it is. Most people probably never will, but there you go.