Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- internal static class CacheComponents<T> where T : struct, IComponent {
- private static int capacity;
- private static int length;
- private static T[] components;
- private static Queue<int> freeIndexes;
- static CacheComponents() {
- capacity = 16;
- length = 0;
- components = new T[capacity];
- freeIndexes = new Queue<int>();
- ComponentsCleaner.Register(CacheTypeIdentifier<T>.typeID, Remove);
- }
- public static int Add(T component) {
- if (freeIndexes.Count > 0) {
- var id = freeIndexes.Dequeue();
- components[id] = component;
- return id;
- }
- if (capacity < length) {
- var newCapacity = capacity * 2;
- Array.Resize(ref components, newCapacity);
- capacity = newCapacity;
- }
- var newID = length;
- components[newID] = component;
- length++;
- return newID;
- }
- public static ref T Get(int id) => ref components[id];
- public static bool Remove(int id) {
- if (length >= id && !freeIndexes.Contains(id)) {
- freeIndexes.Enqueue(id);
- return true;
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement