Repository navigation
StackableList
Namespace: Lowry.Utils.CodeExtensions
public sealed class StackableList<T> : IReadOnlyList<T>Stores a "stack" of unique items.
Using Push will add/move an item to the top of the list Top.
Using Pop will remove a specified item and return a result describing the removal, not the removed item.
Items are unique according to EqualityComparer<T>.Default, not necessarily reference identity. The top is the last item in index order. Indexing and enumeration run from the bottom to the top, unlike Stack<T> enumeration.
T has no generic constraints. There is an implicit public parameterless constructor that creates an empty list; there is no collection constructor. Reference and nullable types can store null.
Upon popping, a StackableListPopResults is returned to indicate whether the item was:
- Removed from the top, and the top spot taken by another item.
- Removed from the top, but the list is now empty.
- Removed from anywhere but the top of the list.
- Not found in the list.
public enum PopResult
{
notFound,
removedFromTop_Replaced,
removedFromTop_Empty,
removed
}public T Top { get; }Returns: the last item, or
default(T)if the list is empty. A stored default value is indistinguishable from an empty list through this property alone; checkCount.
public int Count { get; }Returns: how many items are currently in the stack.
public T this[int index] { get; }Returns: the item found at the specified
index.
An invalid index throws ArgumentOutOfRangeException.
public T Push (T item)Adds an item to the top, or removes an equal item from below the top and appends the supplied item. If the list is nonempty and the supplied item already equals
Top, the list is unchanged.Returns:
- If the list is nonempty and
itemequalsTop, returns the supplieditem, which need not be the same reference as the stored top item.- Otherwise, returns the previous
Top, ordefault(T)when adding to an empty list.
The return value alone cannot reliably distinguish these cases when default values are stored.
An empty list accepts any value, including default(T), such as 0 for int, false for bool, or null for reference types.
public StackableListPopResults<T> Pop (T item)Try to remove an item from the stack.
Returns: A StackableListPopResults with a PopResult describing the removal. Its
itemis the new top only forremovedFromTop_Replaced; it isdefault(T)for every other result.
public void Clear ()Clears all items from the stack.
public IEnumerator<T> GetEnumerator ()Returns: an enumerator that iterates in index order, from bottom to top. Nongeneric enumeration is also supported. Changes to the underlying list invalidate its enumerators.
public int FindIndex (Predicate<T> match)Returns the zero-based index of the first matching item, or
-1if none matches. Anullpredicate throwsArgumentNullException.
public int IndexOf (T item)Returns the zero-based index of the first equal item, or
-1if it is not found.
public bool Contains (T item)Determines whether the items contain a specified element by using the default equality comparer.
public T Find (Predicate<T> match)Returns the first matching item in index order, or
default(T)if none matches. Anullpredicate throwsArgumentNullException.
public bool Exists (Predicate<T> match)Determines whether the items contain elements that match the conditions defined by the specified predicate.
A null predicate throws ArgumentNullException.
👉🏻 Download InputLayers on the Unity Asset Store!