Created
August 28, 2025 18:05
-
-
Save jedjoud10/2646317c1d0f0c14d4b9117da2a595fa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| namespace jedjoud.VoxelTerrain { | |
| public ref struct SpanBackedStack<T> where T: unmanaged { | |
| private Span<T> backing; | |
| private int length; | |
| public int Length => length; | |
| public T this[int index] { | |
| get { return backing[index]; } | |
| } | |
| public static SpanBackedStack<T> New(Span<T> backing) { | |
| SpanBackedStack<T> queue = new SpanBackedStack<T>(); | |
| queue.backing = backing; | |
| queue.length = 0; | |
| return queue; | |
| } | |
| public void Enqueue(T value) { | |
| if (length == backing.Length) { | |
| throw new InvalidOperationException("Backing Span is full. Cannot enqueue"); | |
| } | |
| backing[length] = value; | |
| length++; | |
| } | |
| public bool TryDequeue(out T val) { | |
| if (length == 0) { | |
| val = default; | |
| return false; | |
| } | |
| length--; | |
| val = backing[length]; | |
| return true; | |
| } | |
| public void Clear() { | |
| length = 0; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment