deque

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2025 License: GPL-3.0 Imports: 4 Imported by: 2

Documentation

Overview

Package deque implements a double-ended queue (deque) implemented with a growable ring buffer.

This queue has O(1) amortized inserts and removals from both ends of the container. It also has O(1) indexing like a vector.

The core implementation is "ported" (stolen) from Rust's VecDeque.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Deque

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

Deque is a double-ended queue. The zero value is ready for use.

func From

func From[S ~[]T, T any](slice S) *Deque[T]

From creates a new queue using the given slice as the backing buffer.

func WithCapacity

func WithCapacity[T any](cap int) *Deque[T]

WithCapacity allocates a deque with the given capacity.

func (*Deque[T]) All

func (q *Deque[T]) All() iter.Seq2[int, T]

All returns an iterator over the elements in the deque. It does not pop any elements.

func (*Deque[T]) At

func (q *Deque[T]) At(i int) T

At returns the item at position i. At panics if i < 0 or i >= q.Len().

func (*Deque[T]) Cap

func (q *Deque[T]) Cap() int

Cap returns the number of elements the deque can hold without reallocating.

func (*Deque[T]) Grow

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

Grow makes space for at least n more elements to be inserted in the given deque without reallocation.

func (*Deque[T]) Len

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

Len returns the number of elements in the deque.

func (*Deque[T]) PopAll

func (q *Deque[T]) PopAll() iter.Seq[T]

PopAll empties the deque and returns an iterator over the popped elements. It's not safe to modify the deque while iterating using PopAll.

func (*Deque[T]) PopBack

func (q *Deque[T]) PopBack() (T, bool)

PopBack removes and returns the last item in the deque if it is non-empty.

func (*Deque[T]) PopFront

func (q *Deque[T]) PopFront() (T, bool)

PopFront removes and returns the item at index 0 if the deque is non-empty.

func (*Deque[T]) PushBack

func (q *Deque[T]) PushBack(values ...T)

PushBack appends the given items to the back of the deque.

func (*Deque[T]) PushFront

func (q *Deque[T]) PushFront(values ...T)

PushFront prepends the given items to the front of the deque.

func (*Deque[T]) Reset

func (q *Deque[T]) Reset()

Reset empties the deque, retaining the underlying storage for use by future pushes.

func (*Deque[T]) String added in v1.1.0

func (q *Deque[T]) String() string

String displays the deque as a string, using fmt.Sprint to show each element.

Jump to

Keyboard shortcuts

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