using System; namespace UnityEngine.Rendering { /// /// Generic growable array. /// /// Type of the array. public class DynamicArray where T: new() { T[] m_Array = null; /// /// Number of elements in the array. /// public int size { get; private set; } /// /// Allocated size of the array. /// public int capacity { get { return m_Array.Length; } } /// /// Constructor. /// Defaults to a size of 32 elements. /// public DynamicArray() { m_Array = new T[32]; size = 0; } /// /// Constructor /// /// Number of elements. public DynamicArray(int size) { m_Array = new T[size]; this.size = size; } /// /// Clear the array of all elements. /// public void Clear() { size = 0; } /// /// Add an element to the array. /// /// Element to add to the array. /// The index of the element. public int Add(in T value) { int index = size; // Grow array if needed; if (index >= m_Array.Length) { var newArray = new T[m_Array.Length * 2]; Array.Copy(m_Array, newArray, m_Array.Length); m_Array = newArray; } m_Array[index] = value; size++; return index; } /// /// Resize the Dynamic Array. /// This will reallocate memory if necessary and set the current size of the array to the provided size. /// /// New size for the array. /// Set to true if you want the current content of the array to be kept. public void Resize(int newSize, bool keepContent = false) { if (newSize > m_Array.Length) { if (keepContent) { var newArray = new T[newSize]; Array.Copy(m_Array, newArray, m_Array.Length); m_Array = newArray; } else { m_Array = new T[newSize]; } } size = newSize; } /// /// ref access to an element. /// /// Element index /// The requested element. public ref T this[int index] { get { #if DEVELOPMENT_BUILD || UNITY_EDITOR if (index >= size) throw new IndexOutOfRangeException(); #endif return ref m_Array[index]; } } } }