Skip to content

StackableList

Eric Lowry edited this page Aug 7, 2024 · 2 revisions

Namespace: Lowry.Utils.CodeExtensions

public sealed class StackableList<T> : List<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 an item from the list and return it.

💡General Information

This class functions as a hybrid between a List🔗 and a Stack🔗, offering the possibility of pushing (stacking) unique items from a list, and then popping them (pulling them out).

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.

📄Enums

PopResult

public enum PopResult
{
	notFound,
	removedFromTop_Replaced,
	removedFromTop_Empty,
	removed
}

📄Properties

Top

public T Top {get {...} }

Returns: the item currently at the top of the stack.

📄Methods

Push(T)

public T Push (T item) {...}

Try to add a new item to the top of the stack, or move an already present item to the top.

Returns:

  • If default (usually null): The item was added to an empty list.
  • If same as input: The item was already at the top if the stack.
  • If other item of type T: The item was added to the top of the stack;
    This returned item was previously at the top of the stack.

Pop(T)

public StackableListPopResults<T> Pop (T item) {...}

Try to remove an item from the stack.

Returns: A StackableListPopResults containing an item of type T and a PopResult that indicates the result of the action.

Clone this wiki locally