using System.Collections; using System.Collections.Generic; namespace TcgEngine { /// /// A pool reuses Memory for objects that are constantly created/destroyed, to prevent always allocating more memory /// Makes the AI much much much faster /// public class Pool where T : new() { private HashSet in_use = new HashSet(); private Stack available = new Stack(); public T Create() { if (available.Count > 0) { T elem = available.Pop(); in_use.Add(elem); return elem; } T new_obj = new T(); in_use.Add(new_obj); return new_obj; } public void Dispose(T elem) { in_use.Remove(elem); available.Push(elem); } public void DisposeAll() { foreach (T obj in in_use) available.Push(obj); in_use.Clear(); } public void Clear() { in_use.Clear(); available.Clear(); } public HashSet GetAllActive() { return in_use; } public int Count { get { return in_use.Count; } } public int CountAvailable { get { return available.Count; } } public int CountCapacity { get { return in_use.Count + available.Count; } } } }