| | | 1 | | using MemoryPack; |
| | | 2 | | using System; |
| | | 3 | | using System.Collections; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Runtime.CompilerServices; |
| | | 6 | | using System.Text.Json.Serialization; |
| | | 7 | | |
| | | 8 | | namespace SwiftCollections; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Represents a fast, array-based stack (LIFO - Last-In-First-Out) collection of objects. |
| | | 12 | | /// <para> |
| | | 13 | | /// The <c>SwiftStack<T></c> class provides O(1) time complexity for <c>Push</c> and <c>Pop</c> operations, |
| | | 14 | | /// making it highly efficient for scenarios where performance is critical. |
| | | 15 | | /// It minimizes memory allocations by reusing internal arrays and offers methods |
| | | 16 | | /// like <c>FastClear</c> to quickly reset the stack without deallocating memory. |
| | | 17 | | /// </para> |
| | | 18 | | /// <para> |
| | | 19 | | /// This implementation is optimized for performance and does not perform versioning checks. |
| | | 20 | | /// Modifying the stack during enumeration may result in undefined behavior. |
| | | 21 | | /// </para> |
| | | 22 | | /// </summary> |
| | | 23 | | /// <typeparam name="T">Specifies the type of elements in the stack.</typeparam> |
| | | 24 | | [Serializable] |
| | | 25 | | [JsonConverter(typeof(SwiftStateJsonConverterFactory))] |
| | | 26 | | [MemoryPackable] |
| | | 27 | | public sealed partial class SwiftStack<T> : ISwiftCloneable<T>, IEnumerable<T>, IEnumerable, ICollection<T>, ICollection |
| | | 28 | | { |
| | | 29 | | #region Constants |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// The default initial capacity of the SwiftStack if none is specified. |
| | | 33 | | /// Used to allocate a reasonable starting size to minimize resizing operations. |
| | | 34 | | /// </summary> |
| | | 35 | | public const int DefaultCapacity = 8; |
| | | 36 | | |
| | 3 | 37 | | private static readonly T[] _emptyArray = Array.Empty<T>(); |
| | 3 | 38 | | private static readonly bool _clearReleasedSlots = RuntimeHelpers.IsReferenceOrContainsReferences<T>(); |
| | | 39 | | |
| | | 40 | | #endregion |
| | | 41 | | |
| | | 42 | | #region Fields |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// The internal array that stores elements of the SwiftStack. Resized as needed to |
| | | 46 | | /// accommodate additional elements. Not directly exposed outside the stack. |
| | | 47 | | /// </summary> |
| | | 48 | | private T[] _innerArray; |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// The current number of elements in the SwiftStack. Represents the total count of |
| | | 52 | | /// valid elements stored in the stack, also indicating the arrayIndex of the next insertion point. |
| | | 53 | | /// </summary> |
| | | 54 | | private int _count; |
| | | 55 | | |
| | | 56 | | [NonSerialized] |
| | | 57 | | private uint _version; |
| | | 58 | | |
| | | 59 | | [NonSerialized] |
| | | 60 | | private object _syncRoot; |
| | | 61 | | |
| | | 62 | | #endregion |
| | | 63 | | |
| | | 64 | | #region Constructors |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Initializes a new, empty instance of SwiftStack. |
| | | 68 | | /// </summary> |
| | 141 | 69 | | public SwiftStack() : this(0) { } |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Initializes a new, empty instance of SwiftStack with the specified initial capacity. |
| | | 73 | | /// </summary> |
| | 51 | 74 | | public SwiftStack(int capacity) |
| | 51 | 75 | | { |
| | 51 | 76 | | if (capacity == 0) |
| | 47 | 77 | | _innerArray = _emptyArray; |
| | | 78 | | else |
| | 4 | 79 | | { |
| | 4 | 80 | | capacity = capacity <= DefaultCapacity ? DefaultCapacity : SwiftHashTools.NextPowerOfTwo(capacity); |
| | 4 | 81 | | _innerArray = new T[capacity]; |
| | 4 | 82 | | } |
| | 51 | 83 | | } |
| | | 84 | | |
| | 3 | 85 | | public SwiftStack(IEnumerable<T> items) |
| | 3 | 86 | | { |
| | 3 | 87 | | SwiftThrowHelper.ThrowIfNull(items, nameof(items)); |
| | | 88 | | |
| | 3 | 89 | | if (items is ICollection<T> collection) |
| | 2 | 90 | | { |
| | 2 | 91 | | int count = collection.Count; |
| | 2 | 92 | | if (count == 0) |
| | 1 | 93 | | _innerArray = _emptyArray; |
| | | 94 | | else |
| | 1 | 95 | | { |
| | 1 | 96 | | int capacity = SwiftHashTools.NextPowerOfTwo(count <= DefaultCapacity ? DefaultCapacity : count); |
| | 1 | 97 | | _innerArray = new T[capacity]; |
| | 1 | 98 | | collection.CopyTo(_innerArray, 0); |
| | 1 | 99 | | _count = count; |
| | 1 | 100 | | } |
| | 2 | 101 | | } |
| | | 102 | | else |
| | 1 | 103 | | { |
| | 1 | 104 | | _innerArray = new T[DefaultCapacity]; |
| | 9 | 105 | | foreach (T item in items) |
| | 3 | 106 | | Push(item); |
| | 1 | 107 | | } |
| | 3 | 108 | | } |
| | | 109 | | |
| | | 110 | | /// <summary> |
| | | 111 | | /// Initializes a new instance of the <see cref="SwiftStack{T}"/> class with the specified <see cref="SwiftArraySta |
| | | 112 | | /// </summary> |
| | | 113 | | /// <param name="state">The state containing the internal array, count, offset, and version for initialization.</pa |
| | | 114 | | [MemoryPackConstructor] |
| | 3 | 115 | | public SwiftStack(SwiftArrayState<T> state) |
| | 3 | 116 | | { |
| | 3 | 117 | | State = state; |
| | 3 | 118 | | } |
| | | 119 | | |
| | | 120 | | #endregion |
| | | 121 | | |
| | | 122 | | #region Properties |
| | | 123 | | |
| | | 124 | | /// <inheritdoc cref="_innerArray"/> |
| | | 125 | | [JsonIgnore] |
| | | 126 | | [MemoryPackIgnore] |
| | 3 | 127 | | public T[] InnerArray => _innerArray; |
| | | 128 | | |
| | | 129 | | /// <inheritdoc cref="_count"/> |
| | | 130 | | [JsonIgnore] |
| | | 131 | | [MemoryPackIgnore] |
| | 118 | 132 | | public int Count => _count; |
| | | 133 | | |
| | | 134 | | /// <summary> |
| | | 135 | | /// Gets the total number of elements the SwiftQueue can hold without resizing. |
| | | 136 | | /// Reflects the current allocated size of the internal array. |
| | | 137 | | /// </summary> |
| | | 138 | | [JsonIgnore] |
| | | 139 | | [MemoryPackIgnore] |
| | 12 | 140 | | public int Capacity => _innerArray.Length; |
| | | 141 | | |
| | | 142 | | [JsonIgnore] |
| | | 143 | | [MemoryPackIgnore] |
| | 1 | 144 | | bool ICollection<T>.IsReadOnly => false; |
| | | 145 | | |
| | | 146 | | [JsonIgnore] |
| | | 147 | | [MemoryPackIgnore] |
| | 1 | 148 | | public bool IsSynchronized => false; |
| | | 149 | | |
| | | 150 | | [JsonIgnore] |
| | | 151 | | [MemoryPackIgnore] |
| | 1 | 152 | | object ICollection.SyncRoot => _syncRoot ??= new object(); |
| | | 153 | | |
| | | 154 | | /// <summary> |
| | | 155 | | /// Gets the element at the specified arrayIndex. |
| | | 156 | | /// </summary> |
| | | 157 | | [JsonIgnore] |
| | | 158 | | [MemoryPackIgnore] |
| | | 159 | | public T this[int index] |
| | | 160 | | { |
| | | 161 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 162 | | get |
| | 204 | 163 | | { |
| | 204 | 164 | | SwiftThrowHelper.ThrowIfIndexInvalid(index, _count); |
| | 203 | 165 | | return _innerArray[index]; |
| | 203 | 166 | | } |
| | | 167 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 168 | | set |
| | 1 | 169 | | { |
| | 1 | 170 | | SwiftThrowHelper.ThrowIfIndexInvalid(index, _count); |
| | 1 | 171 | | _innerArray[index] = value; |
| | 1 | 172 | | } |
| | | 173 | | } |
| | | 174 | | |
| | | 175 | | [JsonInclude] |
| | | 176 | | [MemoryPackInclude] |
| | | 177 | | public SwiftArrayState<T> State |
| | | 178 | | { |
| | | 179 | | get |
| | 2 | 180 | | { |
| | 2 | 181 | | var items = new T[_count]; |
| | 2 | 182 | | Array.Copy(_innerArray, 0, items, 0, _count); |
| | 2 | 183 | | return new SwiftArrayState<T>(items); |
| | 2 | 184 | | } |
| | | 185 | | internal set |
| | 3 | 186 | | { |
| | 3 | 187 | | int count = value.Items?.Length ?? 0; |
| | | 188 | | |
| | 3 | 189 | | if (count == 0) |
| | 1 | 190 | | { |
| | 1 | 191 | | _innerArray = _emptyArray; |
| | 1 | 192 | | _count = 0; |
| | 1 | 193 | | _version = 0; |
| | 1 | 194 | | return; |
| | | 195 | | } |
| | | 196 | | |
| | 2 | 197 | | int capacity = SwiftHashTools.NextPowerOfTwo(count <= DefaultCapacity ? DefaultCapacity : count); |
| | | 198 | | |
| | 2 | 199 | | _innerArray = new T[capacity]; |
| | 2 | 200 | | Array.Copy(value.Items, 0, _innerArray, 0, count); |
| | | 201 | | |
| | 2 | 202 | | _count = count; |
| | 2 | 203 | | _version = 0; |
| | 3 | 204 | | } |
| | | 205 | | } |
| | | 206 | | |
| | | 207 | | #endregion |
| | | 208 | | |
| | | 209 | | #region Collection Manipulation |
| | | 210 | | |
| | | 211 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2 | 212 | | void ICollection<T>.Add(T item) => Push(item); |
| | | 213 | | |
| | | 214 | | /// <summary> |
| | | 215 | | /// Inserts an object at the top of the SwiftStack. |
| | | 216 | | /// </summary> |
| | | 217 | | /// <param name="item"></param> |
| | | 218 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 219 | | public void Push(T item) |
| | 278 | 220 | | { |
| | 278 | 221 | | if ((uint)_count == (uint)_innerArray.Length) |
| | 41 | 222 | | Resize(_innerArray.Length * 2); |
| | 278 | 223 | | _innerArray[_count++] = item; |
| | 278 | 224 | | _version++; |
| | 278 | 225 | | } |
| | | 226 | | |
| | | 227 | | /// <summary> |
| | | 228 | | /// Pushes the elements of the specified span onto the stack in order. |
| | | 229 | | /// </summary> |
| | | 230 | | /// <param name="items">The span whose elements should be pushed.</param> |
| | | 231 | | public void PushRange(ReadOnlySpan<T> items) |
| | 6 | 232 | | { |
| | 6 | 233 | | if (items.Length == 0) |
| | 1 | 234 | | return; |
| | | 235 | | |
| | 5 | 236 | | if (_count + items.Length > _innerArray.Length) |
| | 4 | 237 | | { |
| | 4 | 238 | | int newCapacity = SwiftHashTools.NextPowerOfTwo(_count + items.Length); |
| | 4 | 239 | | Resize(newCapacity); |
| | 4 | 240 | | } |
| | | 241 | | |
| | 5 | 242 | | items.CopyTo(_innerArray.AsSpan(_count, items.Length)); |
| | 5 | 243 | | _count += items.Length; |
| | 5 | 244 | | _version++; |
| | 6 | 245 | | } |
| | | 246 | | |
| | | 247 | | bool ICollection<T>.Remove(T item) |
| | 1 | 248 | | { |
| | 1 | 249 | | throw new NotSupportedException("Remove is not supported on Stack."); |
| | | 250 | | } |
| | | 251 | | |
| | | 252 | | /// <summary> |
| | | 253 | | /// Removes and returns the object at the top of the SwiftStack. |
| | | 254 | | /// </summary> |
| | | 255 | | /// <returns></returns> |
| | | 256 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 257 | | public T Pop() |
| | 3 | 258 | | { |
| | 4 | 259 | | if ((uint)_count == 0) throw new InvalidOperationException("Stack is empty."); |
| | 2 | 260 | | T item = _innerArray[--_count]; |
| | 2 | 261 | | if (_clearReleasedSlots) |
| | 1 | 262 | | _innerArray[_count] = default; |
| | 2 | 263 | | _version++; |
| | 2 | 264 | | return item; |
| | 2 | 265 | | } |
| | | 266 | | |
| | | 267 | | /// <summary> |
| | | 268 | | /// Removes all elements from the SwiftStack, resetting its count to zero. |
| | | 269 | | /// </summary> |
| | | 270 | | public void Clear() |
| | 5 | 271 | | { |
| | 6 | 272 | | if (_count == 0) return; |
| | 4 | 273 | | if (_clearReleasedSlots) |
| | 1 | 274 | | Array.Clear(_innerArray, 0, _count); |
| | 4 | 275 | | _count = 0; |
| | 4 | 276 | | _version++; |
| | 5 | 277 | | } |
| | | 278 | | |
| | | 279 | | /// <summary> |
| | | 280 | | /// Clears the SwiftStack without releasing the reference to the stored elements. |
| | | 281 | | /// Use FastClear() when you want to quickly reset the list without reallocating memory. |
| | | 282 | | /// </summary> |
| | | 283 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 284 | | public void FastClear() |
| | 1 | 285 | | { |
| | 1 | 286 | | _count = 0; |
| | 1 | 287 | | _version++; |
| | 1 | 288 | | } |
| | | 289 | | |
| | | 290 | | #endregion |
| | | 291 | | |
| | | 292 | | #region Capacity Management |
| | | 293 | | |
| | | 294 | | public void EnsureCapacity(int capacity) |
| | 1 | 295 | | { |
| | 1 | 296 | | capacity = SwiftHashTools.NextPowerOfTwo(capacity); |
| | 1 | 297 | | if (capacity > _innerArray.Length) |
| | 1 | 298 | | Resize(capacity); |
| | 1 | 299 | | } |
| | | 300 | | |
| | | 301 | | /// <summary> |
| | | 302 | | /// Ensures that the capacity of the stack is sufficient to accommodate the specified number of elements. |
| | | 303 | | /// The stack capacity can increase by double to balance memory allocation efficiency and space. |
| | | 304 | | /// </summary> |
| | | 305 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 306 | | private void Resize(int newSize) |
| | 46 | 307 | | { |
| | 46 | 308 | | int newCapacity = newSize <= DefaultCapacity ? DefaultCapacity : newSize; |
| | 46 | 309 | | T[] newArray = new T[newCapacity]; |
| | 46 | 310 | | if ((uint)_count > 0) |
| | 8 | 311 | | Array.Copy(_innerArray, 0, newArray, 0, _count); |
| | 46 | 312 | | _innerArray = newArray; |
| | 46 | 313 | | _version++; |
| | 46 | 314 | | } |
| | | 315 | | |
| | | 316 | | |
| | | 317 | | /// <summary> |
| | | 318 | | /// Sets the capacity of a <see cref="SwiftStack{T}"/> to the actual |
| | | 319 | | /// number of elements it contains, rounded up to a nearby next power of 2 value. |
| | | 320 | | /// </summary> |
| | | 321 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 322 | | public void TrimCapacity() |
| | 2 | 323 | | { |
| | 2 | 324 | | int newCapacity = _count <= DefaultCapacity ? DefaultCapacity : SwiftHashTools.NextPowerOfTwo(_count); |
| | 2 | 325 | | T[] newArray = new T[newCapacity]; |
| | 2 | 326 | | if ((uint)_count > 0) |
| | 1 | 327 | | Array.Copy(_innerArray, 0, newArray, 0, _count); |
| | 2 | 328 | | _innerArray = newArray; |
| | 2 | 329 | | _version++; |
| | 2 | 330 | | } |
| | | 331 | | |
| | | 332 | | #endregion |
| | | 333 | | |
| | | 334 | | #region Utility Methods |
| | | 335 | | |
| | | 336 | | /// <summary> |
| | | 337 | | /// Returns the object at the top of the SwiftStack without removing it. |
| | | 338 | | /// </summary> |
| | | 339 | | /// <returns></returns> |
| | | 340 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 341 | | public T Peek() |
| | 7 | 342 | | { |
| | 8 | 343 | | if ((uint)_count == 0) throw new InvalidOperationException("Stack is empty."); |
| | 6 | 344 | | return _innerArray[_count - 1]; |
| | 6 | 345 | | } |
| | | 346 | | |
| | | 347 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2 | 348 | | public override string ToString() => (uint)_count == 0 ? $"{typeof(SwiftStack<T>)}: Empty" : $"{typeof(SwiftStack<T> |
| | | 349 | | |
| | | 350 | | /// <summary> |
| | | 351 | | /// Returns a mutable span over the populated portion of the stack. |
| | | 352 | | /// </summary> |
| | | 353 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2 | 354 | | public Span<T> AsSpan() => _innerArray.AsSpan(0, _count); |
| | | 355 | | |
| | | 356 | | /// <summary> |
| | | 357 | | /// Returns a read-only span over the populated portion of the stack. |
| | | 358 | | /// </summary> |
| | | 359 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 4 | 360 | | public ReadOnlySpan<T> AsReadOnlySpan() => _innerArray.AsSpan(0, _count); |
| | | 361 | | |
| | | 362 | | public void CopyTo(T[] array, int arrayIndex) |
| | 4 | 363 | | { |
| | 4 | 364 | | SwiftThrowHelper.ThrowIfNull(array, nameof(array)); |
| | 4 | 365 | | if ((uint)arrayIndex > array.Length) throw new ArgumentOutOfRangeException(); |
| | 3 | 366 | | if ((uint)(array.Length - arrayIndex) < (uint)_count) throw new ArgumentException("Destination array is not long |
| | | 367 | | |
| | 1 | 368 | | Array.Copy(_innerArray, 0, array, arrayIndex, _count); |
| | 1 | 369 | | } |
| | | 370 | | |
| | | 371 | | /// <summary> |
| | | 372 | | /// Copies the populated elements of the SwiftStack into the specified destination span. |
| | | 373 | | /// </summary> |
| | | 374 | | /// <param name="destination">The destination span.</param> |
| | | 375 | | public void CopyTo(Span<T> destination) |
| | 2 | 376 | | { |
| | 2 | 377 | | if (destination.Length < _count) |
| | 1 | 378 | | throw new ArgumentException("Destination span is not long enough.", nameof(destination)); |
| | | 379 | | |
| | 1 | 380 | | AsSpan().CopyTo(destination); |
| | 1 | 381 | | } |
| | | 382 | | |
| | | 383 | | void ICollection.CopyTo(Array array, int arrayIndex) |
| | 3 | 384 | | { |
| | 3 | 385 | | SwiftThrowHelper.ThrowIfNull(array, nameof(array)); |
| | 4 | 386 | | if ((uint)array.Rank != 1) throw new ArgumentException("Array must be single dimensional."); |
| | 3 | 387 | | if ((uint)array.GetLowerBound(0) != 0) throw new ArgumentException("Array must have zero-based indexing."); |
| | 1 | 388 | | if ((uint)arrayIndex > array.Length) throw new ArgumentOutOfRangeException(); |
| | 1 | 389 | | if ((uint)(array.Length - arrayIndex) < _count) throw new ArgumentException("Destination array is not long enoug |
| | | 390 | | |
| | | 391 | | try |
| | 1 | 392 | | { |
| | 8 | 393 | | for (int i = 0; (uint)i < (uint)_count; i++) |
| | 3 | 394 | | array.SetValue(_innerArray[i], arrayIndex++); |
| | 1 | 395 | | } |
| | 0 | 396 | | catch (ArrayTypeMismatchException) |
| | 0 | 397 | | { |
| | 0 | 398 | | throw new ArgumentException("Invalid array type."); |
| | | 399 | | } |
| | 1 | 400 | | } |
| | | 401 | | |
| | | 402 | | public void CloneTo(ICollection<T> output) |
| | 1 | 403 | | { |
| | 1 | 404 | | output.Clear(); |
| | 6 | 405 | | for (int i = 0; (uint)i < (uint)_count; i++) |
| | 2 | 406 | | output.Add(_innerArray[i]); |
| | 1 | 407 | | } |
| | | 408 | | |
| | | 409 | | public bool Contains(T item) |
| | 2 | 410 | | { |
| | 2 | 411 | | EqualityComparer<T> comparer = EqualityComparer<T>.Default; |
| | 10 | 412 | | for (int i = 0; i < _count; i++) |
| | 4 | 413 | | { |
| | 4 | 414 | | if (comparer.Equals(_innerArray[i], item)) |
| | 1 | 415 | | return true; |
| | 3 | 416 | | } |
| | 1 | 417 | | return false; |
| | 2 | 418 | | } |
| | | 419 | | |
| | | 420 | | /// <summary> |
| | | 421 | | /// Determines whether the <see cref="SwiftStack{T}"/> contains an element that matches the conditions defined by th |
| | | 422 | | /// </summary> |
| | | 423 | | /// <param name="match">The predicate that defines the conditions of the element to search for.</param> |
| | | 424 | | /// <returns><c>true</c> if the <see cref="SwiftStack{T}"/> contains one or more elements that match the specified p |
| | | 425 | | public bool Exists(Predicate<T> match) |
| | 3 | 426 | | { |
| | 3 | 427 | | SwiftThrowHelper.ThrowIfNull(match, nameof(match)); |
| | | 428 | | |
| | 12 | 429 | | for (int i = _count - 1; i >= 0; i--) |
| | 5 | 430 | | { |
| | 5 | 431 | | if (match(_innerArray[i])) |
| | 1 | 432 | | return true; |
| | 4 | 433 | | } |
| | | 434 | | |
| | 1 | 435 | | return false; |
| | 2 | 436 | | } |
| | | 437 | | |
| | | 438 | | /// <summary> |
| | | 439 | | /// Searches for an element that matches the conditions defined by the specified predicate, and returns the first ma |
| | | 440 | | /// </summary> |
| | | 441 | | /// <param name="match">The predicate that defines the conditions of the element to search for.</param> |
| | | 442 | | /// <returns>The first element that matches the conditions defined by the specified predicate, if found; otherwise, |
| | | 443 | | public T Find(Predicate<T> match) |
| | 2 | 444 | | { |
| | 2 | 445 | | SwiftThrowHelper.ThrowIfNull(match, nameof(match)); |
| | | 446 | | |
| | 8 | 447 | | for (int i = _count - 1; i >= 0; i--) |
| | 3 | 448 | | { |
| | 3 | 449 | | if (match(_innerArray[i])) |
| | 1 | 450 | | return _innerArray[i]; |
| | 2 | 451 | | } |
| | | 452 | | |
| | 1 | 453 | | return default; |
| | 2 | 454 | | } |
| | | 455 | | |
| | | 456 | | #endregion |
| | | 457 | | |
| | | 458 | | #region Enumerators |
| | 20 | 459 | | public SwiftStackEnumerator GetEnumerator() => new SwiftStackEnumerator(this); |
| | 9 | 460 | | IEnumerator<T> IEnumerable<T>.GetEnumerator() => GetEnumerator(); |
| | 3 | 461 | | IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
| | | 462 | | |
| | | 463 | | public struct SwiftStackEnumerator : IEnumerator<T>, IEnumerator, IDisposable |
| | | 464 | | { |
| | | 465 | | private readonly SwiftStack<T> _stack; |
| | | 466 | | private readonly T[] _array; |
| | | 467 | | private readonly uint _version; |
| | | 468 | | private readonly int _count; |
| | | 469 | | private int _index; |
| | | 470 | | |
| | | 471 | | private T _current; |
| | | 472 | | |
| | | 473 | | internal SwiftStackEnumerator(SwiftStack<T> stack) |
| | 20 | 474 | | { |
| | 20 | 475 | | _stack = stack; |
| | 20 | 476 | | _array = stack._innerArray; |
| | 20 | 477 | | _count = stack._count; |
| | 20 | 478 | | _version = stack._version; |
| | 20 | 479 | | _index = -2; // Enumerator not started |
| | 20 | 480 | | _current = default; |
| | 20 | 481 | | } |
| | | 482 | | |
| | 430 | 483 | | public T Current => _current; |
| | | 484 | | |
| | | 485 | | object IEnumerator.Current |
| | | 486 | | { |
| | | 487 | | get |
| | 2 | 488 | | { |
| | 3 | 489 | | if ((uint)_index > _count) throw new InvalidOperationException("Bad enumeration"); |
| | 1 | 490 | | return _current; |
| | 1 | 491 | | } |
| | | 492 | | } |
| | | 493 | | |
| | | 494 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 495 | | public bool MoveNext() |
| | 251 | 496 | | { |
| | 251 | 497 | | if (_version != _stack._version) |
| | 1 | 498 | | throw new InvalidOperationException("Enumerator modified outside of enumeration!"); |
| | | 499 | | |
| | 250 | 500 | | if (_index == -2) |
| | 22 | 501 | | { |
| | 22 | 502 | | _index = _count - 1; |
| | 22 | 503 | | } |
| | | 504 | | else |
| | 228 | 505 | | { |
| | 228 | 506 | | _index--; |
| | 228 | 507 | | } |
| | | 508 | | |
| | 250 | 509 | | if (_index >= 0) |
| | 232 | 510 | | { |
| | 232 | 511 | | _current = _array[_index]; |
| | 232 | 512 | | return true; |
| | | 513 | | } |
| | | 514 | | |
| | 18 | 515 | | _index = -1; |
| | 18 | 516 | | _current = default; |
| | 18 | 517 | | return false; |
| | 250 | 518 | | } |
| | | 519 | | |
| | | 520 | | public void Reset() |
| | 4 | 521 | | { |
| | 4 | 522 | | if (_version != _stack._version) |
| | 1 | 523 | | throw new InvalidOperationException("Enumerator modified outside of enumeration!"); |
| | | 524 | | |
| | 3 | 525 | | _index = -2; |
| | 3 | 526 | | _current = default; |
| | 3 | 527 | | } |
| | | 528 | | |
| | 11 | 529 | | public void Dispose() => _index = -1; |
| | | 530 | | } |
| | | 531 | | |
| | | 532 | | #endregion |
| | | 533 | | } |