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