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