| | 1 | | // Copyright (c) 2020-2024 dotBunny Inc. |
| | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | 3 | | // See the LICENSE file in the project root for more information. |
| | 4 | |
|
| | 5 | | using GDX.Collections.Pooling; |
| | 6 | |
|
| | 7 | | namespace GDX.Collections.Generic |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// An optimized <see cref="System.Collections.Generic.List{T}" />-like data structure. |
| | 11 | | /// </summary> |
| | 12 | | /// <typeparam name="T">The type of the <see cref="object" />s contained within.</typeparam> |
| | 13 | | [VisualScriptingCompatible(1)] |
| | 14 | | public struct SimpleList<T> |
| | 15 | | { |
| | 16 | | /// <summary> |
| | 17 | | /// Internal array of backed data for the <see cref="SimpleList{T}" />. |
| | 18 | | /// </summary> |
| | 19 | | public T[] Array; |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// The current number of occupied elements in the <see cref="CircularBuffer{T}" />. |
| | 23 | | /// </summary> |
| | 24 | | /// <remarks>CAUTION! Changing this will alter the understanding of the data.</remarks> |
| | 25 | | public int Count; |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// Create a <see cref="SimpleList{T}" /> with an initial <paramref name="capacity" />. |
| | 29 | | /// </summary> |
| | 30 | | /// <param name="capacity">An initial sizing for the <see cref="Array" />.</param> |
| | 31 | | public SimpleList(int capacity) |
| 126 | 32 | | { |
| 126 | 33 | | Array = new T[capacity]; |
| 126 | 34 | | Count = 0; |
| 126 | 35 | | } |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// Create a <see cref="SimpleList{T}" /> providing an existing <paramref name="arrayToUse" />. |
| | 39 | | /// </summary> |
| | 40 | | /// <param name="arrayToUse">An existing array to use in the <see cref="SimpleList{T}" />.</param> |
| | 41 | | public SimpleList(T[] arrayToUse) |
| 3 | 42 | | { |
| 3 | 43 | | Array = arrayToUse; |
| 3 | 44 | | Count = 0; |
| 3 | 45 | | } |
| | 46 | |
|
| | 47 | | /// <summary> |
| | 48 | | /// Create a <see cref="SimpleList{T}" /> providing an existing <paramref name="arrayToUse" /> and setting t |
| | 49 | | /// <see cref="Count" />. |
| | 50 | | /// </summary> |
| | 51 | | /// <param name="arrayToUse">An existing array to use in the <see cref="SimpleList{T}" />.</param> |
| | 52 | | /// <param name="count">An existing element count.</param> |
| | 53 | | public SimpleList(T[] arrayToUse, int count) |
| 27 | 54 | | { |
| 27 | 55 | | Array = arrayToUse; |
| 27 | 56 | | Count = count; |
| 27 | 57 | | } |
| | 58 | |
|
| | 59 | | /// <summary> |
| | 60 | | /// Add an item to the <see cref="SimpleList{T}" /> without checking the <see cref="Array" /> size. |
| | 61 | | /// </summary> |
| | 62 | | /// <param name="item">A typed <see cref="object" /> to add.</param> |
| | 63 | | public void AddUnchecked(T item) |
| 337 | 64 | | { |
| 337 | 65 | | Array[Count] = item; |
| 334 | 66 | | ++Count; |
| 334 | 67 | | } |
| | 68 | |
|
| | 69 | | /// <summary> |
| | 70 | | /// Add an item to the <see cref="SimpleList{T}" />, checking if <see cref="Array" /> needs to be resized. |
| | 71 | | /// </summary> |
| | 72 | | /// <param name="item">A typed <see cref="object" /> to add.</param> |
| | 73 | | public void AddWithExpandCheck(T item) |
| 38 | 74 | | { |
| 38 | 75 | | int arrayLength = Array.Length; |
| | 76 | |
|
| 38 | 77 | | if (Count >= arrayLength) |
| 5 | 78 | | { |
| 5 | 79 | | arrayLength = arrayLength == 0 ? 1 : arrayLength; |
| 5 | 80 | | System.Array.Resize(ref Array, arrayLength * 2); |
| 5 | 81 | | } |
| | 82 | |
|
| 38 | 83 | | Array[Count] = item; |
| 38 | 84 | | ++Count; |
| 38 | 85 | | } |
| | 86 | |
|
| | 87 | | /// <summary> |
| | 88 | | /// Add an item to the <see cref="SimpleList{T}" />, checking if <see cref="Array" /> needs to be resized. |
| | 89 | | /// </summary> |
| | 90 | | /// <param name="item">A typed <see cref="object" /> to add.</param> |
| | 91 | | /// <param name="howMuchToExpand">How much to expand the array by.</param> |
| | 92 | | public void AddWithExpandCheck(T item, int howMuchToExpand) |
| 1 | 93 | | { |
| 1 | 94 | | if (Count >= Array.Length) |
| 1 | 95 | | { |
| 1 | 96 | | System.Array.Resize(ref Array, Array.Length + howMuchToExpand); |
| 1 | 97 | | } |
| | 98 | |
|
| 1 | 99 | | Array[Count] = item; |
| 1 | 100 | | ++Count; |
| 1 | 101 | | } |
| | 102 | |
|
| | 103 | | /// <summary> |
| | 104 | | /// Add an item to the <see cref="SimpleList{T}" />, checking if <see cref="Array" /> needs to be resized. |
| | 105 | | /// </summary> |
| | 106 | | /// <param name="item">A typed <see cref="object" /> to add.</param> |
| | 107 | | /// <param name="pool">Pool to allocate the array from if resizing.</param> |
| | 108 | | public void AddExpandNoClear(T item, ArrayPool<T> pool) |
| 5 | 109 | | { |
| 5 | 110 | | T[] array = Array; |
| 5 | 111 | | int count = Count; |
| 5 | 112 | | int arrayLength = array.Length; |
| | 113 | |
|
| 5 | 114 | | if (Count >= arrayLength) |
| 1 | 115 | | { |
| 1 | 116 | | arrayLength = arrayLength == 0 ? 1 : arrayLength; |
| 1 | 117 | | T[] newArray = pool.Get(arrayLength * 2); |
| 1 | 118 | | System.Array.Copy(array, 0, newArray, 0, arrayLength); |
| 1 | 119 | | pool.Return(array); |
| 1 | 120 | | array = newArray; |
| 1 | 121 | | Array = newArray; |
| 1 | 122 | | } |
| | 123 | |
|
| 5 | 124 | | array[count] = item; |
| 5 | 125 | | ++count; |
| 5 | 126 | | Count = count; |
| 5 | 127 | | } |
| | 128 | |
|
| | 129 | | /// <summary> |
| | 130 | | /// Add an item to the <see cref="SimpleList{T}" />, checking if <see cref="Array" /> needs to be resized. C |
| | 131 | | /// old array on resize after copying its contents. |
| | 132 | | /// </summary> |
| | 133 | | /// <param name="item">A typed <see cref="object" /> to add.</param> |
| | 134 | | /// <param name="pool">Pool to allocate the array from if resizing.</param> |
| | 135 | | public void AddExpandClearOld(T item, ArrayPool<T> pool) |
| 3 | 136 | | { |
| 3 | 137 | | T[] array = Array; |
| 3 | 138 | | int count = Count; |
| 3 | 139 | | int arrayLength = array.Length; |
| | 140 | |
|
| 3 | 141 | | if (Count >= arrayLength) |
| 1 | 142 | | { |
| 1 | 143 | | arrayLength = arrayLength == 0 ? 1 : arrayLength; |
| 1 | 144 | | T[] newArray = pool.Get(arrayLength * 2); |
| 1 | 145 | | System.Array.Copy(array, 0, newArray, 0, arrayLength); |
| 1 | 146 | | System.Array.Clear(array, 0, arrayLength); |
| 1 | 147 | | pool.Return(array); |
| 1 | 148 | | array = newArray; |
| 1 | 149 | | Array = newArray; |
| 1 | 150 | | } |
| | 151 | |
|
| 3 | 152 | | array[count] = item; |
| 3 | 153 | | ++count; |
| 3 | 154 | | Count = count; |
| 3 | 155 | | } |
| | 156 | |
|
| | 157 | | /// <summary> |
| | 158 | | /// Add an item to the <see cref="SimpleList{T}" />, checking if <see cref="Array" /> needs to be resized. C |
| | 159 | | /// unused indices of the new array on resize. |
| | 160 | | /// </summary> |
| | 161 | | /// <param name="item">A typed <see cref="object" /> to add.</param> |
| | 162 | | /// <param name="pool">Pool to allocate the array from if resizing.</param> |
| | 163 | | public void AddExpandClearNew(T item, ArrayPool<T> pool) |
| 5 | 164 | | { |
| 5 | 165 | | T[] array = Array; |
| 5 | 166 | | int count = Count; |
| 5 | 167 | | int arrayLength = array.Length; |
| | 168 | |
|
| 5 | 169 | | if (Count >= arrayLength) |
| 1 | 170 | | { |
| 1 | 171 | | arrayLength = arrayLength == 0 ? 1 : arrayLength; |
| 1 | 172 | | T[] newArray = pool.Get(arrayLength * 2); |
| 1 | 173 | | System.Array.Copy(array, 0, newArray, 0, arrayLength); |
| 1 | 174 | | System.Array.Clear(newArray, arrayLength, arrayLength); |
| 1 | 175 | | pool.Return(array); |
| 1 | 176 | | array = newArray; |
| 1 | 177 | | Array = newArray; |
| 1 | 178 | | } |
| | 179 | |
|
| 5 | 180 | | array[count] = item; |
| 5 | 181 | | ++count; |
| 5 | 182 | | Count = count; |
| 5 | 183 | | } |
| | 184 | |
|
| | 185 | | /// <summary> |
| | 186 | | /// Add an item to the <see cref="SimpleList{T}" />, checking if <see cref="Array" /> needs to be resized. |
| | 187 | | /// and new arrays on resize. |
| | 188 | | /// </summary> |
| | 189 | | /// <param name="item">A typed <see cref="object" /> to add.</param> |
| | 190 | | /// <param name="pool">Pool to allocate the array from if resizing.</param> |
| | 191 | | public void AddExpandClearBoth(T item, ArrayPool<T> pool) |
| 5 | 192 | | { |
| 5 | 193 | | T[] array = Array; |
| 5 | 194 | | int count = Count; |
| 5 | 195 | | int arrayLength = array.Length; |
| | 196 | |
|
| 5 | 197 | | if (Count >= arrayLength) |
| 1 | 198 | | { |
| 1 | 199 | | arrayLength = arrayLength == 0 ? 1 : arrayLength; |
| 1 | 200 | | T[] newArray = pool.Get(arrayLength * 2); |
| 1 | 201 | | System.Array.Copy(array, 0, newArray, 0, arrayLength); |
| 1 | 202 | | System.Array.Clear(array, 0, arrayLength); |
| 1 | 203 | | System.Array.Clear(newArray, arrayLength, arrayLength); |
| 1 | 204 | | pool.Return(array); |
| 1 | 205 | | array = newArray; |
| 1 | 206 | | Array = newArray; |
| 1 | 207 | | } |
| | 208 | |
|
| 5 | 209 | | array[count] = item; |
| 5 | 210 | | ++count; |
| 5 | 211 | | Count = count; |
| 5 | 212 | | } |
| | 213 | |
|
| | 214 | | /// <summary> |
| | 215 | | /// Clear out the <see cref="Array" /> in <see cref="SimpleList{T}" /> and sets the <see cref="Count" /> to |
| | 216 | | /// </summary> |
| | 217 | | public void Clear() |
| 57 | 218 | | { |
| 57 | 219 | | System.Array.Clear(Array, 0, Count); |
| 57 | 220 | | Count = 0; |
| 57 | 221 | | } |
| | 222 | |
|
| | 223 | | /// <summary> |
| | 224 | | /// Shrink/compact the backing <see cref="Array" /> so there is no unused space. |
| | 225 | | /// </summary> |
| | 226 | | public void Compact() |
| 0 | 227 | | { |
| 0 | 228 | | int arrayLength = Array.Length; |
| 0 | 229 | | if (Count < arrayLength) |
| 0 | 230 | | { |
| 0 | 231 | | System.Array.Resize(ref Array, Count); |
| 0 | 232 | | } |
| 0 | 233 | | } |
| | 234 | |
|
| | 235 | | /// <summary> |
| | 236 | | /// Insert an item into the <see cref="SimpleList{T}" /> without checking the <see cref="Array" /> size. |
| | 237 | | /// </summary> |
| | 238 | | /// <param name="item">A typed <see cref="object" /> to insert.</param> |
| | 239 | | /// <param name="index">The index in <see cref="Array" /> to add the <paramref name="item" /> at.</param> |
| | 240 | | public void InsertUnchecked(int index, T item) |
| 3 | 241 | | { |
| 3 | 242 | | System.Array.Copy(Array, index, Array, index + 1, Count - index); |
| 3 | 243 | | Array[index] = item; |
| 3 | 244 | | ++Count; |
| 3 | 245 | | } |
| | 246 | |
|
| | 247 | | /// <summary> |
| | 248 | | /// Insert an item into the <see cref="SimpleList{T}" />, checking if <see cref="Array" /> needs to be resiz |
| | 249 | | /// </summary> |
| | 250 | | /// <param name="item">A typed <see cref="object" /> to insert.</param> |
| | 251 | | /// <param name="index">The index in <see cref="Array" /> to add the <paramref name="item" /> at.</param> |
| | 252 | | public void InsertWithExpandCheck(int index, T item) |
| 3 | 253 | | { |
| 3 | 254 | | int arrayLength = Array.Length; |
| 3 | 255 | | if (Count >= arrayLength) |
| 3 | 256 | | { |
| 3 | 257 | | arrayLength = arrayLength == 0 ? 1 : arrayLength; |
| 3 | 258 | | System.Array.Resize(ref Array, arrayLength * 2); |
| 3 | 259 | | } |
| | 260 | |
|
| 3 | 261 | | System.Array.Copy(Array, index, Array, index + 1, Count - index); |
| 3 | 262 | | Array[index] = item; |
| 3 | 263 | | ++Count; |
| 3 | 264 | | } |
| | 265 | |
|
| | 266 | | /// <summary> |
| | 267 | | /// Insert an item into the <see cref="SimpleList{T}" />, checking if <see cref="Array" /> needs to be resiz |
| | 268 | | /// </summary> |
| | 269 | | /// <param name="item">A typed <see cref="object" /> to insert.</param> |
| | 270 | | /// <param name="index">The index in <see cref="Array" /> to add the <paramref name="item" /> at.</param> |
| | 271 | | /// <param name="howMuchToExpand">The number of elements to add at the end of the array when expanding.</param> |
| | 272 | | public void InsertWithExpandCheck(int index, T item, int howMuchToExpand) |
|