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