Class SwiftList<T>
- Namespace
- SwiftCollections
- Assembly
- SwiftCollections.dll
SwiftList<T> is a high-performance, memory-efficient dynamic list designed to outperform
traditional generic lists in speed-critical applications.
By utilizing custom growth and shrink strategies, SwiftList optimizes memory allocation and minimizes resizing overhead, all while maintaining compact storage. With aggressive inlining and optimized algorithms, SwiftList delivers faster iteration, insertion, and overall memory management compared to standard List. It is ideal for scenarios where predictable performance and minimal memory allocations are essential.
This implementation is optimized for performance and does not perform versioning checks. Modifying the list during enumeration may result in undefined behavior.
[JsonConverter(typeof(StateJsonConverterFactory))]
[MemoryPackable(GenerateType.Object)]
public class SwiftList<T> : IStateBacked<SwiftArrayState<T>>, ISwiftCloneable<T>, IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable, IMemoryPackable<SwiftList<T>>, IMemoryPackFormatterRegister
Type Parameters
TSpecifies the type of elements in the list.
- Inheritance
-
SwiftList<T>
- Implements
-
IStateBacked<SwiftArrayState<T>>IList<T>ICollection<T>IEnumerable<T>IMemoryPackable<SwiftList<T>>IMemoryPackFormatterRegister
- Derived
- Inherited Members
- Extension Methods
Remarks
MemoryPack GenerateType: Object
SwiftCollections.SwiftArrayState<T> State
Constructors
SwiftList()
Initializes a new instance of the SwiftList class that is empty and has the default initial capacity.
public SwiftList()
SwiftList(SwiftArrayState<T>)
Initializes a new instance of the SwiftList<T> class with the specified SwiftArrayState<T>.
[MemoryPackConstructor]
public SwiftList(SwiftArrayState<T> state)
Parameters
stateSwiftArrayState<T>The state containing the internal array, count, offset, and version for initialization.
SwiftList(IEnumerable<T>)
Initializes a new instance of the SwiftList<T> class with elements from the specified collection. The collection must have a known count for optimized memory allocation.
public SwiftList(IEnumerable<T> items)
Parameters
itemsIEnumerable<T>
Exceptions
- ArgumentException
Thrown if the input collection does not have a known count.
SwiftList(int)
Initializes a new, empty instance of SwiftList<T> with the specified initial capacity.
public SwiftList(int capacity)
Parameters
capacityint
Fields
DefaultCapacity
The default initial capacity of the SwiftList<T> if none is specified. Used to allocate a reasonable starting size to minimize resizing operations.
public const int DefaultCapacity = 8
Field Value
_count
The current number of elements in the SwiftList. Represents the total count of valid elements stored in the list, also indicating the arrayIndex of the next insertion point.
protected int _count
Field Value
_innerArray
The internal array that stores elements of the SwiftList. Resized as needed to accommodate additional elements. Not directly exposed outside the list.
protected T[] _innerArray
Field Value
- T[]
_version
The version of the SwiftList, used to track modifications.
protected uint _version
Field Value
Properties
Capacity
Gets the total number of elements the SwiftList can hold without resizing. Reflects the current allocated size of the internal array.
[JsonIgnore]
[MemoryPackIgnore]
public int Capacity { get; }
Property Value
Count
The current number of elements in the SwiftList. Represents the total count of valid elements stored in the list, also indicating the arrayIndex of the next insertion point.
[JsonIgnore]
[MemoryPackIgnore]
public int Count { get; }
Property Value
InnerArray
Gets the underlying array that stores the elements of the collection.
[JsonIgnore]
[MemoryPackIgnore]
public T[] InnerArray { get; }
Property Value
- T[]
IsFixedSize
[JsonIgnore]
[MemoryPackIgnore]
public bool IsFixedSize { get; }
Property Value
IsReadOnly
[JsonIgnore]
[MemoryPackIgnore]
public bool IsReadOnly { get; }
Property Value
IsSynchronized
[JsonIgnore]
[MemoryPackIgnore]
public bool IsSynchronized { get; }
Property Value
this[int]
Gets the element at the specified arrayIndex.
[JsonIgnore]
[MemoryPackIgnore]
public T this[int index] { get; set; }
Parameters
indexint
Property Value
- T
State
Gets or sets the current state of the array, including its items and count.
[JsonInclude]
[MemoryPackInclude]
public SwiftArrayState<T> State { get; }
Property Value
Remarks
Setting this property replaces the entire contents of the array with the items from the specified state. The previous contents are discarded. The version is reset when the state is set.
SyncRoot
[JsonIgnore]
[MemoryPackIgnore]
public object SyncRoot { get; }
Property Value
Methods
Add(T)
Adds an object to the end of the SwiftList.
public virtual void Add(T item)
Parameters
itemT
AddRange(IEnumerable<T>)
Adds the elements of the specified collection to the end of the SwiftList.
public virtual void AddRange(IEnumerable<T> items)
Parameters
itemsIEnumerable<T>
Remarks
Known-count sources reserve capacity before enumeration to avoid repeated growth.
AddRange(ReadOnlySpan<T>)
Adds the elements of the specified span to the end of the SwiftList.
public virtual void AddRange(ReadOnlySpan<T> items)
Parameters
itemsReadOnlySpan<T>The span whose elements should be appended.
AddRange(T[])
Adds the elements of the specified array to the end of the SwiftList.
public virtual void AddRange(T[] items)
Parameters
itemsT[]The array whose elements should be appended.
AsReadOnlySpan()
Returns a read-only span over the populated portion of the list.
public ReadOnlySpan<T> AsReadOnlySpan()
Returns
- ReadOnlySpan<T>
AsSpan()
Returns a mutable span over the populated portion of the list.
public Span<T> AsSpan()
Returns
- Span<T>
Clear()
Removes all elements from the SwiftList<T>, resetting its count to zero.
public virtual void Clear()
CloneTo(ICollection<T>)
Clones the entire ISwiftCloneable<T> into a new target ICollection<T>, ensuring that the target list is an exact copy. Clears the target list first to match the structure and state of the source list exactly.
public void CloneTo(ICollection<T> output)
Parameters
outputICollection<T>
Contains(T)
Determines whether an element is in the SwiftList.
public bool Contains(T item)
Parameters
itemT
Returns
CopyTo(SwiftList<T>)
Copies the elements of the SwiftList to the specified target SwiftList. The target list will resize if it lacks sufficient capacity, but retains any existing elements beyond the copied range.
public void CopyTo(SwiftList<T> target)
Parameters
targetSwiftList<T>
CopyTo(Array, int)
public void CopyTo(Array array, int arrayIndex)
Parameters
CopyTo(Span<T>)
Copies the populated elements of the SwiftList into the specified destination span.
public void CopyTo(Span<T> destination)
Parameters
destinationSpan<T>The destination span.
CopyTo(T[], int)
public void CopyTo(T[] array, int arrayIndex)
Parameters
arrayT[]arrayIndexint
EnsureCapacity(int)
Ensures that the capacity of SwiftList<T> is sufficient to accommodate the specified number of elements. The capacity can increase by double to balance memory allocation efficiency and space.
public void EnsureCapacity(int capacity)
Parameters
capacityint
Exists(Predicate<T>)
Determines whether the SwiftList<T> contains an element that matches the conditions defined by the specified predicate.
public bool Exists(Predicate<T> match)
Parameters
matchPredicate<T>The predicate that defines the conditions of the element to search for.
Returns
- bool
trueif the SwiftList<T> contains one or more elements that match the specified predicate; otherwise,false.
FastClear()
Clears the SwiftList<T> without releasing the reference to the stored elements. Use FastClear() when you want to quickly reset the list without reallocating memory.
public void FastClear()
Find(Predicate<T>)
Searches for an element that matches the conditions defined by the specified predicate, and returns the first occurrence within the entire SwiftList<T>.
public T Find(Predicate<T> match)
Parameters
matchPredicate<T>The predicate that defines the conditions of the element to search for.
Returns
- T
The first element that matches the conditions defined by the specified predicate, if found; otherwise, the default value for type
T.
GetEnumerator()
Returns an enumerator that iterates through the SwiftList<T>.
public SwiftList<T>.SwiftListEnumerator GetEnumerator()
Returns
IndexOf(T)
Searches for the specified object and returns the zero-based arrayIndex of the first occurrence within the SwiftList.
public int IndexOf(T item)
Parameters
itemT
Returns
Insert(int, T)
Inserts an element into the SwiftList at the specified arrayIndex.
public virtual void Insert(int index, T item)
Parameters
indexintitemT
Remove(T)
Removes the first occurrence of a specific object from the SwiftList.
public virtual bool Remove(T item)
Parameters
itemT
Returns
RemoveAll(Predicate<T>)
Removes all the elements that match the conditions defined by the specified predicate.
public virtual int RemoveAll(Predicate<T> match)
Parameters
matchPredicate<T>
Returns
RemoveAt(int)
Removes the element at the specified arrayIndex of the SwiftList.
public virtual void RemoveAt(int index)
Parameters
indexint
Resize(int)
Resizes the internal array to accommodate the specified number of elements.
protected void Resize(int newSize)
Parameters
newSizeintThe desired new size of the internal array. Must be greater than or equal to zero.
Remarks
If the specified size is less than or equal to the default capacity, the internal array is set to the default capacity. Existing elements are preserved up to the current count.
Reverse()
Reverses the order of the elements in the entire SwiftList<T>.
public void Reverse()
SortInPlace(IComparer<T>?)
Sorts the populated elements of the SwiftList<T> in place.
public void SortInPlace(IComparer<T>? comparer = null)
Parameters
comparerIComparer<T>The comparer to use, or
nullto use the default comparer.
Remarks
The sort is performed over the active [0..Count) range of the backing array and does not
allocate additional collection storage. Pass a comparer to define a custom order; otherwise the
default comparer for T is used.
SortInPlace<TComparer>(TComparer)
Sorts the populated elements of the SwiftList<T> in place using a struct comparer.
public void SortInPlace<TComparer>(TComparer comparer) where TComparer : struct, IComparer<T>
Parameters
comparerTComparerThe comparer to use.
Type Parameters
TComparer
Remarks
This overload avoids boxing struct comparers and lets the JIT devirtualize comparer calls in hot paths.
Swap(int, int)
Swaps the values of two elements in the SwiftList. This method exchanges the values referenced by two variables.
public void Swap(int indexA, int indexB)
Parameters
ToArray()
Copies the elements of the SwiftList to a new array.
public T[] ToArray()
Returns
- T[]
ToString()
Returns a string that represents the current collection.
public override string ToString()
Returns
- string
A comma-separated list of the collection's elements, or the default string representation if the collection is empty.
Remarks
This method provides a human-readable representation of the collection's contents, which can be useful for debugging or logging purposes.
TrimExcessCapacity()
Reduces the capacity of the SwiftList if the element count falls below 50% of the current capacity. Ensures efficient memory usage by resizing the internal array to match the current count when necessary.
public void TrimExcessCapacity()