Skip to main content

AtomsStack<T> class

Namespace: Arlecchino.Atoms.Collections · Assembly: Arlecchino.Core

A stack held as one piece of application state, put on and taken off the top. AtomsStack.Value reads from the top down, so Value[0] is what AtomsStack.Peek answers.

public abstract class AtomsStack<T> : IReadableAtom<IReadOnlyList<T>>

Implements IReadableAtom<IReadOnlyList<T><T>>

Constructors

MemberSummary
AtomsStack(IReadOnlyList<T>)Creates the stack.

Properties

MemberSummary
CountHow many are on it.
IsEmptyWhether nothing is on it.
RecordsHistoryWhether changes of this stack enter the undo history.
ValueWhat is on the stack now, top first, as a live view rather than a copy. It is read-only all the way down, so every change goes through the members below.

Methods

MemberSummary
Clear()Takes everything off. An empty stack changes nothing.
GetEnumerator()Walks the stack from the top down, so foreach over it reads the way it does over a Stack<T>. Reach for AtomsStack.Value where a sequence is what is wanted, LINQ included.
Peek()Reads the one on top without taking it, and throws when the stack is empty.
Pop()Takes the one on top, and throws when the stack is empty.
Push(T)Puts something on top.
Push(IReadOnlyList<T>)Puts several on at once, the last of them ending up on top — what a loop of AtomsStack.Push would leave, in one notification and one undo step.
Reset(IReadOnlyList<T>)Replaces what is on the stack in one go, for a stack that is rebuilt rather than unwound. Contents equal to what is already there change nothing.
Subscribe(Action)Calls back whenever what is on the stack changes.
TryPeek(out T)Reads the one on top without taking it or throwing.
TryPop(out T)Takes the one on top without throwing when the stack is empty.

Constructors in detail

AtomsStack(IReadOnlyList<T>)

public AtomsStack(IReadOnlyList<T> initial);

Creates the stack.

Parameters

NameTypeDescription
initialIReadOnlyList<T><T>What is already on it, top first; empty when omitted. It is copied, not held.

Properties in detail

Count

public int Count { get; }

How many are on it.

Type int

IsEmpty

public bool IsEmpty { get; }

Whether nothing is on it.

Type bool

RecordsHistory

public abstract bool RecordsHistory { get; }

Whether changes of this stack enter the undo history.

Type bool

Value

public IReadOnlyList<T> Value { get; }

What is on the stack now, top first, as a live view rather than a copy. It is read-only all the way down, so every change goes through the members below.

Type IReadOnlyList<T><T>

Methods in detail

Clear()

public void Clear();

Takes everything off. An empty stack changes nothing.

GetEnumerator()

public List<T> GetEnumerator();

Walks the stack from the top down, so foreach over it reads the way it does over a Stack<T>. Reach for AtomsStack.Value where a sequence is what is wanted, LINQ included.

Returns Enumerator<T><T> — The enumerator, which throws when the stack changes while it is being walked.

Peek()

public T Peek();

Reads the one on top without taking it, and throws when the stack is empty.

Returns T — What is on top.

Pop()

public T Pop();

Takes the one on top, and throws when the stack is empty.

Returns T — What was on top.

Push(T)

public void Push(T item);

Puts something on top.

Parameters

NameTypeDescription
itemTWhat to put on.

Push(IReadOnlyList<T>)

public void Push(IReadOnlyList<T> items);

Puts several on at once, the last of them ending up on top — what a loop of AtomsStack.Push would leave, in one notification and one undo step.

Parameters

NameTypeDescription
itemsIReadOnlyList<T><T>What to put on. Putting none on changes nothing.

Reset(IReadOnlyList<T>)

public void Reset(IReadOnlyList<T> items);

Replaces what is on the stack in one go, for a stack that is rebuilt rather than unwound. Contents equal to what is already there change nothing.

Parameters

NameTypeDescription
itemsIReadOnlyList<T><T>What should be on it instead, top first.

Subscribe(Action)

public IDisposable Subscribe(Action listener);

Calls back whenever what is on the stack changes.

Parameters

NameTypeDescription
listenerActionWhat to run on change.

Returns IDisposable — Dispose it to stop listening.

TryPeek(out T)

public bool TryPeek(out T item);

Reads the one on top without taking it or throwing.

Parameters

NameTypeDescription
itemTWhat is on top, when there is one.

Returns booltrue when there was one.

TryPop(out T)

public bool TryPop(out T item);

Takes the one on top without throwing when the stack is empty.

Parameters

NameTypeDescription
itemTWhat was on top, when there was one.

Returns booltrue when something was taken.