| | 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 | | using Unity.Collections; |
| | 8 | |
|
| | 9 | | namespace GDX.Collections |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// An adapter collection for external data arrays that allows constant-time insertion, deletion, and lookup by |
| | 13 | | /// handle, as well as array-like iteration. |
| | 14 | | /// </summary> |
| | 15 | | public struct NativeArraySparseSet : IDisposable |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Holds references to the sparse array for swapping indices. |
| | 19 | | /// </summary> |
| | 20 | | public NativeArray<int> DenseArray; |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// Holds references to dense array indices. |
| | 24 | | /// </summary> |
| | 25 | | /// <remarks> |
| | 26 | | /// Its own indices are claimed and freed via a free-list. |
| | 27 | | /// </remarks> |
| | 28 | | public NativeArray<int> SparseArray; |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// How many indices are being used currently? |
| | 32 | | /// </summary> |
| | 33 | | public int Count; |
| | 34 | |
|
| | 35 | | /// <summary> |
| | 36 | | /// The first free (currently unused) index in the sparse array. |
| | 37 | | /// </summary> |
| | 38 | | public int FreeIndex; |
| | 39 | |
|
| | 40 | | /// <summary> |
| | 41 | | /// Create a <see cref="NativeArraySparseSet" /> with an <paramref name="initialCapacity" />. |
| | 42 | | /// </summary> |
| | 43 | | /// <param name="initialCapacity">The initial capacity of the sparse and dense int arrays.</param> |
| | 44 | | /// <param name="allocator">The <see cref="Unity.Collections.Allocator" /> type to use.</param> |
| | 45 | | public NativeArraySparseSet(int initialCapacity, Allocator allocator) |
| 14 | 46 | | { |
| 14 | 47 | | DenseArray = new NativeArray<int>(initialCapacity, allocator, NativeArrayOptions.UninitializedMemory); |
| 14 | 48 | | SparseArray = new NativeArray<int>(initialCapacity, allocator, NativeArrayOptions.UninitializedMemory); |
| 14 | 49 | | Count = 0; |
| 14 | 50 | | FreeIndex = 0; |
| | 51 | |
|
| 100 | 52 | | for (int i = 0; i < initialCapacity; i++) |
| 36 | 53 | | { |
| 36 | 54 | | DenseArray[i] = -1; |
| 36 | 55 | | SparseArray[i] = i + 1; |
| 36 | 56 | | } |
| 14 | 57 | | } |
| | 58 | |
|
| | 59 | | /// <summary> |
| | 60 | | /// Create a <see cref="NativeArraySparseSet" /> with an <paramref name="initialCapacity" />. |
| | 61 | | /// </summary> |
| | 62 | | /// <param name="initialCapacity">The initial capacity of the sparse and dense int arrays.</param> |
| | 63 | | /// <param name="allocator">The <see cref="Unity.Collections.Allocator" /> type to use.</param> |
| | 64 | | /// <param name="versionArray">Enables detection of use-after-free errors when using sparse indices as reference |
| | 65 | | public NativeArraySparseSet(int initialCapacity, Allocator allocator, out NativeArray<ulong> versionArray) |
| 17 | 66 | | { |
| 17 | 67 | | DenseArray = new NativeArray<int>(initialCapacity, allocator, NativeArrayOptions.UninitializedMemory); |
| 17 | 68 | | SparseArray = new NativeArray<int>(initialCapacity, allocator, NativeArrayOptions.UninitializedMemory); |
| 17 | 69 | | versionArray = new NativeArray<ulong>(initialCapacity, allocator, NativeArrayOptions.UninitializedMemory); |
| 17 | 70 | | Count = 0; |
| 17 | 71 | | FreeIndex = 0; |
| | 72 | |
|
| 124 | 73 | | for (int i = 0; i < initialCapacity; i++) |
| 45 | 74 | | { |
| 45 | 75 | | DenseArray[i] = -1; |
| 45 | 76 | | SparseArray[i] = i + 1; |
| 45 | 77 | | versionArray[i] = 1; |
| 45 | 78 | | } |
| 17 | 79 | | } |
| | 80 | |
|
| | 81 | | /// <summary> |
| | 82 | | /// Adds a sparse/dense index pair to the set and expands the arrays if necessary. |
| | 83 | | /// </summary> |
| | 84 | | /// <param name="expandBy">How many indices to expand by.</param> |
| | 85 | | /// <param name="sparseIndex">The sparse index allocated.</param> |
| | 86 | | /// <param name="denseIndex">The dense index allocated.</param> |
| | 87 | | /// <param name="allocator">The <see cref="Unity.Collections.Allocator" /> type to use.</param> |
| | 88 | | /// <returns>True if the index pool expanded.</returns> |
| | 89 | | public bool AddWithExpandCheck(int expandBy, out int sparseIndex, out int denseIndex, Allocator allocator) |
| 4 | 90 | | { |
| 4 | 91 | | int indexToClaim = FreeIndex; |
| 4 | 92 | | int currentCapacity = SparseArray.Length; |
| 4 | 93 | | bool needsExpansion = false; |
| | 94 | |
|
| 4 | 95 | | if (indexToClaim >= currentCapacity) |
| 2 | 96 | | { |
| | 97 | | // We're out of space, the last free index points to nothing. Allocate more indices. |
| 2 | 98 | | needsExpansion = true; |
| | 99 | |
|
| 2 | 100 | | int newCapacity = currentCapacity + expandBy; |
| | 101 | |
|
| 2 | 102 | | NativeArray<int> newSparseArray = |
| | 103 | | new NativeArray<int>(newCapacity, allocator, NativeArrayOptions.UninitializedMemory); |
| 2 | 104 | | NativeSlice<int> newSparseArraySlice = new NativeSlice<int>(newSparseArray, 0, currentCapacity); |
| 2 | 105 | | newSparseArraySlice.CopyFrom(SparseArray); |
| 2 | 106 | | SparseArray.Dispose(); |
| 2 | 107 | | SparseArray = newSparseArray; |
| | 108 | |
|
| 2 | 109 | | NativeArray<int> newDenseArray = |
| | 110 | | new NativeArray<int>(newCapacity, allocator, NativeArrayOptions.UninitializedMemory); |
| 2 | 111 | | NativeSlice<int> newDenseArraySlice = new NativeSlice<int>(newDenseArray, 0, currentCapacity); |
| 2 | 112 | | newDenseArraySlice.CopyFrom(DenseArray); |
| 2 | 113 | | DenseArray.Dispose(); |
| 2 | 114 | | DenseArray = newDenseArray; |
| | 115 | |
|
| 24 | 116 | | for (int i = currentCapacity; i < newCapacity; i++) |
| 10 | 117 | | { |
| 10 | 118 | | SparseArray[i] = i + 1; // Build the free list chain. |
| 10 | 119 | | DenseArray[i] = -1; // Set new dense indices as unclaimed. |
| 10 | 120 | | } |
| 2 | 121 | | } |
| | 122 | |
|
| 4 | 123 | | int nextFreeIndex = SparseArray[indexToClaim]; |
| 4 | 124 | | DenseArray[Count] = indexToClaim; // Point the next dense id at our newly claimed sparse index. |
| 4 | 125 | | SparseArray[indexToClaim] = Count; // Point our newly claimed sparse index at the dense index. |
| 4 | 126 | | denseIndex = Count; |
| | 127 | |
|
| 4 | 128 | | ++Count; |
| 4 | 129 | | FreeIndex = nextFreeIndex; // Set the free list head for next time. |
| | 130 | |
|
| 4 | 131 | | sparseIndex = indexToClaim; |
| 4 | 132 | | return needsExpansion; |
| 4 | 133 | | } |
| | 134 | |
|
| | 135 | | /// <summary> |
| | 136 | | /// Adds a sparse/dense index pair to the set and expands the arrays if necessary. |
| | 137 | | /// </summary> |
| | 138 | | /// <param name="expandBy">How many indices to expand by.</param> |
| | 139 | | /// <param name="sparseIndex">The sparse index allocated.</param> |
| | 140 | | /// <param name="denseIndex">The dense index allocated.</param> |
| | 141 | | /// <param name="allocator">The <see cref="Unity.Collections.Allocator" /> type to use.</param> |
| | 142 | | /// <param name="versionArray">Enables detection of use-after-free errors when using sparse indices as reference |
| | 143 | | /// <returns>True if the index pool expanded.</returns> |
| | 144 | | public bool AddWithExpandCheck(int expandBy, out int sparseIndex, out int denseIndex, Allocator allocator, |
| | 145 | | ref NativeArray<ulong> versionArray) |
| 2 | 146 | | { |
| 2 | 147 | | int indexToClaim = FreeIndex; |
| 2 | 148 | | int currentCapacity = SparseArray.Length; |
| 2 | 149 | | bool needsExpansion = false; |
| | 150 | |
|
| 2 | 151 | | if (indexToClaim >= currentCapacity) |
| 1 | 152 | | { |
| | 153 | | // We're out of space, the last free index points to nothing. Allocate more indices. |
| 1 | 154 | | needsExpansion = true; |
| | 155 | |
|
| 1 | 156 | | int newCapacity = currentCapacity + expandBy; |
| | 157 | |
|
| 1 | 158 | | NativeArray<int> newSparseArray = |
| | 159 | | new NativeArray<int>(newCapacity, allocator, NativeArrayOptions.UninitializedMemory); |
| 1 | 160 | | NativeSlice<int> newSparseArraySlice = new NativeSlice<int>(newSparseArray, 0, currentCapacity); |
| 1 | 161 | | newSparseArraySlice.CopyFrom(SparseArray); |
| 1 | 162 | | SparseArray.Dispose(); |
| 1 | 163 | | SparseArray = newSparseArray; |
| | 164 | |
|
| 1 | 165 | | NativeArray<int> newDenseArray = |
| | 166 | | new NativeArray<int>(newCapacity, allocator, NativeArrayOptions.UninitializedMemory); |
| 1 | 167 | | NativeSlice<int> newDenseArraySlice = new NativeSlice<int>(newDenseArray, 0, currentCapacity); |
| 1 | 168 | | newDenseArraySlice.CopyFrom(DenseArray); |
| 1 | 169 | | DenseArray.Dispose(); |
| 1 | 170 | | DenseArray = newDenseArray; |
| | 171 | |
|
| 1 | 172 | | NativeArray<ulong> newVersionArray = |
| | 173 | | new NativeArray<ulong>(newCapacity, allocator, NativeArrayOptions.UninitializedMemory); |
| 1 | 174 | | NativeSlice<ulong> newVersionArraySlice = new NativeSlice<ulong>(newVersionArray, 0, currentCapacity); |
| 1 | 175 | | newVersionArraySlice.CopyFrom(versionArray); |
| 1 | 176 | | versionArray.Dispose(); |
| 1 | 177 | | versionArray = newVersionArray; |
| | 178 | |
|
| 12 | 179 | | for (int i = currentCapacity; i < newCapacity; i++) |
| 5 | 180 | | { |
| 5 | 181 | | SparseArray[i] = i + 1; // Build the free list chain. |
| 5 | 182 | | DenseArray[i] = -1; // Set new dense indices as unclaimed. |
| 5 | 183 | | versionArray[i] = 1; |
| 5 | 184 | | } |
| 1 | 185 | | } |
| | 186 | |
|
| 2 | 187 | | int nextFreeIndex = SparseArray[indexToClaim]; |
| 2 | 188 | | DenseArray[Count] = indexToClaim; // Point the next dense id at our newly claimed sparse index. |
| 2 | 189 | | SparseArray[indexToClaim] = Count; // Point our newly claimed sparse index at the dense index. |
| 2 | 190 | | denseIndex = Count; |
| | 191 | |
|
| 2 | 192 | | ++Count; |
| 2 | 193 | | FreeIndex = nextFreeIndex; // Set the free list head for next time. |
| | 194 | |
|
| 2 | 195 | | sparseIndex = indexToClaim; |
| 2 | 196 | | return needsExpansion; |
| 2 | 197 | | } |
| | 198 | |
|
| | 199 | | /// <summary> |
| | 200 | | /// Adds a sparse/dense index pair to the set without checking if the set needs to expand. |
| | 201 | | /// </summary> |
| | 202 | | /// <param name="sparseIndex">The sparse index allocated.</param> |
| | 203 | | /// <param name="denseIndex">The dense index allocated.</param> |
| | 204 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 205 | | public void AddUnchecked(out int sparseIndex, out int denseIndex) |
| 20 | 206 | | { |
| 20 | 207 | | int indexToClaim = FreeIndex; |
| 20 | 208 | | int nextFreeIndex = SparseArray[indexToClaim]; |
| 19 | 209 | | DenseArray[Count] = indexToClaim; // Point the next dense id at our newly claimed sparse index. |
| 19 | 210 | | SparseArray[indexToClaim] = Count; // Point our newly claimed sparse index at the dense index. |
| | 211 | |
|
| 19 | 212 | | sparseIndex = indexToClaim; |
| 19 | 213 | | denseIndex = Count; |
| 19 | 214 | | ++Count; |
| 19 | 215 | | FreeIndex = nextFreeIndex; // Set the free list head for next time. |
| 19 | 216 | | } |
| | 217 | |
|
| | 218 | | /// <summary> |
| | 219 | | /// Adds a sparse/dense index pair to the set without checking if the set needs to expand. |
| | 220 | | /// </summary> |
| | 221 | | /// <param name="sparseIndex">The sparse index allocated.</param> |
| | 222 | | /// <param name="denseIndex">The dense index allocated.</param> |
| | 223 | | /// <param name="versionArray">The array containing the version number to check against.</param> |
| | 224 | | /// <param name="version">Enables detection of use-after-free errors when using the sparse index as a reference. |
| | 225 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 226 | | public void AddUnchecked(out int sparseIndex, out int denseIndex, NativeArray<ulong> versionArray, |
| | 227 | | out ulong version) |
| 24 | 228 | | { |
| 24 | 229 | | int indexToClaim = FreeIndex; |
| 24 | 230 | | int nextFreeIndex = SparseArray[indexToClaim]; |
| 23 | 231 | | DenseArray[Count] = indexToClaim; // Point the next dense id at our newly claimed sparse index. |
| 23 | 232 | | SparseArray[indexToClaim] = Count; // Point our newly claimed sparse index at the dense index. |
| | 233 | |
|
| 23 | 234 | | version = versionArray[indexToClaim]; |
| 23 | 235 | | sparseIndex = indexToClaim; |
| 23 | 236 | | denseIndex = Count; |
| | 237 | |
|
| 23 | 238 | | ++Count; |
| 23 | 239 | | FreeIndex = nextFreeIndex; // Set the free list head for next time. |
| 23 | 240 | | } |
| | 241 | |
|
| | 242 | | /// <summary> |
| | 243 | | /// Gets the value of the sparse array at the given index without any data validation. |
| | 244 | | /// </summary> |
| | 245 | | /// <param name="sparseIndex">The index to check in the sparse array.</param> |
| | 246 | | /// <returns>The dense index at the given sparse index.</returns> |
| | 247 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 248 | | public int GetDenseIndexUnchecked(int sparseIndex) |
| 1 | 249 | | { |
| 1 | 250 | | return SparseArray[sparseIndex]; |
| 1 | 251 | | } |
| | 252 | |
|
| | 253 | | /// <summary> |
| | 254 | | /// Gets the value of the sparse array at the given index, |
| | 255 | | /// or -1 if the dense and sparse indices don't point to each other or if the dense index is outside the den |
| | 256 | | /// </summary> |
| | 257 | | /// <param name="sparseIndex">The index in the sparse array to check against.</param> |
| | 258 | | /// <returns>The dense index pointed to by the current sparse index, or -1 if invalid.</returns> |
| | 259 | | public int GetDenseIndexWithBoundsCheck(int sparseIndex) |
| 4 | 260 | | { |
| 4 | 261 | | if (sparseIndex >= 0 && sparseIndex < SparseArray.Length) |
| 2 | 262 | | { |
| 2 | 263 | | int denseIndex = SparseArray[sparseIndex]; |
| | 264 | |
|
| 2 | 265 | | if (denseIndex < Count && denseIndex >= 0) |
| 1 | 266 | | { |
| 1 | 267 | | int sparseIndexAtDenseIndex = DenseArray[denseIndex]; |
| | 268 | |
|
| 1 | 269 | | if (sparseIndex == sparseIndexAtDenseIndex) |
| 1 | 270 | | { |
| 1 | 271 | | return denseIndex; |
| | 272 | | } |
| 0 | 273 | | } |
| 1 | 274 | | } |
| | 275 | |
|
| 3 | 276 | | return -1; |
| 4 | 277 | | } |
| | 278 | |
|
| | 279 | | /// <summary> |
| | 280 | | /// Gets the value of the sparse array at the given index, |
| | 281 | | /// or -1 if the version number does not match. |
| | 282 | | /// </summary> |
| | 283 | | /// <param name="sparseIndex">The index in the sparse array to check against.</param> |
| | 284 | | /// <param name="version">The version number associated with the sparse index.</param> |
| | 285 | | /// <param name="versionArray">The array containing the version number to check against.</param> |
| | 286 | | /// <returns>The dense index pointed to by the current sparse index, or -1 if invalid.</returns> |
| | 287 | | public int GetDenseIndexWithVersionCheck(int sparseIndex, ulong version, NativeArray<ulong> versionArray) |
| 4 | 288 | | { |
| 4 | 289 | | int denseIndex = SparseArray[sparseIndex]; |
| 4 | 290 | | ulong versionAtSparseIndex = versionArray[sparseIndex]; |
| | 291 | |
|
| 4 | 292 | | if (version == versionAtSparseIndex) |
| 2 | 293 | | { |
| 2 | 294 | | return denseIndex; |
| | 295 | | } |
| | 296 | |
|
| 2 | 297 | | return -1; |
| 4 | 298 | | } |
| | 299 | |
|
| | 300 | | /// <summary> |
| | 301 | | /// Gets the value of the sparse array at the given index, |
| | 302 | | /// or -1 if the given sparse index is invalid.. |
| | 303 | | /// </summary> |
| | 304 | | /// <param name="sparseIndex">The index in the sparse array to check against.</param> |
| | 305 | | /// <param name="version">The version number associated with the sparse index.</param> |
| | 306 | | /// <param name="versionArray">The array containing the version number to check against.</param> |
| | 307 | | /// <returns>The dense index pointed to by the current sparse index, or -1 if invalid.</returns> |
| | 308 | | public int GetDenseIndexWithBoundsAndVersionCheck(int sparseIndex, ulong version, |
| | 309 | | NativeArray<ulong> versionArray) |
| 7 | 310 | | { |
| 7 | 311 | | if (sparseIndex >= 0 && sparseIndex < SparseArray.Length) |
| 5 | 312 | | { |
| 5 | 313 | | int denseIndex = SparseArray[sparseIndex]; |
| 5 | 314 | | ulong versionAtSparseIndex = versionArray[sparseIndex]; |
| | 315 | |
|
| 5 | 316 | | if (versionAtSparseIndex == version && denseIndex < Count && denseIndex >= 0) |
| 2 | 317 | | { |
| 2 | 318 | | int sparseIndexAtDenseIndex = DenseArray[denseIndex]; |
| | 319 | |
|
| 2 | 320 | | if (sparseIndex == sparseIndexAtDenseIndex) |
| 2 | 321 | | { |
| 2 | 322 | | return denseIndex; |
| | 323 | | } |
| 0 | 324 | | } |
| 3 | 325 | | } |
| | 326 | |
|
| 5 | 327 | | return -1; |
| 7 | 328 | | } |
| | 329 | |
|
| | 330 | | /// <summary> |
| | 331 | | /// Removes the entry corresponding to the sparse index if the entry is within bounds and currently in use. |
| | 332 | | /// </summary> |
| | 333 | | /// <param name="sparseIndexToRemove"> |
| | 334 | | /// The sparse index corresponding to the entry to remove. Cleared to -1 in this |
| | 335 | | /// operation. |
| | 336 | | /// </param> |
| | 337 | | /// <param name="dataIndexToSwapFrom"> |
| | 338 | | /// Set the data array value at this index to default after swapping with the data array |
| | 339 | | /// value at indexToSwapTo. |
| | 340 | | /// </param> |
| | 341 | | /// <param name="dataIndexToSwapTo"> |
| | 342 | | /// Replace the data array value at this index with the data array value at |
| | 343 | | /// indexToSwapFrom. |
| | 344 | | /// </param> |
| | 345 | | /// <returns>True if the index reference was valid, and thus removed.</returns> |
| | 346 | | public bool RemoveWithBoundsCheck(ref int sparseIndexToRemove, out int dataIndexToSwapFrom, |
| | 347 | | out int dataIndexToSwapTo) |
| 4 | 348 | | { |
| 4 | 349 | | dataIndexToSwapFrom = -1; |
| 4 | 350 | | dataIndexToSwapTo = -1; |
| 4 | 351 | | bool didRemove = false; |
| 4 | 352 | | if (sparseIndexToRemove >= 0 && sparseIndexToRemove < SparseArray.Length) |
| 2 | 353 | | { |
| 2 | 354 | | int denseIndexToRemove = SparseArray[sparseIndexToRemove]; |
| | 355 | |
|
| 2 | 356 | | if (denseIndexToRemove >= 0 && denseIndexToRemove < Count) |
| 1 | 357 | | { |
| 1 | 358 | | int sparseIndexAtDenseIndex = DenseArray[denseIndexToRemove]; |
| 1 | 359 | | int newLength = Count - 1; |
| 1 | 360 | | int sparseIndexBeingSwapped = DenseArray[newLength]; |
| | 361 | |
|
| 1 | 362 | | if (denseIndexToRemove < Count && sparseIndexAtDenseIndex == sparseIndexToRemove) |
| 1 | 363 | | { |
| 1 | 364 | | didRemove = true; |
| | 365 | | // Swap the entry being removed with the last entry. |
| 1 | 366 | | SparseArray[sparseIndexBeingSwapped] = denseIndexToRemove; |
| 1 | 367 | | DenseArray[denseIndexToRemove] = sparseIndexBeingSwapped; |
| | 368 | |
|
| 1 | 369 | | dataIndexToSwapFrom = newLength; |
| 1 | 370 | | dataIndexToSwapTo = denseIndexToRemove; |
| | 371 | |
|
| | 372 | | // Clear the dense index, for debugging purposes |
| 1 | 373 | | DenseArray[newLength] = -1; |
| | 374 | |
|
| | 375 | | // Add the sparse index to the free list. |
| 1 | 376 | | SparseArray[sparseIndexToRemove] = FreeIndex; |
| 1 | 377 | | FreeIndex = sparseIndexToRemove; |
| | 378 | |
|
| 1 | 379 | | Count = newLength; |
| 1 | 380 | | } |
| 1 | 381 | | } |
| 2 | 382 | | } |
| | 383 | |
|
| 4 | 384 | | sparseIndexToRemove = -1; |
| | 385 | |
|
| 4 | 386 | | return didRemove; |
| 4 | 387 | | } |
| | 388 | |
|
| | 389 | | /// <summary> |
| | 390 | | /// Removes the associated sparse/dense index pair from active use. |
| | 391 | | /// calls. |
| | 392 | | /// </summary> |
| | 393 | | /// <param name="sparseIndexToRemove">The sparse index to remove.</param> |
| | 394 | | /// <param name="version"> |
| | 395 | | /// The version number of the int used to access the sparse index. Used to guard against accessing |
| | 396 | | /// indices that have been removed and reused. |
| | 397 | | /// </param> |
| | 398 | | /// <param name="versionArray">The array containing the version number to check against.</param> |
| | 399 | | /// <param name="indexToSwapFrom"> |
| | 400 | | /// Set the data array value at this index to default after swapping with the data array |
| | 401 | | /// value at indexToSwapTo. |
| | 402 | | /// </param> |
| | 403 | | /// <param name="indexToSwapTo">Replace the data array value at this index with the data array value at indexToS |
| | 404 | | /// <returns>True if the element was successfully removed.</returns> |
| | 405 | | public bool RemoveWithBoundsAndVersionChecks(ref int sparseIndexToRemove, ulong version, |
| | 406 | | NativeArray<ulong> versionArray, out int indexToSwapFrom, out int indexToSwapTo) |
| 6 | 407 | | { |
| 6 | 408 | | indexToSwapFrom = -1; |
| 6 | 409 | | indexToSwapTo = -1; |
| 6 | 410 | | bool didRemove = false; |
| 6 | 411 | | if (sparseIndexToRemove >= 0 && sparseIndexToRemove < SparseArray.Length) |
| 3 | 412 | | { |
| 3 | 413 | | ulong sparseIndexVersion = versionArray[sparseIndexToRemove]; |
| 3 | 414 | | int denseIndexToRemove = SparseArray[sparseIndexToRemove]; |
| | 415 | |
|
| 3 | 416 | | if (sparseIndexVersion == version && denseIndexToRemove >= 0 && denseIndexToRemove < Count) |
| 2 | 417 | | { |
| 2 | 418 | | int sparseIndexAtDenseIndex = DenseArray[denseIndexToRemove]; |
| 2 | 419 | | int newLength = Count - 1; |
| 2 | 420 | | int sparseIndexBeingSwapped = DenseArray[newLength]; |
| | 421 | |
|
| 2 | 422 | | if (denseIndexToRemove < Count && sparseIndexAtDenseIndex == sparseIndexToRemove) |
| 2 | 423 | | { |
| 2 | 424 | | didRemove = true; |
| 2 | 425 | | versionArray[sparseIndexToRemove] = sparseIndexVersion + 1; |
| | 426 | | // Swap the entry being removed with the last entry. |
| 2 | 427 | | SparseArray[sparseIndexBeingSwapped] = denseIndexToRemove; |
| 2 | 428 | | DenseArray[denseIndexToRemove] = sparseIndexBeingSwapped; |
| | 429 | |
|
| 2 | 430 | | indexToSwapFrom = newLength; |
| 2 | 431 | | indexToSwapTo = denseIndexToRemove; |
| | 432 | |
|
| | 433 | | // Clear the dense index, for debugging purposes |
| 2 | 434 | | DenseArray[newLength] = -1; |
| | 435 | |
|
| | 436 | | // Add the sparse index to the free list. |
| 2 | 437 | | SparseArray[sparseIndexToRemove] = FreeIndex; |
| 2 | 438 | | FreeIndex = sparseIndexToRemove; |
| | 439 | |
|
| 2 | 440 | | Count = newLength; |
| 2 | 441 | | } |
| 2 | 442 | | } |
| 3 | 443 | | } |
| | 444 | |
|
| 6 | 445 | | sparseIndexToRemove = -1; |
| | 446 | |
|
| 6 | 447 | | return didRemove; |
| 6 | 448 | | } |
| | 449 | |
|
| | 450 | | /// <summary> |
| | 451 | | /// Removes the associated sparse/dense index pair from active use. |
| | 452 | | /// </summary> |
| | 453 | | /// <param name="sparseIndexToRemove">The sparse index to remove.</param> |
| | 454 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 455 | | public void RemoveUnchecked(int sparseIndexToRemove) |
| 1 | 456 | | { |
| 1 | 457 | | int denseIndexToRemove = SparseArray[sparseIndexToRemove]; |
| 1 | 458 | | int newLength = Count - 1; |
| 1 | 459 | | int sparseIndexBeingSwapped = DenseArray[newLength]; |
| | 460 | |
|
| | 461 | | // Swap the entry being removed with the last entry. |
| 1 | 462 | | SparseArray[sparseIndexBeingSwapped] = denseIndexToRemove; |
| 1 | 463 | | DenseArray[denseIndexToRemove] = sparseIndexBeingSwapped; |
| | 464 | |
|
| | 465 | | // Clear the dense index, for debugging purposes |
| 1 | 466 | | DenseArray[newLength] = -1; |
| | 467 | |
|
| | 468 | | // Add the sparse index to the free list. |
| 1 | 469 | | SparseArray[sparseIndexToRemove] = FreeIndex; |
| 1 | 470 | | FreeIndex = sparseIndexToRemove; |
| | 471 | |
|
| 1 | 472 | | Count = newLength; |
| 1 | 473 | | } |
| | 474 | |
|
| | 475 | | /// <summary> |
| | 476 | | /// Removes the associated sparse/dense index pair from active use and increments the version. |
| | 477 | | /// </summary> |
| | 478 | | /// <param name="sparseIndexToRemove">The sparse index to remove.</param> |
| | 479 | | /// <param name="versionArray">Enables detection of use-after-free errors when using sparse indices as reference |
| | 480 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 481 | | public void RemoveUnchecked(int sparseIndexToRemove, NativeArray<ulong> versionArray) |
| 1 | 482 | | { |
| 1 | 483 | | int denseIndexToRemove = SparseArray[sparseIndexToRemove]; |
| 1 | 484 | | int newLength = Count - 1; |
| 1 | 485 | | int sparseIndexBeingSwapped = DenseArray[newLength]; |
| | 486 | |
|
| | 487 | | // Swap the entry being removed with the last entry. |
| 1 | 488 | | SparseArray[sparseIndexBeingSwapped] = denseIndexToRemove; |
| 1 | 489 | | DenseArray[denseIndexToRemove] = sparseIndexBeingSwapped; |
| | 490 | |
|
| | 491 | | // Clear the dense index, for debugging purposes |
| 1 | 492 | | DenseArray[newLength] = -1; |
| | 493 | |
|
| | 494 | | // Add the sparse index to the free list. |
| 1 | 495 | | SparseArray[sparseIndexToRemove] = FreeIndex; |
| 1 | 496 | | versionArray[sparseIndexToRemove] += 1; |
| 1 | 497 | | FreeIndex = sparseIndexToRemove; |
| | 498 | |
|
| 1 | 499 | | Count = newLength; |
| 1 | 500 | | } |
| | 501 | |
|
| | 502 | | /// <summary> |
| | 503 | | /// Removes the associated sparse/dense index pair from active use. |
| | 504 | | /// Out parameters used to manage parallel data arrays. |
| | 505 | | /// </summary> |
| | 506 | | /// <param name="sparseIndexToRemove">The sparse index to remove.</param> |
| | 507 | | /// <param name="indexToSwapTo">Replace the data array value at this index with the data array value at indexToS |
| | 508 | | /// <param name="indexToSwapFrom"> |
| | 509 | | /// Set the data array value at this index to default after swapping with the data array |
| | 510 | | /// value at indexToSwapTo. |
| | 511 | | /// </param> |
| | 512 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 513 | | public void RemoveUnchecked(int sparseIndexToRemove, out int indexToSwapFrom, out int indexToSwapTo) |
| 1 | 514 | | { |
| 1 | 515 | | int denseIndexToRemove = SparseArray[sparseIndexToRemove]; |
| 1 | 516 | | int newLength = Count - 1; |
| 1 | 517 | | int sparseIndexBeingSwapped = DenseArray[newLength]; |
| | 518 | |
|
| | 519 | | // Swap the entry being removed with the last entry. |
| 1 | 520 | | SparseArray[sparseIndexBeingSwapped] = denseIndexToRemove; |
| 1 | 521 | | DenseArray[denseIndexToRemove] = sparseIndexBeingSwapped; |
| | 522 | |
|
| | 523 | | // Clear the dense index, for debugging purposes |
| 1 | 524 | | DenseArray[newLength] = -1; |
| | 525 | |
|
| | 526 | | // Add the sparse index to the free list. |
| 1 | 527 | | SparseArray[sparseIndexToRemove] = FreeIndex; |
| 1 | 528 | | FreeIndex = sparseIndexToRemove; |
| | 529 | |
|
| 1 | 530 | | Count = newLength; |
| | 531 | |
|
| 1 | 532 | | indexToSwapTo = denseIndexToRemove; |
| 1 | 533 | | indexToSwapFrom = newLength; |
| 1 | 534 | | } |
| | 535 | |
|
| | 536 | | /// <summary> |
| | 537 | | /// Removes the associated sparse/dense index pair from active use and increments the version. |
| | 538 | | /// Out parameters used to manage parallel data arrays. |
| | 539 | | /// </summary> |
| | 540 | | /// <param name="sparseIndexToRemove">The sparse index to remove.</param> |
| | 541 | | /// <param name="versionArray">Enables detection of use-after-free errors when using sparse indices as reference |
| | 542 | | /// <param name="indexToSwapTo">Replace the data array value at this index with the data array value at indexToS |
| | 543 | | /// <param name="indexToSwapFrom"> |
| | 544 | | /// Set the data array value at this index to default after swapping with the data array |
| | 545 | | /// value at indexToSwapTo. |
| | 546 | | /// </param> |
| | 547 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 548 | | public void RemoveUnchecked(int sparseIndexToRemove, NativeArray<ulong> versionArray, out int indexToSwapFrom, |
| | 549 | | out int indexToSwapTo) |
| 2 | 550 | | { |
| 2 | 551 | | int denseIndexToRemove = SparseArray[sparseIndexToRemove]; |
| 2 | 552 | | int newLength = Count - 1; |
| 2 | 553 | | int sparseIndexBeingSwapped = DenseArray[newLength]; |
| | 554 | |
|
| | 555 | | // Swap the entry being removed with the last entry. |
| 2 | 556 | | SparseArray[sparseIndexBeingSwapped] = denseIndexToRemove; |
| 2 | 557 | | DenseArray[denseIndexToRemove] = sparseIndexBeingSwapped; |
| | 558 | |
|
| | 559 | | // Clear the dense index, for debugging purposes |
| 2 | 560 | | DenseArray[newLength] = -1; |
| | 561 | |
|
| | 562 | | // Add the sparse index to the free list. |
| 2 | 563 | | SparseArray[sparseIndexToRemove] = FreeIndex; |
| 2 | 564 | | versionArray[sparseIndexToRemove] += 1; |
| 2 | 565 | | FreeIndex = sparseIndexToRemove; |
| | 566 | |
|
| 2 | 567 | | Count = newLength; |
| | 568 | |
|
| 2 | 569 | | indexToSwapTo = denseIndexToRemove; |
| 2 | 570 | | indexToSwapFrom = newLength; |
| 2 | 571 | | } |
| | 572 | |
|
| | 573 | | /// <summary> |
| | 574 | | /// Removes the associated sparse/dense index pair from active use. |
| | 575 | | /// </summary> |
| | 576 | | /// <param name="denseIndexToRemove">The dense index associated with the sparse index to remove.</param> |
| | 577 | | public void RemoveUncheckedFromDenseIndex(int denseIndexToRemove) |
| 1 | 578 | | { |
| 1 | 579 | | int sparseIndexToRemove = DenseArray[denseIndexToRemove]; |
| 1 | 580 | | int newLength = Count - 1; |
| 1 | 581 | | int sparseIndexBeingSwapped = DenseArray[newLength]; |
| | 582 | |
|
| | 583 | | // Swap the entry being removed with the last entry. |
| 1 | 584 | | SparseArray[sparseIndexBeingSwapped] = denseIndexToRemove; |
| 1 | 585 | | DenseArray[denseIndexToRemove] = sparseIndexBeingSwapped; |
| | 586 | |
|
| | 587 | | // Clear the dense index, for debugging purposes |
| 1 | 588 | | DenseArray[newLength] = -1; |
| | 589 | |
|
| | 590 | | // Add the sparse index to the free list. |
| 1 | 591 | | SparseArray[sparseIndexToRemove] = FreeIndex; |
| 1 | 592 | | FreeIndex = sparseIndexToRemove; |
| | 593 | |
|
| 1 | 594 | | Count = newLength; |
| 1 | 595 | | } |
| | 596 | |
|
| | 597 | | /// <summary> |
| | 598 | | /// Removes the associated sparse/dense index pair from active use. |
| | 599 | | /// </summary> |
| | 600 | | /// <param name="denseIndexToRemove">The dense index associated with the sparse index to remove.</param> |
| | 601 | | /// <param name="versionArray">Enables detection of use-after-free errors when using sparse indices as reference |
| | 602 | | public void RemoveUncheckedFromDenseIndex(int denseIndexToRemove, NativeArray<ulong> versionArray) |
| 1 | 603 | | { |
| 1 | 604 | | int sparseIndexToRemove = DenseArray[denseIndexToRemove]; |
| 1 | 605 | | int newLength = Count - 1; |
| 1 | 606 | | int sparseIndexBeingSwapped = DenseArray[newLength]; |
| | 607 | |
|
| | 608 | | // Swap the entry being removed with the last entry. |
| 1 | 609 | | SparseArray[sparseIndexBeingSwapped] = denseIndexToRemove; |
| 1 | 610 | | DenseArray[denseIndexToRemove] = sparseIndexBeingSwapped; |
| | 611 | |
|
| | 612 | | // Clear the dense index, for debugging purposes |
| 1 | 613 | | DenseArray[newLength] = -1; |
| | 614 | |
|
| | 615 | | // Add the sparse index to the free list. |
| 1 | 616 | | SparseArray[sparseIndexToRemove] = FreeIndex; |
| 1 | 617 | | versionArray[sparseIndexToRemove] += 1; |
| 1 | 618 | | FreeIndex = sparseIndexToRemove; |
| | 619 | |
|
| 1 | 620 | | Count = newLength; |
| 1 | 621 | | } |
| | 622 | |
|
| | 623 | | /// <summary> |
| | 624 | | /// Removes the associated sparse/dense index pair from active use. |
| | 625 | | /// Out parameter used to manage parallel data arrays. |
| | 626 | | /// </summary> |
| | 627 | | /// <param name="denseIndexToRemove">The sparse index to remove.</param> |
| | 628 | | /// <param name="indexToSwapFrom"> |
| | 629 | | /// Set the data array value at this index to default after swapping with the data array |
| | 630 | | /// value at denseIndexToRemove. |
| | 631 | | /// </param> |
| | 632 | | public void RemoveUncheckedFromDenseIndex(int denseIndexToRemove, out int indexToSwapFrom) |
| 1 | 633 | | { |
| 1 | 634 | | int sparseIndexToRemove = DenseArray[denseIndexToRemove]; |
| 1 | 635 | | int newLength = Count - 1; |
| 1 | 636 | | int sparseIndexBeingSwapped = DenseArray[newLength]; |
| | 637 | |
|
| | 638 | | // Swap the entry being removed with the last entry. |
| 1 | 639 | | SparseArray[sparseIndexBeingSwapped] = denseIndexToRemove; |
| 1 | 640 | | DenseArray[denseIndexToRemove] = sparseIndexBeingSwapped; |
| | 641 | |
|
| | 642 | | // Clear the dense index, for debugging purposes |
| 1 | 643 | | DenseArray[newLength] = -1; |
| | 644 | |
|
| | 645 | | // Add the sparse index to the free list. |
| 1 | 646 | | SparseArray[sparseIndexToRemove] = FreeIndex; |
| 1 | 647 | | FreeIndex = sparseIndexToRemove; |
| | 648 | |
|
| 1 | 649 | | Count = newLength; |
| | 650 | |
|
| 1 | 651 | | indexToSwapFrom = newLength; |
| 1 | 652 | | } |
| | 653 | |
|
| | 654 | | /// <summary> |
| | 655 | | /// Removes the associated sparse/dense index pair from active use. |
| | 656 | | /// Out parameter used to manage parallel data arrays. |
| | 657 | | /// </summary> |
| | 658 | | /// <param name="denseIndexToRemove">The sparse index to remove.</param> |
| | 659 | | /// <param name="versionArray">Enables detection of use-after-free errors when using sparse indices as reference |
| | 660 | | /// <param name="indexToSwapFrom"> |
| | 661 | | /// Set the data array value at this index to default after swapping with the data array |
| | 662 | | /// value at denseIndexToRemove. |
| | 663 | | /// </param> |
| | 664 | | public void RemoveUncheckedFromDenseIndex(int denseIndexToRemove, NativeArray<ulong> versionArray, |
| | 665 | | out int indexToSwapFrom) |
| 1 | 666 | | { |
| 1 | 667 | | int sparseIndexToRemove = DenseArray[denseIndexToRemove]; |
| 1 | 668 | | int newLength = Count - 1; |
| 1 | 669 | | int sparseIndexBeingSwapped = DenseArray[newLength]; |
| | 670 | |
|
| | 671 | | // Swap the entry being removed with the last entry. |
| 1 | 672 | | SparseArray[sparseIndexBeingSwapped] = denseIndexToRemove; |
| 1 | 673 | | DenseArray[denseIndexToRemove] = sparseIndexBeingSwapped; |
| | 674 | |
|
| | 675 | | // Clear the dense index, for debugging purposes |
|