Documentation
¶
Index ¶
- func ForwardApply[T any](stack *LinkedListStack[T], f func(item T))
- func ForwardFold[T any, G any](stack *LinkedListStack[T], initialAccumulator G, ...) G
- func ForwardMap[T any](stack *LinkedListStack[T], f func(item T) T)
- func ReverseApply[T any](stack *LinkedListStack[T], f func(item T))
- func ReverseFold[T any, G any](stack *LinkedListStack[T], initialAccumulator G, ...) G
- func ReverseMap[T any](stack *LinkedListStack[T], f func(item T) T)
- type LinkedListStack
- func (stack *LinkedListStack[T]) Add(item T)
- func (stack *LinkedListStack[T]) Find(predicate func(item T) bool) (T, error)
- func (stack *LinkedListStack[T]) FindAll(predicate func(item T) bool) []T
- func (stack *LinkedListStack[T]) ForwardIterator() iter.Seq2[int, T]
- func (stack *LinkedListStack[T]) Items() []T
- func (stack *LinkedListStack[T]) Peek() (T, error)
- func (stack *LinkedListStack[T]) Remove() (T, error)
- func (stack *LinkedListStack[T]) ReverseIterator() iter.Seq2[int, T]
- func (stack *LinkedListStack[T]) Size() int
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ForwardApply ¶ added in v1.1.0
func ForwardApply[T any](stack *LinkedListStack[T], f func(item T))
Iterate over the stack in the forward direction (bottom to top) and apply a function to each item.
Idiomatic Go should likely use ForwardIterator() rather than functional methods.
It is expected that ForwardApply does *not* update the stack items. To modify the stack items, use ForwardMap. To accumulate values over the stack, use ForwardFold.
Internally, this method calls linkedlist.ForwardApply
func ForwardFold ¶ added in v1.1.0
func ForwardFold[T any, G any](stack *LinkedListStack[T], initialAccumulator G, f func(item T, accumulator G) G) G
Iterate over the stack (bottom to top) and apply the function f to it. The function f also takes the current value of the accumulator. The results of f become the new value of the accumulator at each step.
This function returns the final accumulator.
Idiomatic Go should likely use ForwardIterator() rather than functional methods.
This function is not a method on LinkedListStack to allow for generic accumulators.
Internally, this method calls linkedlist.ForwardFold
func ForwardMap ¶ added in v1.1.0
func ForwardMap[T any](stack *LinkedListStack[T], f func(item T) T)
Iterate over the stack in the forward direction (bottom to top) and apply a function to each item The result of this function is then assigned to the node at each step.
Idiomatic Go should likely use ForwardIterator() rather than functional methods.
ForwardMap can update the node items by returning the update value. If you do not need to modify the stack items, use ForwardApply. To accumulate values over the stack, use ForwardFold.
Internally, this method calls linkedlist.ForwardMap
func ReverseApply ¶ added in v1.1.0
func ReverseApply[T any](stack *LinkedListStack[T], f func(item T))
Iterate over the stack in the reverse direction (top to bottom) and apply a function to each item.
Idiomatic Go should likely use ReverseIterator() rather than functional methods.
It is expected that ReverseApply does *not* update the stack items. To modify the stack items, use ReverseMap. To accumulate values over the stack, use ReverseFold.
Internally, this method calls linkedlist.ReverseApply
func ReverseFold ¶ added in v1.1.0
func ReverseFold[T any, G any](stack *LinkedListStack[T], initialAccumulator G, f func(item T, accumulator G) G) G
Iterate over the stack (top to bottom) and apply the function f to it. The function f also takes the current value of the accumulator. The results of f become the new value of the accumulator at each step.
This function returns the final accumulator.
Idiomatic Go should likely use ReverseIterator() rather than functional methods.
This function is not a method on LinkedListStack to allow for generic accumulators.
Internally, this method calls linkedlist.ReverseFold
func ReverseMap ¶ added in v1.1.0
func ReverseMap[T any](stack *LinkedListStack[T], f func(item T) T)
Iterate over the stack in the reverse direction (top to bottom) and apply a function to each item The result of this function is then assigned to the node at each step.
Idiomatic Go should likely use ReverseIterator() rather than functional methods.
ReverseMap can update the node items by returning the update value. If you do not need to modify the stack items, use ReverseApply. To accumulate values over the stack, use ReverseFold.
Internally, this method calls linkedlist.ReverseMap
Types ¶
type LinkedListStack ¶
type LinkedListStack[T any] struct { // contains filtered or unexported fields }
Implement a stack using a linked list.
Stacks are a last in, first out data structure. Items added to the stack are removed in the reverse order they are added.
func New ¶
func New[T any]() *LinkedListStack[T]
Create a new LinkedListStack using github.com/hmcalister/Go-DSA/list/LinkedList as a backing data structure.
func (*LinkedListStack[T]) Add ¶
func (stack *LinkedListStack[T]) Add(item T)
Add an item to the top of the stack.
func (*LinkedListStack[T]) Find ¶
func (stack *LinkedListStack[T]) Find(predicate func(item T) bool) (T, error)
Find the first item in a stack matching a predicate. The stack is traversed from top to bottom.
Returns (item, nil) if the item is present, or (*new(T), dsa_error.ErrorItemNotFound) if the item is not present.
func (*LinkedListStack[T]) FindAll ¶
func (stack *LinkedListStack[T]) FindAll(predicate func(item T) bool) []T
Find all items in a stack matching a predicate. The stack is traversed from top to bottom.
Returns all items from the stack that match the predicate.
func (*LinkedListStack[T]) ForwardIterator ¶ added in v1.2.0
func (stack *LinkedListStack[T]) ForwardIterator() iter.Seq2[int, T]
Iterate over the items of the stack in the forward direction (bottom to top). Returns both the index (as counted from the bottom of the stack) and item. This method is not concurrency safe. For concurrent applications, consider using a mutex, or pull the data out using Items().
func (*LinkedListStack[T]) Items ¶ added in v1.1.0
func (stack *LinkedListStack[T]) Items() []T
Get all items from the stack. This method allocates an array of length equal to the number of items.
func (*LinkedListStack[T]) Peek ¶
func (stack *LinkedListStack[T]) Peek() (T, error)
Peek at the top item in the stack.
Returns a dsa_error.ErrorDataStructureEmpty error if the stack is empty.
func (*LinkedListStack[T]) Remove ¶
func (stack *LinkedListStack[T]) Remove() (T, error)
Remove an item from the top of the stack.
Returns a dsa_error.ErrorDataStructureEmpty error if the stack is empty.
func (*LinkedListStack[T]) ReverseIterator ¶ added in v1.2.0
func (stack *LinkedListStack[T]) ReverseIterator() iter.Seq2[int, T]
Iterate over the items of the stack in the reverse direction (top to bottom). Returns both the index (as counted from the top of the stack) and item. This method is not concurrency safe. For concurrent applications, consider using a mutex, or pull the data out using Items().
func (*LinkedListStack[T]) Size ¶
func (stack *LinkedListStack[T]) Size() int
Get the size of the stack, the number of items in the stack.