Skip to content

Instantly share code, notes, and snippets.

@JSandusky
Created May 26, 2019 01:48
Show Gist options
  • Save JSandusky/6679d969b71b2b5963035e4155878a05 to your computer and use it in GitHub Desktop.
Save JSandusky/6679d969b71b2b5963035e4155878a05 to your computer and use it in GitHub Desktop.
Crude swap+pop container
// Swap to pop array of structs
public class StructArray<T> where T : struct
{
public T[] items_;
public int Count { get; private set; }
public int Capacity { get; private set; }
public StructArray(int capacity)
{
Resize(capacity);
Count = 0;
}
public void Resize(int newSize)
{
Array.Resize(ref items_, newSize);
Capacity = newSize;
}
public int GetNextIndex()
{
if (Count == Capacity)
return -1;
++Count;
return Count - 1;
}
public void Remove(int idx)
{
if (idx == -1)
return;
items_[idx] = items_[Count - 1];
--Count;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment