Struct StackSlim<T>
- Namespace
- LfrlAnvil
- Assembly
- LfrlAnvil.Core.dll
Represents a slim version of last-in, first-out collection of objects.
public struct StackSlim<T>
Type Parameters
T
Element type.
- Inherited Members
- Extension Methods
Properties
Capacity
Gets the number of maximum elements that this stack can contain without resizing the underlying buffer.
public int Capacity { get; }
Property Value
Count
Gets the number of elements contained in this stack.
public readonly int Count { get; }
Property Value
IsEmpty
Specifies whether or not this stack is empty.
public bool IsEmpty { get; }
Property Value
this[int]
Gets a reference to an element at the given index.
public ref T this[int index] { get; }
Parameters
index
intThe zero-based index of the element reference to get.
Property Value
- T
Exceptions
- ArgumentOutOfRangeException
When
index
is out of bounds.
Methods
AsMemory()
Creates a new ReadOnlyMemory<T> instance that represents a view of this stack.
[Pure]
public ReadOnlyMemory<T> AsMemory()
Returns
- ReadOnlyMemory<T>
New ReadOnlyMemory<T> instance.
AsSpan()
Creates a new ReadOnlySpan<T> instance that represents a view of this stack.
[Pure]
public ReadOnlySpan<T> AsSpan()
Returns
- ReadOnlySpan<T>
New ReadOnlySpan<T> instance.
Clear()
Removes all elements from this stack.
public void Clear()
Create(int)
Creates a new empty StackSlim<T> instance.
[Pure]
public static StackSlim<T> Create(int minCapacity = 0)
Parameters
Returns
- StackSlim<T>
New StackSlim<T> instance.
GetEnumerator()
Creates a new ReadOnlySpan<T>.Enumerator instance for this stack.
[Pure]
public ReadOnlySpan<T>.Enumerator GetEnumerator()
Returns
- ReadOnlySpan<T>.Enumerator
New ReadOnlySpan<T>.Enumerator instance.
Pop()
Attempts to remove the top item from this stack.
public bool Pop()
Returns
- bool
true when stack was not empty and top item was removed, otherwise false.
PopRange(int)
Attempts to remove a range of top items from this stack.
public int PopRange(int count)
Parameters
count
intNumber of elements to remove.
Returns
- int
Actual number of removed elements.
Remarks
Does nothing, when count
is less than or equal to 0.
When count
is greater than stack's Count,
then clears the stack and returns its old Count.
Push(T)
Adds item
to the top of this stack.
public void Push(T item)
Parameters
item
TItem to add.
PushRange(ReadOnlySpan<T>)
Adds a range of items
to the top of this stack.
public void PushRange(ReadOnlySpan<T> items)
Parameters
items
ReadOnlySpan<T>Items to add.
Remarks
Does nothing when items
are empty.
ResetCapacity(int)
Attempts to increase or decrease this stack's Capacity, while ensuring that all current elements will fit.
public void ResetCapacity(int minCapacity = 0)
Parameters
Top()
Returns a reference to the element at the top of this stack.
[Pure]
public ref T Top()
Returns
- T
Reference to the top element.
Remarks
May return an invalid reference, when this stack is empty.
TryPop(out T)
Attempts to remove and return the top item from this stack.
public bool TryPop(out T item)
Parameters
item
Tout parameter that returns the removed top item.
Returns
- bool
true when stack was not empty and top item was removed, otherwise false.