using System; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace MoonTools.ECS.Collections { public unsafe class NativeArray : IDisposable where T : unmanaged { private T* Array; private int count; private int capacity; public int Count => count; public ReverseSpanEnumerator GetEnumerator() => new ReverseSpanEnumerator(new Span(Array, Count)); private bool disposed; public NativeArray(int capacity = 16) { this.capacity = capacity; Array = (T*) NativeMemory.Alloc((nuint) (capacity * Unsafe.SizeOf())); count = 0; } public ref T this[int i] => ref Array[i]; public void Add(T item) { if (count >= capacity) { capacity *= 2; Array = (T*) NativeMemory.Realloc(Array, (nuint) (capacity * Unsafe.SizeOf())); } Array[count] = item; count += 1; } public void Clear() { count = 0; } protected virtual void Dispose(bool disposing) { if (!disposed) { NativeMemory.Free(Array); Array = null; disposed = true; } } ~NativeArray() { // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method Dispose(disposing: false); } public void Dispose() { // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method Dispose(disposing: true); GC.SuppressFinalize(this); } } }