Files
RobustToolbox/Robust.Shared/Utility/GrowableStack.cs
metalgearsloth 348ab70a8d Update B2DynamicTree (#5332)
* Update B2DynamicTree

* API updates

* weh

* forcing it

* Fix all of the bugs

* Rebuild

* A crumb of danger

* Fix merge conflicts
2025-03-08 14:15:47 +11:00

57 lines
1.4 KiB
C#

using System;
namespace Robust.Shared.Utility
{
// Based on Box2D's b2GrowableStack.
/// <summary>
/// This is a growable LIFO stack with an initial capacity of N.
/// If the stack size exceeds the initial capacity, the heap is used
/// to increase the size of the stack.
/// </summary>
/// <typeparam name="T">The type of elements in the stack.</typeparam>
internal ref struct GrowableStack<T> where T : unmanaged
{
private Span<T> _stack;
private int _count;
private int _capacity;
/// <summary>
/// Creates the growable stack with the allocated space as stack space.
/// </summary>
public GrowableStack(Span<T> stackSpace)
{
_stack = stackSpace;
_capacity = stackSpace.Length;
_count = 0;
}
internal ref T this[int index] => ref _stack[index];
public void Push(in T element)
{
if (_count == _capacity)
{
_capacity *= 2;
var oldStack = _stack;
_stack = GC.AllocateUninitializedArray<T>(_capacity);
oldStack.CopyTo(_stack);
}
_stack[_count] = element;
++_count;
}
public T Pop()
{
--_count;
return _stack[_count];
}
public int GetCount()
{
return _count;
}
}
}