Table of Contents

Class SwiftStack<T>

Namespace
SwiftCollections
Assembly
SwiftCollections.dll

Represents a fast, array-based stack (LIFO - Last-In-First-Out) collection of objects.

The SwiftStack<T> class provides O(1) time complexity for Push and Pop operations, making it highly efficient for scenarios where performance is critical. It minimizes memory allocations by reusing internal arrays and offers methods like FastClear to quickly reset the stack without deallocating memory.

This implementation is optimized for performance and does not perform versioning checks. Modifying the stack during enumeration may result in undefined behavior.

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

Type Parameters

T

Specifies the type of elements in the stack.

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

Remarks

MemoryPack GenerateType: Object

SwiftCollections.SwiftArrayState<T> State

Constructors

SwiftStack()

Initializes a new, empty instance of SwiftStack.

public SwiftStack()

SwiftStack(SwiftArrayState<T>)

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

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

Parameters

state SwiftArrayState<T>

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

SwiftStack(IEnumerable<T>)

Initializes a new instance of the SwiftStack<T> class that contains elements copied from the specified collection.

public SwiftStack(IEnumerable<T> items)

Parameters

items IEnumerable<T>

The collection whose elements are copied to the new stack. Cannot be null.

Remarks

The elements are copied onto the stack in the order they are returned by the enumerator of the collection, so that the last element in the collection becomes the top of the stack.

SwiftStack(int)

Initializes a new, empty instance of SwiftStack with the specified initial capacity.

public SwiftStack(int capacity)

Parameters

capacity int

Fields

DefaultCapacity

The default initial capacity of the SwiftStack 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 total number of elements the SwiftQueue can hold without resizing. Reflects the current allocated size of the internal array.

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

Property Value

int

Count

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

Property Value

int

InnerArray

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

Property Value

T[]

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; set; }

Parameters

index int

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

SwiftArrayState<T>

Remarks

Setting this property replaces the entire contents of the array with the items from the specified state. The setter is intended for internal use and may reset the array's version and capacity.

Methods

AsReadOnlySpan()

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

public ReadOnlySpan<T> AsReadOnlySpan()

Returns

ReadOnlySpan<T>

AsSpan()

Returns a mutable span over the populated portion of the stack.

public Span<T> AsSpan()

Returns

Span<T>

Clear()

Removes all elements from the SwiftStack, resetting its count to zero.

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(Span<T>)

Copies the populated elements of the SwiftStack into the specified destination span.

public void CopyTo(Span<T> destination)

Parameters

destination Span<T>

The destination span.

CopyTo(T[], int)

Copies the elements of the collection to the specified array, starting at the specified array index.

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

Parameters

array T[]

The one-dimensional array that is the destination of the elements copied from the collection. The array must have zero-based indexing.

arrayIndex int

The zero-based index in the destination array at which copying begins.

Exceptions

ArgumentOutOfRangeException

Thrown when arrayIndex is less than 0 or greater than the length of array.

ArgumentException

Thrown when the number of elements in the source collection is greater than the available space from arrayIndex to the end of array.

EnsureCapacity(int)

Ensures that the internal storage has at least the specified capacity, resizing if necessary.

public void EnsureCapacity(int capacity)

Parameters

capacity int

The minimum number of elements that the internal storage should be able to hold. Must be non-negative.

Remarks

If the current capacity is less than the specified value, the internal storage is increased to the next power of two greater than or equal to capacity. No action is taken if the current capacity is sufficient.

Exists(Predicate<T>)

Determines whether the SwiftStack<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 SwiftStack<T> contains one or more elements that match the specified predicate; otherwise, false.

FastClear()

Clears the SwiftStack 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 matching element in stack enumeration 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()

public SwiftStack<T>.SwiftStackEnumerator GetEnumerator()

Returns

SwiftStack<T>.SwiftStackEnumerator

Peek()

Returns the object at the top of the SwiftStack without removing it.

public T Peek()

Returns

T

Pop()

Removes and returns the object at the top of the SwiftStack.

public T Pop()

Returns

T

Push(T)

Inserts an object at the top of the SwiftStack.

public void Push(T item)

Parameters

item T

PushRange(ReadOnlySpan<T>)

Pushes the elements of the specified span onto the stack in order.

public void PushRange(ReadOnlySpan<T> items)

Parameters

items ReadOnlySpan<T>

The span whose elements should be pushed.

ToString()

Returns a string that represents the current stack, including its type and the number of elements it contains.

public override string ToString()

Returns

string

A string containing the type name and the current element count if the stack is not empty; otherwise, a string indicating that the stack is empty.

TrimCapacity()

Sets the capacity of a SwiftStack<T> to the actual number of elements it contains, rounded up to a nearby next power of 2 value.

public void TrimCapacity()