arrayqueue

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2024 License: BSD-2-Clause, ISC Imports: 5 Imported by: 0

Documentation

Overview

Package arrayqueue implements a queue backed by array list.

Structure is not thread safe.

Reference: https://en.wikipedia.org/wiki/Queue_(abstract_data_type)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Iterator

type Iterator[T comparable] struct {
	// contains filtered or unexported fields
}

Iterator returns a stateful iterator whose values can be fetched by an index.

func (*Iterator[T]) Begin

func (iterator *Iterator[T]) Begin()

Begin resets the iterator to its initial state (one-before-first) Call Next() to fetch the first element if any.

func (*Iterator[T]) End

func (iterator *Iterator[T]) End()

End moves the iterator past the last element (one-past-the-end). Call Prev() to fetch the last element if any.

func (*Iterator[T]) First

func (iterator *Iterator[T]) First() bool

First moves the iterator to the first element and returns true if there was a first element in the container. If First() returns true, then first element's index and value can be retrieved by Index() and Value(). Modifies the state of the iterator.

func (*Iterator[T]) Index

func (iterator *Iterator[T]) Index() int

Index returns the current element's index. Does not modify the state of the iterator.

func (*Iterator[T]) Last

func (iterator *Iterator[T]) Last() bool

Last moves the iterator to the last element and returns true if there was a last element in the container. If Last() returns true, then last element's index and value can be retrieved by Index() and Value(). Modifies the state of the iterator.

func (*Iterator[T]) Next

func (iterator *Iterator[T]) Next() bool

Next moves the iterator to the next element and returns true if there was a next element in the container. If Next() returns true, then next element's index and value can be retrieved by Index() and Value(). If Next() was called for the first time, then it will point the iterator to the first element if it exists. Modifies the state of the iterator.

func (*Iterator[T]) NextTo

func (iterator *Iterator[T]) NextTo(f func(index int, value T) bool) bool

NextTo moves the iterator to the next element from current position that satisfies the condition given by the passed function, and returns true if there was a next element in the container. If NextTo() returns true, then next element's index and value can be retrieved by Index() and Value(). Modifies the state of the iterator.

func (*Iterator[T]) Prev

func (iterator *Iterator[T]) Prev() bool

Prev moves the iterator to the previous element and returns true if there was a previous element in the container. If Prev() returns true, then previous element's index and value can be retrieved by Index() and Value(). Modifies the state of the iterator.

func (*Iterator[T]) PrevTo

func (iterator *Iterator[T]) PrevTo(f func(index int, value T) bool) bool

PrevTo moves the iterator to the previous element from current position that satisfies the condition given by the passed function, and returns true if there was a next element in the container. If PrevTo() returns true, then next element's index and value can be retrieved by Index() and Value(). Modifies the state of the iterator.

func (*Iterator[T]) Value

func (iterator *Iterator[T]) Value() T

Value returns the current element's value. Does not modify the state of the iterator.

type Queue

type Queue[T comparable] struct {
	// contains filtered or unexported fields
}

Queue holds elements in an array-list

func New

func New[T comparable]() *Queue[T]

New instantiates a new empty queue

func (*Queue[T]) Clear

func (queue *Queue[T]) Clear()

Clear removes all elements from the queue.

func (*Queue[T]) Dequeue

func (queue *Queue[T]) Dequeue() (value T, ok bool)

Dequeue removes first element of the queue and returns it, or nil if queue is empty. Second return parameter is true, unless the queue was empty and there was nothing to dequeue.

func (*Queue[T]) Empty

func (queue *Queue[T]) Empty() bool

Empty returns true if queue does not contain any elements.

func (*Queue[T]) Enqueue

func (queue *Queue[T]) Enqueue(value T)

Enqueue adds a value to the end of the queue

func (*Queue[T]) FromJSON

func (queue *Queue[T]) FromJSON(data []byte) error

FromJSON populates the queue from the input JSON representation.

func (*Queue[T]) Iterator

func (queue *Queue[T]) Iterator() *Iterator[T]

Iterator returns a stateful iterator whose values can be fetched by an index.

func (*Queue[T]) MarshalJSON

func (queue *Queue[T]) MarshalJSON() ([]byte, error)

MarshalJSON @implements json.Marshaler

func (*Queue[T]) Peek

func (queue *Queue[T]) Peek() (value T, ok bool)

Peek returns first element of the queue without removing it, or nil if queue is empty. Second return parameter is true, unless the queue was empty and there was nothing to peek.

func (*Queue[T]) Size

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

Size returns number of elements within the queue.

func (*Queue[T]) String

func (queue *Queue[T]) String() string

String returns a string representation of container

func (*Queue[T]) ToJSON

func (queue *Queue[T]) ToJSON() ([]byte, error)

ToJSON outputs the JSON representation of the queue.

func (*Queue[T]) UnmarshalJSON

func (queue *Queue[T]) UnmarshalJSON(bytes []byte) error

UnmarshalJSON @implements json.Unmarshaler

func (*Queue[T]) Values

func (queue *Queue[T]) Values() []T

Values returns all elements in the queue (FIFO order).

Jump to

Keyboard shortcuts

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