You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1002 B
C#
48 lines
1002 B
C#
using System;
|
|
|
|
namespace Fusion.Addons.SimpleKCC
|
|
{
|
|
internal sealed class KCCFastStack<T> where T : class, new()
|
|
{
|
|
private T[] _items;
|
|
|
|
private int _count;
|
|
|
|
public KCCFastStack(int capacity, bool createInstances)
|
|
{
|
|
_items = new T[capacity];
|
|
_count = 0;
|
|
if (createInstances)
|
|
{
|
|
_count = capacity;
|
|
for (int i = 0; i < capacity; i++)
|
|
{
|
|
_items[i] = new T();
|
|
}
|
|
}
|
|
}
|
|
|
|
public T PopOrCreate()
|
|
{
|
|
if (_count > 0)
|
|
{
|
|
_count--;
|
|
return _items[_count];
|
|
}
|
|
|
|
return new T();
|
|
}
|
|
|
|
public void Push(T item)
|
|
{
|
|
if (_count == _items.Length)
|
|
{
|
|
Array.Resize(ref _items, _items.Length * 2);
|
|
}
|
|
|
|
_items[_count] = item;
|
|
_count++;
|
|
}
|
|
}
|
|
}
|