< Summary

Line coverage
98%
Covered lines: 332
Uncovered lines: 5
Coverable lines: 337
Total lines: 792
Line coverage: 98.5%
Branch coverage
93%
Covered branches: 101
Total branches: 108
Branch coverage: 93.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
File 1: .cctor()100%11100%
File 2: .cctor()100%11100%
File 2: .ctor()100%11100%
File 2: .ctor(...)100%44100%
File 2: .ctor(...)100%44100%
File 2: .ctor(...)100%11100%
File 2: get_InnerArray()100%11100%
File 2: get_Capacity()100%11100%
File 2: get_Count()100%11100%
File 2: get_IsReadOnly()100%11100%
File 2: get_IsSynchronized()100%11100%
File 2: System.Collections.ICollection.get_SyncRoot()100%22100%
File 2: System.Collections.IList.get_IsFixedSize()100%11100%
File 2: System.Collections.IList.get_Item(...)100%11100%
File 2: System.Collections.IList.set_Item(...)100%11100%
File 2: get_Item(...)100%22100%
File 2: set_Item(...)50%22100%
File 2: get_State()100%11100%
File 2: set_State(...)83.33%66100%
File 2: Add(...)100%22100%
File 2: System.Collections.IList.Add(...)100%11100%
File 2: AddRange(...)83.33%7675%
File 2: AddRange(...)100%11100%
File 2: AddRange(...)100%44100%
File 2: Remove(...)100%22100%
File 2: System.Collections.IList.Remove(...)100%11100%
File 2: RemoveAt(...)100%44100%
File 2: RemoveAll(...)100%1818100%
File 2: Insert(...)83.33%6690%
File 2: System.Collections.IList.Insert(...)100%11100%
File 2: Reverse()100%11100%
File 2: Sort()100%11100%
File 2: Sort(...)100%11100%
File 2: Clear()100%22100%
File 2: FastClear()100%11100%
File 2: EnsureCapacity(...)100%22100%
File 2: Resize(...)100%44100%
File 2: TrimExcessCapacity()100%22100%
File 2: IndexOf(...)100%11100%
File 2: System.Collections.IList.IndexOf(...)100%11100%
File 2: ToArray()100%11100%
File 2: AsSpan()100%11100%
File 2: AsReadOnlySpan()100%11100%
File 2: ToString()100%22100%
File 2: Contains(...)100%11100%
File 2: Exists(...)100%44100%
File 2: Find(...)100%44100%
File 2: System.Collections.IList.Contains(...)100%11100%
File 2: Swap(...)100%11100%
File 2: CopyTo(...)100%22100%
File 2: CopyTo(...)75%44100%
File 2: CopyTo(...)100%22100%
File 2: CopyTo(...)75%88100%
File 2: CloneTo(...)100%22100%
File 2: GetEnumerator()100%11100%
File 2: System.Collections.Generic.IEnumerable<T>.GetEnumerator()100%11100%
File 2: System.Collections.IEnumerable.GetEnumerator()100%11100%
File 2: .ctor(...)100%11100%
File 2: get_Current()100%11100%
File 2: System.Collections.IEnumerator.get_Current()100%22100%
File 2: MoveNext()100%44100%
File 2: Reset()100%22100%
File 2: Dispose()100%11100%

File(s)

/_/src/SwiftCollections/obj/Debug/net8.0/MemoryPack.Generator/MemoryPack.Generator.MemoryPackGenerator/SwiftCollections.SwiftList_T_.MemoryPackFormatter.g.cs

File '/_/src/SwiftCollections/obj/Debug/net8.0/MemoryPack.Generator/MemoryPack.Generator.MemoryPackGenerator/SwiftCollections.SwiftList_T_.MemoryPackFormatter.g.cs' does not exist (any more).

/home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections/Collection/SwiftList.cs

#LineLine coverage
 1using MemoryPack;
 2using System;
 3using System.Collections;
 4using System.Collections.Generic;
 5using System.Runtime.CompilerServices;
 6using System.Text.Json.Serialization;
 7
 8namespace SwiftCollections;
 9
 10/// <summary>
 11/// <c>SwiftList&lt;T&gt;</c> is a high-performance, memory-efficient dynamic list designed to outperform
 12/// traditional generic lists in speed-critical applications.
 13/// <para>
 14/// By utilizing custom growth and
 15/// shrink strategies, SwiftList optimizes memory allocation and minimizes resizing overhead,
 16/// all while maintaining compact storage. With aggressive inlining and optimized algorithms,
 17/// SwiftList delivers faster iteration, insertion, and overall memory management compared to
 18/// standard List. It is ideal for scenarios where predictable performance and minimal
 19/// memory allocations are essential.
 20/// </para>
 21/// <para>
 22/// This implementation is optimized for performance and does not perform versioning checks.
 23/// Modifying the list during enumeration may result in undefined behavior.
 24/// </para>
 25/// </summary>
 26/// <typeparam name="T">Specifies the type of elements in the list.</typeparam>
 27[Serializable]
 28[JsonConverter(typeof(SwiftStateJsonConverterFactory))]
 29[MemoryPackable]
 30public partial class SwiftList<T> : ISwiftCloneable<T>, IEnumerable<T>, IEnumerable, ICollection<T>, ICollection, IList<
 31{
 32    #region Constants
 33
 34    /// <summary>
 35    /// The default initial capacity of the <see cref="SwiftList{T}"/> if none is specified.
 36    /// Used to allocate a reasonable starting size to minimize resizing operations.
 37    /// </summary>
 38    public const int DefaultCapacity = 8;
 39
 240    private static readonly T[] _emptyArray = Array.Empty<T>();
 241    private static readonly bool _clearReleasedSlots = RuntimeHelpers.IsReferenceOrContainsReferences<T>();
 42
 43    #endregion
 44
 45    #region Fields
 46
 47    /// <summary>
 48    /// The internal array that stores elements of the SwiftList. Resized as needed to
 49    /// accommodate additional elements. Not directly exposed outside the list.
 50    /// </summary>
 51    protected T[] _innerArray;
 52
 53    /// <summary>
 54    /// The current number of elements in the SwiftList. Represents the total count of
 55    /// valid elements stored in the list, also indicating the arrayIndex of the next insertion point.
 56    /// </summary>
 57    protected int _count;
 58
 59    /// <summary>
 60    /// The version of the SwiftList, used to track modifications.
 61    /// </summary>
 62    [NonSerialized]
 63    protected uint _version;
 64
 65    /// <summary>
 66    /// An object that can be used to synchronize access to the SwiftList.
 67    /// </summary>
 68    [NonSerialized]
 69    private object _syncRoot;
 70
 71    #endregion
 72
 73    #region Constructors
 74
 30075    public SwiftList() : this(0) { }
 76
 77    /// <summary>
 78    /// Initializes a new, empty instance of <see cref="SwiftList{T}"/> with the specified initial capacity.
 79    /// </summary>
 11280    public SwiftList(int capacity)
 11281    {
 11282        if (capacity == 0)
 10083            _innerArray = _emptyArray;
 84        else
 1285        {
 1286            capacity = SwiftHashTools.NextPowerOfTwo(capacity <= DefaultCapacity ? DefaultCapacity : capacity);
 1287            _innerArray = new T[capacity];
 1288        }
 11289    }
 90
 91    /// <summary>
 92    /// Initializes a new instance of the <see cref="SwiftList{T}"/> class with elements from the specified collection.
 93    /// The collection must have a known count for optimized memory allocation.
 94    /// </summary>
 95    /// <exception cref="ArgumentException">Thrown if the input collection does not have a known count.</exception>
 1296    public SwiftList(IEnumerable<T> items)
 1297    {
 1298        SwiftThrowHelper.ThrowIfNull(items, nameof(items));
 99
 12100        if (items is ICollection<T> collection)
 9101        {
 9102            int count = collection.Count;
 9103            if (count == 0)
 1104                _innerArray = _emptyArray;
 105            else
 8106            {
 8107                _innerArray = new T[count];
 8108                collection.CopyTo(_innerArray, 0);
 8109                _count = count;
 8110            }
 9111        }
 112        else
 3113        {
 3114            _innerArray = new T[DefaultCapacity];
 3115            AddRange(items); // Will handle capacity increases as needed
 3116        }
 12117    }
 118
 119    ///  <summary>
 120    ///  Initializes a new instance of the <see cref="SwiftList{T}"/> class with the specified <see cref="SwiftArrayStat
 121    ///  </summary>
 122    ///  <param name="state">The state containing the internal array, count, offset, and version for initialization.</pa
 123    [MemoryPackConstructor]
 9124    public SwiftList(SwiftArrayState<T> state)
 9125    {
 9126        State = state;
 9127    }
 128
 129    #endregion
 130
 131    #region Properties
 132
 133    [JsonIgnore]
 134    [MemoryPackIgnore]
 8135    public T[] InnerArray => _innerArray;
 136
 137    /// <summary>
 138    /// Gets the total number of elements the SwiftList can hold without resizing.
 139    /// Reflects the current allocated size of the internal array.
 140    /// </summary>
 141    [JsonIgnore]
 142    [MemoryPackIgnore]
 14143    public int Capacity => _innerArray.Length;
 144
 145    /// <inheritdoc cref="_count"/>
 146    [JsonIgnore]
 147    [MemoryPackIgnore]
 141148    public int Count => _count;
 149
 150    [JsonIgnore]
 151    [MemoryPackIgnore]
 1152    public bool IsReadOnly => false;
 153
 154    [JsonIgnore]
 155    [MemoryPackIgnore]
 1156    public bool IsSynchronized => false;
 157
 158    [JsonIgnore]
 159    [MemoryPackIgnore]
 1160    object ICollection.SyncRoot => _syncRoot ??= new object();
 161
 162    [JsonIgnore]
 163    [MemoryPackIgnore]
 1164    bool IList.IsFixedSize => false;
 165
 166    [JsonIgnore]
 167    [MemoryPackIgnore]
 168    object IList.this[int index]
 169    {
 1170        get => this[index];
 171        set
 2172        {
 173            try
 2174            {
 2175                this[index] = (T)value;
 1176            }
 1177            catch
 1178            {
 1179                throw new NotSupportedException($"Unsupported value type for {value}");
 180            }
 1181        }
 182    }
 183
 184    /// <summary>
 185    /// Gets the element at the specified arrayIndex.
 186    /// </summary>
 187    [JsonIgnore]
 188    [MemoryPackIgnore]
 189    public T this[int index]
 190    {
 191        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 192        get
 2255193        {
 2259194            if ((uint)index >= (uint)_count) throw new ArgumentOutOfRangeException(nameof(index));
 2251195            return _innerArray[index];
 2251196        }
 197        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 198        set
 1514199        {
 1514200            if ((uint)index >= (uint)_count) throw new ArgumentOutOfRangeException(nameof(index));
 1514201            _innerArray[index] = value;
 1514202        }
 203    }
 204
 205    [JsonInclude]
 206    [MemoryPackInclude]
 207    public SwiftArrayState<T> State
 208    {
 209        get
 8210        {
 8211            var items = new T[_count];
 8212            Array.Copy(_innerArray, 0, items, 0, _count);
 8213            return new SwiftArrayState<T>(items);
 8214        }
 215        internal set
 9216        {
 9217            int count = value.Items?.Length ?? 0;
 218
 9219            if (count == 0)
 1220            {
 1221                _innerArray = _emptyArray;
 1222                _count = 0;
 1223            }
 224            else
 8225            {
 8226                _innerArray = new T[count <= DefaultCapacity ? DefaultCapacity : count];
 8227                Array.Copy(value.Items, 0, _innerArray, 0, count);
 8228                _count = count;
 8229            }
 230
 9231            _version = 0;
 9232        }
 233    }
 234
 235    #endregion
 236
 237    #region Collection Manipulation
 238
 239    /// <summary>
 240    /// Adds an object to the end of the SwiftList.
 241    /// </summary>
 242    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 243    public virtual void Add(T item)
 111449244    {
 111449245        if ((uint)_count == (uint)_innerArray.Length)
 127246            Resize(_innerArray.Length * 2);
 111449247        _innerArray[_count++] = item;
 111449248        _version++;
 111449249    }
 250
 251    int IList.Add(object value)
 2252    {
 253        try
 2254        {
 2255            Add((T)value);
 1256        }
 1257        catch (InvalidCastException)
 1258        {
 1259            throw new NotSupportedException($"Wrong value type for {value}");
 260        }
 261
 1262        return _count - 1;
 1263    }
 264
 265    /// <summary>
 266    /// Adds the elements of the specified collection to the end of the SwiftList.
 267    /// </summary>
 268    public virtual void AddRange(IEnumerable<T> items)
 7269    {
 7270        SwiftThrowHelper.ThrowIfNull(items, nameof(items));
 271
 7272        if (items is ICollection<T> collection)
 3273        {
 274            // Ensure capacity to fit all new items
 3275            if (_count + collection.Count > _innerArray.Length)
 0276            {
 0277                int newCapacity = SwiftHashTools.NextPowerOfTwo(_count + collection.Count);
 0278                Resize(newCapacity);
 0279            }
 280
 281            // Copy new items directly into the internal array
 3282            collection.CopyTo(_innerArray, _count);
 3283            _count += collection.Count;
 3284            _version++;
 285
 3286            return;
 287        }
 288
 289        // Fallback for non-ICollection, adding each item individually
 42290        foreach (T item in items)
 15291            Add(item);
 7292    }
 293
 294    /// <summary>
 295    /// Adds the elements of the specified array to the end of the SwiftList.
 296    /// </summary>
 297    /// <param name="items">The array whose elements should be appended.</param>
 298    public virtual void AddRange(T[] items)
 1299    {
 1300        SwiftThrowHelper.ThrowIfNull(items, nameof(items));
 1301        AddRange(items.AsSpan());
 1302    }
 303
 304    /// <summary>
 305    /// Adds the elements of the specified span to the end of the SwiftList.
 306    /// </summary>
 307    /// <param name="items">The span whose elements should be appended.</param>
 308    public virtual void AddRange(ReadOnlySpan<T> items)
 3309    {
 3310        if (items.Length == 0)
 1311            return;
 312
 2313        if (_count + items.Length > _innerArray.Length)
 1314        {
 1315            int newCapacity = SwiftHashTools.NextPowerOfTwo(_count + items.Length);
 1316            Resize(newCapacity);
 1317        }
 318
 2319        items.CopyTo(_innerArray.AsSpan(_count, items.Length));
 2320        _count += items.Length;
 2321        _version++;
 3322    }
 323
 324    /// <summary>
 325    /// Removes the first occurrence of a specific object from the SwiftList.
 326    /// </summary>
 327    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 328    public virtual bool Remove(T item)
 3329    {
 3330        int index = IndexOf(item);
 4331        if (index < 0) return false; // Item not found return false;
 2332        RemoveAt(index);
 2333        return true;
 3334    }
 335
 336    void IList.Remove(object value)
 2337    {
 338        try
 2339        {
 2340            Remove((T)value);
 1341        }
 1342        catch (InvalidCastException)
 1343        {
 1344            throw new NotSupportedException($"Wrong value type for {value}");
 345        }
 1346    }
 347
 348    /// <summary>
 349    /// Removes the element at the specified arrayIndex of the SwiftList.
 350    /// </summary>
 351    public virtual void RemoveAt(int index)
 508352    {
 509353        if ((uint)index >= (uint)_count) throw new ArgumentOutOfRangeException(nameof(index));
 507354        Array.Copy(_innerArray, index + 1, _innerArray, index, _count - index - 1);
 507355        _count--;
 507356        if (_clearReleasedSlots)
 2357            _innerArray[_count] = default;
 507358        _version++;
 507359    }
 360
 361    /// <summary>
 362    /// Removes all the elements that match the conditions defined by the specified predicate.
 363    /// </summary>
 364    public virtual int RemoveAll(Predicate<T> match)
 11365    {
 11366        SwiftThrowHelper.ThrowIfNull(match, nameof(match));
 367
 10368        int i = 0;
 369        // Move to the first element that should be removed
 40370        while (i < _count && !match(_innerArray[i])) i++;
 371
 13372        if (i >= _count) return 0;  // No items to remove
 373
 7374        int j = i + 1;
 50017375        while (j < _count)
 50010376        {
 377            // Find the next element to keep
 150026378            while (j < _count && match(_innerArray[j])) j++;
 379
 50010380            if (j < _count)
 50006381                _innerArray[i++] = _innerArray[j++];
 50010382        }
 383
 7384        int removedCount = _count - i;
 7385        if (_clearReleasedSlots && removedCount > 0)
 2386            Array.Clear(_innerArray, i, removedCount);
 387
 7388        _count = i;
 389
 7390        _version++;
 391
 7392        return removedCount;
 10393    }
 394
 395    /// <summary>
 396    /// Inserts an element into the SwiftList at the specified arrayIndex.
 397    /// </summary>
 398    public virtual void Insert(int index, T item)
 7399    {
 8400        if ((uint)index > (uint)_count) throw new ArgumentOutOfRangeException(nameof(index));
 6401        if ((uint)_count == (uint)_innerArray.Length)
 0402            Resize(_innerArray.Length * 2);
 6403        if ((uint)index < (uint)_count)
 4404            Array.Copy(_innerArray, index, _innerArray, index + 1, _count - index);
 6405        _innerArray[index] = item;
 6406        _count++;
 6407        _version++;
 6408    }
 409
 410    void IList.Insert(int index, object value)
 2411    {
 412        try
 2413        {
 2414            Insert(index, (T)value);
 1415        }
 1416        catch (InvalidCastException)
 1417        {
 1418            throw new NotSupportedException($"Wrong value type for {value}");
 419        }
 1420    }
 421
 422    /// <summary>
 423    /// Reverses the order of the elements in the entire <see cref="SwiftList{T}"/>.
 424    /// </summary>
 425    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 426    public void Reverse()
 1427    {
 1428        Array.Reverse(_innerArray, 0, _count);
 1429        _version++;
 1430    }
 431
 432    /// <summary>
 433    /// Sorts the elements in the <see cref="SwiftList{T}"/> using the <see cref="IComparable"/> interface implementatio
 434    /// </summary>
 435    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 436    public void Sort()
 1437    {
 1438        Array.Sort(_innerArray, 0, _count, Comparer<T>.Default);
 1439        _version++;
 1440    }
 441
 442    /// <summary>
 443    /// Sorts the elements in the entire <see cref="SwiftList{T}"/> using the specified comparer.
 444    /// </summary>
 445    /// <param name="comparer">The comparer to use for comparing elements.</param>
 446    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 447    public void Sort(IComparer<T> comparer)
 2448    {
 2449        Array.Sort(_innerArray, 0, _count, comparer);
 2450        _version++;
 2451    }
 452
 453    /// <summary>
 454    /// Removes all elements from the <see cref="SwiftList{T}"/>, resetting its count to zero.
 455    /// </summary>
 456    public virtual void Clear()
 9457    {
 9458        if (_clearReleasedSlots)
 1459            Array.Clear(_innerArray, 0, _count);
 9460        _count = 0;
 9461        _version++;
 9462    }
 463
 464    /// <summary>
 465    /// Clears the <see cref="SwiftList{T}"/> 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()
 1470    {
 1471        _count = 0;
 1472        _version++;
 1473    }
 474
 475    #endregion
 476
 477    #region Capacity Management
 478
 479    /// <summary>
 480    /// Ensures that the capacity of <see cref="SwiftList{T}"/> is sufficient to accommodate the specified number of ele
 481    /// The capacity can increase by double to balance memory allocation efficiency and space.
 482    /// </summary>
 483    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 484    public void EnsureCapacity(int capacity)
 2485    {
 2486        capacity = SwiftHashTools.NextPowerOfTwo(capacity);
 2487        if (capacity > _innerArray.Length)
 1488            Resize(capacity);
 2489    }
 490
 491    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 492    protected void Resize(int newSize)
 130493    {
 130494        int newCapacity = newSize <= DefaultCapacity ? DefaultCapacity : newSize;
 495
 130496        T[] newArray = new T[newCapacity];
 130497        if (_count > 0)
 40498            Array.Copy(_innerArray, 0, newArray, 0, _count);
 130499        _innerArray = newArray;
 130500        _version++;
 130501    }
 502
 503    /// <summary>
 504    /// Reduces the capacity of the SwiftList if the element count falls below 50% of the current capacity.
 505    /// Ensures efficient memory usage by resizing the internal array to match the current count when necessary.
 506    /// </summary>
 507    public void TrimExcessCapacity()
 2508    {
 2509        int newCapacity = _count < DefaultCapacity ? DefaultCapacity : SwiftHashTools.NextPowerOfTwo(_count);
 2510        Array.Resize(ref _innerArray, newCapacity);
 2511        _version++;
 2512    }
 513
 514    #endregion
 515
 516    #region Utility Methods
 517
 518    /// <summary>
 519    /// Searches for the specified object and returns the zero-based arrayIndex of the first occurrence within the Swift
 520    /// </summary>
 521    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 513522    public int IndexOf(T item) => Array.IndexOf(_innerArray, item, 0, _count);
 523
 524    int IList.IndexOf(object value)
 2525    {
 2526        int index = -1;
 527        try
 2528        {
 2529            index = Array.IndexOf(_innerArray, (T)value, 0, _count);
 1530        }
 1531        catch
 1532        {
 1533            throw new NotSupportedException($"Unsupported value type for {value}");
 534        }
 1535        return index;
 1536    }
 537
 538    /// <summary>
 539    /// Copies the elements of the SwiftList to a new array.
 540    /// </summary>
 541    public T[] ToArray()
 27542    {
 27543        T[] result = new T[_count];
 27544        Array.Copy(_innerArray, result, _count);
 27545        return result;
 27546    }
 547
 548    /// <summary>
 549    /// Returns a mutable span over the populated portion of the list.
 550    /// </summary>
 551    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 2552    public Span<T> AsSpan() => _innerArray.AsSpan(0, _count);
 553
 554    /// <summary>
 555    /// Returns a read-only span over the populated portion of the list.
 556    /// </summary>
 557    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 1558    public ReadOnlySpan<T> AsReadOnlySpan() => _innerArray.AsSpan(0, _count);
 559
 560    /// <summary>
 561    /// Returns a string that represents the current object.
 562    /// </summary>
 563    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 2564    public override string ToString() => _count == 0 ? base.ToString() : string.Join(", ", this);
 565
 566    /// <summary>
 567    /// Determines whether an element is in the SwiftList.
 568    /// </summary>
 569    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 3570    public bool Contains(T item) => IndexOf(item) != -1;
 571
 572    /// <summary>
 573    /// Determines whether the <see cref="SwiftList{T}"/> contains an element that matches the conditions defined by the
 574    /// </summary>
 575    /// <param name="match">The predicate that defines the conditions of the element to search for.</param>
 576    /// <returns><c>true</c> if the <see cref="SwiftList{T}"/> contains one or more elements that match the specified pr
 577    public bool Exists(Predicate<T> match)
 3578    {
 3579        SwiftThrowHelper.ThrowIfNull(match, nameof(match));
 580
 14581        for (int i = 0; i < _count; i++)
 6582        {
 6583            if (match(_innerArray[i]))
 1584                return true;
 5585        }
 586
 1587        return false;
 2588    }
 589
 590    /// <summary>
 591    /// Searches for an element that matches the conditions defined by the specified predicate, and returns the first oc
 592    /// </summary>
 593    /// <param name="match">The predicate that defines the conditions of the element to search for.</param>
 594    /// <returns>The first element that matches the conditions defined by the specified predicate, if found; otherwise, 
 595    public T Find(Predicate<T> match)
 4596    {
 4597        SwiftThrowHelper.ThrowIfNull(match, nameof(match));
 598
 20599        for (int i = 0; i < _count; i++)
 8600        {
 8601            if (match(_innerArray[i]))
 1602                return _innerArray[i];
 7603        }
 604
 2605        return default;
 3606    }
 607
 608    bool IList.Contains(object value)
 2609    {
 2610        int index = -1;
 611        try
 2612        {
 2613            index = IndexOf((T)value);
 1614        }
 1615        catch
 1616        {
 1617            throw new NotSupportedException($"Unsupported value type for {value}");
 618        }
 1619        return index != -1;
 1620    }
 621
 622    /// <summary>
 623    /// Swaps the values of two elements in the SwiftList.
 624    /// This method exchanges the values referenced by two variables.
 625    /// </summary>
 626    /// <param name="indexA">The first element to swap.</param>
 627    /// <param name="indexB">The second element to swap.</param>
 628    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 1629    public void Swap(int indexA, int indexB) => (_innerArray[indexB], _innerArray[indexA]) = (_innerArray[indexA], _inne
 630
 631    /// <summary>
 632    /// Copies the elements of the SwiftList to the specified target SwiftList.
 633    /// The target list will resize if it lacks sufficient capacity,
 634    /// but retains any existing elements beyond the copied range.
 635    /// </summary>
 636    public void CopyTo(SwiftList<T> target)
 2637    {
 2638        if (_count + 1 > target._innerArray.Length)
 1639            target.Resize(target._innerArray.Length * 2);
 2640        Array.Copy(_innerArray, 0, target._innerArray, 0, _count);
 2641        target._count = _count;
 2642        target._version++;
 2643    }
 644
 645    public void CopyTo(T[] array, int arrayIndex)
 4646    {
 4647        SwiftThrowHelper.ThrowIfNull(array, nameof(array));
 4648        if ((uint)arrayIndex > array.Length) throw new ArgumentOutOfRangeException(nameof(arrayIndex));
 6649        if (array.Length - arrayIndex < _count) throw new ArgumentException("Destination array is not long enough.", nam
 650
 2651        Array.Copy(_innerArray, 0, array, arrayIndex, _count);
 2652    }
 653
 654    /// <summary>
 655    /// Copies the populated elements of the SwiftList into the specified destination span.
 656    /// </summary>
 657    /// <param name="destination">The destination span.</param>
 658    public void CopyTo(Span<T> destination)
 2659    {
 2660        if (destination.Length < _count)
 1661            throw new ArgumentException("Destination span is not long enough.", nameof(destination));
 662
 1663        AsSpan().CopyTo(destination);
 1664    }
 665
 666    public void CopyTo(Array array, int arrayIndex)
 3667    {
 3668        SwiftThrowHelper.ThrowIfNull(array, nameof(array));
 4669        if (array.Rank != 1) throw new ArgumentException("Array must be single dimensional.", nameof(array));
 3670        if (array.GetLowerBound(0) != 0) throw new ArgumentException("Array must have zero-based indexing.", nameof(arra
 1671        if ((uint)arrayIndex > array.Length) throw new ArgumentOutOfRangeException(nameof(arrayIndex));
 1672        if (array.Length - arrayIndex < _count) throw new ArgumentException("Destination array is not long enough.", nam
 673
 1674        Array.Copy(_innerArray, 0, array, arrayIndex, _count);
 1675    }
 676
 677    public void CloneTo(ICollection<T> output)
 1678    {
 1679        output.Clear();
 9680        foreach (var item in this)
 3681            output.Add(item);
 1682    }
 683
 684    #endregion
 685
 686    #region Enumerators
 687
 688    /// <summary>
 689    /// Returns an enumerator that iterates through the <see cref="SwiftList{T}"/>.
 690    /// </summary>
 25691    public SwiftListEnumerator GetEnumerator() => new SwiftListEnumerator(this);
 20692    IEnumerator<T> IEnumerable<T>.GetEnumerator() => GetEnumerator();
 2693    IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
 694
 695    public struct SwiftListEnumerator : IEnumerator<T>, IEnumerator, IDisposable
 696    {
 697        private readonly SwiftList<T> _list;
 698        private readonly T[] _array;
 699        private readonly uint _count;
 700        private readonly uint _version;
 701        private uint _index;
 702
 703        private T _current;
 704
 705        public SwiftListEnumerator(SwiftList<T> list)
 25706        {
 25707            _list = list;
 25708            _array = list._innerArray;
 25709            _count = (uint)list._count;
 25710            _version = list._version;
 25711            _index = 0;
 25712            _current = default;
 25713        }
 714
 430715        public T Current => _current;
 716
 717        object IEnumerator.Current
 718        {
 719            [MethodImpl(MethodImplOptions.AggressiveInlining)]
 720            get
 3721            {
 4722                if (_index >= _count) throw new InvalidOperationException("Bad enumeration");
 2723                return _current;
 2724            }
 725        }
 726
 727        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 728        public bool MoveNext()
 245729        {
 245730            if (_version != _list._version)
 1731                throw new InvalidOperationException("Collection was modified during enumeration.");
 732
 265733            if (_index >= _count) return false;
 223734            _current = _array[_index++];
 223735            return true;
 244736        }
 737
 738        public void Reset()
 2739        {
 2740            if (_version != _list._version)
 1741                throw new InvalidOperationException("Collection was modified during enumeration.");
 742
 1743            _index = 0;
 1744            _current = default;
 1745        }
 746
 42747        public void Dispose() { }
 748    }
 749
 750    #endregion
 751}

Methods/Properties

.cctor()
.cctor()
.ctor()
.ctor(System.Int32)
.ctor(System.Collections.Generic.IEnumerable`1<T>)
.ctor(SwiftCollections.SwiftArrayState`1<T>)
get_InnerArray()
get_Capacity()
get_Count()
get_IsReadOnly()
get_IsSynchronized()
System.Collections.ICollection.get_SyncRoot()
System.Collections.IList.get_IsFixedSize()
System.Collections.IList.get_Item(System.Int32)
System.Collections.IList.set_Item(System.Int32,System.Object)
get_Item(System.Int32)
set_Item(System.Int32,T)
get_State()
set_State(SwiftCollections.SwiftArrayState`1<T>)
Add(T)
System.Collections.IList.Add(System.Object)
AddRange(System.Collections.Generic.IEnumerable`1<T>)
AddRange(T[])
AddRange(System.ReadOnlySpan`1<T>)
Remove(T)
System.Collections.IList.Remove(System.Object)
RemoveAt(System.Int32)
RemoveAll(System.Predicate`1<T>)
Insert(System.Int32,T)
System.Collections.IList.Insert(System.Int32,System.Object)
Reverse()
Sort()
Sort(System.Collections.Generic.IComparer`1<T>)
Clear()
FastClear()
EnsureCapacity(System.Int32)
Resize(System.Int32)
TrimExcessCapacity()
IndexOf(T)
System.Collections.IList.IndexOf(System.Object)
ToArray()
AsSpan()
AsReadOnlySpan()
ToString()
Contains(T)
Exists(System.Predicate`1<T>)
Find(System.Predicate`1<T>)
System.Collections.IList.Contains(System.Object)
Swap(System.Int32,System.Int32)
CopyTo(SwiftCollections.SwiftList`1<T>)
CopyTo(T[],System.Int32)
CopyTo(System.Span`1<T>)
CopyTo(System.Array,System.Int32)
CloneTo(System.Collections.Generic.ICollection`1<T>)
GetEnumerator()
System.Collections.Generic.IEnumerable<T>.GetEnumerator()
System.Collections.IEnumerable.GetEnumerator()
.ctor(SwiftCollections.SwiftList`1<T>)
get_Current()
System.Collections.IEnumerator.get_Current()
MoveNext()
Reset()
Dispose()