diff --git a/OpenRA.Game/Primitives/PositionalQueue.cs b/OpenRA.Game/Primitives/PositionalQueue.cs index 248d90eae3b0..480230d6de0f 100644 --- a/OpenRA.Game/Primitives/PositionalQueue.cs +++ b/OpenRA.Game/Primitives/PositionalQueue.cs @@ -6,18 +6,22 @@ namespace OpenRA.Primitives { public class PositionalQueue : IEnumerable { - T[] items; + Entry[] items; int currentIndex = 0; int end = 0; int absoluteIndex = 0; int size; - private readonly T empty; - public PositionalQueue(int size = 2, T empty = default) + struct Entry { - items = new T[size]; + public T Value; + public bool HasValue; + } + + public PositionalQueue(int size = 2) + { + items = new Entry[size]; this.size = size; - this.empty = empty; end = this.size - 1; } @@ -46,17 +50,21 @@ public void EnqueueAt(int index, T i) } } - if (!Equals(items[pos], empty)) + if (items[pos].HasValue) { throw new ArgumentException("An element with the same key already exists in the "); } - items[pos] = i; + items[pos] = new Entry + { + Value = i, + HasValue = true + }; } void Resize(int increaseBy) { - var temp = new T[size + increaseBy]; + var temp = new Entry[size + increaseBy]; if (absoluteIndex > 0) { @@ -90,14 +98,14 @@ public T Dequeue() absoluteIndex = 0; } - return item; + return item.Value; } public T Peek() { var item = items[absoluteIndex]; - return item; + return item.Value; } public bool HasItem() @@ -139,9 +147,9 @@ public bool TryGet(int index, out T item) var tItem = items[pos]; - if (!Equals(tItem, empty)) + if (tItem.HasValue) { - item = tItem; + item = tItem.Value; return true; }