Documentation
¶
Overview ¶
Package queue implements an array-based FIFO queue.
Index ¶
- type Queue
- func (q *Queue[T]) Add(v T)
- func (q *Queue[T]) Append(ts []T) []T
- func (q *Queue[T]) Back() T
- func (q *Queue[T]) Clear()
- func (q *Queue[T]) Each(f func(T) bool)
- func (q *Queue[T]) Front() T
- func (q *Queue[T]) Grow(n int) *Queue[T]
- func (q *Queue[T]) IsEmpty() bool
- func (q *Queue[T]) Len() int
- func (q *Queue[T]) Peek(n int) (T, bool)
- func (q *Queue[T]) Pop() (T, bool)
- func (q *Queue[T]) PopLast() (T, bool)
- func (q *Queue[T]) Push(v T)
- func (q *Queue[T]) Slice() []T
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 NewSize ¶ added in v0.8.1
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]) 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 ¶
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
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]) Peek ¶
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 ¶
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
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.