| | | 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) |
| | 3 | 273 | | { |
| | 3 | 274 | | int arrayLength = Array.Length; |
| | 3 | 275 | | if (Count >= arrayLength) |
| | 3 | 276 | | { |
| | 3 | 277 | | arrayLength = arrayLength == 0 ? 1 : arrayLength; |
| | 3 | 278 | | System.Array.Resize(ref Array, arrayLength + howMuchToExpand); |
| | 3 | 279 | | } |
| | | 280 | | |
| | 3 | 281 | | System.Array.Copy(Array, index, Array, index + 1, Count - index); |
| | 3 | 282 | | Array[index] = item; |
| | 3 | 283 | | ++Count; |
| | 3 | 284 | | } |
| | | 285 | | |
| | | 286 | | /// <summary> |
| | | 287 | | /// Insert an item into the <see cref="SimpleList{T}" />, checking if <see cref="Array" /> needs to be resiz |
| | | 288 | | /// </summary> |
| | | 289 | | /// <param name="item">A typed <see cref="object" /> to insert.</param> |
| | | 290 | | /// <param name="index">The index in <see cref="Array" /> to add the <paramref name="item" /> at.</param> |
| | | 291 | | /// <param name="pool">Pool to allocate the array from if resizing.</param> |
| | | 292 | | public void InsertExpandNoClear(int index, T item, ArrayPool<T> pool) |
| | 1 | 293 | | { |
| | 1 | 294 | | T[] array = Array; |
| | 1 | 295 | | int count = Count; |
| | 1 | 296 | | int arrayLength = array.Length; |
| | | 297 | | |
| | 1 | 298 | | if (Count >= arrayLength) |
| | 1 | 299 | | { |
| | 1 | 300 | | arrayLength = arrayLength == 0 ? 1 : arrayLength; |
| | 1 | 301 | | T[] newArray = pool.Get(arrayLength * 2); |
| | 1 | 302 | | System.Array.Copy(array, 0, newArray, 0, arrayLength); |
| | 1 | 303 | | pool.Return(array); |
| | 1 | 304 | | array = newArray; |
| | 1 | 305 | | Array = newArray; |
| | 1 | 306 | | } |
| | | 307 | | |
| | 1 | 308 | | System.Array.Copy(array, index, array, index + 1, Count - index); |
| | 1 | 309 | | array[index] = item; |
| | 1 | 310 | | ++count; |
| | 1 | 311 | | Count = count; |
| | 1 | 312 | | } |
| | | 313 | | |
| | | 314 | | /// <summary> |
| | | 315 | | /// Insert an item into the <see cref="SimpleList{T}" />, checking if <see cref="Array" /> needs to be resiz |
| | | 316 | | /// the old array on resize after copying its contents. |
| | | 317 | | /// </summary> |
| | | 318 | | /// <param name="item">A typed <see cref="object" /> to insert.</param> |
| | | 319 | | /// <param name="index">The index in <see cref="Array" /> to add the <paramref name="item" /> at.</param> |
| | | 320 | | /// <param name="pool">Pool to allocate the array from if resizing.</param> |
| | | 321 | | public void InsertExpandClearOld(int index, T item, ArrayPool<T> pool) |
| | 1 | 322 | | { |
| | 1 | 323 | | T[] array = Array; |
| | 1 | 324 | | int count = Count; |
| | 1 | 325 | | int arrayLength = array.Length; |
| | | 326 | | |
| | 1 | 327 | | if (Count >= arrayLength) |
| | 1 | 328 | | { |
| | 1 | 329 | | arrayLength = arrayLength == 0 ? 1 : arrayLength; |
| | 1 | 330 | | T[] newArray = pool.Get(arrayLength * 2); |
| | 1 | 331 | | System.Array.Copy(array, 0, newArray, 0, arrayLength); |
| | 1 | 332 | | System.Array.Clear(array, 0, arrayLength); |
| | 1 | 333 | | pool.Return(array); |
| | 1 | 334 | | array = newArray; |
| | 1 | 335 | | Array = newArray; |
| | 1 | 336 | | } |
| | | 337 | | |
| | 1 | 338 | | System.Array.Copy(array, index, array, index + 1, Count - index); |
| | 1 | 339 | | array[index] = item; |
| | 1 | 340 | | ++count; |
| | 1 | 341 | | Count = count; |
| | 1 | 342 | | } |
| | | 343 | | |
| | | 344 | | /// <summary> |
| | | 345 | | /// Insert an item into the <see cref="SimpleList{T}" />, checking if <see cref="Array" /> needs to be resiz |
| | | 346 | | /// unused indices of the new array on resize. |
| | | 347 | | /// </summary> |
| | | 348 | | /// <param name="item">A typed <see cref="object" /> to insert.</param> |
| | | 349 | | /// <param name="index">The index in <see cref="Array" /> to add the <paramref name="item" /> at.</param> |
| | | 350 | | /// <param name="pool">Pool to allocate the array from if resizing.</param> |
| | | 351 | | public void InsertExpandClearNew(int index, T item, ArrayPool<T> pool) |
| | 1 | 352 | | { |
| | 1 | 353 | | T[] array = Array; |
| | 1 | 354 | | int count = Count; |
| | 1 | 355 | | int arrayLength = array.Length; |
| | | 356 | | |
| | 1 | 357 | | if (Count >= arrayLength) |
| | 1 | 358 | | { |
| | 1 | 359 | | arrayLength = arrayLength == 0 ? 1 : arrayLength; |
| | 1 | 360 | | T[] newArray = pool.Get(arrayLength * 2); |
| | 1 | 361 | | System.Array.Copy(array, 0, newArray, 0, arrayLength); |
| | 1 | 362 | | System.Array.Clear(newArray, arrayLength, arrayLength); |
| | 1 | 363 | | pool.Return(array); |
| | 1 | 364 | | array = newArray; |
| | 1 | 365 | | Array = newArray; |
| | 1 | 366 | | } |
| | | 367 | | |
| | 1 | 368 | | System.Array.Copy(array, index, array, index + 1, Count - index); |
| | 1 | 369 | | array[index] = item; |
| | 1 | 370 | | ++count; |
| | 1 | 371 | | Count = count; |
| | 1 | 372 | | } |
| | | 373 | | |
| | | 374 | | /// <summary> |
| | | 375 | | /// Insert an item into the <see cref="SimpleList{T}" />, checking if <see cref="Array" /> needs to be resiz |
| | | 376 | | /// old and new arrays on resize. |
| | | 377 | | /// </summary> |
| | | 378 | | /// <param name="item">A typed <see cref="object" /> to insert.</param> |
| | | 379 | | /// <param name="index">The index in <see cref="Array" /> to add the <paramref name="item" /> at.</param> |
| | | 380 | | /// <param name="pool">Pool to allocate the array from if resizing.</param> |
| | | 381 | | public void InsertExpandClearBoth(int index, T item, ArrayPool<T> pool) |
| | 1 | 382 | | { |
| | 1 | 383 | | T[] array = Array; |
| | 1 | 384 | | int count = Count; |
| | 1 | 385 | | int arrayLength = array.Length; |
| | | 386 | | |
| | 1 | 387 | | if (Count >= arrayLength) |
| | 1 | 388 | | { |
| | 1 | 389 | | arrayLength = arrayLength == 0 ? 1 : arrayLength; |
| | 1 | 390 | | T[] newArray = pool.Get(arrayLength * 2); |
| | 1 | 391 | | System.Array.Copy(array, 0, newArray, 0, arrayLength); |
| | 1 | 392 | | System.Array.Clear(array, 0, arrayLength); |
| | 1 | 393 | | System.Array.Clear(newArray, arrayLength, arrayLength); |
| | 1 | 394 | | pool.Return(array); |
| | 1 | 395 | | array = newArray; |
| | 1 | 396 | | Array = newArray; |
| | 1 | 397 | | } |
| | | 398 | | |
| | 1 | 399 | | System.Array.Copy(array, index, array, index + 1, Count - index); |
| | 1 | 400 | | array[index] = item; |
| | 1 | 401 | | ++count; |
| | 1 | 402 | | Count = count; |
| | 1 | 403 | | } |
| | | 404 | | |
| | | 405 | | /// <summary> |
| | | 406 | | /// Remove an item from the <see cref="SimpleList{T}" /> at a specific <paramref name="index" />. |
| | | 407 | | /// </summary> |
| | | 408 | | /// <param name="index">The target index.</param> |
| | | 409 | | public void RemoveAt(int index) |
| | 23 | 410 | | { |
| | 23 | 411 | | int newLength = Count - 1; |
| | | 412 | | |
| | 23 | 413 | | Array[index] = default; |
| | | 414 | | |
| | 23 | 415 | | if (index < Array.Length) |
| | 23 | 416 | | { |
| | 23 | 417 | | System.Array.Copy(Array, index + 1, Array, index, newLength - index); |
| | 23 | 418 | | } |
| | | 419 | | |
| | 23 | 420 | | Count = newLength; |
| | 23 | 421 | | } |
| | | 422 | | |
| | | 423 | | /// <summary> |
| | | 424 | | /// Remove an item from the <see cref="SimpleList{T}" /> at a specific <paramref name="index" />, swapping i |
| | | 425 | | /// item from the highest used index. |
| | | 426 | | /// </summary> |
| | | 427 | | /// <param name="index">The target index.</param> |
| | | 428 | | public void RemoveAtSwapBack(int index) |
| | 15 | 429 | | { |
| | 15 | 430 | | int newLength = Count - 1; |
| | 15 | 431 | | T[] array = Array; |
| | 15 | 432 | | array[index] = array[newLength]; |
| | 15 | 433 | | array[newLength] = default; |
| | | 434 | | |
| | 15 | 435 | | Count = newLength; |
| | 15 | 436 | | } |
| | | 437 | | |
| | | 438 | | /// <summary> |
| | | 439 | | /// Remove the last element in the <see cref="SimpleList{T}" />. |
| | | 440 | | /// </summary> |
| | | 441 | | public void RemoveFromBack() |
| | 1 | 442 | | { |
| | 1 | 443 | | int newLength = Count - 1; |
| | 1 | 444 | | Array[newLength] = default; |
| | 1 | 445 | | Count = newLength; |
| | 1 | 446 | | } |
| | | 447 | | |
| | | 448 | | /// <summary> |
| | | 449 | | /// Resizes the <see cref="Array" />, ensuring there are the provided number of empty spots in it. |
| | | 450 | | /// </summary> |
| | | 451 | | /// <param name="numberToReserve">Number of desired empty spots.</param> |
| | | 452 | | public void Reserve(int numberToReserve) |
| | 1 | 453 | | { |
| | 1 | 454 | | int combinedLength = Count + numberToReserve; |
| | | 455 | | |
| | 1 | 456 | | if (combinedLength > Array.Length) |
| | 1 | 457 | | { |
| | 1 | 458 | | System.Array.Resize(ref Array, combinedLength); |
| | 1 | 459 | | } |
| | 1 | 460 | | } |
| | | 461 | | |
| | | 462 | | /// <summary> |
| | | 463 | | /// Resizes the <see cref="Array" />, ensuring there are the provided number of empty spots in it. |
| | | 464 | | /// </summary> |
| | | 465 | | /// <param name="numberToReserve">Number of desired empty spots.</param> |
| | | 466 | | /// <param name="pool">Pool to allocate the array from if resizing.</param> |
| | | 467 | | public void ReserveNoClear(int numberToReserve, ArrayPool<T> pool) |
| | 1 | 468 | | { |
| | 1 | 469 | | T[] array = Array; |
| | 1 | 470 | | int count = Count; |
| | 1 | 471 | | int arrayLength = array.Length; |
| | | 472 | | |
| | 1 | 473 | | int combinedLength = count + numberToReserve; |
| | | 474 | | |
| | 1 | 475 | | if (combinedLength > arrayLength) |
| | 1 | 476 | | { |
| | 1 | 477 | | T[] newArray = pool.Get(combinedLength); |
| | 1 | 478 | | System.Array.Copy(array, 0, newArray, 0, arrayLength); |
| | 1 | 479 | | pool.Return(array); |
| | 1 | 480 | | Array = newArray; |
| | 1 | 481 | | } |
| | 1 | 482 | | } |
| | | 483 | | |
| | | 484 | | /// <summary> |
| | | 485 | | /// Resizes the <see cref="Array" />, ensuring there are the provided number of empty spots in it. Clears th |
| | | 486 | | /// on resize after copying its contents. |
| | | 487 | | /// </summary> |
| | | 488 | | /// <param name="numberToReserve">Number of desired empty spots.</param> |
| | | 489 | | /// <param name="pool">Pool to allocate the array from if resizing.</param> |
| | | 490 | | public void ReserveClearOld(int numberToReserve, ArrayPool<T> pool) |
| | 1 | 491 | | { |
| | 1 | 492 | | T[] array = Array; |
| | 1 | 493 | | int count = Count; |
| | 1 | 494 | | int arrayLength = array.Length; |
| | | 495 | | |
| | 1 | 496 | | int combinedLength = count + numberToReserve; |
| | | 497 | | |
| | 1 | 498 | | if (combinedLength > arrayLength) |
| | 1 | 499 | | { |
| | 1 | 500 | | T[] newArray = pool.Get(combinedLength); |
| | 1 | 501 | | System.Array.Copy(array, 0, newArray, 0, arrayLength); |
| | 1 | 502 | | System.Array.Clear(array, 0, arrayLength); |
| | 1 | 503 | | pool.Return(array); |
| | 1 | 504 | | Array = newArray; |
| | 1 | 505 | | } |
| | 1 | 506 | | } |
| | | 507 | | |
| | | 508 | | /// <summary> |
| | | 509 | | /// Resizes the <see cref="Array" />, ensuring there are the provided number of empty spots in it. Clears u |
| | | 510 | | /// indices of the new array on resize. |
| | | 511 | | /// </summary> |
| | | 512 | | /// <param name="numberToReserve">Number of desired empty spots.</param> |
| | | 513 | | /// <param name="pool">Pool to allocate the array from if resizing.</param> |
| | | 514 | | public void ReserveClearNew(int numberToReserve, ArrayPool<T> pool) |
| | 1 | 515 | | { |
| | 1 | 516 | | T[] array = Array; |
| | 1 | 517 | | int count = Count; |
| | 1 | 518 | | int arrayLength = array.Length; |
| | | 519 | | |
| | 1 | 520 | | int combinedLength = count + numberToReserve; |
| | | 521 | | |
| | 1 | 522 | | if (combinedLength > arrayLength) |
| | 1 | 523 | | { |
| | 1 | 524 | | T[] newArray = pool.Get(combinedLength); |
| | 1 | 525 | | System.Array.Copy(array, 0, newArray, 0, arrayLength); |
| | 1 | 526 | | System.Array.Clear(newArray, arrayLength, arrayLength); |
| | 1 | 527 | | pool.Return(array); |
| | 1 | 528 | | Array = newArray; |
| | 1 | 529 | | } |
| | 1 | 530 | | } |
| | | 531 | | |
| | | 532 | | /// <summary> |
| | | 533 | | /// Resizes the <see cref="Array" />, ensuring there are the provided number of empty spots in it. Clears ol |
| | | 534 | | /// arrays on resize. |
| | | 535 | | /// </summary> |
| | | 536 | | /// <param name="numberToReserve">Number of desired empty spots.</param> |
| | | 537 | | /// <param name="pool">Pool to allocate the array from if resizing.</param> |
| | | 538 | | public void ReserveClearBoth(int numberToReserve, ArrayPool<T> pool) |
| | 1 | 539 | | { |
| | 1 | 540 | | T[] array = Array; |
| | 1 | 541 | | int count = Count; |
| | 1 | 542 | | int arrayLength = array.Length; |
| | | 543 | | |
| | 1 | 544 | | int combinedLength = count + numberToReserve; |
| | | 545 | | |
| | 1 | 546 | | if (combinedLength > arrayLength) |
| | 1 | 547 | | { |
| | 1 | 548 | | T[] newArray = pool.Get(combinedLength); |
| | 1 | 549 | | System.Array.Copy(array, 0, newArray, 0, arrayLength); |
| | 1 | 550 | | System.Array.Clear(array, 0, arrayLength); |
| | 1 | 551 | | System.Array.Clear(newArray, arrayLength, arrayLength); |
| | 1 | 552 | | pool.Return(array); |
| | 1 | 553 | | Array = newArray; |
| | 1 | 554 | | } |
| | 1 | 555 | | } |
| | | 556 | | |
| | | 557 | | /// <summary> |
| | | 558 | | /// Reverse the order of <see cref="Array" />. |
| | | 559 | | /// </summary> |
| | | 560 | | public void Reverse() |
| | 3 | 561 | | { |
| | | 562 | | // ReSharper disable once TooWideLocalVariableScope |
| | | 563 | | T temporaryStorage; |
| | | 564 | | |
| | 3 | 565 | | int lastIndex = Count - 1; |
| | 3 | 566 | | int middleIndex = Count / 2; |
| | | 567 | | |
| | 18 | 568 | | for (int currentElementIndex = 0; currentElementIndex < middleIndex; currentElementIndex++) |
| | 6 | 569 | | { |
| | | 570 | | // Store the swap value |
| | 6 | 571 | | temporaryStorage = Array[currentElementIndex]; |
| | 6 | 572 | | int swapElementIndex = lastIndex - currentElementIndex; |
| | | 573 | | |
| | | 574 | | // Swap values |
| | 6 | 575 | | Array[currentElementIndex] = Array[swapElementIndex]; |
| | 6 | 576 | | Array[swapElementIndex] = temporaryStorage; |
| | 6 | 577 | | } |
| | 3 | 578 | | } |
| | | 579 | | } |
| | | 580 | | } |