| | 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 System; |
| | 6 | | using System.Runtime.CompilerServices; |
| | 7 | |
|
| | 8 | | namespace GDX.Collections |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// An adapter collection for external data arrays that allows constant-time insertion, deletion, and lookup by |
| | 12 | | /// handle, as well as array-like iteration. |
| | 13 | | /// </summary> |
| | 14 | | public struct SparseSet |
| | 15 | | { |
| | 16 | | /// <summary> |
| | 17 | | /// Holds references to the sparse array for swapping indices. |
| | 18 | | /// </summary> |
| | 19 | | public int[] DenseArray; |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// Holds references to dense array indices. |
| | 23 | | /// </summary> |
| | 24 | | /// <remarks> |
| | 25 | | /// Its own indices are claimed and freed via a free-list. |
| | 26 | | /// </remarks> |
| | 27 | | public int[] SparseArray; |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// How many indices are being used currently? |
| | 31 | | /// </summary> |
| | 32 | | public int Count; |
| | 33 | |
|
| | 34 | | /// <summary> |
| | 35 | | /// The first free (currently unused) index in the sparse array. |
| | 36 | | /// </summary> |
| | 37 | | public int FreeIndex; |
| | 38 | |
|
| | 39 | | /// <summary> |
| | 40 | | /// Create a <see cref="SparseSet" /> with an <paramref name="initialCapacity" />. |
| | 41 | | /// </summary> |
| | 42 | | /// <param name="initialCapacity">The initial capacity of the sparse and dense int arrays.</param> |
| | 43 | | public SparseSet(int initialCapacity) |
| 38 | 44 | | { |
| 38 | 45 | | DenseArray = new int[initialCapacity]; |
| 38 | 46 | | SparseArray = new int[initialCapacity]; |
| 38 | 47 | | Count = 0; |
| 38 | 48 | | FreeIndex = 0; |
| | 49 | |
|
| 228 | 50 | | for (int i = 0; i < initialCapacity; i++) |
| 76 | 51 | | { |
| 76 | 52 | | DenseArray[i] = -1; |
| 76 | 53 | | SparseArray[i] = i + 1; |
| 76 | 54 | | } |
| 38 | 55 | | } |
| | 56 | |
|
| | 57 | | /// <summary> |
| | 58 | | /// Create a <see cref="SparseSet" /> with an <paramref name="initialCapacity" />. |
| | 59 | | /// </summary> |
| | 60 | | /// <param name="initialCapacity">The initial capacity of the sparse and dense int arrays.</param> |
| | 61 | | /// <param name="versionArray">Array containing version numbers to check sparse references against.</param> |
| | 62 | | public SparseSet(int initialCapacity, out ulong[] versionArray) |
| 16 | 63 | | { |
| 16 | 64 | | DenseArray = new int[initialCapacity]; |
| 16 | 65 | | SparseArray = new int[initialCapacity]; |
| 16 | 66 | | versionArray = new ulong[initialCapacity]; |
| 16 | 67 | | Count = 0; |
| 16 | 68 | | FreeIndex = 0; |
| | 69 | |
|
| 116 | 70 | | for (int i = 0; i < initialCapacity; i++) |
| 42 | 71 | | { |
| 42 | 72 | | DenseArray[i] = -1; |
| 42 | 73 | | SparseArray[i] = i + 1; |
| 42 | 74 | | versionArray[i] = 1; |
| 42 | 75 | | } |
| 16 | 76 | | } |
| | 77 | |
|
| | 78 | | /// <summary> |
| | 79 | | /// Adds a sparse/dense index pair to the set and expands the arrays if necessary. |
| | 80 | | /// </summary> |
| | 81 | | /// <param name="expandBy">How many indices to expand by.</param> |
| | 82 | | /// <param name="sparseIndex">The sparse index allocated.</param> |
| | 83 | | /// <param name="denseIndex">The dense index allocated.</param> |
| | 84 | | /// <returns>True if the index pool expanded.</returns> |
| | 85 | | public bool AddWithExpandCheck(int expandBy, out int sparseIndex, out int denseIndex) |
| 6 | 86 | | { |
| 6 | 87 | | int indexToClaim = FreeIndex; |
| 6 | 88 | | int currentCapacity = SparseArray.Length; |
| 6 | 89 | | bool needsExpansion = false; |
| | 90 | |
|
| 6 | 91 | | if (indexToClaim >= currentCapacity) |
| 3 | 92 | | { |
| | 93 | | // We're out of space, the last free index points to nothing. Allocate more indices. |
| 3 | 94 | | needsExpansion = true; |
| | 95 | |
|
| 3 | 96 | | int newCapacity = currentCapacity + expandBy; |
| | 97 | |
|
| 3 | 98 | | int[] newSparseArray = new int[newCapacity]; |
| 3 | 99 | | Array.Copy(SparseArray, 0, newSparseArray, 0, currentCapacity); |
| 3 | 100 | | SparseArray = newSparseArray; |
| | 101 | |
|
| 3 | 102 | | int[] newDenseArray = new int[newCapacity]; |
| 3 | 103 | | Array.Copy(DenseArray, 0, newDenseArray, 0, currentCapacity); |
| 3 | 104 | | DenseArray = newDenseArray; |
| | 105 | |
|
| 36 | 106 | | for (int i = currentCapacity; i < newCapacity; i++) |
| 15 | 107 | | { |
| 15 | 108 | | SparseArray[i] = i + 1; // Build the free list chain. |
| 15 | 109 | | DenseArray[i] = -1; // Set new dense indices as unclaimed. |
| 15 | 110 | | } |
| 3 | 111 | | } |
| | 112 | |
|
| 6 | 113 | | int nextFreeIndex = SparseArray[indexToClaim]; |
| 6 | 114 | | DenseArray[Count] = indexToClaim; // Point the next dense id at our newly claimed sparse index. |
| 6 | 115 | | SparseArray[indexToClaim] = Count; // Point our newly claimed sparse index at the dense index. |
| 6 | 116 | | denseIndex = Count; |
| | 117 | |
|
| 6 | 118 | | ++Count; |
| 6 | 119 | | FreeIndex = nextFreeIndex; // Set the free list head for next time. |
| | 120 | |
|
| 6 | 121 | | sparseIndex = indexToClaim; |
| 6 | 122 | | return needsExpansion; |
| 6 | 123 | | } |
| | 124 | |
|
| | 125 | | /// <summary> |
| | 126 | | /// Adds a sparse/dense index pair to the set and expands the arrays if necessary. |
| | 127 | | /// </summary> |
| | 128 | | /// <param name="expandBy">How many indices to expand by.</param> |
| | 129 | | /// <param name="sparseIndex">The sparse index allocated.</param> |
| | 130 | | /// <param name="denseIndex">The dense index allocated.</param> |
| | 131 | | /// <param name="versionArray">The array containing the version number to check against.</param> |
| | 132 | | /// <param name="version">Enables detection of use-after-free errors when using the sparse index as a reference. |
| | 133 | | /// <returns>True if the index pool expanded.</returns> |
| | 134 | | public bool AddWithExpandCheck(int expandBy, out int sparseIndex, out int denseIndex, ref ulong[] versionArray, |
| | 135 | | out ulong version) |
| 0 | 136 | | { |
| 0 | 137 | | int indexToClaim = FreeIndex; |
| 0 | 138 | | int currentCapacity = SparseArray.Length; |
| 0 | 139 | | bool needsExpansion = false; |
| | 140 | |
|
| 0 | 141 | | if (indexToClaim >= currentCapacity) |
| 0 | 142 | | { |
| | 143 | | // We're out of space, the last free index points to nothing. Allocate more indices. |
| 0 | 144 | | needsExpansion = true; |
| | 145 | |
|
| 0 | 146 | | int newCapacity = currentCapacity + expandBy; |
| | 147 | |
|
| 0 | 148 | | int[] newSparseArray = new int[newCapacity]; |
| 0 | 149 | | Array.Copy(SparseArray, 0, newSparseArray, 0, currentCapacity); |
| 0 | 150 | | SparseArray = newSparseArray; |
| | 151 | |
|
| 0 | 152 | | int[] newDenseArray = new int[newCapacity]; |
| 0 | 153 | | Array.Copy(DenseArray, 0, newDenseArray, 0, currentCapacity); |
| 0 | 154 | | DenseArray = newDenseArray; |
| | 155 | |
|
| 0 | 156 | | ulong[] newVersionArray = new ulong[newCapacity]; |
| 0 | 157 | | Array.Copy(versionArray, 0, newVersionArray, 0, currentCapacity); |
| 0 | 158 | | versionArray = newVersionArray; |
| | 159 | |
|
| 0 | 160 | | for (int i = currentCapacity; i < newCapacity; i++) |
| 0 | 161 | | { |
| 0 | 162 | | SparseArray[i] = i + 1; // Build the free list chain. |
| 0 | 163 | | DenseArray[i] = -1; // Set new dense indices as unclaimed. |
| 0 | 164 | | newVersionArray[i] = 1; |
| 0 | 165 | | } |
| 0 | 166 | | } |
| | 167 | |
|
| 0 | 168 | | int nextFreeIndex = SparseArray[indexToClaim]; |
| 0 | 169 | | DenseArray[Count] = indexToClaim; // Point the next dense id at our newly claimed sparse index. |
| 0 | 170 | | SparseArray[indexToClaim] = Count; // Point our newly claimed sparse index at the dense index. |
| 0 | 171 | | denseIndex = Count; |
| | 172 | |
|
| 0 | 173 | | version = versionArray[indexToClaim]; |
| | 174 | |
|
| 0 | 175 | | ++Count; |
| 0 | 176 | | FreeIndex = nextFreeIndex; // Set the free list head for next time. |
| | 177 | |
|
| 0 | 178 | | sparseIndex = indexToClaim; |
| 0 | 179 | | return needsExpansion; |
| 0 | 180 | | } |
| | 181 | |
|
| | 182 | | /// <summary> |
| | 183 | | /// Adds a sparse/dense index pair to the set without checking if the set needs to expand. |
| | 184 | | /// </summary> |
| | 185 | | /// <param name="sparseIndex">The sparse index allocated.</param> |
| | 186 | | /// <param name="denseIndex">The dense index allocated.</param> |
| | 187 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 188 | | public void AddUnchecked(out int sparseIndex, out int denseIndex) |
| 20 | 189 | | { |
| 20 | 190 | | int indexToClaim = FreeIndex; |
| 20 | 191 | | int nextFreeIndex = SparseArray[indexToClaim]; |
| 19 | 192 | | DenseArray[Count] = indexToClaim; // Point the next dense id at our newly claimed sparse index. |
| 19 | 193 | | SparseArray[indexToClaim] = Count; // Point our newly claimed sparse index at the dense index. |
| | 194 | |
|
| 19 | 195 | | sparseIndex = indexToClaim; |
| 19 | 196 | | denseIndex = Count; |
| 19 | 197 | | ++Count; |
| 19 | 198 | | FreeIndex = nextFreeIndex; // Set the free list head for next time. |
| 19 | 199 | | } |
| | 200 | |
|
| | 201 | | /// <summary> |
| | 202 | | /// Adds a sparse/dense index pair to the set without checking if the set needs to expand. |
| | 203 | | /// </summary> |
| | 204 | | /// <param name="sparseIndex">The sparse index allocated.</param> |
| | 205 | | /// <param name="denseIndex">The dense index allocated.</param> |
| | 206 | | /// <param name="versionArray">The array containing the version number to check against.</param> |
| | 207 | | /// <param name="version">Enables detection of use-after-free errors when using the sparse index as a reference. |
| | 208 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 209 | | public void AddUnchecked(out int sparseIndex, out int denseIndex, ulong[] versionArray, out ulong version) |
| 24 | 210 | | { |
| 24 | 211 | | int indexToClaim = FreeIndex; |
| 24 | 212 | | int nextFreeIndex = SparseArray[indexToClaim]; |
| 23 | 213 | | DenseArray[Count] = indexToClaim; // Point the next dense id at our newly claimed sparse index. |
| 23 | 214 | | SparseArray[indexToClaim] = Count; // Point our newly claimed sparse index at the dense index. |
| | 215 | |
|
| 23 | 216 | | version = versionArray[indexToClaim]; |
| 23 | 217 | | sparseIndex = indexToClaim; |
| 23 | 218 | | denseIndex = Count; |
| | 219 | |
|
| 23 | 220 | | ++Count; |
| 23 | 221 | | FreeIndex = nextFreeIndex; // Set the free list head for next time. |
| 23 | 222 | | } |
| | 223 | |
|
| | 224 | | /// <summary> |
| | 225 | | /// Gets the value of the sparse array at the given index without any data validation. |
| | 226 | | /// </summary> |
| | 227 | | /// <param name="sparseIndex">The index to check in the sparse array.</param> |
| | 228 | | /// <returns>The dense index at the given sparse index.</returns> |
| | 229 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 230 | | public int GetDenseIndexUnchecked(int sparseIndex) |
| 1 | 231 | | { |
| 1 | 232 | | return SparseArray[sparseIndex]; |
| 1 | 233 | | } |
| | 234 | |
|
| | 235 | | /// <summary> |
| | 236 | | /// Gets the value of the sparse array at the given index, |
| | 237 | | /// or -1 if the dense and sparse indices don't point to each other or if the dense index is outside the den |
| | 238 | | /// </summary> |
| | 239 | | /// <param name="sparseIndex">The index in the sparse array to check against.</param> |
| | 240 | | /// <returns>The dense index pointed to by the current sparse index, or -1 if invalid.</returns> |
| | 241 | | public int GetDenseIndexWithBoundsCheck(int sparseIndex) |
| 4 | 242 | | { |
| 4 | 243 | | if (sparseIndex >= 0 && sparseIndex < SparseArray.Length) |
| 2 | 244 | | { |
| 2 | 245 | | int denseIndex = SparseArray[sparseIndex]; |
| | 246 | |
|
| 2 | 247 | | if (denseIndex < Count && denseIndex >= 0) |
| 1 | 248 | | { |
| 1 | 249 | | int sparseIndexAtDenseIndex = DenseArray[denseIndex]; |
| | 250 | |
|
| 1 | 251 | | if (sparseIndex == sparseIndexAtDenseIndex) |
| 1 | 252 | | { |
| 1 | 253 | | return denseIndex; |
| | 254 | | } |
| 0 | 255 | | } |
| 1 | 256 | | } |
| | 257 | |
|
| 3 | 258 | | return -1; |
| 4 | 259 | | } |
| | 260 | |
|
| | 261 | | /// <summary> |
| | 262 | | /// Gets the value of the sparse array at the given index, |
| | 263 | | /// or -1 if the version number does not match. |
| | 264 | | /// </summary> |
| | 265 | | /// <param name="sparseIndex">The index in the sparse array to check against.</param> |
| | 266 | | /// <param name="version">The version number associated with the sparse index.</param> |
| | 267 | | /// <param name="versionArray">The array containing the version number to check against.</param> |
| | 268 | | /// <returns>The dense index pointed to by the current sparse index, or -1 if invalid.</returns> |
| | 269 | | public int GetDenseIndexWithVersionCheck(int sparseIndex, ulong version, ulong[] versionArray) |
| 4 | 270 | | { |
| 4 | 271 | | int denseIndex = SparseArray[sparseIndex]; |
| 4 | 272 | | ulong versionAtSparseIndex = versionArray[sparseIndex]; |
| | 273 | |
|
| 4 | 274 | | if (version == versionAtSparseIndex) |
| 2 | 275 | | { |
| 2 | 276 | | return denseIndex; |
| | 277 | | } |
| | 278 | |
|
| 2 | 279 | | return -1; |
| 4 | 280 | | } |
| | 281 | |
|
| | 282 | | /// <summary> |
| | 283 | | /// Gets the value of the sparse array at the given index, |
| | 284 | | /// or -1 if the given sparse index is invalid.. |
| | 285 | | /// </summary> |
| | 286 | | /// <param name="sparseIndex">The index in the sparse array to check against.</param> |
| | 287 | | /// <param name="version">The version number associated with the sparse index.</param> |
| | 288 | | /// <param name="versionArray">The array containing the version number to check against.</param> |
| | 289 | | /// <returns>The dense index pointed to by the current sparse index, or -1 if invalid.</returns> |
| | 290 | | public int GetDenseIndexWithBoundsAndVersionCheck(int sparseIndex, ulong version, ulong[] versionArray) |
| 7 | 291 | | { |
| 7 | 292 | | if (sparseIndex >= 0 && sparseIndex < SparseArray.Length) |
| 5 | 293 | | { |
| 5 | 294 | | int denseIndex = SparseArray[sparseIndex]; |
| 5 | 295 | | ulong versionAtSparseIndex = versionArray[sparseIndex]; |
| | 296 | |
|
| 5 | 297 | | if (versionAtSparseIndex == version && denseIndex < Count && denseIndex >= 0) |
| 2 | 298 | | { |
| 2 | 299 | | int sparseIndexAtDenseIndex = DenseArray[denseIndex]; |
| | 300 | |
|
| 2 | 301 | | if (sparseIndex == sparseIndexAtDenseIndex) |
| 2 | 302 | | { |
| 2 | 303 | | return denseIndex; |
| | 304 | | } |
| 0 | 305 | | } |
| 3 | 306 | | } |
| | 307 | |
|
| 5 | 308 | | return -1; |
| 7 | 309 | | } |
| | 310 | |
|
| | 311 | | /// <summary> |
| | 312 | | /// Frees the allocated entry corresponding to the sparse index. |
| | 313 | | /// WARNING: Will not protect against accidentally removing twice if the index in question was recycled betw |
| | 314 | | /// calls. |
| | 315 | | /// <param name="sparseIndexToRemove">The sparse index corresponding to the entry to remove.</param> |
| | 316 | | /// <param name="dataIndexToSwapTo"> |
| | 317 | | /// Replace the data array value at this index with the data array value at |
| | 318 | | /// indexToSwapFrom. |
| | 319 | | /// </param> |
| | 320 | | /// <param name="dataIndexToSwapFrom"> |
| | 321 | | /// Set the data array value at this index to default after swapping with the data array |
| | 322 | | /// value at dataIndexToSwapTo. |
| | 323 | | /// </param> |
| | 324 | | /// </summary> |
| | 325 | | /// <returns>True if the entry was valid and thus removed.</returns> |
| | 326 | | public bool RemoveWithBoundsCheck(ref int sparseIndexToRemove, out int dataIndexToSwapFrom, |
| | 327 | | out int dataIndexToSwapTo) |
| 4 | 328 | | { |
| 4 | 329 | | dataIndexToSwapFrom = -1; |
| 4 | 330 | | dataIndexToSwapTo = -1; |
| 4 | 331 | | bool didRemove = false; |
| 4 | 332 | | if (sparseIndexToRemove >= 0 && sparseIndexToRemove < SparseArray.Length) |
| 2 | 333 | | { |
| 2 | 334 | | int denseIndexToRemove = SparseArray[sparseIndexToRemove]; |
| | 335 | |
|
| 2 | 336 | | if (denseIndexToRemove >= 0 && denseIndexToRemove < Count) |
| 1 | 337 | | { |
| 1 | 338 | | int sparseIndexAtDenseIndex = DenseArray[denseIndexToRemove]; |
| 1 | 339 | | int newLength = Count - 1; |
| 1 | 340 | | int sparseIndexBeingSwapped = DenseArray[newLength]; |
| | 341 | |
|
| 1 | 342 | | if (denseIndexToRemove < Count && sparseIndexAtDenseIndex == sparseIndexToRemove) |
| 1 | 343 | | { |
| 1 | 344 | | didRemove = true; |
| | 345 | | // Swap the entry being removed with the last entry. |
| 1 | 346 | | SparseArray[sparseIndexBeingSwapped] = denseIndexToRemove; |
| 1 | 347 | | DenseArray[denseIndexToRemove] = sparseIndexBeingSwapped; |
| | 348 | |
|
| 1 | 349 | | dataIndexToSwapFrom = newLength; |
| 1 | 350 | | dataIndexToSwapTo = denseIndexToRemove; |
| | 351 | |
|
| | 352 | | // Clear the dense index, for debugging purposes |
| 1 | 353 | | DenseArray[newLength] = -1; |
| | 354 | |
|
| | 355 | | // Add the sparse index to the free list. |
| 1 | 356 | | SparseArray[sparseIndexToRemove] = FreeIndex; |
| 1 | 357 | | FreeIndex = sparseIndexToRemove; |
| | 358 | |
|
| 1 | 359 | | Count = newLength; |
| 1 | 360 | | } |
| 1 | 361 | | } |
| 2 | 362 | | } |
| | 363 | |
|
| 4 | 364 | | sparseIndexToRemove = -1; |
| | 365 | |
|
| 4 | 366 | | return didRemove; |
| 4 | 367 | | } |
| | 368 | |
|
| | 369 | | /// <summary> |
| | 370 | | /// Removes the allocated entry corresponding to the sparse index. |
| | 371 | | /// Indicates which dense indices were swapped as a result of removing the entry. |
| | 372 | | /// </summary> |
| | 373 | | /// <param name="sparseIndexToRemove">The sparse index to remove.</param> |
| | 374 | | /// <param name="version"> |
| | 375 | | /// The version number of the int used to access the sparse index. Used to guard against accessing |
| | 376 | | /// indices that have been removed and reused. |
| | 377 | | /// </param> |
| | 378 | | /// <param name="versionArray">The array where version numbers to check against are stored.</param> |
| | 379 | | /// <param name="dataIndexToSwapTo"> |
| | 380 | | /// Replace the data array value at this index with the data array value at |
| | 381 | | /// indexToSwapFrom. |
| | 382 | | /// </param> |
| | 383 | | /// <param name="dataIndexToSwapFrom"> |
| | 384 | | /// Set the data array value at this index to default after swapping with the data array |
| | 385 | | /// value at dataIndexToSwapTo. |
| | 386 | | /// </param> |
| | 387 | | /// <returns>Whether or not the remove attempt succeeded.</returns> |
| | 388 | | public bool RemoveWithVersionCheck(int sparseIndexToRemove, ulong version, ulong[] versionArray, |
| | 389 | | out int dataIndexToSwapFrom, out int dataIndexToSwapTo) |
| 2 | 390 | | { |
| 2 | 391 | | int denseIndexToRemove = SparseArray[sparseIndexToRemove]; |
| 2 | 392 | | ulong versionAtSparseIndex = versionArray[sparseIndexToRemove]; |
| | 393 | |
|
| 2 | 394 | | dataIndexToSwapFrom = -1; |
| 2 | 395 | | dataIndexToSwapTo = -1; |
| | 396 | |
|
| 2 | 397 | | bool succeeded = versionAtSparseIndex == version; |
| | 398 | |
|
| 2 | 399 | | if (succeeded) |
| 1 | 400 | | { |
| 1 | 401 | | int newLength = Count - 1; |
| 1 | 402 | | int sparseIndexBeingSwapped = DenseArray[newLength]; |
| | 403 | |
|
| | 404 | | // Swap the entry being removed with the last entry. |
| 1 | 405 | | SparseArray[sparseIndexBeingSwapped] = denseIndexToRemove; |
| 1 | 406 | | DenseArray[denseIndexToRemove] = sparseIndexBeingSwapped; |
| | 407 | |
|
| | 408 | | // Clear the dense index, for debugging purposes |
| 1 | 409 | | DenseArray[newLength] = -1; |
| | 410 | |
|
| | 411 | | // Add the sparse index to the free list. |
| 1 | 412 | | SparseArray[sparseIndexToRemove] = FreeIndex; |
| 1 | 413 | | versionArray[sparseIndexToRemove] = versionArray[sparseIndexToRemove] + 1; |
| 1 | 414 | | FreeIndex = sparseIndexToRemove; |
| | 415 | |
|
| 1 | 416 | | Count = newLength; |
| | 417 | |
|
| 1 | 418 | | dataIndexToSwapTo = denseIndexToRemove; |
| 1 | 419 | | dataIndexToSwapFrom = newLength; |
| 1 | 420 | | } |
| | 421 | |
|
| 2 | 422 | | return succeeded; |
| 2 | 423 | | } |
| | 424 | |
|
| | 425 | | /// <summary> |
| | 426 | | /// Removes the allocated entry corresponding to the sparse index. |
| | 427 | | /// </summary> |
| | 428 | | /// <param name="sparseIndexToRemove">The sparse index corresponding to the entry to remove.</param> |
| | 429 | | /// <param name="version"> |
| | 430 | | /// The version number of the int used to access the sparse index. Used to guard against erroneously accessi |
| | 431 | | /// freed indices currently in use with an outdated reference. |
| | 432 | | /// </param> |
| | 433 | | /// <param name="dataIndexToSwapTo"> |
| | 434 | | /// Replace the data array value at this index with the data array value at |
| | 435 | | /// indexToSwapFrom. |
| | 436 | | /// </param> |
| | 437 | | /// <param name="dataIndexToSwapFrom"> |
| | 438 | | /// Set the data array value at this index to default after swapping with the data array |
| | 439 | | /// value at dataIndexToSwapTo. |
| | 440 | | /// </param> |
| | 441 | | /// <param name="versionArray">The array where version numbers to check against are stored.</param> |
| | 442 | | /// <returns>True if the entry was valid and thus removed.</returns> |
| | 443 | | public bool RemoveWithBoundsAndVersionChecks(ref int sparseIndexToRemove, ulong version, |
| | 444 | | ulong[] versionArray, out int dataIndexToSwapFrom, out int dataIndexToSwapTo) |
| 6 | 445 | | { |
| 6 | 446 | | dataIndexToSwapFrom = -1; |
| 6 | 447 | | dataIndexToSwapTo = -1; |
| 6 | 448 | | bool didRemove = false; |
| 6 | 449 | | if (sparseIndexToRemove >= 0 && sparseIndexToRemove < SparseArray.Length) |
| 3 | 450 | | { |
| 3 | 451 | | ulong sparseIndexVersion = versionArray[sparseIndexToRemove]; |
| 3 | 452 | | int denseIndexToRemove = SparseArray[sparseIndexToRemove]; |
| | 453 | |
|
| 3 | 454 | | if (sparseIndexVersion == version && denseIndexToRemove >= 0 && denseIndexToRemove < Count) |
| 2 | 455 | | { |
| 2 | 456 | | int sparseIndexAtDenseIndex = DenseArray[denseIndexToRemove]; |
| 2 | 457 | | int newLength = Count - 1; |
| 2 | 458 | | int sparseIndexBeingSwapped = DenseArray[newLength]; |
| | 459 | |
|
| 2 | 460 | | if (denseIndexToRemove < Count && sparseIndexAtDenseIndex == sparseIndexToRemove) |
| 2 | 461 | | { |
| 2 | 462 | | didRemove = true; |
| 2 | 463 | | versionArray[sparseIndexToRemove] = sparseIndexVersion + 1; |
| | 464 | | // Swap the entry being removed with the last entry. |
| 2 | 465 | | SparseArray[sparseIndexBeingSwapped] = denseIndexToRemove; |
| 2 | 466 | | DenseArray[denseIndexToRemove] = sparseIndexBeingSwapped; |
| | 467 | |
|
| 2 | 468 | | dataIndexToSwapFrom = newLength; |
| 2 | 469 | | dataIndexToSwapTo = denseIndexToRemove; |
| | 470 | |
|
| | 471 | | // Clear the dense index, for debugging purposes |
| 2 | 472 | | DenseArray[newLength] = -1; |
| | 473 | |
|
| | 474 | | // Add the sparse index to the free list. |
| 2 | 475 | | SparseArray[sparseIndexToRemove] = FreeIndex; |
| 2 | 476 | | FreeIndex = sparseIndexToRemove; |
| | 477 | |
|
| 2 | 478 | | Count = newLength; |
| 2 | 479 | | } |
| 2 | 480 | | } |
| 3 | 481 | | } |
| | 482 | |
|
| 6 | 483 | | sparseIndexToRemove = -1; |
| | 484 | |
|
| 6 | 485 | | return didRemove; |
| 6 | 486 | | } |
| | 487 | |
|
| | 488 | | /// <summary> |
| | 489 | | /// Removes the associated sparse/dense index pair from active use. |
| | 490 | | /// </summary> |
| | 491 | | /// <param name="sparseIndexToRemove">The sparse index to remove.</param> |
| | 492 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 493 | | public void RemoveUnchecked(int sparseIndexToRemove) |
| 1 | 494 | | { |
| 1 | 495 | | int denseIndexToRemove = SparseArray[sparseIndexToRemove]; |
| 1 | 496 | | int newLength = Count - 1; |
| 1 | 497 | | int sparseIndexBeingSwapped = DenseArray[newLength]; |
| | 498 | |
|
| | 499 | | // Swap the entry being removed with the last entry. |
| 1 | 500 | | SparseArray[sparseIndexBeingSwapped] = denseIndexToRemove; |
| 1 | 501 | | DenseArray[denseIndexToRemove] = sparseIndexBeingSwapped; |
| | 502 | |
|
| | 503 | | // Clear the dense index, for debugging purposes |
| 1 | 504 | | DenseArray[newLength] = -1; |
| | 505 | |
|
| | 506 | | // Add the sparse index to the free list. |
| 1 | 507 | | SparseArray[sparseIndexToRemove] = FreeIndex; |
| 1 | 508 | | FreeIndex = sparseIndexToRemove; |
| | 509 | |
|
| 1 | 510 | | Count = newLength; |
| 1 | 511 | | } |
| | 512 | |
|
| | 513 | | /// <summary> |
| | 514 | | /// Removes the associated sparse/dense index pair from active use. |
| | 515 | | /// Out parameters used to manage parallel data arrays. |
| | 516 | | /// </summary> |
| | 517 | | /// <param name="sparseIndexToRemove">The sparse index to remove.</param> |
| | 518 | | /// <param name="indexToSwapTo">Replace the data array value at this index with the data array value at indexToS |
| | 519 | | /// <param name="indexToSwapFrom"> |
| | 520 | | /// Set the data array value at this index to default after swapping with the data array |
| | 521 | | /// value at indexToSwapTo. |
| | 522 | | /// </param> |
| | 523 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 524 | | public void RemoveUnchecked(int sparseIndexToRemove, out int indexToSwapFrom, out int indexToSwapTo) |
| 1 | 525 | | { |
| 1 | 526 | | int denseIndexToRemove = SparseArray[sparseIndexToRemove]; |
| 1 | 527 | | int newLength = Count - 1; |
| 1 | 528 | | int sparseIndexBeingSwapped = DenseArray[newLength]; |
| | 529 | |
|
| | 530 | | // Swap the entry being removed with the last entry. |
| 1 | 531 | | SparseArray[sparseIndexBeingSwapped] = denseIndexToRemove; |
| 1 | 532 | | DenseArray[denseIndexToRemove] = sparseIndexBeingSwapped; |
| | 533 | |
|
| | 534 | | // Clear the dense index, for debugging purposes |
| 1 | 535 | | DenseArray[newLength] = -1; |
| | 536 | |
|
| | 537 | | // Add the sparse index to the free list. |
| 1 | 538 | | SparseArray[sparseIndexToRemove] = FreeIndex; |
| 1 | 539 | | FreeIndex = sparseIndexToRemove; |
| | 540 | |
|
| 1 | 541 | | Count = newLength; |
| | 542 | |
|
| 1 | 543 | | indexToSwapTo = denseIndexToRemove; |
| 1 | 544 | | indexToSwapFrom = newLength; |
| 1 | 545 | | } |
| | 546 | |
|
| | 547 | | /// <summary> |
| | 548 | | /// Removes the associated sparse/dense index pair from active use and increments the version. |
| | 549 | | /// Out parameters used to manage parallel data arrays. |
| | 550 | | /// </summary> |
| | 551 | | /// <param name="sparseIndexToRemove">The sparse index to remove.</param> |
| | 552 | | /// <param name="versionArray">The array where version numbers to check against are stored.</param> |
| | 553 | | /// <param name="indexToSwapTo">Replace the data array value at this index with the data array value at indexToS |
| | 554 | | /// <param name="indexToSwapFrom"> |
| | 555 | | /// Set the data array value at this index to default after swapping with the data array |
| | 556 | | /// value at indexToSwapTo. |
| | 557 | | /// </param> |
| | 558 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 559 | | public void RemoveUnchecked(int sparseIndexToRemove, ulong[] versionArray, out int indexToSwapFrom, |
| | 560 | | out int indexToSwapTo) |
| 2 | 561 | | { |
| 2 | 562 | | int denseIndexToRemove = SparseArray[sparseIndexToRemove]; |
| 2 | 563 | | int newLength = Count - 1; |
| 2 | 564 | | int sparseIndexBeingSwapped = DenseArray[newLength]; |
| | 565 | |
|
| | 566 | | // Swap the entry being removed with the last entry. |
| 2 | 567 | | SparseArray[sparseIndexBeingSwapped] = denseIndexToRemove; |
| 2 | 568 | | DenseArray[denseIndexToRemove] = sparseIndexBeingSwapped; |
| | 569 | |
|
| | 570 | | // Clear the dense index, for debugging purposes |
| 2 | 571 | | DenseArray[newLength] = -1; |
| | 572 | |
|
| | 573 | | // Add the sparse index to the free list. |
| 2 | 574 | | SparseArray[sparseIndexToRemove] = FreeIndex; |
| 2 | 575 | | versionArray[sparseIndexToRemove] = versionArray[sparseIndexToRemove] + 1; |
| 2 | 576 | | FreeIndex = sparseIndexToRemove; |
| | 577 | |
|
| 2 | 578 | | Count = newLength; |
| | 579 | |
|
| 2 | 580 | | indexToSwapTo = denseIndexToRemove; |
| 2 | 581 | | indexToSwapFrom = newLength; |
| 2 | 582 | | } |
| | 583 | |
|
| | 584 | | /// <summary> |
| | 585 | | /// Removes the associated sparse/dense index pair from active use. |
| | 586 | | /// </summary> |
| | 587 | | /// <param name="denseIndexToRemove">The dense index associated with the sparse index to remove.</param> |
| | 588 | | public void RemoveUncheckedFromDenseIndex(int denseIndexToRemove) |
| 1 | 589 | | { |
| 1 | 590 | | int sparseIndexToRemove = DenseArray[denseIndexToRemove]; |
| 1 | 591 | | int newLength = Count - 1; |
| 1 | 592 | | int sparseIndexBeingSwapped = DenseArray[newLength]; |
| | 593 | |
|
| | 594 | | // Swap the entry being removed with the last entry. |
| 1 | 595 | | SparseArray[sparseIndexBeingSwapped] = denseIndexToRemove; |
| 1 | 596 | | DenseArray[denseIndexToRemove] = sparseIndexBeingSwapped; |
| | 597 | |
|
| | 598 | | // Clear the dense index, for debugging purposes |
| 1 | 599 | | DenseArray[newLength] = -1; |
| | 600 | |
|
| | 601 | | // Add the sparse index to the free list. |
| 1 | 602 | | SparseArray[sparseIndexToRemove] = FreeIndex; |
| 1 | 603 | | FreeIndex = sparseIndexToRemove; |
| | 604 | |
|
| 1 | 605 | | Count = newLength; |
| 1 | 606 | | } |
| | 607 | |
|
| | 608 | | /// <summary> |
| | 609 | | /// Removes the associated sparse/dense index pair from active use. |
| | 610 | | /// </summary> |
| | 611 | | /// <param name="denseIndexToRemove">The dense index associated with the sparse index to remove.</param> |
| | 612 | | /// <param name="versionArray">The array where version numbers to check against are stored.</param> |
| | 613 | | public void RemoveUncheckedFromDenseIndex(int denseIndexToRemove, ulong[] versionArray) |
| 1 | 614 | | { |
| 1 | 615 | | int sparseIndexToRemove = DenseArray[denseIndexToRemove]; |
| 1 | 616 | | int newLength = Count - 1; |
| 1 | 617 | | int sparseIndexBeingSwapped = DenseArray[newLength]; |
| | 618 | |
|
| | 619 | | // Swap the entry being removed with the last entry. |
| 1 | 620 | | SparseArray[sparseIndexBeingSwapped] = denseIndexToRemove; |
| 1 | 621 | | DenseArray[denseIndexToRemove] = sparseIndexBeingSwapped; |
| | 622 | |
|
| | 623 | | // Clear the dense index, for debugging purposes |
| 1 | 624 | | DenseArray[newLength] = -1; |
| | 625 | |
|
| | 626 | | // Add the sparse index to the free list. |
| 1 | 627 | | SparseArray[sparseIndexToRemove] = FreeIndex; |
| 1 | 628 | | versionArray[sparseIndexToRemove] += 1; |
| 1 | 629 | | FreeIndex = sparseIndexToRemove; |
| | 630 | |
|
| 1 | 631 | | Count = newLength; |
| 1 | 632 | | } |
| | 633 | |
|
| | 634 | | /// <summary> |
| | 635 | | /// Removes the associated sparse/dense index pair from active use. |
| | 636 | | /// Out parameter used to manage parallel data arrays. |
| | 637 | | /// </summary> |
| | 638 | | /// <param name="denseIndexToRemove">The sparse index to remove.</param> |
| | 639 | | /// <param name="indexToSwapFrom"> |
| | 640 | | /// Set the data array value at this index to default after swapping with the data array |
| | 641 | | /// value at denseIndexToRemove. |
| | 642 | | /// </param> |
| | 643 | | public void RemoveUncheckedFromDenseIndex(int denseIndexToRemove, out int indexToSwapFrom) |
| 1 | 644 | | { |
| 1 | 645 | | int sparseIndexToRemove = DenseArray[denseIndexToRemove]; |
| 1 | 646 | | int newLength = Count - 1; |
| 1 | 647 | | int sparseIndexBeingSwapped = DenseArray[newLength]; |
| | 648 | |
|
| | 649 | | // Swap the entry being removed with the last entry. |
| 1 | 650 | | SparseArray[sparseIndexBeingSwapped] = denseIndexToRemove; |
| 1 | 651 | | DenseArray[denseIndexToRemove] = sparseIndexBeingSwapped; |
| | 652 | |
|
| | 653 | | // Clear the dense index, for debugging purposes |
| 1 | 654 | | DenseArray[newLength] = -1; |
| | 655 | |
|
| | 656 | | // Add the sparse index to the free list. |
| 1 | 657 | | SparseArray[sparseIndexToRemove] = FreeIndex; |
| 1 | 658 | | FreeIndex = sparseIndexToRemove; |
| | 659 | |
|
| 1 | 660 | | Count = newLength; |
| | 661 | |
|
| 1 | 662 | | indexToSwapFrom = newLength; |
| 1 | 663 | | } |
| | 664 | |
|
| | 665 | | /// <summary> |
| | 666 | | /// Removes the associated sparse/dense index pair from active use. |
| | 667 | | /// Out parameter used to manage parallel data arrays. |
| | 668 | | /// </summary> |
| | 669 | | /// <param name="denseIndexToRemove">The sparse index to remove.</param> |
| | 670 | | /// <param name="versionArray">The array where version numbers to check against are stored.</param> |
| | 671 | | /// <param name="indexToSwapFrom"> |
| | 672 | | /// Set the data array value at this index to default after swapping with the data array |
| | 673 | | /// value at denseIndexToRemove. |
| | 674 | | /// </param> |
| | 675 | | public void RemoveUncheckedFromDenseIndex(int denseIndexToRemove, ulong[] versionArray, out int indexToSwapFrom) |
| 1 | 676 | | { |
| 1 | 677 | | int sparseIndexToRemove = DenseArray[denseIndexToRemove]; |
| 1 | 678 | | int newLength = Count - 1; |
| 1 | 679 | | int sparseIndexBeingSwapped = DenseArray[newLength]; |
| | 680 | |
|
| | 681 | | // Swap the entry being removed with the last entry. |
| 1 | 682 | | SparseArray[sparseIndexBeingSwapped] = denseIndexToRemove; |
| 1 | 683 | | DenseArray[denseIndexToRemove] = sparseIndexBeingSwapped; |
| | 684 | |
|
| | 685 | | // Clear the dense index, for debugging purposes |
| 1 | 686 | | DenseArray[newLength] = -1; |
| | 687 | |
|
| | 688 | | // Add the sparse index to the free list. |
| 1 | 689 | | SparseArray[sparseIndexToRemove] = FreeIndex; |
| 1 | 690 | | versionArray[sparseIndexToRemove] = versionArray[sparseIndexToRemove] + 1; |
| 1 | 691 | | FreeIndex = sparseIndexToRemove; |
| | 692 | |
|
| 1 | 693 | | Count = newLength; |
| | 694 | |
|
| 1 | 695 | | indexToSwapFrom = newLength; |
| 1 | 696 | | } |
| | 697 | |
|
| | 698 | | /// <summary> |
| | 699 | | /// Clear the dense and sparse arrays. |
| | 700 | | /// </summary> |
| | 701 | | public void Clear() |
| 1 | 702 | | { |
| 1 | 703 | | int capacity = SparseArray.Length; |
| 8 | 704 | | for (int i = 0; i < capacity; i++) |
| 3 | 705 | | { |
| 3 | 706 | | DenseArray[i] = -1; |
| 3 | 707 | | SparseArray[i] = i + 1; |
| 3 | 708 | | } |
| | 709 | |
|
| 1 | 710 | | FreeIndex = 0; |
| 1 | 711 | | Count = 0; |
| 1 | 712 | | } |
| | 713 | |
|
| | 714 | | /// <summary> |
| | 715 | | /// Clear the dense and sparse arrays. |
| | 716 | | /// </summary> |
| | 717 | | /// /// |
| | 718 | | /// <param name="versionArray">Array containing version numbers to check against.</param> |
| | 719 | | public void Clear(ulong[] versionArray) |
| 1 | 720 | | { |
| 1 | 721 | | int capacity = SparseArray.Length; |
| 8 | 722 | | for (int i = 0; i < capacity; i++) |
| 3 | 723 | | { |
| 3 | 724 | | DenseArray[i] = -1; |
| 3 | 725 | | SparseArray[i] = i + 1; |
| 3 | 726 | | versionArray[i] = versionArray[i] + 1; |
| 3 | 727 | | } |
| | 728 | |
|
| 1 | 729 | | FreeIndex = 0; |
| 1 | 730 | | Count = 0; |
| 1 | 731 | | } |
| | 732 | |
|
| | 733 | | /// <summary> |
| | 734 | | /// Clear the dense and sparse arrays and reset the version array. |
| | 735 | | /// Note: Only clear the version array if you are sure there are no outstanding dependencies on version numb |
| | 736 | | /// </summary> |
| | 737 | | /// /// |
| | 738 | | /// <param name="versionArray">Array containing version numbers to check against.</param> |
| | 739 | | public void ClearWithVersionArrayReset(ulong[] versionArray) |
| 1 | 740 | | { |
| 1 | 741 | | int capacity = SparseArray.Length; |
| 8 | 742 | | for (int i = 0; i < capacity; i++) |
| 3 | 743 | | { |
| 3 | 744 | | DenseArray[i] = -1; |
| 3 | 745 | | SparseArray[i] = i + 1; |
| 3 | 746 | | versionArray[i] = 1; |
| 3 | 747 | | } |
| | 748 | |
|
| 1 | 749 | | FreeIndex = 0; |
| 1 | 750 | | Count = 0; |
| 1 | 751 | | } |
| | 752 | |
|
| | 753 | | /// <summary> |
| | 754 | | /// Reallocate the dense and sparse arrays with additional capacity. |
| | 755 | | /// </summary> |
| | 756 | | /// <param name="extraCapacity">How many indices to expand the dense and sparse arrays by.</param> |
| | 757 | | public void Expand(int extraCapacity) |
| 1 | 758 | | { |
| 1 | 759 | | int currentCapacity = SparseArray.Length; |
| 1 | 760 | | int newCapacity = currentCapacity + extraCapacity; |
| | 761 | |
|
| 1 | 762 | | int[] newSparseArray = new int[newCapacity]; |
| 1 | 763 | | Array.Copy(SparseArray, 0, newSparseArray, 0, currentCapacity); |
| 1 | 764 | | SparseArray = newSparseArray; |
| | 765 | |
|
| 1 | 766 | | int[] newDenseArray = new int[newCapacity]; |
| 1 | 767 | | Array.Copy(DenseArray, 0, newDenseArray, 0, currentCapacity); |
| 1 | 768 | | DenseArray = newDenseArray; |
| | 769 | |
|
| 8 | 770 | | for (int i = currentCapacity; i < newCapacity; i++) |
| 3 | 771 | | { |
| 3 | 772 | | DenseArray[i] = -1; // Set new dense indices as unclaimed. |
| 3 | 773 | | SparseArray[i] = i + 1; // Build the free list chain. |
| 3 | 774 | | } |
| 1 | 775 | | } |
| | 776 | |
|
| | 777 | | /// <summary> |
| | 778 | | /// Reallocate the dense and sparse arrays with additional capacity. |
| | 779 | | /// </summary> |
| | 780 | | /// <param name="extraCapacity">How many indices to expand the dense and sparse arrays by.</param> |
| | 781 | | /// <param name="versionArray">Array containing version numbers to check against.</param> |
| | 782 | | public void Expand(int extraCapacity, ref ulong[] versionArray) |
| 2 | 783 | | { |
| 2 | 784 | | int currentCapacity = SparseArray.Length; |
| 2 | 785 | | int newCapacity = currentCapacity + extraCapacity; |
| | 786 | |
|
| 2 | 787 | | int[] newSparseArray = new int[newCapacity]; |
| 2 | 788 | | Array.Copy(SparseArray, 0, newSparseArray, 0, currentCapacity); |
| 2 | 789 | | SparseArray = newSparseArray; |
| | 790 | |
|
| 2 | 791 | | int[] newDenseArray = new int[newCapacity]; |
| 2 | 792 | | Array.Copy(DenseArray, 0, newDenseArray, 0, currentCapacity); |
| 2 | 793 | | DenseArray = newDenseArray; |
| | 794 | |
|
| 2 | 795 | | ulong[] newVersionArray = new ulong[newCapacity]; |
| 2 | 796 | | Array.Copy(versionArray, 0, newVersionArray, 0, currentCapacity); |
| 2 | 797 | | versionArray = newVersionArray; |
| | 798 | |
|
| 16 | 799 | | for (int i = currentCapacity; i < newCapacity; i++) |
| 6 | 800 | | { |
| 6 | 801 | | DenseArray[i] = -1; // Set new dense indices as unclaimed. |
| 6 | 802 | | SparseArray[i] = i + 1; // Build the free list chain. |
| 6 | 803 | | versionArray[i] = 1; |
| 6 | 804 | | } |
| 2 | 805 | | } |
| | 806 | |
|
| | 807 | | public void Reserve(int numberToReserve) |
| 1 | 808 | | { |
| 1 | 809 | | int currentCapacity = SparseArray.Length; |
| 1 | 810 | | int currentCount = Count; |
| 1 | 811 | | int newCount = currentCount + numberToReserve; |
| | 812 | |
|
| 1 | 813 | | if (newCount > currentCapacity) |
| 1 | 814 | | { |
| 1 | 815 | | int[] newSparseArray = new int[newCount]; |
| 1 | 816 | | Array.Copy(SparseArray, 0, newSparseArray, 0, currentCapacity); |
| 1 | 817 | | SparseArray = newSparseArray; |
| | 818 | |
|
| 1 | 819 | | int[] newDenseArray = new int[newCount]; |
| 1 | 820 | | Array.Copy(DenseArray, 0, newDenseArray, 0, currentCapacity); |
| 1 | 821 | | DenseArray = newDenseArray; |
| | 822 | |
|
| 4 | 823 | | for (int i = currentCapacity; i < newCount; i++) |
| 1 | 824 | | { |
| 1 | 825 | | DenseArray[i] = -1; // Set new dense indices as unclaimed. |
| 1 | 826 | | SparseArray[i] = i + 1; // Build the free list chain. |
| 1 | 827 | | } |
| 1 | 828 | | } |
| 1 | 829 | | } |
| | 830 | |
|
| | 831 | | public void Reserve(int numberToReserve, ref ulong[] versionArray) |
| 0 | 832 | | { |
| 0 | 833 | | int currentCapacity = SparseArray.Length; |
| 0 | 834 | | int currentCount = Count; |
| 0 | 835 | | int newCount = currentCount + numberToReserve; |
| | 836 | |
|
| 0 | 837 | | if (newCount > currentCapacity) |
| 0 | 838 | | { |
| 0 | 839 | | int[] newSparseArray = new int[newCount]; |
| 0 | 840 | | Array.Copy(SparseArray, 0, newSparseArray, 0, currentCapacity); |
| 0 | 841 | | SparseArray = newSparseArray; |
| | 842 | |
|
| 0 | 843 | | int[] newDenseArray = new int[newCount]; |
| 0 | 844 | | Array.Copy(DenseArray, 0, newDenseArray, 0, currentCapacity); |
| 0 | 845 | | DenseArray = newDenseArray; |
| | 846 | |
|
| 0 | 847 | | ulong[] newVersionArray = new ulong[newCount]; |
| 0 | 848 | | Array.Copy(versionArray, 0, newVersionArray, 0, currentCapacity); |
| 0 | 849 | | versionArray = newVersionArray; |
| | 850 | |
|
| 0 | 851 | | for (int i = currentCapacity; i < newCount; i++) |
| 0 | 852 | | { |
| 0 | 853 | | DenseArray[i] = -1; // Set new dense indices as unclaimed. |
| 0 | 854 | | SparseArray[i] = i + 1; // Build the free list chain. |
| 0 | 855 | | versionArray[i] = 1; |
| 0 | 856 | | } |
| 0 | 857 | | } |
| 0 | 858 | | } |
| | 859 | |
|
| | 860 | | #region Parallel array method duplicates |
| | 861 | |
|
| | 862 | | /// <summary> |
| | 863 | | /// Adds a sparse/dense index pair to the set and expands the arrays if necessary. |
| | 864 | | /// </summary> |
| | 865 | | /// <returns>True if the index pool expanded.</returns> |
| | 866 | | public bool AddWithExpandCheck<T0>(T0 obj0, ref T0[] array0, out int lookupIndex, int howMuchToExpand = 16) |
| 2 | 867 | | { |
| 2 | 868 | | int indexToClaim = FreeIndex; |
| 2 | 869 | | int currentCapacity = SparseArray.Length; |
| 2 | 870 | | bool needsExpansion = false; |
| | 871 | |
|
| 2 | 872 | | if (indexToClaim >= currentCapacity) |
| 1 | 873 | | { |
| 1 | 874 | | needsExpansion = true; |
| | 875 | | // We're out of space, the last free index points to nothing. Allocate more indices. |
| 1 | 876 | | int newCapacity = currentCapacity + howMuchToExpand; |
| | 877 | |
|
| 1 | 878 | | int[] newSparseArray = new int[newCapacity]; |
| 1 | 879 | | Array.Copy(SparseArray, 0, newSparseArray, 0, currentCapacity); |
| 1 | 880 | | SparseArray = newSparseArray; |
| | 881 | |
|
| 1 | 882 | | int[] newDenseArray = new int[newCapacity]; |
| 1 | 883 | | Array.Copy(DenseArray, 0, newDenseArray, 0, currentCapacity); |
| 1 | 884 | | DenseArray = newDenseArray; |
| | 885 | |
|
| 1 | 886 | | T0[] newArray0 = new T0[newCapacity]; |
| 1 | 887 | | Array.Copy(array0, 0, newArray0, 0, currentCapacity); |
| 1 | 888 | | array0 = newArray0; |
| | 889 | |
|
| 12 | 890 | | for (int i = currentCapacity; i < newCapacity; i++) |
| 5 | 891 | | { |
| 5 | 892 | | SparseArray[i] = i + 1; // Build the free list chain. |
| 5 | 893 | | DenseArray[i] = -1; // Set new dense indices as unclaimed. |
| 5 | 894 | | } |
| 1 | 895 | | } |
| | 896 | |
|
| 2 | 897 | | int nextFreeIndex = SparseArray[indexToClaim]; |
| | 898 | |
|
| 2 | 899 | | DenseArray[Count] = indexToClaim; // Point the next dense id at our newly claimed sparse index. |
| 2 | 900 | | SparseArray[indexToClaim] = Count; // Point our newly claimed sparse index at the dense index. |
| | 901 | |
|
| 2 | 902 | | array0[Count] = obj0; |
| | 903 | |
|
| 2 | 904 | | ++Count; |
| 2 | 905 | | FreeIndex = nextFreeIndex; // Set the free list head for next time. |
| | 906 | |
|
| 2 | 907 | | lookupIndex = indexToClaim; |
| 2 | 908 | | return needsExpansion; |
| 2 | 909 | | } |
| | 910 | |
|
| | 911 | | /// <summary> |
| | 912 | | /// Adds a sparse/dense index pair to the set and expands the arrays if necessary. |
| | 913 | | /// </summary> |
| | 914 | | /// <returns>True if the index pool expanded.</returns> |
| | 915 | | public bool AddWithExpandCheck<T0, T1>(T0 obj0, ref T0[] array0, T1 obj1, ref T1[] array1, out int lookupIndex, |
| | 916 | | int howMuchToExpand = 16) |
| 2 | 917 | | { |
| 2 | 918 | | int indexToClaim = FreeIndex; |
| 2 | 919 | | int currentCapacity = SparseArray.Length; |
| 2 | 920 | | bool needsExpansion = false; |
| | 921 | |
|
| 2 | 922 | | if (indexToClaim >= currentCapacity) |
| 1 | 923 | | { |
| 1 | 924 | | needsExpansion = true; |
| | 925 | | // We're out of space, the last free index points to nothing. Allocate more indices. |
| 1 | 926 | | int newCapacity = currentCapacity + howMuchToExpand; |
| | 927 | |
|
| 1 | 928 | | int[] newSparseArray = new int[newCapacity]; |
| 1 | 929 | | Array.Copy(SparseArray, 0, newSparseArray, 0, currentCapacity); |
| 1 | 930 | | SparseArray = newSparseArray; |
| | 931 | |
|
| 1 | 932 | | int[] newDenseArray = new int[newCapacity]; |
| 1 | 933 | | Array.Copy(DenseArray, 0, newDenseArray, 0, currentCapacity); |
| 1 | 934 | | DenseArray = newDenseArray; |
| | 935 | |
|
| 1 | 936 | | T0[] newArray0 = new T0[newCapacity]; |
| 1 | 937 | | Array.Copy(array0, 0, newArray0, 0, currentCapacity); |
| 1 | 938 | | array0 = newArray0; |
| | 939 | |
|
| 1 | 940 | | T1[] newArray1 = new T1[newCapacity]; |
| 1 | 941 | | Array.Copy(array1, 0, newArray1, 0, currentCapacity); |
| 1 | 942 | | array1 = newArray1; |
| | 943 | |
|
| 12 | 944 | | for (int i = currentCapacity; i < newCapacity; i++) |
| 5 | 945 | | { |
| 5 | 946 | | SparseArray[i] = i + 1; // Build the free list chain. |
| 5 | 947 | | DenseArray[i] = -1; // Set new dense indices as unclaimed. |
| 5 | 948 | | } |
| 1 | 949 | | } |
| | 950 | |
|
| 2 | 951 | | int nextFreeIndex = SparseArray[indexToClaim]; |
| | 952 | |
|
| 2 | 953 | | DenseArray[Count] = indexToClaim; // Point the next dense id at our newly claimed sparse index. |
| 2 | 954 | | SparseArray[indexToClaim] = Count; // Point our newly claimed sparse index at the dense index. |
| | 955 | |
|
| 2 | 956 | | array0[Count] = obj0; |
| 2 | 957 | | array1[Count] = obj1; |
| | 958 | |
|
| 2 | 959 | | ++Count; |
| 2 | 960 | | FreeIndex = nextFreeIndex; // Set the free list head for next time. |
| | 961 | |
|
| 2 | 962 | | lookupIndex = indexToClaim; |
| 2 | 963 | | return needsExpansion; |
| 2 | 964 | | } |
| | 965 | |
|
| | 966 | | /// <summary> |
| | 967 | | /// Adds a sparse/dense index pair to the set and expands the arrays if necessary. |
| | 968 | | /// </summary> |
| | 969 | | /// <returns>True if the index pool expanded.</returns> |
| | 970 | | public bool AddWithExpandCheck<T0, T1, T2>(T0 obj0, ref T0[] array0, T1 obj1, ref T1[] array1, T2 obj2, |
| | 971 | | ref T2[] array2, |
| | 972 | | out int lookupIndex, int howMuchToExpand = 16) |
| 2 | 973 | | { |
| 2 | 974 | | int indexToClaim = FreeIndex; |
| 2 | 975 | | int currentCapacity = SparseArray.Length; |
| 2 | 976 | | bool needsExpansion = false; |
| | 977 | |
|
| 2 | 978 | | if (indexToClaim >= currentCapacity) |
| 1 | 979 | | { |
| 1 | 980 | | needsExpansion = true; |
| | 981 | | // We're out of space, the last free index points to nothing. Allocate more indices. |
| 1 | 982 | | int newCapacity = currentCapacity + howMuchToExpand; |
| | 983 | |
|
| 1 | 984 | | int[] newSparseArray = new int[newCapacity]; |
| 1 | 985 | | Array.Copy(SparseArray, 0, newSparseArray, 0, currentCapacity); |
| 1 | 986 | | SparseArray = newSparseArray; |
| | 987 | |
|
| 1 | 988 | | int[] newDenseArray = new int[newCapacity]; |
| 1 | 989 | | Array.Copy(DenseArray, 0, newDenseArray, 0, currentCapacity); |
| 1 | 990 | | DenseArray = newDenseArray; |
| | 991 | |
|
| 1 | 992 | | T0[] newArray0 = new T0[newCapacity]; |
| 1 | 993 | | Array.Copy(array0, 0, newArray0, 0, currentCapacity); |
| 1 | 994 | | array0 = newArray0; |
| | 995 | |
|
| 1 | 996 | | T1[] newArray1 = new T1[newCapacity]; |
| 1 | 997 | | Array.Copy(array1, 0, newArray1, 0, currentCapacity); |
| 1 | 998 | | array1 = newArray1; |
| | 999 | |
|
| 1 | 1000 | | T2[] newArray2 = new T2[newCapacity]; |
| 1 | 1001 | | Array.Copy(array2, 0, newArray2, 0, currentCapacity); |
| 1 | 1002 | | array2 = newArray2; |
| | 1003 | |
|
| 12 | 1004 | | for (int i = currentCapacity; i < newCapacity; i++) |
| 5 | 1005 | | { |
| 5 | 1006 | | SparseArray[i] = i + 1; // Build the free list chain. |
| 5 | 1007 | | DenseArray[i] = -1; // Set new dense indices as unclaimed. |
| 5 | 1008 | | } |
| 1 | 1009 | | } |
| | 1010 | |
|
| 2 | 1011 | | int nextFreeIndex = SparseArray[indexToClaim]; |
| | 1012 | |
|
| 2 | 1013 | | DenseArray[Count] = indexToClaim; // Point the next dense id at our newly claimed sparse index. |
| 2 | 1014 | | SparseArray[indexToClaim] = Count; // Point our newly claimed sparse index at the dense index. |
| | 1015 | |
|
| 2 | 1016 | | array0[Count] = obj0; |
| 2 | 1017 | | array1[Count] = obj1; |
| 2 | 1018 | | array2[Count] = obj2; |
| | 1019 | |
|
| 2 | 1020 | | ++Count; |
| 2 | 1021 | | FreeIndex = nextFreeIndex; // Set the free list head for next time. |
| | 1022 | |
|
| 2 | 1023 | | lookupIndex = indexToClaim; |
| 2 | 1024 | | return needsExpansion; |
| 2 | 1025 | | } |
| | 1026 | |
|
| | 1027 | | /// <summary> |
| | 1028 | | /// Adds a sparse/dense index pair to the set and expands the arrays if necessary. |
| | 1029 | | /// </summary> |
| | 1030 | | /// <returns>True if the index pool expanded.</returns> |
| | 1031 | | public bool AddWithExpandCheck<T0, T1, T2, T3>(T0 obj0, ref T0[] array0, T1 obj1, ref T1[] array1, T2 obj2, |
| | 1032 | | ref T2[] array2, T3 obj3, ref T3[] array3, out int lookupIndex, int howMuchToExpand = 16) |
| 2 | 1033 | | { |
| 2 | 1034 | | int indexToClaim = FreeIndex; |
| 2 | 1035 | | int currentCapacity = SparseArray.Length; |
| 2 | 1036 | | bool needsExpansion = false; |
| | 1037 | |
|
| 2 | 1038 | | if (indexToClaim >= currentCapacity) |
| 1 | 1039 | | { |
| 1 | 1040 | | needsExpansion = true; |
| | 1041 | | // We're out of space, the last free index points to nothing. Allocate more indices. |
| 1 | 1042 | | int newCapacity = currentCapacity + howMuchToExpand; |
| | 1043 | |
|
| 1 | 1044 | | int[] newSparseArray = new int[newCapacity]; |
| 1 | 1045 | | Array.Copy(SparseArray, 0, newSparseArray, 0, currentCapacity); |
| 1 | 1046 | | SparseArray = newSparseArray; |
| | 1047 | |
|
| 1 | 1048 | | int[] newDenseArray = new int[newCapacity]; |
| 1 | 1049 | | Array.Copy(DenseArray, 0, newDenseArray, 0, currentCapacity); |
| 1 | 1050 | | DenseArray = newDenseArray; |
| | 1051 | |
|
| 1 | 1052 | | T0[] newArray0 = new T0[newCapacity]; |
| 1 | 1053 | | Array.Copy(array0, 0, newArray0, 0, currentCapacity); |
| 1 | 1054 | | array0 = newArray0; |
| | 1055 | |
|
| 1 | 1056 | | T1[] newArray1 = new T1[newCapacity]; |
| 1 | 1057 | | Array.Copy(array1, 0, newArray1, 0, currentCapacity); |
| 1 | 1058 | | array1 = newArray1; |
| | 1059 | |
|
| 1 | 1060 | | T2[] newArray2 = new T2[newCapacity]; |
| 1 | 1061 | | Array.Copy(array2, 0, newArray2, 0, currentCapacity); |
| 1 | 1062 | | array2 = newArray2; |
| | 1063 | |
|
| 1 | 1064 | | T3[] newArray3 = new T3[newCapacity]; |
| 1 | 1065 | | Array.Copy(array3, 0, newArray3, 0, currentCapacity); |
| 1 | 1066 | | array3 = newArray3; |
| | 1067 | |
|
| 12 | 1068 | | for (int i = currentCapacity; i < newCapacity; i++) |
| 5 | 1069 | | { |
| 5 | 1070 | | SparseArray[i] = i + 1; // Build the free list chain. |
| 5 | 1071 | | DenseArray[i] = -1; // Set new dense indices as unclaimed. |
| 5 | 1072 | | } |
| 1 | 1073 | | } |
| | 1074 | |
|
| 2 | 1075 | | int nextFreeIndex = SparseArray[indexToClaim]; |
| | 1076 | |
|
| 2 | 1077 | | DenseArray[Count] = indexToClaim; // Point the next dense id at our newly claimed sparse index. |
| 2 | 1078 | | SparseArray[indexToClaim] = Count; // Point our newly claimed sparse index at the dense index. |
| | 1079 | |
|
| 2 | 1080 | | array0[Count] = obj0; |
| 2 | 1081 | | array1[Count] = obj1; |
| 2 | 1082 | | array2[Count] = obj2; |
| 2 | 1083 | | array3[Count] = obj3; |
| | 1084 | |
|
| 2 | 1085 | | ++Count; |
| 2 | 1086 | | FreeIndex = nextFreeIndex; // Set the free list head for next time. |
| | 1087 | |
|
| 2 | 1088 | | lookupIndex = indexToClaim; |
| 2 | 1089 | | return needsExpansion; |
| 2 | 1090 | | } |
| | 1091 | |
|
| | 1092 | | /// <summary> |
| | 1093 | | /// Adds a sparse/dense index pair to the set and expands the arrays if necessary. |
| | 1094 | | /// </summary> |
| | 1095 | | /// <returns>True if the index pool expanded.</returns> |
| | 1096 | | public bool AddWithExpandCheck<T0, T1, T2, T3, T4>(T0 obj0, ref T0[] array0, T1 obj1, ref T1[] array1, T2 obj2, |
| | 1097 | | ref T2[] array2, T3 obj3, ref T3[] array3, T4 obj4, ref T4[] array4, out int lookupIndex, |
| | 1098 | | int howMuchToExpand = 16) |
| 2 | 1099 | | { |
| 2 | 1100 | | int indexToClaim = FreeIndex; |
| 2 | 1101 | | int currentCapacity = SparseArray.Length; |
| 2 | 1102 | | bool needsExpansion = false; |
| | 1103 | |
|
| 2 | 1104 | | if (indexToClaim >= currentCapacity) |
| 1 | 1105 | | { |
| 1 | 1106 | | needsExpansion = true; |
| | 1107 | | // We're out of space, the last free index points to nothing. Allocate more indices. |
| 1 | 1108 | | int newCapacity = currentCapacity + howMuchToExpand; |
| | 1109 | |
|
| 1 | 1110 | | int[] newSparseArray = new int[newCapacity]; |
| 1 | 1111 | | Array.Copy(SparseArray, 0, newSparseArray, 0, currentCapacity); |
| 1 | 1112 | | SparseArray = newSparseArray; |
| | 1113 | |
|
| 1 | 1114 | | int[] newDenseArray = new int[newCapacity]; |
| 1 | 1115 | | Array.Copy(DenseArray, 0, newDenseArray, 0, currentCapacity); |
| 1 | 1116 | | DenseArray = newDenseArray; |
| | 1117 | |
|
| 1 | 1118 | | T0[] newArray0 = new T0[newCapacity]; |
| 1 | 1119 | | Array.Copy(array0, 0, newArray0, 0, currentCapacity); |
| 1 | 1120 | | array0 = newArray0; |
| | 1121 | |
|
| 1 | 1122 | | T1[] newArray1 = new T1[newCapacity]; |
| 1 | 1123 | | Array.Copy(array1, 0, newArray1, 0, currentCapacity); |
| 1 | 1124 | | array1 = newArray1; |
| | 1125 | |
|
| 1 | 1126 | | T2[] newArray2 = new T2[newCapacity]; |
| 1 | 1127 | | Array.Copy(array2, 0, newArray2, 0, currentCapacity); |
| 1 | 1128 | | array2 = newArray2; |
| | 1129 | |
|
| 1 | 1130 | | T3[] newArray3 = new T3[newCapacity]; |
| 1 | 1131 | | Array.Copy(array3, 0, newArray3, 0, currentCapacity); |
| 1 | 1132 | | array3 = newArray3; |
| | 1133 | |
|
| 1 | 1134 | | T4[] newArray4 = new T4[newCapacity]; |
| 1 | 1135 | | Array.Copy(array4, 0, newArray4, 0, currentCapacity); |
| 1 | 1136 | | array4 = newArray4; |
| | 1137 | |
|
| 12 | 1138 | | for (int i = currentCapacity; i < newCapacity; i++) |
| 5 | 1139 | | { |
| 5 | 1140 | | SparseArray[i] = i + 1; // Build the free list chain. |
| 5 | 1141 | | DenseArray[i] = -1; // Set new dense indices as unclaimed. |
| 5 | 1142 | | } |
| 1 | 1143 | | } |
| | 1144 | |
|
| 2 | 1145 | | int nextFreeIndex = SparseArray[indexToClaim]; |
| | 1146 | |
|
| 2 | 1147 | | DenseArray[Count] = indexToClaim; // Point the next dense id at our newly claimed sparse index. |
| 2 | 1148 | | SparseArray[indexToClaim] = Count; // Point our newly claimed sparse index at the dense index. |
| | 1149 | |
|
| 2 | 1150 | | array0[Count] = obj0; |
| 2 | 1151 | | array1[Count] = obj1; |
| 2 | 1152 | | array2[Count] = obj2; |
| 2 | 1153 | | array3[Count] = obj3; |
| 2 | 1154 | | array4[Count] = obj4; |
| | 1155 | |
|
| 2 | 1156 | | ++Count; |
| 2 | 1157 | | FreeIndex = nextFreeIndex; // Set the free list head for next time. |
| | 1158 | |
|
| 2 | 1159 | | lookupIndex = indexToClaim; |
| 2 | 1160 | | return needsExpansion; |
| 2 | 1161 | | } |
| | 1162 | |
|
| | 1163 | | /// <summary> |
| | 1164 | | /// Adds a sparse/dense index pair to the set and expands the arrays if necessary. |
| | 1165 | | /// </summary> |
| | 1166 | | /// <returns>True if the index pool expanded.</returns> |
| | 1167 | | public bool AddWithExpandCheck<T0, T1, T2, T3, T4, T5>(T0 obj0, ref T0[] array0, T1 obj1, ref T1[] array1, |
| | 1168 | | T2 obj2, |
| | 1169 | | ref T2[] array2, T3 obj3, ref T3[] array3, T4 obj4, ref T4[] array4, T5 obj5, ref T5[] array5, |
| | 1170 | | out int lookupIndex, int howMuchToExpand = 16) |
| 2 | 1171 | | { |
| 2 | 1172 | | int indexToClaim = FreeIndex; |
| 2 | 1173 | | int currentCapacity = SparseArray.Length; |
| 2 | 1174 | | bool needsExpansion = false; |
| | 1175 | |
|
| 2 | 1176 | | if (indexToClaim >= currentCapacity) |
| 1 | 1177 | | { |
| 1 | 1178 | | needsExpansion = true; |
| | 1179 | | // We're out of space, the last free index points to nothing. Allocate more indices. |
| 1 | 1180 | | int newCapacity = currentCapacity + howMuchToExpand; |
| | 1181 | |
|
| 1 | 1182 | | int[] newSparseArray = new int[newCapacity]; |
| 1 | 1183 | | Array.Copy(SparseArray, 0, newSparseArray, 0, currentCapacity); |
| 1 | 1184 | | SparseArray = newSparseArray; |
| | 1185 | |
|
| 1 | 1186 | | int[] newDenseArray = new int[newCapacity]; |
| 1 | 1187 | | Array.Copy(DenseArray, 0, newDenseArray, 0, currentCapacity); |
| 1 | 1188 | | DenseArray = newDenseArray; |
| | 1189 | |
|
| 1 | 1190 | | T0[] newArray0 = new T0[newCapacity]; |
| 1 | 1191 | | Array.Copy(array0, 0, newArray0, 0, currentCapacity); |
| 1 | 1192 | | array0 = newArray0; |
| | 1193 | |
|
| 1 | 1194 | | T1[] newArray1 = new T1[newCapacity]; |
| 1 | 1195 | | Array.Copy(array1, 0, newArray1, 0, currentCapacity); |
| 1 | 1196 | | array1 = newArray1; |
| | 1197 | |
|
| 1 | 1198 | | T2[] newArray2 = new T2[newCapacity]; |
| 1 | 1199 | | Array.Copy(array2, 0, newArray2, 0, currentCapacity); |
| 1 | 1200 | | array2 = newArray2; |
| | 1201 | |
|
| 1 | 1202 | | T3[] newArray3 = new T3[newCapacity]; |
| 1 | 1203 | | Array.Copy(array3, 0, newArray3, 0, currentCapacity); |
| 1 | 1204 | | array3 = newArray3; |
| | 1205 | |
|
| 1 | 1206 | | T4[] newArray4 = new T4[newCapacity]; |
| 1 | 1207 | | Array.Copy(array4, 0, newArray4, 0, currentCapacity); |
| 1 | 1208 | | array4 = newArray4; |
| | 1209 | |
|
| 1 | 1210 | | T5[] newArray5 = new T5[newCapacity]; |
| 1 | 1211 | | Array.Copy(array5, 0, newArray5, 0, currentCapacity); |
| 1 | 1212 | | array5 = newArray5; |
| | 1213 | |
|
| 12 | 1214 | | for (int i = currentCapacity; i < newCapacity; i++) |
| 5 | 1215 | | { |
| 5 | 1216 | | SparseArray[i] = i + 1; // Build the free list chain. |
| 5 | 1217 | | DenseArray[i] = -1; // Set new dense indices as unclaimed. |
| 5 | 1218 | | } |
| 1 | 1219 | | } |
| | 1220 | |
|
| 2 | 1221 | | int nextFreeIndex = SparseArray[indexToClaim]; |
| | 1222 | |
|
| 2 | 1223 | | DenseArray[Count] = indexToClaim; // Point the next dense id at our newly claimed sparse index. |
| 2 | 1224 | | SparseArray[indexToClaim] = Count; // Point our newly claimed sparse index at the dense index. |
| | 1225 | |
|
| 2 | 1226 | | array0[Count] = obj0; |
| 2 | 1227 | | array1[Count] = obj1; |
| 2 | 1228 | | array2[Count] = obj2; |
| 2 | 1229 | | array3[Count] = obj3; |
| 2 | 1230 | | array4[Count] = obj4; |
| 2 | 1231 | | array5[Count] = obj5; |
| | 1232 | |
|
| 2 | 1233 | | ++Count; |
| 2 | 1234 | | FreeIndex = nextFreeIndex; // Set the free list head for next time. |
| | 1235 | |
|
| 2 | 1236 | | lookupIndex = indexToClaim; |
| 2 | 1237 | | return needsExpansion; |
| 2 | 1238 | | } |
| | 1239 | |
|
| | 1240 | | /// <summary> |
| | 1241 | | /// Adds a sparse/dense index pair to the set and expands the arrays if necessary. |
| | 1242 | | /// </summary> |
| | 1243 | | /// <returns>True if the index pool expanded.</returns> |
| | 1244 | | public bool AddWithExpandCheck<T0, T1, T2, T3, T4, T5, T6>(T0 obj0, ref T0[] array0, T1 obj1, ref T1[] array1, |
| | 1245 | | T2 obj2, |
| | 1246 | | ref T2[] array2, T3 obj3, ref T3[] array3, T4 obj4, ref T4[] array4, T5 obj5, ref T5[] array5, T6 obj6, |
| | 1247 | | ref T6[] array6, out int lookupIndex, int howMuchToExpand = 16) |
| 2 | 1248 | | { |
| 2 | 1249 | | int indexToClaim = FreeIndex; |
| 2 | 1250 | | int currentCapacity = SparseArray.Length; |
| 2 | 1251 | | bool needsExpansion = false; |
| | 1252 | |
|
| 2 | 1253 | | if (indexToClaim >= currentCapacity) |
| 1 | 1254 | | { |
| 1 | 1255 | | needsExpansion = true; |
| | 1256 | | // We're out of space, the last free index points to nothing. Allocate more indices. |
| 1 | 1257 | | int newCapacity = currentCapacity + howMuchToExpand; |
| | 1258 | |
|
| 1 | 1259 | | int[] newSparseArray = new int[newCapacity]; |
| 1 | 1260 | | Array.Copy(SparseArray, 0, newSparseArray, 0, currentCapacity); |
| 1 | 1261 | | SparseArray = newSparseArray; |
| | 1262 | |
|
| 1 | 1263 | | int[] newDenseArray = new int[newCapacity]; |
| 1 | 1264 | | Array.Copy(DenseArray, 0, newDenseArray, 0, currentCapacity); |
| 1 | 1265 | | DenseArray = newDenseArray; |
| | 1266 | |
|
| 1 | 1267 | | T0[] newArray0 = new T0[newCapacity]; |
| 1 | 1268 | | Array.Copy(array0, 0, newArray0, 0, currentCapacity); |
| 1 | 1269 | | array0 = newArray0; |
| | 1270 | |
|
| 1 | 1271 | | T1[] newArray1 = new T1[newCapacity]; |
| 1 | 1272 | | Array.Copy(array1, 0, newArray1, 0, currentCapacity); |
| 1 | 1273 | | array1 = newArray1; |
| | 1274 | |
|
| 1 | 1275 | | T2[] newArray2 = new T2[newCapacity]; |
| 1 | 1276 | | Array.Copy(array2, 0, newArray2, 0, currentCapacity); |
| 1 | 1277 | | array2 = newArray2; |
| | 1278 | |
|
| 1 | 1279 | | T3[] newArray3 = new T3[newCapacity]; |
| 1 | 1280 | | Array.Copy(array3, 0, newArray3, 0, currentCapacity); |
| 1 | 1281 | | array3 = newArray3; |
| | 1282 | |
|
| 1 | 1283 | | T4[] newArray4 = new T4[newCapacity]; |
| 1 | 1284 | | Array.Copy(array4, 0, newArray4, 0, currentCapacity); |
| 1 | 1285 | | array4 = newArray4; |
| | 1286 | |
|
| 1 | 1287 | | T5[] newArray5 = new T5[newCapacity]; |
| 1 | 1288 | | Array.Copy(array5, 0, newArray5, 0, currentCapacity); |
| 1 | 1289 | | array5 = newArray5; |
| | 1290 | |
|
| 1 | 1291 | | T6[] newArray6 = new T6[newCapacity]; |
| 1 | 1292 | | Array.Copy(array6, 0, newArray6, 0, currentCapacity); |
| 1 | 1293 | | array6 = newArray6; |
| | 1294 | |
|
| 12 | 1295 | | for (int i = currentCapacity; i < newCapacity; i++) |
| 5 | 1296 | | { |
| 5 | 1297 | | SparseArray[i] = i + 1; // Build the free list chain. |
| 5 | 1298 | | DenseArray[i] = -1; // Set new dense indices as unclaimed. |
| 5 | 1299 | | } |
| 1 | 1300 | | } |
| | 1301 | |
|
| 2 | 1302 | | int nextFreeIndex = SparseArray[indexToClaim]; |
| | 1303 | |
|
| 2 | 1304 | | DenseArray[Count] = indexToClaim; // Point the next dense id at our newly claimed sparse index. |
| 2 | 1305 | | SparseArray[indexToClaim] = Count; // Point our newly claimed sparse index at the dense index. |
| | 1306 | |
|
| 2 | 1307 | | array0[Count] = obj0; |
| 2 | 1308 | | array1[Count] = obj1; |
| 2 | 1309 | | array2[Count] = obj2; |
| 2 | 1310 | | array3[Count] = obj3; |
| 2 | 1311 | | array4[Count] = obj4; |
| 2 | 1312 | | array5[Count] = obj5; |
| 2 | 1313 | | array6[Count] = obj6; |
| | 1314 | |
|
| 2 | 1315 | | ++Count; |
| 2 | 1316 | | FreeIndex = nextFreeIndex; // Set the free list head for next time. |
| | 1317 | |
|
| 2 | 1318 | | lookupIndex = indexToClaim; |
| 2 | 1319 | | return needsExpansion; |
| 2 | 1320 | | } |
| | 1321 | |
|
| | 1322 | | /// <summary> |
| | 1323 | | /// Adds a sparse/dense index pair to the set and expands the arrays if necessary. |
| | 1324 | | /// </summary> |
| | 1325 | | /// <returns>True if the index pool expanded.</returns> |
| | 1326 | | public bool AddWithExpandCheck<T0, T1, T2, T3, T4, T5, T6, T7>(T0 obj0, ref T0[] array0, T1 obj1, |
| | 1327 | | ref T1[] array1, T2 obj2, |
| | 1328 | | ref T2[] array2, T3 obj3, ref T3[] array3, T4 obj4, ref T4[] array4, T5 obj5, ref T5[] array5, T6 obj6, |
| | 1329 | | ref T6[] array6, T7 obj7, ref T7[] array7, out int lookupIndex, int howMuchToExpand = 16) |
| 2 | 1330 | | { |
| 2 | 1331 | | int indexToClaim = FreeIndex; |
| 2 | 1332 | | int currentCapacity = SparseArray.Length; |
| 2 | 1333 | | bool needsExpansion = false; |
| | 1334 | |
|
| 2 | 1335 | | if (indexToClaim >= currentCapacity) |
| 1 | 1336 | | { |
| 1 | 1337 | | needsExpansion = true; |
| | 1338 | | // We're out of space, the last free index points to nothing. Allocate more indices. |
| 1 | 1339 | | int newCapacity = currentCapacity + howMuchToExpand; |
| | 1340 | |
|
| 1 | 1341 | | int[] newSparseArray = new int[newCapacity]; |
| 1 | 1342 | | Array.Copy(SparseArray, 0, newSparseArray, 0, currentCapacity); |
| 1 | 1343 | | SparseArray = newSparseArray; |
| | 1344 | |
|
| 1 | 1345 | | int[] newDenseArray = new int[newCapacity]; |
| 1 | 1346 | | Array.Copy(DenseArray, 0, newDenseArray, 0, currentCapacity); |
| 1 | 1347 | | DenseArray = newDenseArray; |
| | 1348 | |
|
| 1 | 1349 | | T0[] newArray0 = new T0[newCapacity]; |
| 1 | 1350 | | Array.Copy(array0, 0, newArray0, 0, currentCapacity); |
| 1 | 1351 | | array0 = newArray0; |
| | 1352 | |
|
| 1 | 1353 | | T1[] newArray1 = new T1[newCapacity]; |
| 1 | 1354 | | Array.Copy(array1, 0, newArray1, 0, currentCapacity); |
| 1 | 1355 | | array1 = newArray1; |
| | 1356 | |
|
| 1 | 1357 | | T2[] newArray2 = new T2[newCapacity]; |
| 1 | 1358 | | Array.Copy(array2, 0, newArray2, 0, currentCapacity); |
| 1 | 1359 | | array2 = newArray2; |
| | 1360 | |
|
| 1 | 1361 | | T3[] newArray3 = new T3[newCapacity]; |
| 1 | 1362 | | Array.Copy(array3, 0, newArray3, 0, currentCapacity); |
| 1 | 1363 | | array3 = newArray3; |
| | 1364 | |
|
| 1 | 1365 | | T4[] newArray4 = new T4[newCapacity]; |
| 1 | 1366 | | Array.Copy(array4, 0, newArray4, 0, currentCapacity); |
| 1 | 1367 | | array4 = newArray4; |
| | 1368 | |
|
| 1 | 1369 | | T5[] newArray5 = new T5[newCapacity]; |
| 1 | 1370 | | Array.Copy(array5, 0, newArray5, 0, currentCapacity); |
| 1 | 1371 | | array5 = newArray5; |
| | 1372 | |
|
| 1 | 1373 | | T6[] newArray6 = new T6[newCapacity]; |
| 1 | 1374 | | Array.Copy(array6, 0, newArray6, 0, currentCapacity); |
| 1 | 1375 | | array6 = newArray6; |
| | 1376 | |
|
| 1 | 1377 | | T7[] newArray7 = new T7[newCapacity]; |
| 1 | 1378 | | Array.Copy(array7, 0, newArray7, 0, currentCapacity); |
| 1 | 1379 | | array7 = newArray7; |
| | 1380 | |
|
| 12 | 1381 | | for (int i = currentCapacity; i < newCapacity; i++) |
| 5 | 1382 | | { |
| 5 | 1383 | | SparseArray[i] = i + 1; // Build the free list chain. |
| 5 | 1384 | | DenseArray[i] = -1; // Set new dense indices as unclaimed. |
| 5 | 1385 | | } |
| 1 | 1386 | | } |
| | 1387 | |
|
| 2 | 1388 | | int nextFreeIndex = SparseArray[indexToClaim]; |
| | 1389 | |
|
| 2 | 1390 | | DenseArray[Count] = indexToClaim; // Point the next dense id at our newly claimed sparse index. |
| 2 | 1391 | | SparseArray[indexToClaim] = Count; // Point our newly claimed sparse index at the dense index. |
| | 1392 | |
|
| 2 | 1393 | | array0[Count] = obj0; |
| 2 | 1394 | | array1[Count] = obj1; |
| 2 | 1395 | | array2[Count] = obj2; |
| 2 | 1396 | | array3[Count] = obj3; |
| 2 | 1397 | | array4[Count] = obj4; |
| 2 | 1398 | | array5[Count] = obj5; |
| 2 | 1399 | | array6[Count] = obj6; |
| 2 | 1400 | | array7[Count] = obj7; |
| | 1401 | |
|
| 2 | 1402 | | ++Count; |
| 2 | 1403 | | FreeIndex = nextFreeIndex; // Set the free list head for next time. |
| | 1404 | |
|
| 2 | 1405 | | lookupIndex = indexToClaim; |
| 2 | 1406 | | return needsExpansion; |
| 2 | 1407 | | } |
| | 1408 | |
|
| | 1409 | | /// <summary> |
| | 1410 | | /// Adds to the set without checking if the set needs to expand. |
| | 1411 | | /// </summary> |
| | 1412 | | /// <returns>The sparse index allocated</returns> |
| | 1413 | | public int AddUnchecked<T0>(T0 obj0, T0[] array0) |
| 4 | 1414 | | { |
| 4 | 1415 | | int indexToClaim = FreeIndex; |
| 4 | 1416 | | int nextFreeIndex = SparseArray[indexToClaim]; |
| 4 | 1417 | | DenseArray[Count] = indexToClaim; // Point the next dense id at our newly claimed sparse index. |
| 4 | 1418 | | SparseArray[indexToClaim] = Count; // Point our newly claimed sparse index at the dense index. |
| | 1419 | |
|
| 4 | 1420 | | array0[Count] = obj0; |
| | 1421 | |
|
| 4 | 1422 | | ++Count; |
| 4 | 1423 | | FreeIndex = nextFreeIndex; // Set the free list head for next time. |
| | 1424 | |
|
| 4 | 1425 | | return indexToClaim; |
| 4 | 1426 | | } |
| | 1427 | |
|
| | 1428 | | /// <summary> |
| | 1429 | | /// Adds to the set without checking if the set needs to expand. |
| | 1430 | | /// </summary> |
| | 1431 | | /// <returns>The sparse index allocated</returns> |
| | 1432 | | public int AddUnchecked<T0, T1>(T0 obj0, T0[] array0, T1 obj1, T1[] array1) |
| 4 | 1433 | | { |
| 4 | 1434 | | int indexToClaim = FreeIndex; |
| 4 | 1435 | | int nextFreeIndex = SparseArray[indexToClaim]; |
| 4 | 1436 | | DenseArray[Count] = indexToClaim; // Point the next dense id at our newly claimed sparse index. |
| 4 | 1437 | | SparseArray[indexToClaim] = Count; // Point our newly claimed sparse index at the dense index. |
| | 1438 | |
|
| 4 | 1439 | | array0[Count] = obj0; |
| 4 | 1440 | | array1[Count] = obj1; |
| | 1441 | |
|
| 4 | 1442 | | ++Count; |
| 4 | 1443 | | FreeIndex = nextFreeIndex; // Set the free list head for next time. |
| | 1444 | |
|
| 4 | 1445 | | return indexToClaim; |
| 4 | 1446 | | } |
| | 1447 | |
|
| | 1448 | | /// <summary> |
| | 1449 | | /// Adds to the set without checking if the set needs to expand. |
| | 1450 | | /// </summary> |
| | 1451 | | /// <returns>The sparse index allocated</returns> |
| | 1452 | | public int AddUnchecked<T0, T1, T2>(T0 obj0, T0[] array0, T1 obj1, T1[] array1, T2 obj2, T2[] array2) |
| 4 | 1453 | | { |
| 4 | 1454 | | int indexToClaim = FreeIndex; |
| 4 | 1455 | | int nextFreeIndex = SparseArray[indexToClaim]; |
| 4 | 1456 | | DenseArray[Count] = indexToClaim; // Point the next dense id at our newly claimed sparse index. |
| 4 | 1457 | | SparseArray[indexToClaim] = Count; // Point our newly claimed sparse index at the dense index. |
| | 1458 | |
|
| 4 | 1459 | | array0[Count] = obj0; |
| 4 | 1460 | | array1[Count] = obj1; |
| 4 | 1461 | | array2[Count] = obj2; |
| | 1462 | |
|
| 4 | 1463 | | ++Count; |
| 4 | 1464 | | FreeIndex = nextFreeIndex; // Set the free list head for next time. |
| | 1465 | |
|
| 4 | 1466 | | return indexToClaim; |
| 4 | 1467 | | } |
| | 1468 | |
|
| | 1469 | | /// <summary> |
| | 1470 | | /// Adds to the set without checking if the set needs to expand. |
| | 1471 | | /// </summary> |
| | 1472 | | /// <returns>The sparse index allocated</returns> |
| | 1473 | | public int AddUnchecked<T0, T1, T2, T3>(T0 obj0, T0[] array0, T1 obj1, T1[] array1, T2 obj2, T2[] array2, |
| | 1474 | | T3 obj3, |
| | 1475 | | T3[] array3) |
| 4 | 1476 | | { |
| 4 | 1477 | | int indexToClaim = FreeIndex; |
| 4 | 1478 | | int nextFreeIndex = SparseArray[indexToClaim]; |
| 4 | 1479 | | DenseArray[Count] = indexToClaim; // Point the next dense id at our newly claimed sparse index. |
| 4 | 1480 | | SparseArray[indexToClaim] = Count; // Point our newly claimed sparse index at the dense index. |
| | 1481 | |
|
| 4 | 1482 | | array0[Count] = obj0; |
| 4 | 1483 | | array1[Count] = obj1; |
| 4 | 1484 | | array2[Count] = obj2; |
| 4 | 1485 | | array3[Count] = obj3; |
| | 1486 | |
|
| 4 | 1487 | | ++Count; |
| 4 | 1488 | | FreeIndex = nextFreeIndex; // Set the free list head for next time. |
| | 1489 | |
|
| 4 | 1490 | | return indexToClaim; |
| 4 | 1491 | | } |
| | 1492 | |
|
| | 1493 | | /// <summary> |
| | 1494 | | /// Adds to the set without checking if the set needs to expand. |
| | 1495 | | /// </summary> |
| | 1496 | | /// <returns>The sparse index allocated</returns> |
| | 1497 | | public int AddUnchecked<T0, T1, T2, T3, T4>(T0 obj0, T0[] array0, T1 obj1, T1[] array1, T2 obj2, T2[] array2, |
| | 1498 | | T3 obj3, T3[] array3, T4 obj4, T4[] array4) |
| 4 | 1499 | | { |
| 4 | 1500 | | int indexToClaim = FreeIndex; |
| 4 | 1501 | | int nextFreeIndex = SparseArray[indexToClaim]; |
| 4 | 1502 | | DenseArray[Count] = indexToClaim; // Point the next dense id at our newly claimed sparse index. |
| 4 | 1503 | | SparseArray[indexToClaim] = Count; // Point our newly claimed sparse index at the dense index. |
| | 1504 | |
|
| 4 | 1505 | | array0[Count] = obj0; |
| 4 | 1506 | | array1[Count] = obj1; |
| 4 | 1507 | | array2[Count] = obj2; |
| 4 | 1508 | | array3[Count] = obj3; |
| 4 | 1509 | | array4[Count] = obj4; |
| | 1510 | |
|
| 4 | 1511 | | ++Count; |
| 4 | 1512 | | FreeIndex = nextFreeIndex; // Set the free list head for next time. |
| | 1513 | |
|
| 4 | 1514 | | return indexToClaim; |
| 4 | 1515 | | } |
| | 1516 | |
|
| | 1517 | | /// <summary> |
| | 1518 | | /// Adds to the set without checking if the set needs to expand. |
| | 1519 | | /// </summary> |
| | 1520 | | /// <returns>The sparse index allocated</returns> |
| | 1521 | | public int AddUnchecked<T0, T1, T2, T3, T4, T5>(T0 obj0, T0[] array0, T1 obj1, T1[] array1, T2 obj2, |
| | 1522 | | T2[] array2, |
| | 1523 | | T3 obj3, T3[] array3, T4 obj4, T4[] array4, T5 obj5, T5[] array5) |
| 4 | 1524 | | { |
| 4 | 1525 | | int indexToClaim = FreeIndex; |
| 4 | 1526 | | int nextFreeIndex = SparseArray[indexToClaim]; |
| 4 | 1527 | | DenseArray[Count] = indexToClaim; // Point the next dense id at our newly claimed sparse index. |
| 4 | 1528 | | SparseArray[indexToClaim] = Count; // Point our newly claimed sparse index at the dense index. |
| | 1529 | |
|
| 4 | 1530 | | array0[Count] = obj0; |
| 4 | 1531 | | array1[Count] = obj1; |
| 4 | 1532 | | array2[Count] = obj2; |
| 4 | 1533 | | array3[Count] = obj3; |
| 4 | 1534 | | array4[Count] = obj4; |
| 4 | 1535 | | array5[Count] = obj5; |
| | 1536 | |
|
| 4 | 1537 | | ++Count; |
| 4 | 1538 | | FreeIndex = nextFreeIndex; // Set the free list head for next time. |
| | 1539 | |
|
| 4 | 1540 | | return indexToClaim; |
| 4 | 1541 | | } |
| | 1542 | |
|
| | 1543 | | /// <summary> |
| | 1544 | | /// Adds to the set without checking if the set needs to expand. |
| | 1545 | | /// </summary> |
| | 1546 | | /// <returns>The sparse index allocated</returns> |
| | 1547 | | public int AddUnchecked<T0, T1, T2, T3, T4, T5, T6>(T0 obj0, T0[] array0, T1 obj1, T1[] array1, T2 obj2, |
| | 1548 | | T2[] array2, |
| | 1549 | | T3 obj3, T3[] array3, T4 obj4, T4[] array4, T5 obj5, T5[] array5, T6 obj6, T6[] array6) |
| 4 | 1550 | | { |
| 4 | 1551 | | int indexToClaim = FreeIndex; |
| 4 | 1552 | | int nextFreeIndex = SparseArray[indexToClaim]; |
| 4 | 1553 | | DenseArray[Count] = indexToClaim; // Point the next dense id at our newly claimed sparse index. |
| 4 | 1554 | | SparseArray[indexToClaim] = Count; // Point our newly claimed sparse index at the dense index. |
| | 1555 | |
|
| 4 | 1556 | | array0[Count] = obj0; |
| 4 | 1557 | | array1[Count] = obj1; |
| 4 | 1558 | | array2[Count] = obj2; |
| 4 | 1559 | | array3[Count] = obj3; |
| 4 | 1560 | | array4[Count] = obj4; |
| 4 | 1561 | | array5[Count] = obj5; |
| 4 | 1562 | | array6[Count] = obj6; |
| | 1563 | |
|
| 4 | 1564 | | ++Count; |
| 4 | 1565 | | FreeIndex = nextFreeIndex; // Set the free list head for next time. |
| | 1566 | |
|
| 4 | 1567 | | return indexToClaim; |
| 4 | 1568 | | } |
| | 1569 | |
|
| | 1570 | | /// <summary> |
| | 1571 | | /// Adds to the set without checking if the set needs to expand. |
| | 1572 | | /// </summary> |
| | 1573 | | /// <returns>The sparse index allocated</returns> |
| | 1574 | | public int AddUnchecked<T0, T1, T2, T3, T4, T5, T6, T7>(T0 obj0, T0[] array0, T1 obj1, T1[] array1, T2 obj2, |
| | 1575 | | T2[] array2, T3 obj3, T3[] array3, T4 obj4, T4[] array4, T5 obj5, T5[] array5, T6 obj6, T6[] array6, |
| | 1576 | | T7 obj7, T7[] array7) |
| 4 | 1577 | | { |
| 4 | 1578 | | int indexToClaim = FreeIndex; |
| 4 | 1579 | | int nextFreeIndex = SparseArray[indexToClaim]; |
| 4 | 1580 | | DenseArray[Count] = indexToClaim; // Point the next dense id at our newly claimed sparse index. |
| 4 | 1581 | | SparseArray[indexToClaim] = Count; // Point our newly claimed sparse index at the dense index. |
| | 1582 | |
|
| 4 | 1583 | | array0[Count] = obj0; |
| 4 | 1584 | | array1[Count] = obj1; |
| 4 | 1585 | | array2[Count] = obj2; |
| 4 | 1586 | | array3[Count] = obj3; |
| 4 | 1587 | | array4[Count] = obj4; |
| 4 | 1588 | | array5[Count] = obj5; |
| 4 | 1589 | | array6[Count] = obj6; |
| 4 | 1590 | | array7[Count] = obj7; |
| | 1591 | |
|
| 4 | 1592 | | ++Count; |
| 4 | 1593 | | FreeIndex = nextFreeIndex; // Set the free list head for next time. |
| | 1594 | |
|
| 4 | 1595 | | return indexToClaim; |
| 4 | 1596 | | } |
| | 1597 | |
|
| | 1598 | | public void RemoveUnchecked<T0>(int sparseIndexToRemove, T0[] array0) |
| 2 | 1599 | | { |
| 2 | 1600 | | int denseIndexToRemove = SparseArray[sparseIndexToRemove]; |
| 2 | 1601 | | int newLength = Count - 1; |
| 2 | 1602 | | int sparseIndexBeingSwapped = DenseArray[newLength]; |
| | 1603 | |
|
| | 1604 | | // Swap the entry being removed with the last entry. |
| 2 | 1605 | | SparseArray[sparseIndexBeingSwapped] = denseIndexToRemove; |
| 2 | 1606 | | DenseArray[denseIndexToRemove] = sparseIndexBeingSwapped; |
| 2 | 1607 | | array0[denseIndexToRemove] = array0[newLength]; |
| | 1608 | |
|
| | 1609 | | // Clear the dense index, for debugging purposes |
| 2 | 1610 | | DenseArray[newLength] = -1; |
| 2 | 1611 | | array0[newLength] = default; |
| | 1612 | |
|
| | 1613 | | // Add the sparse index to the free list. |
| 2 | 1614 | | SparseArray[sparseIndexToRemove] = FreeIndex; |
| 2 | 1615 | | FreeIndex = sparseIndexToRemove; |
| | 1616 | |
|
| 2 | 1617 | | Count = newLength; |
| 2 | 1618 | | } |
| | 1619 | |
|
| | 1620 | | public void RemoveUnchecked<T0, T1>(int sparseIndexToRemove, T0[] array0, T1[] array1) |
| 1 | 1621 | | { |
| 1 | 1622 | | int denseIndexToRemove = SparseArray[sparseIndexToRemove]; |
| 1 | 1623 | | int newLength = Count - 1; |
| 1 | 1624 | | int sparseIndexBeingSwapped = DenseArray[newLength]; |
| | 1625 | |
|
| | 1626 | | // Swap the entry being removed with the last entry. |
| 1 | 1627 | | SparseArray[sparseIndexBeingSwapped] = denseIndexToRemove; |
| 1 | 1628 | | DenseArray[denseIndexToRemove] = sparseIndexBeingSwapped; |
| 1 | 1629 | | array0[denseIndexToRemove] = array0[newLength]; |
| 1 | 1630 | | array1[denseIndexToRemove] = array1[newLength]; |
| | 1631 | |
|
| | 1632 | | // Clear the dense index, for debugging purposes |
| 1 | 1633 | | DenseArray[newLength] = -1; |
| 1 | 1634 | | array0[newLength] = default; |
| 1 | 1635 | | array1[newLength] = default; |
| | 1636 | |
|
| | 1637 | | // Add the sparse index to the free list. |
| 1 | 1638 | | SparseArray[sparseIndexToRemove] = FreeIndex; |
| 1 | 1639 | | FreeIndex = sparseIndexToRemove; |
| | 1640 | |
|
| 1 | 1641 | | Count = newLength; |
| 1 | 1642 | | } |
| | 1643 | |
|
| | 1644 | | public void RemoveUnchecked<T0, T1, T2>(int sparseIndexToRemove, T0[] array0, T1[] array1, T2[] array2) |
| 1 | 1645 | | { |
| 1 | 1646 | | int denseIndexToRemove = SparseArray[sparseIndexToRemove]; |
| 1 | 1647 | | int newLength = Count - 1; |
| 1 | 1648 | | int sparseIndexBeingSwapped = DenseArray[newLength]; |
| | 1649 | |
|
| | 1650 | | // Swap the entry being removed with the last entry. |
| 1 | 1651 | | SparseArray[sparseIndexBeingSwapped] = denseIndexToRemove; |
| 1 | 1652 | | DenseArray[denseIndexToRemove] = sparseIndexBeingSwapped; |
| 1 | 1653 | | array0[denseIndexToRemove] = array0[newLength]; |
| 1 | 1654 | | array1[denseIndexToRemove] = array1[newLength]; |
| 1 | 1655 | | array2[denseIndexToRemove] = array2[newLength]; |
| | 1656 | |
|
| | 1657 | | // Clear the dense index, for debugging purposes |
| 1 | 1658 | | DenseArray[newLength] = -1; |
| 1 | 1659 | | array0[newLength] = default; |
| 1 | 1660 | | array1[newLength] = default; |
| 1 | 1661 | | array2[newLength] = default; |
| | 1662 | |
|
| | 1663 | | // Add the sparse index to the free list. |
| 1 | 1664 | | SparseArray[sparseIndexToRemove] = FreeIndex; |
| 1 | 1665 | | FreeIndex = sparseIndexToRemove; |
| | 1666 | |
|
| 1 | 1667 | | Count = newLength; |
| 1 | 1668 | | } |
| | 1669 | |
|
| | 1670 | | public void RemoveUnchecked<T0, T1, T2, T3>(int sparseIndexToRemove, T0[] array0, T1[] array1, T2[] array2, |
| | 1671 | | T3[] array3) |
| 1 | 1672 | | { |
| 1 | 1673 | | int denseIndexToRemove = SparseArray[sparseIndexToRemove]; |
| 1 | 1674 | | int newLength = Count - 1; |
| 1 | 1675 | | int sparseIndexBeingSwapped = DenseArray[newLength]; |
| | 1676 | |
|
| | 1677 | | // Swap the entry being removed with the last entry. |
| 1 | 1678 | | SparseArray[sparseIndexBeingSwapped] = denseIndexToRemove; |
| 1 | 1679 | | DenseArray[denseIndexToRemove] = sparseIndexBeingSwapped; |
| 1 | 1680 | | array0[denseIndexToRemove] = array0[newLength]; |
| 1 | 1681 | | array1[denseIndexToRemove] = array1[newLength]; |
| 1 | 1682 | | array2[denseIndexToRemove] = array2[newLength]; |
| 1 | 1683 | | array3[denseIndexToRemove] = array3[newLength]; |
| | 1684 | |
|
| | 1685 | | // Clear the dense index, for debugging purposes |
| 1 | 1686 | | DenseArray[newLength] = -1; |
| 1 | 1687 | | array0[newLength] = default; |
| 1 | 1688 | | array1[newLength] = default; |
| 1 | 1689 | | array2[newLength] = default; |
| 1 | 1690 | | array3[newLength] = default; |
| | 1691 | |
|
| | 1692 | | // Add the sparse index to the free list. |
| 1 | 1693 | | SparseArray[sparseIndexToRemove] = FreeIndex; |
| 1 | 1694 | | FreeIndex = sparseIndexToRemove; |
| | 1695 | |
|
| 1 | 1696 | | Count = newLength; |
| 1 | 1697 | | } |
| | 1698 | |
|
| | 1699 | | public void RemoveUnchecked<T0, T1, T2, T3, T4>(int sparseIndexToRemove, T0[] array0, T1[] array1, T2[] array2, |
| | 1700 | | T3[] array3, T4[] array4) |
| 1 | 1701 | | { |
| 1 | 1702 | | int denseIndexToRemove = SparseArray[sparseIndexToRemove]; |
| 1 | 1703 | | int newLength = Count - 1; |
| 1 | 1704 | | int sparseIndexBeingSwapped = DenseArray[newLength]; |
| | 1705 | |
|
| | 1706 | | // Swap the entry being removed with the last entry. |
| 1 | 1707 | | SparseArray[sparseIndexBeingSwapped] = denseIndexToRemove; |
| 1 | 1708 | | DenseArray[denseIndexToRemove] = sparseIndexBeingSwapped; |
| 1 | 1709 | | array0[denseIndexToRemove] = array0[newLength]; |
| 1 | 1710 | | array1[denseIndexToRemove] = array1[newLength]; |
| 1 | 1711 | | array2[denseIndexToRemove] = array2[newLength]; |
| 1 | 1712 | | array3[denseIndexToRemove] = array3[newLength]; |
| 1 | 1713 | | array4[denseIndexToRemove] = array4[newLength]; |
| | 1714 | |
|
| | 1715 | | // Clear the dense index, for debugging purposes |
| 1 | 1716 | | DenseArray[newLength] = -1; |
| 1 | 1717 | | array0[newLength] = default; |
| 1 | 1718 | | array1[newLength] = default; |
| 1 | 1719 | | array2[newLength] = default; |
| 1 | 1720 | | array3[newLength] = default; |
| 1 | 1721 | | array4[newLength] = default; |
| | 1722 | |
|
| | 1723 | | // Add the sparse index to the free list. |
| 1 | 1724 | | SparseArray[sparseIndexToRemove] = FreeIndex; |
| 1 | 1725 | | FreeIndex = sparseIndexToRemove; |
| | 1726 | |
|
| 1 | 1727 | | Count = newLength; |
| 1 | 1728 | | } |
| | 1729 | |
|
| | 1730 | | public void RemoveUnchecked<T0, T1, T2, T3, T4, T5>(int sparseIndexToRemove, T0[] array0, T1[] array1, |
| | 1731 | | T2[] array2, |
| | 1732 | | T3[] array3, T4[] array4, T5[] array5) |
| 1 | 1733 | | { |
| 1 | 1734 | | int denseIndexToRemove = SparseArray[sparseIndexToRemove]; |
| 1 | 1735 | | int newLength = Count - 1; |
| 1 | 1736 | | int sparseIndexBeingSwapped = DenseArray[newLength]; |
| | 1737 | |
|
| | 1738 | | // Swap the entry being removed with the last entry. |
| 1 | 1739 | | SparseArray[sparseIndexBeingSwapped] = denseIndexToRemove; |
| 1 | 1740 | | DenseArray[denseIndexToRemove] = sparseIndexBeingSwapped; |
| 1 | 1741 | | array0[denseIndexToRemove] = array0[newLength]; |
| 1 | 1742 | | array1[denseIndexToRemove] = array1[newLength]; |
| 1 | 1743 | | array2[denseIndexToRemove] = array2[newLength]; |
| 1 | 1744 | | array3[denseIndexToRemove] = array3[newLength]; |
| 1 | 1745 | | array4[denseIndexToRemove] = array4[newLength]; |
| 1 | 1746 | | array5[denseIndexToRemove] = array5[newLength]; |
| | 1747 | |
|
| | 1748 | | // Clear the dense index, for debugging purposes |
| 1 | 1749 | | DenseArray[newLength] = -1; |
| 1 | 1750 | | array0[newLength] = default; |
| 1 | 1751 | | array1[newLength] = default; |
| 1 | 1752 | | array2[newLength] = default; |
| 1 | 1753 | | array3[newLength] = default; |
| 1 | 1754 | | array4[newLength] = default; |
| 1 | 1755 | | array5[newLength] = default; |
| | 1756 | |
|
| | 1757 | | // Add the sparse index to the free list. |
| 1 | 1758 | | SparseArray[sparseIndexToRemove] = FreeIndex; |
| 1 | 1759 | | FreeIndex = sparseIndexToRemove; |
| | 1760 | |
|
| 1 | 1761 | | Count = newLength; |
| 1 | 1762 | | } |
| | 1763 | |
|
| | 1764 | | public void RemoveUnchecked<T0, T1, T2, T3, T4, T5, T6>(int sparseIndexToRemove, T0[] array0, T1[] array1, |
| | 1765 | | T2[] array2, T3[] array3, T4[] array4, T5[] array5, T6[] array6) |
| 1 | 1766 | | { |
| 1 | 1767 | | int denseIndexToRemove = SparseArray[sparseIndexToRemove]; |
| 1 | 1768 | | int newLength = Count - 1; |
| 1 | 1769 | | int sparseIndexBeingSwapped = DenseArray[newLength]; |
| | 1770 | |
|
| | 1771 | | // Swap the entry being removed with the last entry. |
| 1 | 1772 | | SparseArray[sparseIndexBeingSwapped] = denseIndexToRemove; |
| 1 | 1773 | | DenseArray[denseIndexToRemove] = sparseIndexBeingSwapped; |
| 1 | 1774 | | array0[denseIndexToRemove] = array0[newLength]; |
| 1 | 1775 | | array1[denseIndexToRemove] = array1[newLength]; |
| 1 | 1776 | | array2[denseIndexToRemove] = array2[newLength]; |
| 1 | 1777 | | array3[denseIndexToRemove] = array3[newLength]; |
| 1 | 1778 | | array4[denseIndexToRemove] = array4[newLength]; |
| 1 | 1779 | | array5[denseIndexToRemove] = array5[newLength]; |
| 1 | 1780 | | array6[denseIndexToRemove] = array6[newLength]; |
| | 1781 | |
|
| | 1782 | | // Clear the dense index, for debugging purposes |
| 1 | 1783 | | DenseArray[newLength] = -1; |
| 1 | 1784 | | array0[newLength] = default; |
| 1 | 1785 | | array1[newLength] = default; |
| 1 | 1786 | | array2[newLength] = default; |
| 1 | 1787 | | array3[newLength] = default; |
| 1 | 1788 | | array4[newLength] = default; |
| 1 | 1789 | | array5[newLength] = default; |
| 1 | 1790 | | array6[newLength] = default; |
| | 1791 | |
|
| | 1792 | | // Add the sparse index to the free list. |
| 1 | 1793 | | SparseArray[sparseIndexToRemove] = FreeIndex; |
| 1 | 1794 | | FreeIndex = sparseIndexToRemove; |
| | 1795 | |
|
| 1 | 1796 | | Count = newLength; |
| 1 | 1797 | | } |
| | 1798 | |
|
| | 1799 | | public void RemoveUnchecked<T0, T1, T2, T3, T4, T5, T6, T7>(int sparseIndexToRemove, T0[] array0, T1[] array1, |
| | 1800 | | T2[] array2, T3[] array3, T4[] array4, T5[] array5, T6[] array6, T7[] array7) |
| 1 | 1801 | | { |
| 1 | 1802 | | int denseIndexToRemove = SparseArray[sparseIndexToRemove]; |
| 1 | 1803 | | int newLength = Count - 1; |
| 1 | 1804 | | int sparseIndexBeingSwapped = DenseArray[newLength]; |
| | 1805 | |
|
| | 1806 | | // Swap the entry being removed with the last entry. |
| 1 | 1807 | | SparseArray[sparseIndexBeingSwapped] = denseIndexToRemove; |
| 1 | 1808 | | DenseArray[denseIndexToRemove] = sparseIndexBeingSwapped; |
| 1 | 1809 | | array0[denseIndexToRemove] = array0[newLength]; |
| 1 | 1810 | | array1[denseIndexToRemove] = array1[newLength]; |
| 1 | 1811 | | array2[denseIndexToRemove] = array2[newLength]; |
| 1 | 1812 | | array3[denseIndexToRemove] = array3[newLength]; |
| 1 | 1813 | | array4[denseIndexToRemove] = array4[newLength]; |
| 1 | 1814 | | array5[denseIndexToRemove] = array5[newLength]; |
| 1 | 1815 | | array6[denseIndexToRemove] = array6[newLength]; |
| 1 | 1816 | | array7[denseIndexToRemove] = array7[newLength]; |
| | 1817 | |
|
| | 1818 | | // Clear the dense index, for debugging purposes |
| 1 | 1819 | | DenseArray[newLength] = -1; |
| 1 | 1820 | | array0[newLength] = default; |
| 1 | 1821 | | array1[newLength] = default; |
| 1 | 1822 | | array2[newLength] = default; |
| 1 | 1823 | | array3[newLength] = default; |
| 1 | 1824 | | array4[newLength] = default; |
| 1 | 1825 | | array5[newLength] = default; |
| 1 | 1826 | | array6[newLength] = default; |
| 1 | 1827 | | array7[newLength] = default; |
| | 1828 | |
|
| | 1829 | | // Add the sparse index to the free list. |
| 1 | 1830 | | SparseArray[sparseIndexToRemove] = FreeIndex; |
| 1 | 1831 | | FreeIndex = sparseIndexToRemove; |
| | 1832 | |
|
| 1 | 1833 | | Count = newLength; |
| 1 | 1834 | | } |
| | 1835 | |
|
| | 1836 | | #endregion |
| | 1837 | | } |
| | 1838 | | } |