arrayqueue

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](queue *ArrayQueue[T], f func(item T))

Iterate over the queue in the forward direction 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 queue items. To modify the queue items, use ForwardMap. To accumulate values over the queue, use ForwardFold.

func ForwardFold added in v1.1.0

func ForwardFold[T any, G any](queue *ArrayQueue[T], initialAccumulator G, f func(item T, accumulator G) G) G

Iterate over the queue 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 ArrayQueue to allow for generic accumulators.

func ForwardMap added in v1.1.0

func ForwardMap[T any](queue *ArrayQueue[T], f func(item T) T)

Iterate over the queue in the forward direction 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 queue items, use ForwardApply. To accumulate values over the queue, use ForwardFold.

func ReverseApply added in v1.1.0

func ReverseApply[T any](queue *ArrayQueue[T], f func(item T))

Iterate over the queue in the reverse direction 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 queue items. To modify the queue items, use ReverseMap. To accumulate values over the queue, use ReverseFold.

func ReverseFold added in v1.1.0

func ReverseFold[T any, G any](queue *ArrayQueue[T], initialAccumulator G, f func(item T, accumulator G) G) G

Iterate over the queue 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 ArrayQueue to allow for generic accumulators.

func ReverseMap added in v1.1.0

func ReverseMap[T any](queue *ArrayQueue[T], f func(item T) T)

Iterate over the queue in the reverse direction 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 queue items, use ReverseApply. To accumulate values over the queue, use ReverseFold.

Types

type ArrayQueue

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

Implement a queue using a array / slice.

Queues are a first in, first out data structure. Items added to the queue are removed in the order they were added.

func New

func New[T any]() *ArrayQueue[T]

Create a new ArrayQueue using github.com/hmcalister/Go-DSA/list/ArrayQueue as a backing data structure.

func (*ArrayQueue[T]) Add

func (queue *ArrayQueue[T]) Add(item T)

Enqueue an item, adding it to the end of the queue.

func (*ArrayQueue[T]) Find

func (queue *ArrayQueue[T]) Find(predicate func(item T) bool) (T, error)

Find the first item in a queue matching a predicate. The queue is traversed from front to back.

Returns (item, nil) if the item is present, or (*new(T), dsa_error.ErrorItemNotFound) if the item is not present.

func (*ArrayQueue[T]) FindAll

func (queue *ArrayQueue[T]) FindAll(predicate func(item T) bool) []T

Find all items in a queue matching a predicate. The queue is traversed from front to back.

Returns all items from the queue that match the predicate.

func (*ArrayQueue[T]) ForwardIterator added in v1.2.0

func (queue *ArrayQueue[T]) ForwardIterator() iter.Seq2[int, T]

Iterate over the items of the queue in the forward direction (front to back). Returns both the index (as counted from the front of the queue) and item. This method is not concurrency safe. For concurrent applications, consider using a mutex, or pull the data out using Items().

func (*ArrayQueue[T]) Items added in v1.1.0

func (queue *ArrayQueue[T]) Items() []T

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

func (*ArrayQueue[T]) Peek

func (queue *ArrayQueue[T]) Peek() (T, error)

Peek at the front item in the queue.

Returns a dsa_error.ErrorDataStructureEmpty if the queue is empty.

func (*ArrayQueue[T]) Remove

func (queue *ArrayQueue[T]) Remove() (T, error)

Dequeue an item, removing from the front of the queue.

Returns a dsa_error.ErrorDataStructureEmpty error if the queue is empty.

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

func (queue *ArrayQueue[T]) ReverseIterator() iter.Seq2[int, T]

Iterate over the items of the queue in the reverse direction (back to front). Returns both the index (as counted from the back of the queue) and item. This method is not concurrency safe. For concurrent applications, consider using a mutex, or pull the data out using Items().

func (*ArrayQueue[T]) Size

func (queue *ArrayQueue[T]) Size() int

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

Jump to

Keyboard shortcuts

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