Table of Contents

Class SwiftSortedList<T>

Namespace
SwiftCollections
Assembly
SwiftCollections.dll

Represents a dynamically sorted collection of elements. Provides efficient O(log n) operations for adding, removing, and checking for the presence of elements.

[JsonConverter(typeof(StateJsonConverterFactory))]
[MemoryPackable(GenerateType.Object)]
public class SwiftSortedList<T> : IStateBacked<SwiftArrayState<T>>, ISwiftCloneable<T>, ICollection<T>, ICollection, IReadOnlyList<T>, IReadOnlyCollection<T>, IEnumerable<T>, IEnumerable, IMemoryPackable<SwiftSortedList<T>>, IMemoryPackFormatterRegister

Type Parameters

T

The type of elements in the collection.

Inheritance
SwiftSortedList<T>
Implements
IStateBacked<SwiftArrayState<T>>
IMemoryPackable<SwiftSortedList<T>>
IMemoryPackFormatterRegister
Inherited Members
Extension Methods

Remarks

The comparer is not serialized. After deserialization the list uses Default.

If a custom comparer is required it can be reapplied using SetComparer(IComparer<T>).

Constructors

SwiftSortedList()

Initializes a new instance of the SwiftSortedList class with the default capacity.

public SwiftSortedList()

Remarks

This constructor creates an empty sorted list. Items added to the list will be automatically ordered according to the default comparer for the item type.

SwiftSortedList(SwiftArrayState<T>)

Initializes a new instance of the SwiftSortedList<T> class with the specified SwiftArrayState<T>.

[MemoryPackConstructor]
public SwiftSortedList(SwiftArrayState<T> state)

Parameters

state SwiftArrayState<T>

The state containing the internal array, count, offset, and version for initialization.

SwiftSortedList(IComparer<T>)

Initializes a new, empty instance of SwiftSortedList<T> uisng the specified IComparer<T>.

public SwiftSortedList(IComparer<T> comparer)

Parameters

comparer IComparer<T>

SwiftSortedList(IEnumerable<T>, IComparer<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 SwiftSortedList(IEnumerable<T> items, IComparer<T>? comparer = null)

Parameters

items IEnumerable<T>
comparer IComparer<T>

Exceptions

ArgumentException

Thrown if the input collection does not have a known count.

SwiftSortedList(int, IComparer<T>?)

Initializes a new, empty instance of SwiftSortedList<T> with the specified initial capacity and IComparer<T>.

public SwiftSortedList(int capacity, IComparer<T>? comparer = null)

Parameters

capacity int

The starting initial capacity.

comparer IComparer<T>

The comparer to use. If null, the default comparer is used.

Fields

DefaultCapacity

The default initial capacity of the SwiftSortedList<T> if none is specified. Used to allocate a reasonable starting size to minimize resizing operations.

public const int DefaultCapacity = 8

Field Value

int

Properties

Capacity

Gets the current capacity of the internal array.

[JsonIgnore]
[MemoryPackIgnore]
public int Capacity { get; }

Property Value

int

Comparer

[JsonIgnore]
[MemoryPackIgnore]
public IComparer<T> Comparer { get; }

Property Value

IComparer<T>

Count

[JsonIgnore]
[MemoryPackIgnore]
public int Count { get; }

Property Value

int

InnerArray

Gets the underlying array that stores the elements of the collection.

[JsonIgnore]
[MemoryPackIgnore]
public T[] InnerArray { get; }

Property Value

T[]

Remarks

The returned array may contain unused elements beyond the logical contents of the collection.

IsReadOnly

[JsonIgnore]
[MemoryPackIgnore]
public bool IsReadOnly { get; }

Property Value

bool

IsSynchronized

[JsonIgnore]
[MemoryPackIgnore]
public bool IsSynchronized { get; }

Property Value

bool

this[int]

Gets the element at the specified arrayIndex.

[JsonIgnore]
[MemoryPackIgnore]
public T this[int index] { get; }

Parameters

index int

Property Value

T

Offset

[JsonIgnore]
[MemoryPackIgnore]
public int Offset { get; }

Property Value

int

State

Gets or sets the current state of the array, including its items and structure.

[JsonInclude]
[MemoryPackInclude]
public SwiftArrayState<T> State { get; }

Property Value

SwiftArrayState<T>

Remarks

Setting this property replaces the entire contents of the array with the specified state. The setter is intended for internal use and may reset internal metadata such as version and comparer. This property is intended for serialization and deserialization scenarios.

SyncRoot

[JsonIgnore]
[MemoryPackIgnore]
public object SyncRoot { get; }

Property Value

object

Version

[JsonIgnore]
[MemoryPackIgnore]
public uint Version { get; }

Property Value

uint

Methods

Add(T)

public void Add(T item)

Parameters

item T

AddRange(IEnumerable<T>)

Adds a range of elements to the collection, ensuring they are sorted and merged efficiently.

public void AddRange(IEnumerable<T> items)

Parameters

items IEnumerable<T>

Remarks

This compacts the active item range for efficiency. Known-count sources reuse existing capacity when possible.

AsReadOnlySpan()

Returns a read-only span over the populated sorted portion of the list.

public ReadOnlySpan<T> AsReadOnlySpan()

Returns

ReadOnlySpan<T>

Clear()

public 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

output ICollection<T>

Contains(T)

public bool Contains(T item)

Parameters

item T

Returns

bool

CopyTo(Array, int)

public void CopyTo(Array array, int arrayIndex)

Parameters

array Array
arrayIndex int

CopyTo(T[], int)

public void CopyTo(T[] array, int arrayIndex)

Parameters

array T[]
arrayIndex int

EnsureCapacity(int)

Ensures that the capacity of SwiftSortedList<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

capacity int

Exists(Predicate<T>)

Determines whether the SwiftSortedList<T> contains an element that matches the conditions defined by the specified predicate.

public bool Exists(Predicate<T> match)

Parameters

match Predicate<T>

The predicate that defines the conditions of the element to search for.

Returns

bool

true if the SwiftSortedList<T> contains one or more elements that match the specified predicate; otherwise, false.

FastClear()

Quickly clears the list by resetting the count and offset without modifying the internal array. Note: This leaves references in the internal array, which may prevent garbage collection of reference types. Use when performance is critical and you are certain that residual references are acceptable.

public void FastClear()

Find(Predicate<T>)

Searches for an element that matches the conditions defined by the specified predicate, and returns the first matching element in sorted order.

public T Find(Predicate<T> match)

Parameters

match Predicate<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 SwiftSortedList<T>.

public SwiftSortedList<T>.SwiftSorterEnumerator GetEnumerator()

Returns

SwiftSortedList<T>.SwiftSorterEnumerator

IndexOf(T)

Searches for the specified item in the sorted collection and returns the arrayIndex of the first occurrence.

public int IndexOf(T item)

Parameters

item T

The item to search for.

Returns

int

The zero-based arrayIndex of the item if found; otherwise, -1.

InsertionPoint(T)

Determines the insertion point for a specified item in the collection. The insertion point is the arrayIndex where the item would be inserted if it were not already present.

public int InsertionPoint(T item)

Parameters

item T

The item for which to find the insertion point.

Returns

int

The insertion point as a zero-based arrayIndex.

PeekMax()

Returns the maximum element in the sorter without removing it.

public T PeekMax()

Returns

T

The maximum element.

PeekMin()

Returns the minimum element in the sorter without removing it.

public T PeekMin()

Returns

T

The minimum element.

PopMax()

Removes and returns the maximum element in the sorter.

public T PopMax()

Returns

T

The maximum element.

PopMin()

Removes and returns the minimum element in the sorter.

public T PopMin()

Returns

T

The minimum element.

Remove(T)

public bool Remove(T item)

Parameters

item T

Returns

bool

RemoveAt(int)

Removes the element at the specified arrayIndex from the sorted list. Shifts elements as needed to maintain the sorted order and efficient space utilization.

public void RemoveAt(int index)

Parameters

index int

The zero-based arrayIndex of the element to remove.

Search(T)

Searches for the specified item in the sorted collection.

public int Search(T item)

Parameters

item T

The item to search for.

Returns

int

The arrayIndex of the item if found or where the item should be inserted if not found.

SetComparer(IComparer<T>)

Sets a new comparer for the sorted list and re-sorts the elements.

public void SetComparer(IComparer<T> comparer)

Parameters

comparer IComparer<T>

The new comparer to use.