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