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
TSpecifies the type of elements in the stack.
- Inheritance
-
SwiftStack<T>
- Implements
-
IStateBacked<SwiftArrayState<T>>ICollection<T>IEnumerable<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
stateSwiftArrayState<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
itemsIEnumerable<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
capacityint
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
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
Count
[JsonIgnore]
[MemoryPackIgnore]
public int Count { get; }
Property Value
InnerArray
[JsonIgnore]
[MemoryPackIgnore]
public T[] InnerArray { get; }
Property Value
- T[]
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 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
outputICollection<T>
Contains(T)
public bool Contains(T item)
Parameters
itemT
Returns
CopyTo(Array, int)
public void CopyTo(Array array, int arrayIndex)
Parameters
CopyTo(Span<T>)
Copies the populated elements of the SwiftStack into the specified destination span.
public void CopyTo(Span<T> destination)
Parameters
destinationSpan<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
arrayT[]The one-dimensional array that is the destination of the elements copied from the collection. The array must have zero-based indexing.
arrayIndexintThe zero-based index in the destination array at which copying begins.
Exceptions
- ArgumentOutOfRangeException
Thrown when
arrayIndexis less than 0 or greater than the length ofarray.- ArgumentException
Thrown when the number of elements in the source collection is greater than the available space from
arrayIndexto the end ofarray.
EnsureCapacity(int)
Ensures that the internal storage has at least the specified capacity, resizing if necessary.
public void EnsureCapacity(int capacity)
Parameters
capacityintThe 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
matchPredicate<T>The predicate that defines the conditions of the element to search for.
Returns
- bool
trueif 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
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()
public SwiftStack<T>.SwiftStackEnumerator GetEnumerator()
Returns
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
itemT
PushRange(ReadOnlySpan<T>)
Pushes the elements of the specified span onto the stack in order.
public void PushRange(ReadOnlySpan<T> items)
Parameters
itemsReadOnlySpan<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()