| | | 1 | | //======================================================================= |
| | | 2 | | // SwiftQueue.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 | | /// <c>SwiftQueue<T></c> is a high-performance, circular buffer-based queue designed for ultra-low-latency enqueue |
| | | 22 | | /// <para> |
| | | 23 | | /// It leverages power-of-two capacities and bitwise arithmetic to eliminate expensive modulo operations, enhancing perf |
| | | 24 | | /// By managing memory efficiently with a wrap-around technique and custom capacity growth strategies, SwiftQueue minimi |
| | | 25 | | /// Aggressive inlining and optimized exception handling further reduce overhead, making SwiftQueue outperform tradition |
| | | 26 | | /// especially in scenarios with high-frequency additions and removals. |
| | | 27 | | /// </para> |
| | | 28 | | /// </summary> |
| | | 29 | | /// <typeparam name="T">Specifies the type of elements in the queue.</typeparam> |
| | | 30 | | [Serializable] |
| | | 31 | | [JsonConverter(typeof(StateJsonConverterFactory))] |
| | | 32 | | [MemoryPackable] |
| | | 33 | | public sealed partial class SwiftQueue<T> : IStateBacked<SwiftArrayState<T>>, ISwiftCloneable<T>, IEnumerable<T>, IEnume |
| | | 34 | | { |
| | | 35 | | #region Constants |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// The default initial capacity of the SwiftQueue if none is specified. |
| | | 39 | | /// Used to allocate a reasonable starting size to minimize resizing operations. |
| | | 40 | | /// </summary> |
| | | 41 | | public const int DefaultCapacity = 8; |
| | | 42 | | |
| | 2 | 43 | | private static readonly T[] _emptyArray = Array.Empty<T>(); |
| | 2 | 44 | | private static readonly bool _clearReleasedSlots = RuntimeHelpers.IsReferenceOrContainsReferences<T>(); |
| | | 45 | | |
| | | 46 | | #endregion |
| | | 47 | | |
| | | 48 | | #region Fields |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// The internal array that stores elements of the SwiftQueue. Resized as needed to |
| | | 52 | | /// accommodate additional elements. Not directly exposed outside the queue. |
| | | 53 | | /// </summary> |
| | | 54 | | private T[] _innerArray; |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// The current number of elements in the SwiftQueue. Represents the total count of |
| | | 58 | | /// valid elements stored in the queue, also indicating the arrayIndex of the next insertion point. |
| | | 59 | | /// </summary> |
| | | 60 | | private int _count; |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// The arrayIndex of the first element in the queue. Adjusts as elements are dequeued. |
| | | 64 | | /// </summary> |
| | | 65 | | private int _head; |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// The arrayIndex at which the next element will be enqueued, wrapping around as needed. |
| | | 69 | | /// </summary> |
| | | 70 | | private int _tail; |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// A bitmask used for efficient modulo operations, derived from the capacity of the internal array. |
| | | 74 | | /// </summary> |
| | | 75 | | private int _mask; |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// A version number used to track modifications to the SwiftQueue to help detect changes during enumeration and ens |
| | | 79 | | /// </summary> |
| | | 80 | | [NonSerialized] |
| | | 81 | | private uint _version; |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// An object used to synchronize access to the SwiftQueue, ensuring thread safety. |
| | | 85 | | /// </summary> |
| | | 86 | | [NonSerialized] |
| | | 87 | | private object? _syncRoot; |
| | | 88 | | |
| | | 89 | | #endregion |
| | | 90 | | |
| | | 91 | | #region Constructors |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// Initializes a new, empty instance of SwiftQueue. |
| | | 95 | | /// </summary> |
| | 90 | 96 | | public SwiftQueue() : this(0) { } |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// Initializes a new, empty instance of SwiftQueue with the specified initial capacity. |
| | | 100 | | /// </summary> |
| | 65 | 101 | | public SwiftQueue(int capacity) |
| | | 102 | | { |
| | 65 | 103 | | if (capacity == 0) |
| | | 104 | | { |
| | 45 | 105 | | _innerArray = _emptyArray; |
| | 45 | 106 | | _mask = 0; |
| | | 107 | | } |
| | | 108 | | else |
| | | 109 | | { |
| | 20 | 110 | | capacity = capacity < DefaultCapacity ? DefaultCapacity : SwiftHashTools.NextPowerOfTwo(capacity); |
| | 20 | 111 | | _innerArray = new T[capacity]; |
| | 20 | 112 | | _mask = _innerArray.Length - 1; |
| | | 113 | | } |
| | 20 | 114 | | } |
| | | 115 | | |
| | | 116 | | /// <summary> |
| | | 117 | | /// Initializes a new instance of SwiftQueue that contains elements copied from the provided items. |
| | | 118 | | /// </summary> |
| | 3 | 119 | | public SwiftQueue(IEnumerable<T> items) |
| | | 120 | | { |
| | 3 | 121 | | SwiftThrowHelper.ThrowIfNull(items, nameof(items)); |
| | 3 | 122 | | _innerArray = _emptyArray; |
| | | 123 | | |
| | 3 | 124 | | if (items is ICollection<T> collection) |
| | | 125 | | { |
| | 2 | 126 | | InitializeFromKnownCountRange(collection, collection.Count); |
| | 2 | 127 | | return; |
| | | 128 | | } |
| | | 129 | | |
| | 1 | 130 | | if (items is IReadOnlyCollection<T> readOnlyCollection) |
| | | 131 | | { |
| | 0 | 132 | | InitializeFromKnownCountRange(readOnlyCollection, readOnlyCollection.Count); |
| | 0 | 133 | | return; |
| | | 134 | | } |
| | | 135 | | |
| | 1 | 136 | | _innerArray = new T[DefaultCapacity]; |
| | 1 | 137 | | _mask = _innerArray.Length - 1; |
| | | 138 | | |
| | 8 | 139 | | foreach (T item in items) |
| | 3 | 140 | | Enqueue(item); |
| | 1 | 141 | | } |
| | | 142 | | |
| | | 143 | | private void InitializeFromKnownCountRange(IEnumerable<T> items, int count) |
| | | 144 | | { |
| | 2 | 145 | | if (count == 0) |
| | | 146 | | { |
| | 0 | 147 | | _innerArray = _emptyArray; |
| | 0 | 148 | | _mask = 0; |
| | 0 | 149 | | return; |
| | | 150 | | } |
| | | 151 | | |
| | 2 | 152 | | int capacity = count < DefaultCapacity ? DefaultCapacity : SwiftHashTools.NextPowerOfTwo(count); |
| | 2 | 153 | | _innerArray = new T[capacity]; |
| | 2 | 154 | | _mask = _innerArray.Length - 1; |
| | | 155 | | |
| | 28 | 156 | | foreach (T item in items) |
| | 12 | 157 | | _innerArray[_count++] = item; |
| | | 158 | | |
| | 2 | 159 | | _tail = _count & _mask; |
| | 2 | 160 | | } |
| | | 161 | | |
| | | 162 | | /// <summary> |
| | | 163 | | /// Initializes a new instance of the <see cref="SwiftQueue{T}"/> class with the specified <see cref="SwiftArraySta |
| | | 164 | | /// </summary> |
| | | 165 | | /// <param name="state">The state containing the internal array, count, offset, and version for initialization.</pa |
| | | 166 | | [MemoryPackConstructor] |
| | 5 | 167 | | public SwiftQueue(SwiftArrayState<T> state) |
| | | 168 | | { |
| | 5 | 169 | | State = state; |
| | | 170 | | |
| | 5 | 171 | | SwiftThrowHelper.ThrowIfNull(_innerArray, nameof(_innerArray)); |
| | 5 | 172 | | } |
| | | 173 | | |
| | | 174 | | #endregion |
| | | 175 | | |
| | | 176 | | #region Properties |
| | | 177 | | |
| | | 178 | | /// <inheritdoc cref="_innerArray"/> |
| | | 179 | | [JsonIgnore] |
| | | 180 | | [MemoryPackIgnore] |
| | 5 | 181 | | public T[] InnerArray => _innerArray; |
| | | 182 | | |
| | | 183 | | /// <inheritdoc cref="_count"/> |
| | | 184 | | [JsonIgnore] |
| | | 185 | | [MemoryPackIgnore] |
| | 129 | 186 | | public int Count => _count; |
| | | 187 | | |
| | | 188 | | /// <summary> |
| | | 189 | | /// Gets the total number of elements the SwiftQueue can hold without resizing. |
| | | 190 | | /// Reflects the current allocated size of the internal array. |
| | | 191 | | /// </summary> |
| | | 192 | | [JsonIgnore] |
| | | 193 | | [MemoryPackIgnore] |
| | 15 | 194 | | public int Capacity => _innerArray.Length; |
| | | 195 | | |
| | | 196 | | /// <inheritdoc/> |
| | | 197 | | [JsonIgnore] |
| | | 198 | | [MemoryPackIgnore] |
| | 1 | 199 | | public bool IsSynchronized => false; |
| | | 200 | | |
| | | 201 | | /// <inheritdoc/> |
| | | 202 | | [JsonIgnore] |
| | | 203 | | [MemoryPackIgnore] |
| | 1 | 204 | | public object SyncRoot => _syncRoot ??= new object(); |
| | | 205 | | |
| | | 206 | | /// <inheritdoc/> |
| | | 207 | | [JsonIgnore] |
| | | 208 | | [MemoryPackIgnore] |
| | 1 | 209 | | public bool IsReadOnly => false; |
| | | 210 | | |
| | | 211 | | /// <summary> |
| | | 212 | | /// Gets the element at the specified arrayIndex. |
| | | 213 | | /// </summary> |
| | | 214 | | [JsonIgnore] |
| | | 215 | | [MemoryPackIgnore] |
| | | 216 | | public T this[int index] |
| | | 217 | | { |
| | | 218 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 219 | | get |
| | | 220 | | { |
| | 201 | 221 | | SwiftThrowHelper.ThrowIfListIndexInvalid(index, _count); |
| | 200 | 222 | | return _innerArray[(_head + index) & _mask]; |
| | | 223 | | } |
| | | 224 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 225 | | set |
| | | 226 | | { |
| | 2 | 227 | | SwiftThrowHelper.ThrowIfListIndexInvalid(index, _count); |
| | 1 | 228 | | _innerArray[(_head + index) & _mask] = value; |
| | 1 | 229 | | } |
| | | 230 | | } |
| | | 231 | | |
| | | 232 | | /// <summary> |
| | | 233 | | /// Gets or sets the current state of the collection, including its items and order. |
| | | 234 | | /// </summary> |
| | | 235 | | /// <remarks> |
| | | 236 | | /// Setting this property replaces the entire contents of the collection with the items from the specified state. |
| | | 237 | | /// Getting this property returns a snapshot of the collection's current items and their order. |
| | | 238 | | /// This property is intended for serialization and deserialization scenarios. |
| | | 239 | | /// </remarks> |
| | | 240 | | [JsonInclude] |
| | | 241 | | [MemoryPackInclude] |
| | | 242 | | public SwiftArrayState<T> State |
| | | 243 | | { |
| | | 244 | | get |
| | | 245 | | { |
| | 2 | 246 | | var items = new T[_count]; |
| | | 247 | | |
| | 404 | 248 | | for (int i = 0; i < _count; i++) |
| | 200 | 249 | | items[i] = _innerArray[(_head + i) & _mask]; |
| | | 250 | | |
| | 2 | 251 | | return new SwiftArrayState<T>(items); |
| | | 252 | | } |
| | | 253 | | internal set |
| | | 254 | | { |
| | 5 | 255 | | SwiftThrowHelper.ThrowIfNull(value.Items, nameof(value.Items)); |
| | | 256 | | |
| | 5 | 257 | | int count = value.Items.Length; |
| | | 258 | | |
| | 5 | 259 | | if (count == 0) |
| | | 260 | | { |
| | 1 | 261 | | _innerArray = _emptyArray; |
| | 1 | 262 | | _count = 0; |
| | 1 | 263 | | _head = 0; |
| | 1 | 264 | | _tail = 0; |
| | 1 | 265 | | _mask = 0; |
| | 1 | 266 | | _version = 0; |
| | 1 | 267 | | return; |
| | | 268 | | } |
| | | 269 | | |
| | 4 | 270 | | int capacity = SwiftHashTools.NextPowerOfTwo(count <= DefaultCapacity ? DefaultCapacity : count); |
| | | 271 | | |
| | 4 | 272 | | _innerArray = new T[capacity]; |
| | 4 | 273 | | Array.Copy(value.Items, 0, _innerArray, 0, count); |
| | | 274 | | |
| | 4 | 275 | | _count = count; |
| | 4 | 276 | | _head = 0; |
| | 4 | 277 | | _tail = count; |
| | 4 | 278 | | _mask = capacity - 1; |
| | | 279 | | |
| | 4 | 280 | | _version = 0; |
| | 4 | 281 | | } |
| | | 282 | | } |
| | | 283 | | |
| | | 284 | | #endregion |
| | | 285 | | |
| | | 286 | | #region Collection Management |
| | | 287 | | |
| | | 288 | | /// <inheritdoc/> |
| | 1 | 289 | | void ICollection<T>.Add(T item) => Enqueue(item); |
| | | 290 | | |
| | | 291 | | /// <summary> |
| | | 292 | | /// Adds an item to the end of the queue. Automatically resizes the queue if the capacity is exceeded. |
| | | 293 | | /// </summary> |
| | | 294 | | /// <param name="item">The item to add to the queue.</param> |
| | | 295 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 296 | | public void Enqueue(T item) |
| | | 297 | | { |
| | 318 | 298 | | if ((uint)_count >= (uint)_innerArray.Length) |
| | 34 | 299 | | Resize(_innerArray.Length * 2); |
| | 318 | 300 | | _innerArray[_tail] = item; |
| | 318 | 301 | | _tail = (_tail + 1) & _mask; |
| | 318 | 302 | | _count++; |
| | 318 | 303 | | _version++; |
| | 318 | 304 | | } |
| | | 305 | | |
| | | 306 | | /// <summary> |
| | | 307 | | /// Adds the elements of the specified collection to the end of the queue. |
| | | 308 | | /// </summary> |
| | | 309 | | /// <remarks> |
| | | 310 | | /// Known-count sources reserve capacity before enumeration to avoid repeated growth. |
| | | 311 | | /// </remarks> |
| | | 312 | | /// <param name="items">The collection of elements to add to the queue. Cannot be null.</param> |
| | | 313 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 314 | | public void EnqueueRange(IEnumerable<T> items) |
| | | 315 | | { |
| | 4 | 316 | | SwiftThrowHelper.ThrowIfNull(items, nameof(items)); |
| | | 317 | | |
| | 4 | 318 | | if (items is ICollection<T> collection) |
| | | 319 | | { |
| | 1 | 320 | | EnqueueKnownCountRange(collection, collection.Count); |
| | 1 | 321 | | return; |
| | | 322 | | } |
| | | 323 | | |
| | 3 | 324 | | if (items is IReadOnlyCollection<T> readOnlyCollection) |
| | | 325 | | { |
| | 1 | 326 | | EnqueueKnownCountRange(readOnlyCollection, readOnlyCollection.Count); |
| | 1 | 327 | | return; |
| | | 328 | | } |
| | | 329 | | |
| | 16 | 330 | | foreach (T item in items) |
| | 6 | 331 | | Enqueue(item); |
| | 2 | 332 | | } |
| | | 333 | | |
| | | 334 | | private void EnqueueKnownCountRange(IEnumerable<T> items, int count) |
| | | 335 | | { |
| | 2 | 336 | | if (count == 0) |
| | 0 | 337 | | return; |
| | | 338 | | |
| | 2 | 339 | | EnsureAdditionalCapacity(count); |
| | | 340 | | |
| | 2 | 341 | | int appendedCount = 0; |
| | 28 | 342 | | foreach (T item in items) |
| | | 343 | | { |
| | 12 | 344 | | _innerArray[_tail] = item; |
| | 12 | 345 | | _tail = (_tail + 1) & _mask; |
| | 12 | 346 | | appendedCount++; |
| | | 347 | | } |
| | | 348 | | |
| | 2 | 349 | | _count += appendedCount; |
| | 2 | 350 | | _version++; |
| | 2 | 351 | | } |
| | | 352 | | |
| | | 353 | | /// <summary> |
| | | 354 | | /// Adds the elements of the specified array to the end of the queue in queue order. |
| | | 355 | | /// </summary> |
| | | 356 | | /// <param name="items">The array whose elements should be enqueued.</param> |
| | | 357 | | public void EnqueueRange(T[] items) |
| | | 358 | | { |
| | 7 | 359 | | SwiftThrowHelper.ThrowIfNull(items, nameof(items)); |
| | 7 | 360 | | EnqueueRange(items.AsSpan()); |
| | 7 | 361 | | } |
| | | 362 | | |
| | | 363 | | /// <summary> |
| | | 364 | | /// Adds the elements of the specified span to the end of the queue in queue order. |
| | | 365 | | /// </summary> |
| | | 366 | | /// <param name="items">The span whose elements should be enqueued.</param> |
| | | 367 | | public void EnqueueRange(ReadOnlySpan<T> items) |
| | | 368 | | { |
| | 30 | 369 | | if (items.Length == 0) |
| | 1 | 370 | | return; |
| | | 371 | | |
| | 29 | 372 | | EnsureAdditionalCapacity(items.Length); |
| | | 373 | | |
| | 316 | 374 | | for (int i = 0; i < items.Length; i++) |
| | | 375 | | { |
| | 129 | 376 | | _innerArray[_tail] = items[i]; |
| | 129 | 377 | | _tail = (_tail + 1) & _mask; |
| | | 378 | | } |
| | | 379 | | |
| | 29 | 380 | | _count += items.Length; |
| | 29 | 381 | | _version++; |
| | 29 | 382 | | } |
| | | 383 | | |
| | | 384 | | private void EnsureAdditionalCapacity(int additionalCount) |
| | | 385 | | { |
| | 31 | 386 | | long requiredCount = (long)_count + additionalCount; |
| | 31 | 387 | | SwiftThrowHelper.ThrowIfTrue(requiredCount > int.MaxValue, message: "The collection is too large."); |
| | 31 | 388 | | EnsureCapacity((int)requiredCount); |
| | 31 | 389 | | } |
| | | 390 | | |
| | | 391 | | /// <summary> |
| | | 392 | | /// Removes and returns the item at the front of the queue. |
| | | 393 | | /// Throws an InvalidOperationException if the queue is empty. |
| | | 394 | | /// </summary> |
| | | 395 | | /// <returns>The item at the front of the queue.</returns> |
| | | 396 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 397 | | public T Dequeue() |
| | | 398 | | { |
| | 83 | 399 | | SwiftThrowHelper.ThrowIfTrue((uint)_count == 0, message: "Queue is Empty"); |
| | 82 | 400 | | T item = _innerArray[_head]; |
| | 82 | 401 | | if (_clearReleasedSlots) |
| | 5 | 402 | | _innerArray[_head] = default!; |
| | 82 | 403 | | _head = (_head + 1) & _mask; |
| | 82 | 404 | | _count--; |
| | 82 | 405 | | _version++; |
| | 82 | 406 | | return item; |
| | | 407 | | } |
| | | 408 | | |
| | | 409 | | /// <summary> |
| | | 410 | | /// Tries to remove and return the item at the front of the queue. |
| | | 411 | | /// </summary> |
| | | 412 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 413 | | public bool TryDequeue(out T item) |
| | | 414 | | { |
| | 3 | 415 | | if ((uint)_count == 0) |
| | | 416 | | { |
| | 1 | 417 | | item = default!; |
| | 1 | 418 | | return false; |
| | | 419 | | } |
| | | 420 | | |
| | 2 | 421 | | item = _innerArray[_head]; |
| | 2 | 422 | | if (_clearReleasedSlots) |
| | 1 | 423 | | _innerArray[_head] = default!; |
| | 2 | 424 | | _head = (_head + 1) & _mask; |
| | 2 | 425 | | _count--; |
| | 2 | 426 | | _version++; |
| | 2 | 427 | | return true; |
| | | 428 | | } |
| | | 429 | | |
| | 1 | 430 | | bool ICollection<T>.Remove(T item) => throw new NotSupportedException("Remove is not supported for SwiftQueue."); |
| | | 431 | | |
| | | 432 | | /// <summary> |
| | | 433 | | /// Returns the item at the front of the queue without removing it. |
| | | 434 | | /// Throws an InvalidOperationException if the queue is empty. |
| | | 435 | | /// </summary> |
| | | 436 | | /// <returns>The item at the front of the queue.</returns> |
| | | 437 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 438 | | public T Peek() |
| | | 439 | | { |
| | 5 | 440 | | SwiftThrowHelper.ThrowIfTrue((uint)_count == 0, message: "Queue is Empty"); |
| | 3 | 441 | | return _innerArray[_head]; |
| | | 442 | | } |
| | | 443 | | |
| | | 444 | | /// <summary> |
| | | 445 | | /// Tries to return the item at the front of the queue without removing it. |
| | | 446 | | /// </summary> |
| | | 447 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 448 | | public bool TryPeek(out T item) |
| | | 449 | | { |
| | 2 | 450 | | if ((uint)_count == 0) |
| | | 451 | | { |
| | 1 | 452 | | item = default!; |
| | 1 | 453 | | return false; |
| | | 454 | | } |
| | | 455 | | |
| | 1 | 456 | | item = _innerArray[_head]; |
| | 1 | 457 | | return true; |
| | | 458 | | } |
| | | 459 | | |
| | | 460 | | /// <summary> |
| | | 461 | | /// Returns the item at the end of the queue without removing it. |
| | | 462 | | /// Throws an InvalidOperationException if the queue is empty. |
| | | 463 | | /// </summary> |
| | | 464 | | /// <returns>The item at the end of the queue.</returns> |
| | | 465 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 466 | | public T PeekTail() |
| | | 467 | | { |
| | 4 | 468 | | SwiftThrowHelper.ThrowIfTrue((uint)_count == 0, message: "Queue is Empty"); |
| | 3 | 469 | | int tailIndex = (_tail - 1) & _mask; |
| | 3 | 470 | | return _innerArray[tailIndex]; |
| | | 471 | | } |
| | | 472 | | |
| | | 473 | | /// <inheritdoc/> |
| | | 474 | | public bool Contains(T item) |
| | | 475 | | { |
| | 4 | 476 | | if ((uint)_count == 0) return false; |
| | | 477 | | |
| | 2 | 478 | | int index = _head; |
| | 20 | 479 | | for (int i = 0; i < _count; i++) |
| | | 480 | | { |
| | 9 | 481 | | if (Equals(_innerArray[index], item)) |
| | 1 | 482 | | return true; |
| | | 483 | | |
| | 8 | 484 | | index = (index + 1) & _mask; |
| | | 485 | | } |
| | | 486 | | |
| | 1 | 487 | | return false; |
| | | 488 | | } |
| | | 489 | | |
| | | 490 | | /// <summary> |
| | | 491 | | /// Determines whether the <see cref="SwiftQueue{T}"/> contains an element that matches the conditions defined by th |
| | | 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="SwiftQueue{T}"/> contains one or more elements that match the specified p |
| | | 495 | | public bool Exists(Predicate<T> match) |
| | | 496 | | { |
| | 3 | 497 | | SwiftThrowHelper.ThrowIfNull(match, nameof(match)); |
| | | 498 | | |
| | 2 | 499 | | int index = _head; |
| | 20 | 500 | | for (int i = 0; i < _count; i++) |
| | | 501 | | { |
| | 9 | 502 | | if (match(_innerArray[index])) |
| | 1 | 503 | | return true; |
| | | 504 | | |
| | 8 | 505 | | index = (index + 1) & _mask; |
| | | 506 | | } |
| | | 507 | | |
| | 1 | 508 | | return false; |
| | | 509 | | } |
| | | 510 | | |
| | | 511 | | /// <summary> |
| | | 512 | | /// Searches for an element that matches the conditions defined by the specified predicate, and returns the first ma |
| | | 513 | | /// </summary> |
| | | 514 | | /// <param name="match">The predicate that defines the conditions of the element to search for.</param> |
| | | 515 | | /// <returns>The first element that matches the conditions defined by the specified predicate, if found; otherwise, |
| | | 516 | | public T Find(Predicate<T> match) |
| | | 517 | | { |
| | 2 | 518 | | SwiftThrowHelper.ThrowIfNull(match, nameof(match)); |
| | | 519 | | |
| | 2 | 520 | | int index = _head; |
| | 22 | 521 | | for (int i = 0; i < _count; i++) |
| | | 522 | | { |
| | 10 | 523 | | T item = _innerArray[index]; |
| | 10 | 524 | | if (match(item)) |
| | 1 | 525 | | return item; |
| | | 526 | | |
| | 9 | 527 | | index = (index + 1) & _mask; |
| | | 528 | | } |
| | | 529 | | |
| | 1 | 530 | | return default!; |
| | | 531 | | } |
| | | 532 | | |
| | | 533 | | /// <summary> |
| | | 534 | | /// Removes all elements from the SwiftQueue, resetting its count to zero. |
| | | 535 | | /// </summary> |
| | | 536 | | public void Clear() |
| | | 537 | | { |
| | 8 | 538 | | if (_count == 0) return; |
| | | 539 | | |
| | 4 | 540 | | if (_clearReleasedSlots) |
| | | 541 | | { |
| | 2 | 542 | | if ((uint)_head < (uint)_tail) |
| | 1 | 543 | | Array.Clear(_innerArray, _head, _count); |
| | | 544 | | else |
| | | 545 | | { |
| | 1 | 546 | | Array.Clear(_innerArray, _head, _innerArray.Length - _head); |
| | 1 | 547 | | Array.Clear(_innerArray, 0, _tail); |
| | | 548 | | } |
| | | 549 | | } |
| | | 550 | | |
| | 4 | 551 | | _count = 0; |
| | 4 | 552 | | _head = 0; |
| | 4 | 553 | | _tail = 0; |
| | 4 | 554 | | _version++; |
| | 4 | 555 | | } |
| | | 556 | | |
| | | 557 | | /// <summary> |
| | | 558 | | /// Clears the SwiftQueue without releasing the reference to the stored elements. |
| | | 559 | | /// Use FastClear() when you want to quickly reset the list without reallocating memory. |
| | | 560 | | /// </summary> |
| | | 561 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 562 | | public void FastClear() |
| | | 563 | | { |
| | 1 | 564 | | _count = 0; |
| | 1 | 565 | | _tail = 0; |
| | 1 | 566 | | _head = 0; |
| | 1 | 567 | | _version++; |
| | 1 | 568 | | } |
| | | 569 | | |
| | | 570 | | #endregion |
| | | 571 | | |
| | | 572 | | #region Capacity Management |
| | | 573 | | |
| | | 574 | | /// <summary> |
| | | 575 | | /// Ensures that the internal storage has at least the specified capacity, resizing if necessary. |
| | | 576 | | /// </summary> |
| | | 577 | | /// <remarks> |
| | | 578 | | /// If the specified capacity is not a power of two, it is rounded up to the next power of two to optimize internal |
| | | 579 | | /// </remarks> |
| | | 580 | | /// <param name="capacity">The minimum number of elements that the internal storage should be able to hold. Must be |
| | | 581 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 582 | | public void EnsureCapacity(int capacity) |
| | | 583 | | { |
| | 33 | 584 | | capacity = SwiftHashTools.NextPowerOfTwo(capacity); // Capacity must be a power of 2 for proper masking |
| | 33 | 585 | | if (capacity > _innerArray.Length) |
| | 9 | 586 | | Resize(capacity); |
| | 33 | 587 | | } |
| | | 588 | | |
| | | 589 | | /// <summary> |
| | | 590 | | /// Ensures that the capacity of the queue is sufficient to accommodate the specified number of elements. |
| | | 591 | | /// The capacity increases to the next power of two greater than or equal to the required minimum capacity. |
| | | 592 | | /// </summary> |
| | | 593 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 594 | | private void Resize(int newSize) |
| | | 595 | | { |
| | 43 | 596 | | var newArray = new T[newSize <= DefaultCapacity ? DefaultCapacity : newSize]; |
| | 43 | 597 | | if ((uint)_count > 0) |
| | | 598 | | { |
| | | 599 | | // If we are not wrapped around... |
| | 10 | 600 | | if ((uint)_head < (uint)_tail) |
| | | 601 | | { |
| | | 602 | | // ...copy from head to tail into new array starting at arrayIndex 0 |
| | 1 | 603 | | Array.Copy(_innerArray, _head, newArray, 0, _count); |
| | | 604 | | } |
| | | 605 | | // Else if we are wrapped around... |
| | | 606 | | else |
| | | 607 | | { |
| | | 608 | | // ...copy from head to end of old array to beginning of new array |
| | 9 | 609 | | Array.Copy(_innerArray, _head, newArray, 0, _innerArray.Length - _head); |
| | | 610 | | // ...copy from start of old array to tail into new array |
| | 9 | 611 | | Array.Copy(_innerArray, 0, newArray, _innerArray.Length - _head, _tail); |
| | | 612 | | } |
| | | 613 | | } |
| | | 614 | | |
| | 43 | 615 | | _innerArray = newArray; |
| | 43 | 616 | | _mask = _innerArray.Length - 1; |
| | 43 | 617 | | _head = 0; |
| | 43 | 618 | | _tail = _count & _mask; |
| | 43 | 619 | | _version++; |
| | 43 | 620 | | } |
| | | 621 | | |
| | | 622 | | /// <summary> |
| | | 623 | | /// Reduces the capacity of the SwiftQueue if the element count is significantly less than the current capacity. |
| | | 624 | | /// This method resizes the internal array to the next power of two greater than or equal to the current count, |
| | | 625 | | /// optimizing memory usage. |
| | | 626 | | /// </summary> |
| | | 627 | | public void TrimExcessCapacity() |
| | | 628 | | { |
| | 5 | 629 | | int newSize = _count < DefaultCapacity ? DefaultCapacity : SwiftHashTools.NextPowerOfTwo(_count); |
| | 6 | 630 | | if (newSize >= _innerArray.Length) return; |
| | | 631 | | |
| | 4 | 632 | | var newArray = new T[newSize]; |
| | | 633 | | |
| | 4 | 634 | | if ((uint)_count != 0) |
| | | 635 | | { |
| | 3 | 636 | | if ((uint)_head < (uint)_tail) |
| | | 637 | | { |
| | | 638 | | // No wrap-around, simple copy |
| | 2 | 639 | | Array.Copy(_innerArray, _head, newArray, 0, _count); |
| | | 640 | | } |
| | | 641 | | else |
| | | 642 | | { |
| | | 643 | | // Wrap-around, copy in two parts |
| | 1 | 644 | | Array.Copy(_innerArray, _head, newArray, 0, _innerArray.Length - _head); |
| | 1 | 645 | | Array.Copy(_innerArray, 0, newArray, _innerArray.Length - _head, _tail); |
| | | 646 | | } |
| | | 647 | | } |
| | | 648 | | |
| | 4 | 649 | | _innerArray = newArray; |
| | 4 | 650 | | _mask = _innerArray.Length - 1; |
| | 4 | 651 | | _head = 0; |
| | 4 | 652 | | _tail = _count & _mask; |
| | 4 | 653 | | _version++; |
| | 4 | 654 | | } |
| | | 655 | | |
| | | 656 | | #endregion |
| | | 657 | | |
| | | 658 | | #region Utility Methods |
| | | 659 | | |
| | | 660 | | /// <summary> |
| | | 661 | | /// Copies the elements of the SwiftQueue to a new array. |
| | | 662 | | /// </summary> |
| | | 663 | | public T[] ToArray() |
| | | 664 | | { |
| | 20 | 665 | | var result = new T[_count]; |
| | 21 | 666 | | if ((uint)_count == 0) return result; |
| | 19 | 667 | | if ((uint)_head < (uint)_tail) |
| | 17 | 668 | | Array.Copy(_innerArray, _head, result, 0, _count); |
| | | 669 | | else |
| | | 670 | | { |
| | 2 | 671 | | int firstPartLength = _innerArray.Length - _head; |
| | 2 | 672 | | Array.Copy(_innerArray, _head, result, 0, firstPartLength); |
| | 2 | 673 | | Array.Copy(_innerArray, 0, result, firstPartLength, _tail); |
| | | 674 | | } |
| | 19 | 675 | | return result; |
| | | 676 | | } |
| | | 677 | | |
| | | 678 | | /// <summary> |
| | | 679 | | /// Returns the current queue contents as up to two read-only spans. |
| | | 680 | | /// </summary> |
| | | 681 | | /// <param name="first">The first contiguous queue segment.</param> |
| | | 682 | | /// <param name="second">The wrapped tail segment, if any.</param> |
| | | 683 | | public void GetSegments(out ReadOnlySpan<T> first, out ReadOnlySpan<T> second) |
| | | 684 | | { |
| | 4 | 685 | | if ((uint)_count == 0) |
| | | 686 | | { |
| | 1 | 687 | | first = ReadOnlySpan<T>.Empty; |
| | 1 | 688 | | second = ReadOnlySpan<T>.Empty; |
| | 1 | 689 | | return; |
| | | 690 | | } |
| | | 691 | | |
| | 3 | 692 | | if ((uint)_head < (uint)_tail) |
| | | 693 | | { |
| | 1 | 694 | | first = _innerArray.AsSpan(_head, _count); |
| | 1 | 695 | | second = ReadOnlySpan<T>.Empty; |
| | 1 | 696 | | return; |
| | | 697 | | } |
| | | 698 | | |
| | 2 | 699 | | int firstPartLength = _innerArray.Length - _head; |
| | 2 | 700 | | first = _innerArray.AsSpan(_head, firstPartLength); |
| | 2 | 701 | | second = _innerArray.AsSpan(0, _tail); |
| | 2 | 702 | | } |
| | | 703 | | |
| | | 704 | | /// <inheritdoc/> |
| | | 705 | | public void CopyTo(Array array, int arrayIndex) |
| | | 706 | | { |
| | 6 | 707 | | SwiftThrowHelper.ThrowIfNull(array, nameof(array)); |
| | 6 | 708 | | SwiftThrowHelper.ThrowIfArgument((uint)array.Rank != 1, nameof(array), "Array must be single dimensional."); |
| | 5 | 709 | | SwiftThrowHelper.ThrowIfArgument((uint)array.GetLowerBound(0) != 0, nameof(array), "Array must have zero-based i |
| | 4 | 710 | | SwiftThrowHelper.ThrowIfArrayIndexInvalid(arrayIndex, array.Length); |
| | 4 | 711 | | SwiftThrowHelper.ThrowIfArgument((uint)(array.Length - arrayIndex) < _count, nameof(array), "Destination array i |
| | | 712 | | |
| | 4 | 713 | | if ((uint)_count == 0) |
| | 1 | 714 | | return; |
| | | 715 | | |
| | | 716 | | try |
| | | 717 | | { |
| | 3 | 718 | | CopyToInternal(array, arrayIndex); |
| | 2 | 719 | | } |
| | 1 | 720 | | catch (ArrayTypeMismatchException) |
| | | 721 | | { |
| | 1 | 722 | | throw new ArgumentException("Invalid array type.", nameof(array)); |
| | | 723 | | } |
| | 2 | 724 | | } |
| | | 725 | | |
| | | 726 | | /// <inheritdoc/> |
| | | 727 | | public void CopyTo(T[] array, int arrayIndex) |
| | | 728 | | { |
| | 3 | 729 | | SwiftThrowHelper.ThrowIfNull(array, nameof(array)); |
| | 3 | 730 | | SwiftThrowHelper.ThrowIfArgument((uint)array.Rank != 1, nameof(array), "Array must be single dimensional."); |
| | 3 | 731 | | SwiftThrowHelper.ThrowIfArgument((uint)array.GetLowerBound(0) != 0, nameof(array), "Array must have zero-based i |
| | 3 | 732 | | SwiftThrowHelper.ThrowIfArrayIndexInvalid(arrayIndex, array.Length); |
| | 3 | 733 | | SwiftThrowHelper.ThrowIfArgument((uint)(array.Length - arrayIndex) < _count, nameof(array), "Destination array i |
| | | 734 | | |
| | 3 | 735 | | if ((uint)_count == 0) return; |
| | | 736 | | |
| | 1 | 737 | | CopyToInternal(array, arrayIndex); |
| | 1 | 738 | | } |
| | | 739 | | |
| | | 740 | | /// <summary> |
| | | 741 | | /// Copies the elements of the SwiftQueue into the specified destination span. |
| | | 742 | | /// </summary> |
| | | 743 | | /// <param name="destination">The destination span.</param> |
| | | 744 | | public void CopyTo(Span<T> destination) |
| | | 745 | | { |
| | 2 | 746 | | SwiftThrowHelper.ThrowIfArgument((uint)destination.Length < _count, nameof(destination), "Destination span is no |
| | | 747 | | |
| | 1 | 748 | | GetSegments(out ReadOnlySpan<T> first, out ReadOnlySpan<T> second); |
| | 1 | 749 | | first.CopyTo(destination); |
| | | 750 | | |
| | 1 | 751 | | if (second.Length > 0) |
| | 1 | 752 | | second.CopyTo(destination[first.Length..]); |
| | 1 | 753 | | } |
| | | 754 | | |
| | | 755 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 756 | | private void CopyToInternal(Array destination, int arrayIndex) |
| | | 757 | | { |
| | 4 | 758 | | if ((uint)_head < (uint)_tail) |
| | | 759 | | { |
| | 2 | 760 | | Array.Copy(_innerArray, _head, destination, arrayIndex, _count); |
| | | 761 | | } |
| | | 762 | | else |
| | | 763 | | { |
| | 2 | 764 | | int firstPartLength = _innerArray.Length - _head; |
| | 2 | 765 | | Array.Copy(_innerArray, _head, destination, arrayIndex, firstPartLength); |
| | 2 | 766 | | Array.Copy(_innerArray, 0, destination, arrayIndex + firstPartLength, _tail); |
| | | 767 | | } |
| | 2 | 768 | | } |
| | | 769 | | |
| | | 770 | | /// <inheritdoc/> |
| | | 771 | | public void CloneTo(ICollection<T> output) |
| | | 772 | | { |
| | 1 | 773 | | SwiftThrowHelper.ThrowIfNull(output, nameof(output)); |
| | 1 | 774 | | output.Clear(); |
| | 12 | 775 | | foreach (var item in this) |
| | 5 | 776 | | output.Add(item); |
| | 1 | 777 | | } |
| | | 778 | | |
| | | 779 | | #endregion |
| | | 780 | | |
| | | 781 | | #region Enumerators |
| | | 782 | | |
| | | 783 | | /// <summary> |
| | | 784 | | /// Returns an enumerator that iterates through the SwiftList. |
| | | 785 | | /// </summary> |
| | 16 | 786 | | public SwiftQueueEnumerator GetEnumerator() => new(this); |
| | 10 | 787 | | IEnumerator<T> IEnumerable<T>.GetEnumerator() => GetEnumerator(); |
| | 2 | 788 | | IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
| | | 789 | | |
| | | 790 | | /// <summary> |
| | | 791 | | /// Enumerates the elements of a <see cref="SwiftQueue{T}"/> in the order they would be dequeued. |
| | | 792 | | /// </summary> |
| | | 793 | | public struct SwiftQueueEnumerator : IEnumerator<T>, IEnumerator, IDisposable |
| | | 794 | | { |
| | | 795 | | private readonly SwiftQueue<T> _queue; |
| | | 796 | | private readonly T[] _array; |
| | | 797 | | private readonly uint _version; |
| | | 798 | | private uint _index; |
| | | 799 | | private uint _currentIndex; |
| | | 800 | | |
| | | 801 | | private T _current; |
| | | 802 | | |
| | | 803 | | internal SwiftQueueEnumerator(SwiftQueue<T> queue) |
| | | 804 | | { |
| | 16 | 805 | | _queue = queue; |
| | 16 | 806 | | _array = queue._innerArray; |
| | 16 | 807 | | _version = queue._version; |
| | 16 | 808 | | _index = 0; |
| | 16 | 809 | | _currentIndex = (uint)queue._head - 1; |
| | 16 | 810 | | _current = default!; |
| | 16 | 811 | | } |
| | | 812 | | |
| | | 813 | | /// <inheritdoc/> |
| | 416 | 814 | | public readonly T Current => _current; |
| | | 815 | | |
| | | 816 | | readonly object IEnumerator.Current |
| | | 817 | | { |
| | | 818 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 819 | | get |
| | | 820 | | { |
| | 3 | 821 | | SwiftThrowHelper.ThrowIfTrue(_index >= (uint)_queue._count, message: "Bad enumeration"); |
| | 2 | 822 | | return _current!; |
| | | 823 | | } |
| | | 824 | | } |
| | | 825 | | |
| | | 826 | | /// <inheritdoc/> |
| | | 827 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 828 | | public bool MoveNext() |
| | | 829 | | { |
| | 236 | 830 | | SwiftThrowHelper.ThrowIfTrue(_version != _queue._version, message: "Collection was modified during enumerati |
| | | 831 | | |
| | 236 | 832 | | _index++; |
| | 250 | 833 | | if (_index > (uint)_queue._count) return false; |
| | 222 | 834 | | _currentIndex++; |
| | 223 | 835 | | if (_currentIndex == _array.Length) _currentIndex = 0; |
| | 222 | 836 | | _current = _array[_currentIndex]; |
| | 222 | 837 | | return true; |
| | | 838 | | } |
| | | 839 | | |
| | | 840 | | /// <inheritdoc/> |
| | | 841 | | public void Reset() |
| | | 842 | | { |
| | 2 | 843 | | SwiftThrowHelper.ThrowIfTrue(_version != _queue._version, message: "Collection was modified during enumerati |
| | | 844 | | |
| | 2 | 845 | | _index = 0; |
| | 2 | 846 | | _currentIndex = (uint)_queue._head - 1; |
| | 2 | 847 | | _current = default!; |
| | 2 | 848 | | } |
| | | 849 | | |
| | | 850 | | /// <inheritdoc/> |
| | 12 | 851 | | public void Dispose() => _index = 0; |
| | | 852 | | } |
| | | 853 | | |
| | | 854 | | #endregion |
| | | 855 | | } |