arraystack

package
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 13, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ForwardApply added in v1.1.0

func ForwardApply[T any](stack *ArrayStack[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.

func ForwardFold added in v1.1.0

func ForwardFold[T any, G any](stack *ArrayStack[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 ArrayStack to allow for generic accumulators.

func ForwardMap added in v1.1.0

func ForwardMap[T any](stack *ArrayStack[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.

func ReverseApply added in v1.1.0

func ReverseApply[T any](stack *ArrayStack[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.

func ReverseFold added in v1.1.0

func ReverseFold[T any, G any](stack *ArrayStack[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 ArrayStack to allow for generic accumulators.

func ReverseMap added in v1.1.0

func ReverseMap[T any](stack *ArrayStack[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.

Types

type ArrayStack

type ArrayStack[T any] struct {
	// contains filtered or unexported fields
}

Implement a stack using a array (slice) as the backing data structure.

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]() *ArrayStack[T]

Create a new ArrayStack using an array as a backing data structure.

func (*ArrayStack[T]) Add

func (stack *ArrayStack[T]) Add(item T)

Add an item to the top of the stack.

func (*ArrayStack[T]) Find

func (stack *ArrayStack[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 (*ArrayStack[T]) FindAll

func (stack *ArrayStack[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 (*ArrayStack[T]) ForwardIterator added in v1.2.0

func (stack *ArrayStack[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 (*ArrayStack[T]) Items added in v1.1.0

func (stack *ArrayStack[T]) Items() []T

Get all items from the stack. This method allocates an array of length equal to the number of items..

func (*ArrayStack[T]) Peek

func (stack *ArrayStack[T]) Peek() (T, error)

Peek at the top item in the stack.

Returns a dsa_error.ErrorDataStructureEmpty if stack is empty.

func (*ArrayStack[T]) Remove

func (stack *ArrayStack[T]) Remove() (T, error)

Remove an item from the top of the stack.

Returns a dsa_error.ErrorDataStructureEmpty if the stack is empty.

func (*ArrayStack[T]) ReverseIterator added in v1.2.0

func (stack *ArrayStack[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 (*ArrayStack[T]) Size

func (stack *ArrayStack[T]) Size() int

Get the size of the stack, the number of items in the stack.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL