Last active
April 10, 2022 13:03
-
-
Save andrew-raphael-lukasik/709c19dd2305a2d1a0fa32ee82717b5c to your computer and use it in GitHub Desktop.
NativeStack
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
// 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