-
Notifications
You must be signed in to change notification settings - Fork 0
/
CircularQueue.cs
54 lines (47 loc) · 1.23 KB
/
CircularQueue.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Flux.Utilities
{
public class CircularQueue<T> : Queue<T>
{
public CircularQueue()
{
}
public CircularQueue(int capacity) : base(capacity)
{
}
public CircularQueue(IEnumerable<T> collection) : base(collection)
{
}
public new T Dequeue()
{
T tmp = base.Dequeue();
Enqueue(tmp);
return tmp;
}
public void CycleTo(T item)
{
// Using a for loop here to avoid and endless cycle if no match is found!
T tmp = Peek();
for (int i = 0; i < Count; i++)
{
if (!tmp.Equals(item))
{
// Toss it to the back.
Dequeue();
// Check the next one.
tmp = Peek();
}
else
{
// We've hit our item. Stop here!
return;
}
}
// No item was found, so make sure we end up where we started from.
Dequeue();
}
}
}