| | | 1 | | //======================================================================= |
| | | 2 | | // SwiftBucket.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024–present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using Chronicler; |
| | | 9 | | using MemoryPack; |
| | | 10 | | using SwiftCollections.Diagnostics; |
| | | 11 | | using SwiftCollections.Utility; |
| | | 12 | | using System; |
| | | 13 | | using System.Collections; |
| | | 14 | | using System.Collections.Generic; |
| | | 15 | | using System.Runtime.CompilerServices; |
| | | 16 | | using System.Text.Json.Serialization; |
| | | 17 | | |
| | | 18 | | namespace SwiftCollections; |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Represents a high-performance bucket collection that assigns and manages stable integer indices |
| | | 22 | | /// for stored items. Provides O(1) insertion, removal, and lookup by internally generated index. |
| | | 23 | | /// </summary> |
| | | 24 | | /// <remarks> |
| | | 25 | | /// Unlike <see cref="SwiftSparseMap{T}"/>, which requires callers to provide the key used to store values, |
| | | 26 | | /// <see cref="SwiftBucket{T}"/> internally generates and manages indices for each inserted item. |
| | | 27 | | /// |
| | | 28 | | /// These indices remain stable for the lifetime of the item unless it is removed. |
| | | 29 | | /// |
| | | 30 | | /// The container is optimized for scenarios requiring: |
| | | 31 | | /// <list type="bullet"> |
| | | 32 | | /// <item> |
| | | 33 | | /// <description>Stable handles or identifiers.</description> |
| | | 34 | | /// </item> |
| | | 35 | | /// <item> |
| | | 36 | | /// <description>Fast addition and removal.</description> |
| | | 37 | | /// </item> |
| | | 38 | | /// <item> |
| | | 39 | | /// <description>Dense storage and iteration performance.</description> |
| | | 40 | | /// </item> |
| | | 41 | | /// </list> |
| | | 42 | | /// |
| | | 43 | | /// **Efficient Lookups Using Indices**: |
| | | 44 | | /// When you add items to the bucket using the <see cref="Add"/> method, it returns an arrayIndex that you can store ext |
| | | 45 | | /// You can then use this arrayIndex to access the item directly via the indexer, and check if it's still present using |
| | | 46 | | /// This approach allows for O(1) time complexity for lookups and existence checks, avoiding the need for O(n) searches |
| | | 47 | | /// |
| | | 48 | | /// **Note**: iteration over the collection does not follow any guaranteed order and depends on internal allocation. |
| | | 49 | | /// </remarks> |
| | | 50 | | /// <typeparam name="T">Specifies the type of elements in the bucket.</typeparam> |
| | | 51 | | [Serializable] |
| | | 52 | | [JsonConverter(typeof(StateJsonConverterFactory))] |
| | | 53 | | [MemoryPackable] |
| | | 54 | | public sealed partial class SwiftBucket<T> : IStateBacked<SwiftBucketState<T>>, ISwiftCloneable<T>, IEnumerable<T>, ICol |
| | | 55 | | { |
| | | 56 | | #region Constants |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Represents the default initial capacity used when no specific capacity is provided. |
| | | 60 | | /// </summary> |
| | | 61 | | public const int DefaultCapacity = 8; |
| | | 62 | | |
| | | 63 | | #endregion |
| | | 64 | | |
| | | 65 | | #region Fields |
| | | 66 | | |
| | | 67 | | private Entry[] _innerArray; |
| | | 68 | | |
| | | 69 | | private int _count; |
| | | 70 | | |
| | | 71 | | private int _peakCount; |
| | | 72 | | |
| | | 73 | | private SwiftIntStack _freeIndices; |
| | | 74 | | |
| | | 75 | | [NonSerialized] |
| | | 76 | | private uint _version; |
| | | 77 | | |
| | | 78 | | [NonSerialized] |
| | | 79 | | private object? _syncRoot; |
| | | 80 | | |
| | | 81 | | #endregion |
| | | 82 | | |
| | | 83 | | #region Nested Types |
| | | 84 | | |
| | | 85 | | [Serializable] |
| | | 86 | | private struct Entry |
| | | 87 | | { |
| | | 88 | | public T Value; |
| | | 89 | | public bool IsUsed; |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | #endregion |
| | | 93 | | |
| | | 94 | | #region Constructors |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// Initializes a new instance of the <see cref="SwiftBucket{T}"/> class. |
| | | 98 | | /// </summary> |
| | 70 | 99 | | public SwiftBucket() : this(DefaultCapacity) { } |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// Initializes a new instance of the <see cref="SwiftBucket{T}"/> class with the specified capacity. |
| | | 103 | | /// </summary> |
| | | 104 | | /// <param name="capacity">The initial capacity of the bucket.</param> |
| | 44 | 105 | | public SwiftBucket(int capacity) |
| | | 106 | | { |
| | 44 | 107 | | capacity = capacity <= DefaultCapacity ? DefaultCapacity : SwiftHashTools.NextPowerOfTwo(capacity); |
| | 44 | 108 | | _innerArray = new Entry[capacity]; |
| | 44 | 109 | | _freeIndices = new SwiftIntStack(capacity); |
| | 44 | 110 | | } |
| | | 111 | | |
| | | 112 | | /// <summary> |
| | | 113 | | /// Initializes a new instance of the <see cref="SwiftBucket{T}"/> class with the specified <see cref="SwiftArraySt |
| | | 114 | | /// </summary> |
| | | 115 | | /// <param name="state">The state containing the internal array, count, offset, and version for initialization.</pa |
| | | 116 | | [MemoryPackConstructor] |
| | 8 | 117 | | public SwiftBucket(SwiftBucketState<T> state) |
| | | 118 | | { |
| | 8 | 119 | | State = state; |
| | | 120 | | |
| | 7 | 121 | | SwiftThrowHelper.ThrowIfNull(_innerArray, nameof(state.Items)); |
| | 7 | 122 | | SwiftThrowHelper.ThrowIfNull(_freeIndices, nameof(state.FreeIndices)); |
| | 7 | 123 | | } |
| | | 124 | | |
| | | 125 | | #endregion |
| | | 126 | | |
| | | 127 | | #region Properties |
| | | 128 | | |
| | | 129 | | /// <summary> |
| | | 130 | | /// Gets the number of elements contained in the <see cref="SwiftBucket{T}"/>. |
| | | 131 | | /// </summary> |
| | | 132 | | [JsonIgnore] |
| | | 133 | | [MemoryPackIgnore] |
| | 120 | 134 | | public int Count => _count; |
| | | 135 | | |
| | | 136 | | /// <summary> |
| | | 137 | | /// Gets the highest value recorded for the count during the lifetime of the object. |
| | | 138 | | /// </summary> |
| | | 139 | | [JsonIgnore] |
| | | 140 | | [MemoryPackIgnore] |
| | 10 | 141 | | public int PeakCount => _peakCount; |
| | | 142 | | |
| | | 143 | | /// <summary> |
| | | 144 | | /// Gets the total capacity of the <see cref="SwiftBucket{T}"/>. |
| | | 145 | | /// </summary> |
| | | 146 | | [JsonIgnore] |
| | | 147 | | [MemoryPackIgnore] |
| | 16 | 148 | | public int Capacity => _innerArray.Length; |
| | | 149 | | |
| | | 150 | | /// <summary> |
| | | 151 | | /// Gets or sets the element at the specified arrayIndex. |
| | | 152 | | /// Throws <see cref="InvalidOperationException"/> if the arrayIndex is invalid or unallocated. |
| | | 153 | | /// </summary> |
| | | 154 | | /// <param name="index">The zero-based arrayIndex of the element to get or set.</param> |
| | | 155 | | [JsonIgnore] |
| | | 156 | | [MemoryPackIgnore] |
| | | 157 | | public T this[int index] |
| | | 158 | | { |
| | | 159 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 160 | | get |
| | | 161 | | { |
| | 228 | 162 | | SwiftThrowHelper.ThrowIfTrue(!IsAllocated(index), nameof(index), message: "Index is out of range or unalloca |
| | 225 | 163 | | return _innerArray[index].Value; |
| | | 164 | | } |
| | | 165 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 166 | | set |
| | | 167 | | { |
| | 1 | 168 | | SwiftThrowHelper.ThrowIfTrue(!IsAllocated(index), nameof(index), message: "Index is out of range or unalloca |
| | 1 | 169 | | _innerArray[index].Value = value; |
| | 1 | 170 | | _version++; |
| | 1 | 171 | | } |
| | | 172 | | } |
| | | 173 | | |
| | | 174 | | ///<inheritdoc/> |
| | | 175 | | [JsonIgnore] |
| | | 176 | | [MemoryPackIgnore] |
| | 1 | 177 | | public bool IsReadOnly => false; |
| | | 178 | | |
| | | 179 | | ///<inheritdoc/> |
| | | 180 | | [JsonIgnore] |
| | | 181 | | [MemoryPackIgnore] |
| | 1 | 182 | | public bool IsSynchronized => false; |
| | | 183 | | |
| | | 184 | | ///<inheritdoc/> |
| | | 185 | | [JsonIgnore] |
| | | 186 | | [MemoryPackIgnore] |
| | 1 | 187 | | public object SyncRoot => _syncRoot ??= new object(); |
| | | 188 | | |
| | | 189 | | /// <summary> |
| | | 190 | | /// Gets or sets the current state of the bucket, including all items, allocation status, and free indices. |
| | | 191 | | /// </summary> |
| | | 192 | | /// <remarks> |
| | | 193 | | /// Use this property to capture or restore the complete state of the bucket, such as for serialization or checkpoin |
| | | 194 | | /// Setting this property replaces the entire internal state, including items and allocation metadata. |
| | | 195 | | /// </remarks> |
| | | 196 | | [JsonInclude] |
| | | 197 | | [MemoryPackInclude] |
| | | 198 | | public SwiftBucketState<T> State |
| | | 199 | | { |
| | | 200 | | get |
| | | 201 | | { |
| | 2 | 202 | | int length = _innerArray.Length; |
| | | 203 | | |
| | 2 | 204 | | var items = new T[length]; |
| | 2 | 205 | | var allocated = new bool[length]; |
| | | 206 | | |
| | 516 | 207 | | for (int i = 0; i < length; i++) |
| | | 208 | | { |
| | 256 | 209 | | if (_innerArray[i].IsUsed) |
| | | 210 | | { |
| | 200 | 211 | | items[i] = _innerArray[i].Value; |
| | 200 | 212 | | allocated[i] = true; |
| | | 213 | | } |
| | | 214 | | } |
| | | 215 | | |
| | 2 | 216 | | int[] free = new int[_freeIndices.Count]; |
| | 2 | 217 | | Array.Copy(_freeIndices.Array, free, _freeIndices.Count); |
| | | 218 | | |
| | 2 | 219 | | return new SwiftBucketState<T>( |
| | 2 | 220 | | items, |
| | 2 | 221 | | allocated, |
| | 2 | 222 | | free, |
| | 2 | 223 | | _peakCount |
| | 2 | 224 | | ); |
| | | 225 | | } |
| | | 226 | | internal set |
| | | 227 | | { |
| | 8 | 228 | | var items = value.Items ?? Array.Empty<T>(); |
| | 8 | 229 | | var allocated = value.Allocated ?? Array.Empty<bool>(); |
| | 8 | 230 | | var freeIndices = value.FreeIndices ?? Array.Empty<int>(); |
| | | 231 | | |
| | 8 | 232 | | int sourceLength = Math.Max(items.Length, allocated.Length); |
| | 8 | 233 | | int capacity = sourceLength < DefaultCapacity ? DefaultCapacity : SwiftHashTools.NextPowerOfTwo(sourceLength |
| | | 234 | | |
| | 8 | 235 | | _innerArray = new Entry[capacity]; |
| | 8 | 236 | | _freeIndices = new SwiftIntStack(capacity); |
| | | 237 | | |
| | 8 | 238 | | _count = 0; |
| | 8 | 239 | | int maxReferencedIndex = RestoreAllocatedEntries(items, allocated, sourceLength); |
| | 8 | 240 | | maxReferencedIndex = RestoreFreeIndices(freeIndices, capacity, maxReferencedIndex); |
| | 7 | 241 | | _peakCount = NormalizePeakCount(value.PeakCount, maxReferencedIndex, capacity); |
| | 7 | 242 | | _version = 0; |
| | 7 | 243 | | } |
| | | 244 | | } |
| | | 245 | | |
| | | 246 | | private int RestoreAllocatedEntries(T[] items, bool[] allocated, int sourceLength) |
| | | 247 | | { |
| | 8 | 248 | | int maxReferencedIndex = -1; |
| | 544 | 249 | | for (int i = 0; i < sourceLength; i++) |
| | | 250 | | { |
| | 264 | 251 | | if (allocated.Length <= i || !allocated[i]) |
| | | 252 | | continue; |
| | | 253 | | |
| | 206 | 254 | | if (items.Length > i) |
| | 206 | 255 | | _innerArray[i].Value = items[i]; |
| | | 256 | | |
| | 206 | 257 | | _innerArray[i].IsUsed = true; |
| | 206 | 258 | | _count++; |
| | 206 | 259 | | maxReferencedIndex = i; |
| | | 260 | | } |
| | | 261 | | |
| | 8 | 262 | | return maxReferencedIndex; |
| | | 263 | | } |
| | | 264 | | |
| | | 265 | | private int RestoreFreeIndices(int[] freeIndices, int capacity, int maxReferencedIndex) |
| | | 266 | | { |
| | 19 | 267 | | foreach (var index in freeIndices) |
| | | 268 | | { |
| | 2 | 269 | | SwiftThrowHelper.ThrowIfTrue((uint)index >= (uint)capacity, message: "Free index is out of range."); |
| | | 270 | | |
| | 1 | 271 | | _freeIndices.Push(index); |
| | 1 | 272 | | if (index > maxReferencedIndex) |
| | 1 | 273 | | maxReferencedIndex = index; |
| | | 274 | | } |
| | | 275 | | |
| | 7 | 276 | | return maxReferencedIndex; |
| | | 277 | | } |
| | | 278 | | |
| | | 279 | | private static int NormalizePeakCount(int peakCount, int maxReferencedIndex, int capacity) |
| | | 280 | | { |
| | 7 | 281 | | if (peakCount < 0) |
| | 1 | 282 | | peakCount = 0; |
| | | 283 | | |
| | 7 | 284 | | int normalizedPeak = Math.Max(peakCount, maxReferencedIndex + 1); |
| | 7 | 285 | | return normalizedPeak > capacity ? capacity : normalizedPeak; |
| | | 286 | | } |
| | | 287 | | |
| | | 288 | | #endregion |
| | | 289 | | |
| | | 290 | | #region Collection Management |
| | | 291 | | |
| | | 292 | | /// <summary> |
| | | 293 | | /// Adds an item to the bucket and returns its arrayIndex. |
| | | 294 | | /// </summary> |
| | | 295 | | /// <param name="item">The item to add.</param> |
| | | 296 | | /// <returns>The arrayIndex where the item was added.</returns> |
| | | 297 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 298 | | public int Add(T item) |
| | | 299 | | { |
| | | 300 | | int index; |
| | 100543 | 301 | | if ((uint)_freeIndices.Count == 0) |
| | | 302 | | { |
| | 100413 | 303 | | index = _peakCount++; |
| | 100413 | 304 | | if ((uint)index >= (uint)_innerArray.Length) |
| | 24 | 305 | | Resize(_innerArray.Length * 2); |
| | | 306 | | } |
| | 130 | 307 | | else index = _freeIndices.Pop(); |
| | | 308 | | |
| | 100543 | 309 | | _innerArray[index].Value = item; |
| | 100543 | 310 | | _innerArray[index].IsUsed = true; |
| | 100543 | 311 | | _count++; |
| | 100543 | 312 | | _version++; |
| | 100543 | 313 | | return index; |
| | | 314 | | } |
| | | 315 | | |
| | 1 | 316 | | void ICollection<T>.Add(T item) => Add(item); |
| | | 317 | | |
| | | 318 | | /// <summary> |
| | | 319 | | /// Inserts an item at the specified arrayIndex. |
| | | 320 | | /// If an item already exists at that arrayIndex, it will be replaced. |
| | | 321 | | /// </summary> |
| | | 322 | | /// <param name="index">The arrayIndex at which to insert the item.</param> |
| | | 323 | | /// <param name="item">The item to insert.</param> |
| | | 324 | | public void InsertAt(int index, T item) |
| | | 325 | | { |
| | 7 | 326 | | SwiftThrowHelper.ThrowIfNegative(index, nameof(index)); |
| | 7 | 327 | | if ((uint)index >= (uint)_innerArray.Length) |
| | 1 | 328 | | Resize(SwiftHashTools.NextPowerOfTwo(index + 1)); |
| | 7 | 329 | | if (!_innerArray[index].IsUsed) |
| | | 330 | | { |
| | 6 | 331 | | _count++; |
| | 6 | 332 | | if ((uint)index >= (uint)_peakCount) |
| | 6 | 333 | | _peakCount = index + 1; |
| | | 334 | | } |
| | 7 | 335 | | _innerArray[index].Value = item; |
| | 7 | 336 | | _innerArray[index].IsUsed = true; |
| | 7 | 337 | | _version++; |
| | 7 | 338 | | } |
| | | 339 | | |
| | | 340 | | /// <summary> |
| | | 341 | | /// Removes the first occurrence of a specific object from the bucket. |
| | | 342 | | /// </summary> |
| | | 343 | | /// <param name="item">The object to remove.</param> |
| | | 344 | | /// <returns><c>true</c> if item was successfully removed; otherwise, <c>false</c>.</returns> |
| | | 345 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 346 | | public bool TryRemove(T item) |
| | | 347 | | { |
| | 3 | 348 | | int index = IndexOf(item); |
| | 4 | 349 | | if (index < 0) return false; |
| | 2 | 350 | | RemoveAt(index); |
| | 2 | 351 | | return true; |
| | | 352 | | } |
| | | 353 | | |
| | 1 | 354 | | bool ICollection<T>.Remove(T item) => TryRemove(item); |
| | | 355 | | |
| | | 356 | | /// <summary> |
| | | 357 | | /// Removes the item at the specified arrayIndex if it has been allocated. |
| | | 358 | | /// </summary> |
| | | 359 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 360 | | public bool TryRemoveAt(int index) |
| | | 361 | | { |
| | 50260 | 362 | | if (IsAllocated(index)) |
| | | 363 | | { |
| | 50259 | 364 | | RemoveAt(index); |
| | 50259 | 365 | | return true; |
| | | 366 | | } |
| | 1 | 367 | | return false; |
| | | 368 | | } |
| | | 369 | | |
| | | 370 | | /// <summary> |
| | | 371 | | /// Removes the item at the specified arrayIndex. |
| | | 372 | | /// </summary> |
| | | 373 | | /// <param name="index">The arrayIndex of the item to remove.</param> |
| | | 374 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 375 | | public void RemoveAt(int index) |
| | | 376 | | { |
| | 50262 | 377 | | _innerArray[index] = default; |
| | 50262 | 378 | | _count--; |
| | 50262 | 379 | | _freeIndices.Push(index); |
| | 50262 | 380 | | _version++; |
| | 50262 | 381 | | } |
| | | 382 | | |
| | | 383 | | /// <summary> |
| | | 384 | | /// Removes all items from the bucket. |
| | | 385 | | /// </summary> |
| | | 386 | | public void Clear() |
| | | 387 | | { |
| | 3 | 388 | | if ((uint)_count == 0) return; |
| | 8 | 389 | | for (int i = 0; i < _peakCount; i++) |
| | 3 | 390 | | _innerArray[i] = default; |
| | 1 | 391 | | _freeIndices.Reset(); |
| | 1 | 392 | | _count = 0; |
| | 1 | 393 | | _peakCount = 0; |
| | 1 | 394 | | _version++; |
| | 1 | 395 | | } |
| | | 396 | | |
| | | 397 | | #endregion |
| | | 398 | | |
| | | 399 | | #region Capacity Management |
| | | 400 | | |
| | | 401 | | /// <summary> |
| | | 402 | | /// Ensures that the internal storage has at least the specified capacity, expanding it if necessary. |
| | | 403 | | /// </summary> |
| | | 404 | | /// <remarks> |
| | | 405 | | /// If the current capacity is less than the specified value, the internal storage is increased to |
| | | 406 | | /// the next power of two greater than or equal to the requested capacity. |
| | | 407 | | /// No action is taken if the current capacity is sufficient. |
| | | 408 | | /// </remarks> |
| | | 409 | | /// <param name="capacity">The minimum number of elements that the internal storage should be able to hold. Must be |
| | | 410 | | public void EnsureCapacity(int capacity) |
| | | 411 | | { |
| | 4 | 412 | | capacity = SwiftHashTools.NextPowerOfTwo(capacity); |
| | 4 | 413 | | if (capacity > _innerArray.Length) |
| | 3 | 414 | | Resize(capacity); |
| | | 415 | | else |
| | 1 | 416 | | _freeIndices.EnsureCapacity(capacity); |
| | 1 | 417 | | } |
| | | 418 | | |
| | | 419 | | private void Resize(int newSize) |
| | | 420 | | { |
| | 28 | 421 | | int newCapacity = newSize; |
| | 28 | 422 | | int copyLength = Math.Min(_peakCount, _innerArray.Length); |
| | | 423 | | |
| | 28 | 424 | | Entry[] newArray = new Entry[newCapacity]; |
| | 28 | 425 | | if (copyLength > 0) |
| | 27 | 426 | | Array.Copy(_innerArray, 0, newArray, 0, copyLength); |
| | 28 | 427 | | _innerArray = newArray; |
| | 28 | 428 | | _freeIndices.EnsureCapacity(newCapacity); |
| | | 429 | | |
| | 28 | 430 | | _version++; |
| | 28 | 431 | | } |
| | | 432 | | |
| | | 433 | | /// <summary> |
| | | 434 | | /// Reduces unused tail capacity while preserving stable handles for all currently allocated entries. |
| | | 435 | | /// </summary> |
| | | 436 | | public void TrimExcessCapacity() |
| | | 437 | | { |
| | 4 | 438 | | int newPeak = GetLivePeakCount(); |
| | 4 | 439 | | int newCapacity = newPeak <= DefaultCapacity ? DefaultCapacity : SwiftHashTools.NextPowerOfTwo(newPeak); |
| | | 440 | | |
| | 4 | 441 | | Entry[] newArray = new Entry[newCapacity]; |
| | 4 | 442 | | if (newPeak > 0) |
| | 2 | 443 | | Array.Copy(_innerArray, 0, newArray, 0, newPeak); |
| | | 444 | | |
| | 4 | 445 | | _innerArray = newArray; |
| | 4 | 446 | | _peakCount = newPeak; |
| | 4 | 447 | | RebuildFreeIndices(); |
| | | 448 | | |
| | 4 | 449 | | _version++; |
| | 4 | 450 | | } |
| | | 451 | | |
| | | 452 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 453 | | private int GetLivePeakCount() |
| | | 454 | | { |
| | 10 | 455 | | for (int i = _peakCount - 1; i >= 0; i--) |
| | 3 | 456 | | if (_innerArray[i].IsUsed) |
| | 2 | 457 | | return i + 1; |
| | | 458 | | |
| | 2 | 459 | | return 0; |
| | | 460 | | } |
| | | 461 | | |
| | | 462 | | private void RebuildFreeIndices() |
| | | 463 | | { |
| | 4 | 464 | | _freeIndices = new SwiftIntStack(_innerArray.Length); |
| | | 465 | | |
| | 78 | 466 | | for (int i = 0; i < _peakCount; i++) |
| | | 467 | | { |
| | 35 | 468 | | if (!_innerArray[i].IsUsed) |
| | 30 | 469 | | _freeIndices.Push(i); |
| | | 470 | | } |
| | 4 | 471 | | } |
| | | 472 | | |
| | | 473 | | #endregion |
| | | 474 | | |
| | | 475 | | #region Utility Methods |
| | | 476 | | |
| | | 477 | | /// <summary> |
| | | 478 | | /// Attempts to get the value at the specified arrayIndex. |
| | | 479 | | /// </summary> |
| | | 480 | | /// <param name="key">The arrayIndex of the item to get.</param> |
| | | 481 | | /// <param name="value">When this method returns, contains the value associated with the specified arrayIndex, if th |
| | | 482 | | /// <returns><c>true</c> if the bucket contains an element at the specified arrayIndex; otherwise, <c>false</c>.</re |
| | | 483 | | public bool TryGetValue(int key, out T value) |
| | | 484 | | { |
| | 4 | 485 | | if (!IsAllocated(key)) |
| | | 486 | | { |
| | 3 | 487 | | value = default!; |
| | 3 | 488 | | return false; |
| | | 489 | | } |
| | | 490 | | |
| | 1 | 491 | | value = _innerArray[key].Value; |
| | 1 | 492 | | return true; |
| | | 493 | | } |
| | | 494 | | |
| | | 495 | | /// <summary> |
| | | 496 | | /// Determines whether the bucket contains a specific value. |
| | | 497 | | /// </summary> |
| | | 498 | | /// <param name="item">The object to locate in the bucket.</param> |
| | | 499 | | /// <returns><c>true</c> if item is found; otherwise, <c>false</c>.</returns> |
| | | 500 | | /// <remarks> |
| | | 501 | | /// This method performs a linear search and has a time complexity of O(n). |
| | | 502 | | /// It is recommended to store the indices returned by the <see cref="Add"/> method for faster lookups using the ind |
| | | 503 | | /// </remarks> |
| | | 504 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 6 | 505 | | public bool Contains(T item) => IndexOf(item) != -1; |
| | | 506 | | |
| | | 507 | | /// <summary> |
| | | 508 | | /// Determines whether the <see cref="SwiftBucket{T}"/> contains an element that matches the conditions defined by t |
| | | 509 | | /// </summary> |
| | | 510 | | /// <param name="match">The predicate that defines the conditions of the element to search for.</param> |
| | | 511 | | /// <returns><c>true</c> if the <see cref="SwiftBucket{T}"/> contains one or more elements that match the specified |
| | | 512 | | public bool Exists(Predicate<T> match) |
| | | 513 | | { |
| | 2 | 514 | | SwiftThrowHelper.ThrowIfNull(match, nameof(match)); |
| | | 515 | | |
| | 2 | 516 | | uint count = 0; |
| | | 517 | | |
| | 12 | 518 | | for (int i = 0; i < _peakCount && count < (uint)_count; i++) |
| | | 519 | | { |
| | 5 | 520 | | if (_innerArray[i].IsUsed) |
| | | 521 | | { |
| | 5 | 522 | | if (match(_innerArray[i].Value)) |
| | 1 | 523 | | return true; |
| | | 524 | | |
| | 4 | 525 | | count++; |
| | | 526 | | } |
| | | 527 | | } |
| | | 528 | | |
| | 1 | 529 | | return false; |
| | | 530 | | } |
| | | 531 | | |
| | | 532 | | /// <summary> |
| | | 533 | | /// Searches for an element that matches the conditions defined by the specified predicate, and returns the first ma |
| | | 534 | | /// </summary> |
| | | 535 | | /// <param name="match">The predicate that defines the conditions of the element to search for.</param> |
| | | 536 | | /// <returns>The first element that matches the conditions defined by the specified predicate, if found; otherwise, |
| | | 537 | | public T Find(Predicate<T> match) |
| | | 538 | | { |
| | 2 | 539 | | SwiftThrowHelper.ThrowIfNull(match, nameof(match)); |
| | | 540 | | |
| | 2 | 541 | | uint count = 0; |
| | | 542 | | |
| | 10 | 543 | | for (int i = 0; i < _peakCount && count < (uint)_count; i++) |
| | | 544 | | { |
| | 4 | 545 | | if (_innerArray[i].IsUsed) |
| | | 546 | | { |
| | 4 | 547 | | T item = _innerArray[i].Value; |
| | 4 | 548 | | if (match(item)) |
| | 1 | 549 | | return item; |
| | | 550 | | |
| | 3 | 551 | | count++; |
| | | 552 | | } |
| | | 553 | | } |
| | | 554 | | |
| | 1 | 555 | | return default!; |
| | | 556 | | } |
| | | 557 | | |
| | | 558 | | /// <summary> |
| | | 559 | | /// Determines whether the element at the specified index is currently allocated. |
| | | 560 | | /// </summary> |
| | | 561 | | /// <param name="index">The zero-based index of the element to check. Must be greater than or equal to 0 and less th |
| | | 562 | | /// <returns>true if the element at the specified index is allocated; otherwise, false.</returns> |
| | | 563 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 50495 | 564 | | public bool IsAllocated(int index) => !((uint)index >= (uint)_innerArray.Length) && _innerArray[index].IsUsed; |
| | | 565 | | |
| | | 566 | | /// <summary> |
| | | 567 | | /// Searches for the specified object and returns the zero-based arrayIndex of the first occurrence within the bucke |
| | | 568 | | /// </summary> |
| | | 569 | | /// <param name="item">The object to locate in the bucket.</param> |
| | | 570 | | /// <returns> |
| | | 571 | | /// The zero-based arrayIndex of the first occurrence of <paramref name="item"/> within the bucket, if found; otherw |
| | | 572 | | /// </returns> |
| | | 573 | | /// <remarks> |
| | | 574 | | /// This method performs a linear search and has a time complexity of O(n). |
| | | 575 | | /// It is recommended to store the indices returned by the <see cref="Add"/> method for faster lookups using the ind |
| | | 576 | | /// </remarks> |
| | | 577 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 578 | | public int IndexOf(T item) |
| | | 579 | | { |
| | 11 | 580 | | uint count = 0; |
| | | 581 | | |
| | 11 | 582 | | if (item == null) |
| | | 583 | | { |
| | 14 | 584 | | for (int i = 0; i < (uint)_peakCount && count < (uint)_count; i++) |
| | | 585 | | { |
| | 6 | 586 | | if (_innerArray[i].IsUsed) |
| | | 587 | | { |
| | 6 | 588 | | if (_innerArray[i].Value == null) |
| | 2 | 589 | | return i; |
| | 4 | 590 | | count++; |
| | | 591 | | } |
| | | 592 | | } |
| | | 593 | | |
| | 1 | 594 | | return -1; |
| | | 595 | | } |
| | | 596 | | |
| | 40 | 597 | | for (int j = 0; j < (uint)_peakCount && count < (uint)_count; j++) |
| | | 598 | | { |
| | 17 | 599 | | if (_innerArray[j].IsUsed) |
| | | 600 | | { |
| | 12 | 601 | | if (EqualityComparer<T>.Default.Equals(_innerArray[j].Value, item)) |
| | 5 | 602 | | return j; |
| | 7 | 603 | | count++; |
| | | 604 | | } |
| | | 605 | | } |
| | 3 | 606 | | return -1; |
| | | 607 | | } |
| | | 608 | | |
| | | 609 | | /// <summary> |
| | | 610 | | /// Copies the elements of the bucket to an <see cref="Array"/>, starting at a particular Array arrayIndex. |
| | | 611 | | /// </summary> |
| | | 612 | | /// <param name="array">The one-dimensional Array that is the destination of the elements copied from bucket.</param |
| | | 613 | | /// <param name="arrayIndex">The zero-based arrayIndex in array at which copying begins.</param> |
| | | 614 | | public void CopyTo(T[] array, int arrayIndex) |
| | | 615 | | { |
| | 1 | 616 | | SwiftThrowHelper.ThrowIfNull(array, nameof(array)); |
| | 1 | 617 | | SwiftThrowHelper.ThrowIfArrayIndexInvalid(arrayIndex, array.Length); |
| | 1 | 618 | | SwiftThrowHelper.ThrowIfArgument(array.Length - arrayIndex < _count, nameof(array), "The array is not large enou |
| | | 619 | | |
| | 1 | 620 | | uint count = 0; |
| | 8 | 621 | | for (uint i = 0; i < (uint)_peakCount && count < (uint)_count; i++) |
| | | 622 | | { |
| | 3 | 623 | | if (_innerArray[i].IsUsed) |
| | | 624 | | { |
| | 3 | 625 | | array[arrayIndex++] = _innerArray[i].Value; |
| | 3 | 626 | | count++; |
| | | 627 | | } |
| | | 628 | | } |
| | 1 | 629 | | } |
| | | 630 | | |
| | | 631 | | void ICollection.CopyTo(Array array, int arrayIndex) |
| | | 632 | | { |
| | 7 | 633 | | SwiftThrowHelper.ThrowIfNull(array, nameof(array)); |
| | 7 | 634 | | SwiftThrowHelper.ThrowIfArgument((uint)array.Rank != 1, nameof(array), "Array must be single dimensional."); |
| | 6 | 635 | | SwiftThrowHelper.ThrowIfArgument((uint)array.GetLowerBound(0) != 0, nameof(array), "Array must have zero-based i |
| | 5 | 636 | | SwiftThrowHelper.ThrowIfArrayIndexInvalid(arrayIndex, array.Length); |
| | 4 | 637 | | SwiftThrowHelper.ThrowIfArgument(array.Length - arrayIndex < _count, nameof(array), "The array is not large enou |
| | | 638 | | |
| | | 639 | | try |
| | | 640 | | { |
| | 3 | 641 | | uint count = 0; |
| | 10 | 642 | | for (uint i = 0; i < (uint)_peakCount && count < (uint)_count; i++) |
| | | 643 | | { |
| | 4 | 644 | | if (_innerArray[i].IsUsed) |
| | | 645 | | { |
| | 4 | 646 | | array.SetValue(_innerArray[i].Value, arrayIndex++); |
| | 2 | 647 | | count++; |
| | | 648 | | } |
| | | 649 | | } |
| | 1 | 650 | | } |
| | 2 | 651 | | catch (InvalidCastException) |
| | | 652 | | { |
| | 2 | 653 | | throw new ArgumentException("Invalid array type."); |
| | | 654 | | } |
| | 1 | 655 | | } |
| | | 656 | | |
| | | 657 | | /// <inheritdoc/> |
| | | 658 | | public void CloneTo(ICollection<T> output) |
| | | 659 | | { |
| | 1 | 660 | | output.Clear(); |
| | 1 | 661 | | uint count = 0; |
| | 6 | 662 | | for (uint i = 0; i < (uint)_peakCount && count < (uint)_count; i++) |
| | | 663 | | { |
| | 2 | 664 | | if (_innerArray[i].IsUsed) |
| | | 665 | | { |
| | 2 | 666 | | output.Add(_innerArray[i].Value); |
| | 2 | 667 | | count++; |
| | | 668 | | } |
| | | 669 | | } |
| | 1 | 670 | | } |
| | | 671 | | |
| | | 672 | | #endregion |
| | | 673 | | |
| | | 674 | | #region Enumerator |
| | | 675 | | |
| | | 676 | | /// <summary> |
| | | 677 | | /// Returns an enumerator that iterates through the <see cref="SwiftBucket{T}"/>. |
| | | 678 | | /// </summary> |
| | | 679 | | /// <returns>An enumerator for the bucket.</returns> |
| | 8 | 680 | | public SwiftBucketEnumerator GetEnumerator() => new(this); |
| | 4 | 681 | | IEnumerator<T> IEnumerable<T>.GetEnumerator() => GetEnumerator(); |
| | 1 | 682 | | IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
| | | 683 | | |
| | | 684 | | /// <summary> |
| | | 685 | | /// Enumerates the elements of a <see cref="SwiftBucket{T}"/> collection. |
| | | 686 | | /// </summary> |
| | | 687 | | /// <remarks> |
| | | 688 | | /// The enumerator provides read-only, forward-only iteration over the elements in the <see cref="SwiftBucket{T}"/>. |
| | | 689 | | /// The enumerator is invalidated if the collection is modified after the enumerator is created. |
| | | 690 | | /// </remarks> |
| | | 691 | | public struct SwiftBucketEnumerator : IEnumerator<T>, IDisposable |
| | | 692 | | { |
| | | 693 | | private readonly SwiftBucket<T> _bucket; |
| | | 694 | | private readonly Entry[] _entries; |
| | | 695 | | private readonly uint _version; |
| | | 696 | | private int _index; |
| | | 697 | | private T _current; |
| | | 698 | | |
| | | 699 | | internal SwiftBucketEnumerator(SwiftBucket<T> bucket) |
| | | 700 | | { |
| | 8 | 701 | | _bucket = bucket; |
| | 8 | 702 | | _entries = bucket._innerArray; |
| | 8 | 703 | | _version = bucket._version; |
| | 8 | 704 | | _index = -1; |
| | 8 | 705 | | _current = default!; |
| | 8 | 706 | | } |
| | | 707 | | |
| | | 708 | | /// <inheritdoc/> |
| | 410 | 709 | | public T Current => _current; |
| | | 710 | | |
| | | 711 | | object IEnumerator.Current |
| | | 712 | | { |
| | | 713 | | get |
| | | 714 | | { |
| | 1 | 715 | | SwiftThrowHelper.ThrowIfTrue(_index > (uint)_bucket._count, message: "Enumerator is before the first ele |
| | 1 | 716 | | return _current!; |
| | | 717 | | } |
| | | 718 | | } |
| | | 719 | | |
| | | 720 | | /// <inheritdoc/> |
| | | 721 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 722 | | public bool MoveNext() |
| | | 723 | | { |
| | 219 | 724 | | SwiftThrowHelper.ThrowIfTrue(_version != _bucket._version, message: "Enumerator modified outside of enumerat |
| | | 725 | | |
| | 218 | 726 | | uint count = (uint)_bucket._peakCount; |
| | 219 | 727 | | while (++_index < count) |
| | | 728 | | { |
| | 212 | 729 | | if (_entries[_index].IsUsed) |
| | | 730 | | { |
| | 211 | 731 | | _current = _entries[_index].Value; |
| | 211 | 732 | | return true; |
| | | 733 | | } |
| | | 734 | | } |
| | 7 | 735 | | return false; |
| | | 736 | | } |
| | | 737 | | |
| | | 738 | | /// <inheritdoc/> |
| | | 739 | | public void Reset() |
| | | 740 | | { |
| | 1 | 741 | | SwiftThrowHelper.ThrowIfTrue(_version != _bucket._version, message: "Enumerator modified outside of enumerat |
| | | 742 | | |
| | 1 | 743 | | _index = -1; |
| | 1 | 744 | | _current = default!; |
| | 1 | 745 | | } |
| | | 746 | | |
| | | 747 | | /// <inheritdoc/> |
| | 6 | 748 | | public void Dispose() => _index = -1; |
| | | 749 | | } |
| | | 750 | | |
| | | 751 | | #endregion |
| | | 752 | | } |