Skip to content

Instantly share code, notes, and snippets.

@andrew-raphael-lukasik
Last active April 10, 2022 13:03
Show Gist options
  • Save andrew-raphael-lukasik/709c19dd2305a2d1a0fa32ee82717b5c to your computer and use it in GitHub Desktop.
Save andrew-raphael-lukasik/709c19dd2305a2d1a0fa32ee82717b5c to your computer and use it in GitHub Desktop.
NativeStack
// src: https://gist.github.com/andrew-raphael-lukasik/709c19dd2305a2d1a0fa32ee82717b5c
using Unity.Collections;
public unsafe struct NativeStack <T> : System.IDisposable
where T : unmanaged
{
NativeList<T> _list;
public bool IsCreated => _list.IsCreated;
public int Length => _list.Length;
public NativeStack ( int initialCapacity , Allocator allocator ) => _list = new NativeList<T>( initialCapacity , allocator );
public T this [ int i ]
{
get => _list[i];
set => _list[i] = value;
}
public void Push ( T item ) => _list.Add(item);
public T Pop ()
{
T removedItem = this[0];
_list.RemoveAt(0);
return removedItem;
}
public T Peek () => this[0];
public void Clear () => _list.Clear();
public void Dispose () { if( _list.IsCreated ) _list.Dispose(); }
public NativeArray<T> AsArray () => _list.AsArray();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment