Skip to content

Commit

Permalink
dd
Browse files Browse the repository at this point in the history
  • Loading branch information
teinarss committed Nov 30, 2023
1 parent 0a6597a commit c7b2480
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions OpenRA.Game/Primitives/PositionalQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@ namespace OpenRA.Primitives
{
public class PositionalQueue<T> : IEnumerable<T>
{
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;

}
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit c7b2480

Please sign in to comment.