Created
May 26, 2019 01:48
-
-
Save JSandusky/6679d969b71b2b5963035e4155878a05 to your computer and use it in GitHub Desktop.
Crude swap+pop container
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
// 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