| | | 1 | | using Chronicler; |
| | | 2 | | using MemoryPack; |
| | | 3 | | using System; |
| | | 4 | | using System.Collections; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Text.Json.Serialization; |
| | | 7 | | |
| | | 8 | | namespace SwiftCollections; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Represents a high-performance generational bucket that assigns stable handles to stored items. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <remarks> |
| | | 14 | | /// <para> |
| | | 15 | | /// <see cref="SwiftGenerationalBucket{T}"/> is similar to <see cref="SwiftBucket{T}"/> but adds |
| | | 16 | | /// <b>generation tracking</b> to prevent stale references from accessing reused slots. |
| | | 17 | | /// </para> |
| | | 18 | | /// |
| | | 19 | | /// <para> |
| | | 20 | | /// When an item is added, a <see cref="SwiftHandle"/> containing both an index and generation |
| | | 21 | | /// is returned. If the item is removed and the slot reused later, the generation value |
| | | 22 | | /// changes, causing older handles to automatically become invalid. |
| | | 23 | | /// </para> |
| | | 24 | | /// |
| | | 25 | | /// <para> |
| | | 26 | | /// This pattern is widely used to safely reference objects without risking accidental access |
| | | 27 | | /// to recycled memory slots. |
| | | 28 | | /// </para> |
| | | 29 | | /// |
| | | 30 | | /// <para> |
| | | 31 | | /// Key characteristics: |
| | | 32 | | /// <list type="bullet"> |
| | | 33 | | /// <item><description>O(1) insertion and removal.</description></item> |
| | | 34 | | /// <item><description>Stable handles for the lifetime of stored items.</description></item> |
| | | 35 | | /// <item><description>Automatic invalidation of stale handles via generation counters.</description></item> |
| | | 36 | | /// <item><description>Cache-friendly contiguous storage.</description></item> |
| | | 37 | | /// </list> |
| | | 38 | | /// </para> |
| | | 39 | | /// |
| | | 40 | | /// <para> |
| | | 41 | | /// Use <see cref="SwiftBucket{T}"/> when raw indices are acceptable. |
| | | 42 | | /// Use <see cref="SwiftGenerationalBucket{T}"/> when handle safety is required. |
| | | 43 | | /// </para> |
| | | 44 | | /// </remarks> |
| | | 45 | | /// <typeparam name="T">Specifies the type of elements stored in the bucket.</typeparam> |
| | | 46 | | [Serializable] |
| | | 47 | | [JsonConverter(typeof(StateJsonConverterFactory))] |
| | | 48 | | [MemoryPackable] |
| | | 49 | | public sealed partial class SwiftGenerationalBucket<T> : IStateBacked<SwiftGenerationalBucketState<T>>, ISwiftCloneable< |
| | | 50 | | { |
| | | 51 | | #region Nested Types |
| | | 52 | | |
| | | 53 | | private struct Entry |
| | | 54 | | { |
| | | 55 | | public uint Generation; |
| | | 56 | | public bool IsUsed; |
| | | 57 | | public T Value; |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | #endregion |
| | | 61 | | |
| | | 62 | | #region Constants |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Represents the default initial capacity for the collection. |
| | | 66 | | /// </summary> |
| | | 67 | | /// <remarks> |
| | | 68 | | /// Use this constant when initializing the collection to its default size. |
| | | 69 | | /// The value is typically used to optimize memory allocation for small collections. |
| | | 70 | | /// </remarks> |
| | | 71 | | public const int DefaultCapacity = 8; |
| | | 72 | | |
| | | 73 | | #endregion |
| | | 74 | | |
| | | 75 | | #region Fields |
| | | 76 | | |
| | | 77 | | private Entry[] _entries; |
| | | 78 | | private SwiftIntStack _freeIndices; |
| | | 79 | | |
| | | 80 | | private int _count; |
| | | 81 | | private int _peak; |
| | | 82 | | |
| | | 83 | | private uint _version; |
| | | 84 | | |
| | | 85 | | #endregion |
| | | 86 | | |
| | | 87 | | #region Constructors |
| | | 88 | | |
| | | 89 | | /// <summary> |
| | | 90 | | /// Initializes a new instance of the SwiftGenerationalBucket class with the default capacity. |
| | | 91 | | /// </summary> |
| | 30 | 92 | | public SwiftGenerationalBucket() : this(DefaultCapacity) { } |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// Initializes a new instance of the SwiftGenerationalBucket class with the specified initial capacity. |
| | | 96 | | /// </summary> |
| | | 97 | | /// <remarks> |
| | | 98 | | /// The actual capacity will be set to the next power of two greater than or equal to the specified capacity, |
| | | 99 | | /// or to the default capacity if the specified value is too small. |
| | | 100 | | /// This ensures efficient internal storage and lookup performance. |
| | | 101 | | /// </remarks> |
| | | 102 | | /// <param name="capacity"> |
| | | 103 | | /// The initial number of elements that the bucket can contain. |
| | | 104 | | /// If less than or equal to the default capacity, the default capacity is used. |
| | | 105 | | /// Must be a non-negative integer. |
| | | 106 | | /// </param> |
| | 17 | 107 | | public SwiftGenerationalBucket(int capacity) |
| | | 108 | | { |
| | 17 | 109 | | capacity = capacity <= DefaultCapacity |
| | 17 | 110 | | ? DefaultCapacity |
| | 17 | 111 | | : SwiftHashTools.NextPowerOfTwo(capacity); |
| | | 112 | | |
| | 17 | 113 | | _entries = new Entry[capacity]; |
| | 17 | 114 | | _freeIndices = new SwiftIntStack(capacity); |
| | 17 | 115 | | } |
| | | 116 | | |
| | | 117 | | /// <summary> |
| | | 118 | | /// Initializes a new instance of the <see cref="SwiftGenerationalBucket{T}"/> class with the specified <see cref=" |
| | | 119 | | /// </summary> |
| | | 120 | | /// <param name="state">The state containing the internal array, count, offset, and version for initialization.</pa |
| | | 121 | | [MemoryPackConstructor] |
| | 6 | 122 | | public SwiftGenerationalBucket(SwiftGenerationalBucketState<T> state) |
| | | 123 | | { |
| | 6 | 124 | | State = state; |
| | | 125 | | // Ensure that the internal structures are initialized even if the state is null or incomplete. |
| | 5 | 126 | | _entries ??= new Entry[DefaultCapacity]; |
| | 5 | 127 | | _freeIndices ??= new SwiftIntStack(DefaultCapacity); |
| | 5 | 128 | | } |
| | | 129 | | |
| | | 130 | | #endregion |
| | | 131 | | |
| | | 132 | | #region Properties |
| | | 133 | | |
| | | 134 | | /// <summary> |
| | | 135 | | /// Gets the number of elements contained in the collection. |
| | | 136 | | /// </summary> |
| | | 137 | | [JsonIgnore] |
| | | 138 | | [MemoryPackIgnore] |
| | 9 | 139 | | public int Count => _count; |
| | | 140 | | |
| | | 141 | | /// <summary> |
| | | 142 | | /// Gets the total number of elements that the internal data structure can hold without resizing. |
| | | 143 | | /// </summary> |
| | | 144 | | /// <remarks> |
| | | 145 | | /// This value represents the allocated size of the underlying storage, which may be greater than the actual number |
| | | 146 | | /// Capacity is always greater than or equal to the current count of elements. |
| | | 147 | | /// </remarks> |
| | | 148 | | [JsonIgnore] |
| | | 149 | | [MemoryPackIgnore] |
| | 2 | 150 | | public int Capacity => _entries.Length; |
| | | 151 | | |
| | | 152 | | /// <summary> |
| | | 153 | | /// Gets or sets the current state of the generational bucket. |
| | | 154 | | /// </summary> |
| | | 155 | | /// <remarks> |
| | | 156 | | /// This property provides a snapshot of the bucket's internal state, which can be used for serialization, diagnosti |
| | | 157 | | /// or restoring the bucket to a previous state. |
| | | 158 | | /// Setting this property replaces the entire state of the bucket, including its contents and allocation metadata. |
| | | 159 | | /// </remarks> |
| | | 160 | | [JsonInclude] |
| | | 161 | | [MemoryPackInclude] |
| | | 162 | | public SwiftGenerationalBucketState<T> State |
| | | 163 | | { |
| | | 164 | | get |
| | | 165 | | { |
| | 3 | 166 | | int length = _entries.Length; |
| | | 167 | | |
| | 3 | 168 | | var items = new T[length]; |
| | 3 | 169 | | var allocated = new bool[length]; |
| | 3 | 170 | | var generations = new uint[length]; |
| | | 171 | | |
| | 166 | 172 | | for (int i = 0; i < length; i++) |
| | | 173 | | { |
| | 80 | 174 | | generations[i] = _entries[i].Generation; |
| | | 175 | | |
| | 80 | 176 | | if (_entries[i].IsUsed) |
| | | 177 | | { |
| | 54 | 178 | | items[i] = _entries[i].Value; |
| | 54 | 179 | | allocated[i] = true; |
| | | 180 | | } |
| | | 181 | | } |
| | | 182 | | |
| | 3 | 183 | | int[] free = new int[_freeIndices.Count]; |
| | 3 | 184 | | Array.Copy(_freeIndices.Array, free, _freeIndices.Count); |
| | | 185 | | |
| | 3 | 186 | | return new SwiftGenerationalBucketState<T>( |
| | 3 | 187 | | items, |
| | 3 | 188 | | allocated, |
| | 3 | 189 | | generations, |
| | 3 | 190 | | free, |
| | 3 | 191 | | _peak |
| | 3 | 192 | | ); |
| | | 193 | | } |
| | | 194 | | internal set |
| | | 195 | | { |
| | 6 | 196 | | var items = value.Items ?? Array.Empty<T>(); |
| | 6 | 197 | | var allocated = value.Allocated ?? Array.Empty<bool>(); |
| | 6 | 198 | | var generations = value.Generations ?? Array.Empty<uint>(); |
| | 6 | 199 | | var freeIndices = value.FreeIndices ?? Array.Empty<int>(); |
| | | 200 | | |
| | 6 | 201 | | int sourceLength = GetStateSourceLength(items, allocated, generations); |
| | 6 | 202 | | int capacity = GetStateCapacity(sourceLength); |
| | 6 | 203 | | InitializeStateStorage(capacity, freeIndices.Length); |
| | | 204 | | |
| | 6 | 205 | | int maxReferencedIndex = RestoreEntries(items, allocated, generations, sourceLength); |
| | 6 | 206 | | maxReferencedIndex = RestoreFreeIndices(freeIndices, capacity, maxReferencedIndex); |
| | | 207 | | |
| | 5 | 208 | | _peak = NormalizePeak(value.Peak, maxReferencedIndex, capacity); |
| | 5 | 209 | | _version = 0; |
| | 5 | 210 | | } |
| | | 211 | | } |
| | | 212 | | |
| | | 213 | | private static int GetStateSourceLength(T[] items, bool[] allocated, uint[] generations) |
| | | 214 | | { |
| | 6 | 215 | | return Math.Max(items.Length, Math.Max(allocated.Length, generations.Length)); |
| | | 216 | | } |
| | | 217 | | |
| | | 218 | | private static int GetStateCapacity(int sourceLength) |
| | | 219 | | { |
| | 6 | 220 | | if (sourceLength < DefaultCapacity) |
| | 4 | 221 | | return DefaultCapacity; |
| | | 222 | | |
| | 2 | 223 | | return SwiftHashTools.NextPowerOfTwo(sourceLength); |
| | | 224 | | } |
| | | 225 | | |
| | | 226 | | private void InitializeStateStorage(int capacity, int freeIndexCount) |
| | | 227 | | { |
| | 6 | 228 | | _entries = new Entry[capacity]; |
| | 6 | 229 | | _freeIndices = new SwiftIntStack(Math.Max(SwiftIntStack.DefaultCapacity, freeIndexCount)); |
| | 6 | 230 | | _count = 0; |
| | 6 | 231 | | } |
| | | 232 | | |
| | | 233 | | private int RestoreEntries(T[] items, bool[] allocated, uint[] generations, int sourceLength) |
| | | 234 | | { |
| | 6 | 235 | | int maxReferencedIndex = -1; |
| | 168 | 236 | | for (int i = 0; i < sourceLength; i++) |
| | | 237 | | { |
| | 78 | 238 | | ref Entry entry = ref _entries[i]; |
| | 78 | 239 | | bool isAllocated = IsStateIndexAllocated(allocated, i); |
| | | 240 | | |
| | 78 | 241 | | entry.Generation = GetStateGeneration(generations, i); |
| | 78 | 242 | | if (entry.Generation != 0 || isAllocated) |
| | 59 | 243 | | maxReferencedIndex = i; |
| | | 244 | | |
| | 78 | 245 | | if (isAllocated) |
| | 58 | 246 | | RestoreAllocatedEntry(ref entry, items, i); |
| | | 247 | | } |
| | | 248 | | |
| | 6 | 249 | | return maxReferencedIndex; |
| | | 250 | | } |
| | | 251 | | |
| | | 252 | | private static uint GetStateGeneration(uint[] generations, int index) |
| | | 253 | | { |
| | 78 | 254 | | if (generations.Length <= index) |
| | 0 | 255 | | return 0; |
| | | 256 | | |
| | 78 | 257 | | return generations[index]; |
| | | 258 | | } |
| | | 259 | | |
| | | 260 | | private static bool IsStateIndexAllocated(bool[] allocated, int index) |
| | | 261 | | { |
| | 78 | 262 | | return allocated.Length > index && allocated[index]; |
| | | 263 | | } |
| | | 264 | | |
| | | 265 | | private void RestoreAllocatedEntry(ref Entry entry, T[] items, int index) |
| | | 266 | | { |
| | 58 | 267 | | if (items.Length > index) |
| | 58 | 268 | | entry.Value = items[index]; |
| | | 269 | | |
| | 58 | 270 | | entry.IsUsed = true; |
| | 58 | 271 | | _count++; |
| | 58 | 272 | | } |
| | | 273 | | |
| | | 274 | | private int RestoreFreeIndices(int[] freeIndices, int capacity, int maxReferencedIndex) |
| | | 275 | | { |
| | 15 | 276 | | foreach (var index in freeIndices) |
| | | 277 | | { |
| | 2 | 278 | | SwiftThrowHelper.ThrowIfArgument((uint)index >= (uint)capacity, message: "Free index is out of range."); |
| | | 279 | | |
| | 1 | 280 | | _freeIndices.Push(index); |
| | 1 | 281 | | if (index > maxReferencedIndex) |
| | 1 | 282 | | maxReferencedIndex = index; |
| | | 283 | | } |
| | | 284 | | |
| | 5 | 285 | | return maxReferencedIndex; |
| | | 286 | | } |
| | | 287 | | |
| | | 288 | | private static int NormalizePeak(int peak, int maxReferencedIndex, int capacity) |
| | | 289 | | { |
| | 5 | 290 | | if (peak < 0) |
| | 1 | 291 | | peak = 0; |
| | | 292 | | |
| | 5 | 293 | | return Math.Min(Math.Max(peak, maxReferencedIndex + 1), capacity); |
| | | 294 | | } |
| | | 295 | | |
| | | 296 | | #endregion |
| | | 297 | | |
| | | 298 | | #region Core Operations |
| | | 299 | | |
| | | 300 | | /// <summary> |
| | | 301 | | /// Adds the specified value to the collection and returns a handle that can be used to reference it. |
| | | 302 | | /// </summary> |
| | | 303 | | /// <remarks> |
| | | 304 | | /// The returned handle can be used to access or remove the value later. |
| | | 305 | | /// Handles are only valid as long as the value remains in the collection. |
| | | 306 | | /// </remarks> |
| | | 307 | | /// <param name="value">The value to add to the collection.</param> |
| | | 308 | | /// <returns>A <see cref="SwiftHandle"/> that uniquely identifies the added value within the collection.</returns> |
| | | 309 | | public SwiftHandle Add(T value) |
| | | 310 | | { |
| | | 311 | | int index; |
| | | 312 | | |
| | 342 | 313 | | if (_freeIndices.Count == 0) |
| | | 314 | | { |
| | 340 | 315 | | index = _peak++; |
| | | 316 | | |
| | 340 | 317 | | if ((uint)index >= (uint)_entries.Length) |
| | 16 | 318 | | Resize(_entries.Length * 2); |
| | | 319 | | } |
| | | 320 | | else |
| | | 321 | | { |
| | 2 | 322 | | index = _freeIndices.Pop(); |
| | | 323 | | } |
| | | 324 | | |
| | 342 | 325 | | ref Entry entry = ref _entries[index]; |
| | | 326 | | |
| | 342 | 327 | | entry.Value = value; |
| | 342 | 328 | | entry.IsUsed = true; |
| | | 329 | | |
| | 342 | 330 | | _count++; |
| | 342 | 331 | | _version++; |
| | | 332 | | |
| | 342 | 333 | | return new SwiftHandle(index, entry.Generation); |
| | | 334 | | } |
| | | 335 | | |
| | | 336 | | /// <summary> |
| | | 337 | | /// Attempts to retrieve the value associated with the specified handle. |
| | | 338 | | /// </summary> |
| | | 339 | | /// <remarks> |
| | | 340 | | /// Use this method to safely attempt retrieval without throwing an exception if the handle is invalid or the entry |
| | | 341 | | /// </remarks> |
| | | 342 | | /// <param name="handle">The handle used to identify the entry to retrieve.</param> |
| | | 343 | | /// <param name="value"> |
| | | 344 | | /// When this method returns, contains the value associated with the specified handle if the handle is valid |
| | | 345 | | /// and the entry is in use; otherwise, the default value for the type of the value parameter. |
| | | 346 | | /// This parameter is passed uninitialized. |
| | | 347 | | /// </param> |
| | | 348 | | /// <returns>true if the value was found and retrieved successfully; otherwise, false.</returns> |
| | | 349 | | public bool TryGet(SwiftHandle handle, out T value) |
| | | 350 | | { |
| | 210 | 351 | | if ((uint)handle.Index >= (uint)_entries.Length) |
| | | 352 | | { |
| | 1 | 353 | | value = default!; |
| | 1 | 354 | | return false; |
| | | 355 | | } |
| | | 356 | | |
| | 209 | 357 | | ref Entry entry = ref _entries[handle.Index]; |
| | | 358 | | |
| | 209 | 359 | | if (!entry.IsUsed || entry.Generation != handle.Generation) |
| | | 360 | | { |
| | 2 | 361 | | value = default!; |
| | 2 | 362 | | return false; |
| | | 363 | | } |
| | | 364 | | |
| | 207 | 365 | | value = entry.Value; |
| | 207 | 366 | | return true; |
| | | 367 | | } |
| | | 368 | | |
| | | 369 | | /// <summary> |
| | | 370 | | /// Returns a reference to the value associated with the specified handle. |
| | | 371 | | /// </summary> |
| | | 372 | | /// <param name="handle"> |
| | | 373 | | /// A handle that identifies the entry whose value is to be accessed. |
| | | 374 | | /// The handle must be valid and refer to an existing entry. |
| | | 375 | | /// </param> |
| | | 376 | | /// <returns>A reference to the value of type T associated with the specified handle.</returns> |
| | | 377 | | /// <exception cref="InvalidOperationException">Thrown if the handle does not refer to a valid or currently used ent |
| | | 378 | | public ref T GetRef(SwiftHandle handle) |
| | | 379 | | { |
| | 1 | 380 | | ref Entry entry = ref _entries[handle.Index]; |
| | | 381 | | |
| | 1 | 382 | | SwiftThrowHelper.ThrowIfTrue(!entry.IsUsed || entry.Generation != handle.Generation, message: "Invalid handle"); |
| | | 383 | | |
| | 1 | 384 | | return ref entry.Value; |
| | | 385 | | } |
| | | 386 | | |
| | | 387 | | /// <summary> |
| | | 388 | | /// Removes the entry associated with the specified handle from the collection. |
| | | 389 | | /// </summary> |
| | | 390 | | /// <remarks> |
| | | 391 | | /// If the handle does not refer to a valid or currently used entry, the method returns false and no action is taken |
| | | 392 | | /// Removing an entry invalidates the handle for future operations. |
| | | 393 | | /// </remarks> |
| | | 394 | | /// <param name="handle">The handle identifying the entry to remove. The handle must refer to a valid, currently use |
| | | 395 | | /// <returns>true if the entry was successfully removed; otherwise, false.</returns> |
| | | 396 | | public bool Remove(SwiftHandle handle) |
| | | 397 | | { |
| | 3 | 398 | | if ((uint)handle.Index >= (uint)_entries.Length) |
| | 1 | 399 | | return false; |
| | | 400 | | |
| | 2 | 401 | | ref Entry entry = ref _entries[handle.Index]; |
| | | 402 | | |
| | 2 | 403 | | if (!entry.IsUsed || entry.Generation != handle.Generation) |
| | 0 | 404 | | return false; |
| | | 405 | | |
| | 2 | 406 | | entry.Value = default!; |
| | 2 | 407 | | entry.IsUsed = false; |
| | | 408 | | |
| | 2 | 409 | | entry.Generation++; |
| | | 410 | | |
| | 2 | 411 | | _freeIndices.Push(handle.Index); |
| | | 412 | | |
| | 2 | 413 | | _count--; |
| | 2 | 414 | | _version++; |
| | | 415 | | |
| | 2 | 416 | | return true; |
| | | 417 | | } |
| | | 418 | | |
| | | 419 | | /// <summary> |
| | | 420 | | /// Determines whether the specified handle refers to a valid and currently used entry. |
| | | 421 | | /// </summary> |
| | | 422 | | /// <remarks> |
| | | 423 | | /// A handle may become invalid if the referenced entry has been removed or replaced. |
| | | 424 | | /// Use this method to check handle validity before accessing the associated entry. |
| | | 425 | | /// </remarks> |
| | | 426 | | /// <param name="handle">The handle to validate. The handle must have been obtained from this collection; otherwise, |
| | | 427 | | /// <returns>true if the handle is valid and refers to an active entry; otherwise, false.</returns> |
| | | 428 | | public bool IsValid(SwiftHandle handle) |
| | | 429 | | { |
| | 1 | 430 | | if ((uint)handle.Index >= (uint)_entries.Length) |
| | 0 | 431 | | return false; |
| | | 432 | | |
| | 1 | 433 | | ref Entry entry = ref _entries[handle.Index]; |
| | | 434 | | |
| | 1 | 435 | | return entry.IsUsed && entry.Generation == handle.Generation; |
| | | 436 | | } |
| | | 437 | | |
| | | 438 | | #endregion |
| | | 439 | | |
| | | 440 | | #region Capacity |
| | | 441 | | |
| | | 442 | | /// <summary> |
| | | 443 | | /// Ensures that the underlying storage has at least the specified capacity, expanding it if necessary. |
| | | 444 | | /// </summary> |
| | | 445 | | /// <remarks> |
| | | 446 | | /// If the current capacity is less than the specified value, the storage is resized to accommodate at least that ma |
| | | 447 | | /// The actual capacity may be rounded up to the next power of two for performance reasons. |
| | | 448 | | /// </remarks> |
| | | 449 | | /// <param name="capacity">The minimum number of elements that the storage should be able to hold. Must be a non-neg |
| | | 450 | | public void EnsureCapacity(int capacity) |
| | | 451 | | { |
| | 1 | 452 | | capacity = SwiftHashTools.NextPowerOfTwo(capacity); |
| | 1 | 453 | | if (capacity > _entries.Length) |
| | 1 | 454 | | Resize(capacity); |
| | 1 | 455 | | } |
| | | 456 | | |
| | | 457 | | private void Resize(int newSize) |
| | | 458 | | { |
| | 17 | 459 | | int newCapacity = newSize <= DefaultCapacity ? DefaultCapacity : newSize; |
| | | 460 | | |
| | 17 | 461 | | Entry[] newArray = new Entry[newCapacity]; |
| | 17 | 462 | | Array.Copy(_entries, newArray, _entries.Length); |
| | 17 | 463 | | _entries = newArray; |
| | 17 | 464 | | } |
| | | 465 | | |
| | | 466 | | #endregion |
| | | 467 | | |
| | | 468 | | #region Utility |
| | | 469 | | |
| | | 470 | | /// <inheritdoc/> |
| | | 471 | | public void CloneTo(ICollection<T> output) |
| | | 472 | | { |
| | 1 | 473 | | SwiftThrowHelper.ThrowIfNull(output, nameof(output)); |
| | | 474 | | |
| | 1 | 475 | | output.Clear(); |
| | | 476 | | |
| | 1 | 477 | | uint count = 0; |
| | 1 | 478 | | uint peak = (uint)_peak; |
| | | 479 | | |
| | 42 | 480 | | for (uint i = 0; i < peak && count < (uint)_count; i++) |
| | | 481 | | { |
| | 20 | 482 | | if (_entries[i].IsUsed) |
| | | 483 | | { |
| | 20 | 484 | | output.Add(_entries[i].Value); |
| | 20 | 485 | | count++; |
| | | 486 | | } |
| | | 487 | | } |
| | 1 | 488 | | } |
| | | 489 | | |
| | | 490 | | /// <summary> |
| | | 491 | | /// Determines whether the <see cref="SwiftGenerationalBucket{T}"/> contains an element that matches the conditions |
| | | 492 | | /// </summary> |
| | | 493 | | /// <param name="match">The predicate that defines the conditions of the element to search for.</param> |
| | | 494 | | /// <returns><c>true</c> if the <see cref="SwiftGenerationalBucket{T}"/> contains one or more elements that match th |
| | | 495 | | public bool Exists(Predicate<T> match) |
| | | 496 | | { |
| | 1 | 497 | | SwiftThrowHelper.ThrowIfNull(match, nameof(match)); |
| | | 498 | | |
| | 1 | 499 | | uint count = 0; |
| | 1 | 500 | | uint peak = (uint)_peak; |
| | | 501 | | |
| | 4 | 502 | | for (uint i = 0; i < peak && count < (uint)_count; i++) |
| | | 503 | | { |
| | 2 | 504 | | if (_entries[i].IsUsed) |
| | | 505 | | { |
| | 2 | 506 | | if (match(_entries[i].Value)) |
| | 1 | 507 | | return true; |
| | | 508 | | |
| | 1 | 509 | | count++; |
| | | 510 | | } |
| | | 511 | | } |
| | | 512 | | |
| | 0 | 513 | | return false; |
| | | 514 | | } |
| | | 515 | | |
| | | 516 | | /// <summary> |
| | | 517 | | /// Searches for an element that matches the conditions defined by the specified predicate, and returns the first ma |
| | | 518 | | /// </summary> |
| | | 519 | | /// <param name="match">The predicate that defines the conditions of the element to search for.</param> |
| | | 520 | | /// <returns>The first element that matches the conditions defined by the specified predicate, if found; otherwise, |
| | | 521 | | public T Find(Predicate<T> match) |
| | | 522 | | { |
| | 2 | 523 | | SwiftThrowHelper.ThrowIfNull(match, nameof(match)); |
| | | 524 | | |
| | 2 | 525 | | uint count = 0; |
| | 2 | 526 | | uint peak = (uint)_peak; |
| | | 527 | | |
| | 10 | 528 | | for (uint i = 0; i < peak && count < (uint)_count; i++) |
| | | 529 | | { |
| | 4 | 530 | | if (_entries[i].IsUsed) |
| | | 531 | | { |
| | 4 | 532 | | T item = _entries[i].Value; |
| | 4 | 533 | | if (match(item)) |
| | 1 | 534 | | return item; |
| | | 535 | | |
| | 3 | 536 | | count++; |
| | | 537 | | } |
| | | 538 | | } |
| | | 539 | | |
| | 1 | 540 | | return default!; |
| | | 541 | | } |
| | | 542 | | |
| | | 543 | | #endregion |
| | | 544 | | |
| | | 545 | | #region Enumeration |
| | | 546 | | |
| | | 547 | | /// <inheritdoc cref="IEnumerable.GetEnumerator()"/> |
| | 6 | 548 | | public SwiftGenerationalBucketEnumerator GetEnumerator() => new(this); |
| | 1 | 549 | | IEnumerator<T> IEnumerable<T>.GetEnumerator() => GetEnumerator(); |
| | 1 | 550 | | IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
| | | 551 | | |
| | | 552 | | /// <summary> |
| | | 553 | | /// Enumerates the elements of a <see cref="SwiftGenerationalBucket{T}"/> collection in a forward-only, read-only ma |
| | | 554 | | /// </summary> |
| | | 555 | | /// <remarks> |
| | | 556 | | /// The enumerator is invalidated if the underlying collection is modified during enumeration. |
| | | 557 | | /// In such cases, subsequent calls to MoveNext will throw an InvalidOperationException. |
| | | 558 | | /// This enumerator is typically obtained by calling GetEnumerator on a <see cref="SwiftGenerationalBucket{T}"/> ins |
| | | 559 | | /// </remarks> |
| | | 560 | | public struct SwiftGenerationalBucketEnumerator : IEnumerator<T> |
| | | 561 | | { |
| | | 562 | | private readonly SwiftGenerationalBucket<T> _bucket; |
| | | 563 | | private readonly uint _version; |
| | | 564 | | private int _index; |
| | | 565 | | private T _current; |
| | | 566 | | |
| | | 567 | | internal SwiftGenerationalBucketEnumerator(SwiftGenerationalBucket<T> bucket) |
| | | 568 | | { |
| | 6 | 569 | | _bucket = bucket; |
| | 6 | 570 | | _version = bucket._version; |
| | 6 | 571 | | _index = -1; |
| | 6 | 572 | | _current = default!; |
| | 6 | 573 | | } |
| | | 574 | | |
| | | 575 | | /// <inheritdoc/> |
| | 103 | 576 | | public readonly T Current => _current; |
| | | 577 | | |
| | 1 | 578 | | readonly object IEnumerator.Current => _current ?? throw new InvalidOperationException(); |
| | | 579 | | |
| | | 580 | | /// <inheritdoc/> |
| | | 581 | | public bool MoveNext() |
| | | 582 | | { |
| | 110 | 583 | | SwiftThrowHelper.ThrowIfTrue(_version != _bucket._version, message: "Collection modified"); |
| | | 584 | | |
| | 109 | 585 | | uint peak = (uint)_bucket._peak; |
| | 109 | 586 | | while (++_index < peak) |
| | | 587 | | { |
| | 106 | 588 | | if (_bucket._entries[_index].IsUsed) |
| | | 589 | | { |
| | 106 | 590 | | _current = _bucket._entries[_index].Value; |
| | 106 | 591 | | return true; |
| | | 592 | | } |
| | | 593 | | } |
| | | 594 | | |
| | 3 | 595 | | return false; |
| | | 596 | | } |
| | | 597 | | |
| | | 598 | | /// <inheritdoc/> |
| | | 599 | | public void Reset() |
| | | 600 | | { |
| | 1 | 601 | | _index = -1; |
| | 1 | 602 | | _current = default!; |
| | 1 | 603 | | } |
| | | 604 | | |
| | | 605 | | /// <inheritdoc/> |
| | 3 | 606 | | public void Dispose() => _index = -1; |
| | | 607 | | } |
| | | 608 | | |
| | | 609 | | #endregion |
| | | 610 | | } |