DynamicArray.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. namespace UnityEngine.Rendering
  3. {
  4. /// <summary>
  5. /// Generic growable array.
  6. /// </summary>
  7. /// <typeparam name="T">Type of the array.</typeparam>
  8. public class DynamicArray<T> where T: new()
  9. {
  10. T[] m_Array = null;
  11. /// <summary>
  12. /// Number of elements in the array.
  13. /// </summary>
  14. public int size { get; private set; }
  15. /// <summary>
  16. /// Allocated size of the array.
  17. /// </summary>
  18. public int capacity { get { return m_Array.Length; } }
  19. /// <summary>
  20. /// Constructor.
  21. /// Defaults to a size of 32 elements.
  22. /// </summary>
  23. public DynamicArray()
  24. {
  25. m_Array = new T[32];
  26. size = 0;
  27. }
  28. /// <summary>
  29. /// Constructor
  30. /// </summary>
  31. /// <param name="size">Number of elements.</param>
  32. public DynamicArray(int size)
  33. {
  34. m_Array = new T[size];
  35. this.size = size;
  36. }
  37. /// <summary>
  38. /// Clear the array of all elements.
  39. /// </summary>
  40. public void Clear()
  41. {
  42. size = 0;
  43. }
  44. /// <summary>
  45. /// Add an element to the array.
  46. /// </summary>
  47. /// <param name="value">Element to add to the array.</param>
  48. /// <returns>The index of the element.</returns>
  49. public int Add(in T value)
  50. {
  51. int index = size;
  52. // Grow array if needed;
  53. if (index >= m_Array.Length)
  54. {
  55. var newArray = new T[m_Array.Length * 2];
  56. Array.Copy(m_Array, newArray, m_Array.Length);
  57. m_Array = newArray;
  58. }
  59. m_Array[index] = value;
  60. size++;
  61. return index;
  62. }
  63. /// <summary>
  64. /// Resize the Dynamic Array.
  65. /// This will reallocate memory if necessary and set the current size of the array to the provided size.
  66. /// </summary>
  67. /// <param name="newSize">New size for the array.</param>
  68. /// <param name="keepContent">Set to true if you want the current content of the array to be kept.</param>
  69. public void Resize(int newSize, bool keepContent = false)
  70. {
  71. if (newSize > m_Array.Length)
  72. {
  73. if (keepContent)
  74. {
  75. var newArray = new T[newSize];
  76. Array.Copy(m_Array, newArray, m_Array.Length);
  77. m_Array = newArray;
  78. }
  79. else
  80. {
  81. m_Array = new T[newSize];
  82. }
  83. }
  84. size = newSize;
  85. }
  86. /// <summary>
  87. /// ref access to an element.
  88. /// </summary>
  89. /// <param name="index">Element index</param>
  90. /// <returns>The requested element.</returns>
  91. public ref T this[int index]
  92. {
  93. get
  94. {
  95. #if DEVELOPMENT_BUILD || UNITY_EDITOR
  96. if (index >= size)
  97. throw new IndexOutOfRangeException();
  98. #endif
  99. return ref m_Array[index];
  100. }
  101. }
  102. }
  103. }