Skip to content

Instantly share code, notes, and snippets.

@jedjoud10
Created August 28, 2025 18:05
Show Gist options
  • Select an option

  • Save jedjoud10/2646317c1d0f0c14d4b9117da2a595fa to your computer and use it in GitHub Desktop.

Select an option

Save jedjoud10/2646317c1d0f0c14d4b9117da2a595fa to your computer and use it in GitHub Desktop.
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