queue

package
v0.29.2 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2026 License: BSD-3-Clause Imports: 2 Imported by: 3

Documentation

Overview

Package queue implements an array-based FIFO queue.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Queue

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

Queue is an array-based queue of values. A zero Queue is ready for use. Items can be added and removed at either end of the queue. Use Queue.Add and Queue.Pop for first-in, first-out semantics; use Queue.Push and Queue.PopLast for last-in, first-out semantics.

Add, Push, Pop, and PopLast operations take amortized O(1) time and storage. All other operations on a Queue are constant time and space.

func New added in v0.8.1

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

New constructs a new empty Queue.

func NewSize added in v0.8.1

func NewSize[T any](n int) *Queue[T]

NewSize constructs a new empty Queue with storage pre-allocated for n items. The queue will automatically grow beyond the initial size as needed.

func (*Queue[T]) Add

func (q *Queue[T]) Add(v T)

Add adds v to the end (tail) of q.

func (*Queue[T]) Append added in v0.24.3

func (q *Queue[T]) Append(ts []T) []T

Append appends the values of q to ts in order from oldest to newest, and returns the resulting slice.

func (*Queue[T]) Back added in v0.25.3

func (q *Queue[T]) Back() T

Back returns the backmost (newest) element of q. If q is empty, Back returns a zero value. This is equivalent to Peek(-1).

func (*Queue[T]) Clear

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

Clear discards all the values in q, leaving it empty.

func (*Queue[T]) Each

func (q *Queue[T]) Each(f func(T) bool)

Each is a range function that calls f with each value, in q, in order from oldest to newest. If f returns false, Each returns immediately.

func (*Queue[T]) Front

func (q *Queue[T]) Front() T

Front returns the frontmost (oldest) element of q. If q is empty, Front returns a zero value. This is equivalent to Peek(0).

func (*Queue[T]) Grow added in v0.28.1

func (q *Queue[T]) Grow(n int) *Queue[T]

Grow increases the allocated capacity of q, if necessary, to guarantee space for another n elements. The existing contents of q are not changed. It will panic if n is negative or too large to be allocated. Grow returns q to allow chaining.

func (*Queue[T]) IsEmpty

func (q *Queue[T]) IsEmpty() bool

IsEmpty reports whether q is empty.

func (*Queue[T]) Len

func (q *Queue[T]) Len() int

Len reports the number of entries in q.

func (*Queue[T]) Peek

func (q *Queue[T]) Peek(n int) (T, bool)

Peek reports whether q has a value at offset n from the front of the queue, and if so returns its value. Peek(0) returns the same value as Queue.Front. Negative offsets count forward from the back of the queue.

func (*Queue[T]) Pop

func (q *Queue[T]) Pop() (T, bool)

Pop reports whether q is non-empty, and if so removes and returns its frontmost (oldest) value. If q is empty, Pop returns a zero value.

func (*Queue[T]) PopLast added in v0.20.0

func (q *Queue[T]) PopLast() (T, bool)

PopLast reports whether q is non-empty, and if so removes and returns its rearmost (newest) value. If q is empty, PopLast returns a zero value.

func (*Queue[T]) Push added in v0.20.0

func (q *Queue[T]) Push(v T)

Push adds v to the front (head) of q.

func (*Queue[T]) Slice

func (q *Queue[T]) Slice() []T

Slice returns a slice of the values of q in order from oldest to newest. If q is empty, Slice returns nil.

Jump to

Keyboard shortcuts

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