deque

package module
v2.24.2 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2024 License: MIT Imports: 3 Imported by: 2

README

deque GoDoc Coverage Status

Deque is deque container using Go generics.

Wikipedia says:

In computer science, a double-ended queue (abbreviated to deque, pronounced deck, like "cheque") is an abstract data type that generalizes a queue, for which elements can be added to or removed from either the front (head) or back (tail).

Usage

// Make a new deque of ints
d := deque.Of(9, 8, 7, 6)

// Sort it
sort.Sort(deque.Sortable[int]{d})
// d is 6, 7, 8, 9

// Add 5, 4, 3, 2, 1 to the front
for i := 5; i > 0; i-- {
    d.PushFront(i)
}

// Deque{ len: 9, cap: 16, items: [1, 2, 3, 4, 5, 6, 7, 8, 9]}
fmt.Println(d)

// Now reverse loop through items
// Prints 9 8 7 6 5 4 3 2 1
for _, n := range d.Reverse() {
    fmt.Print(n, " ")
}
fmt.Println()

Documentation

Overview

Package deque provides a type-safe, slice-backed, double-ended queue.

See https://en.wikipedia.org/wiki/Double-ended_queue

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. It is not concurrency safe.

func Make

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

Make creates a deque with a prereserved capacity.

func Of

func Of[T any](items ...T) *Deque[T]

Of constructs a new Deque.

func (*Deque[T]) At

func (d *Deque[T]) At(n int) (t T, ok bool)

At returns the zero indexed nth item of the deque, if any.

func (*Deque[T]) Back

func (d *Deque[T]) Back() (t T, ok bool)

Back returns the last value of the deque, if any.

func (*Deque[T]) Cap

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

Cap returns the total current capacity of the deque.

func (*Deque[T]) Clip

func (d *Deque[T]) Clip()

Clip removes unused capacity from the deque.

func (*Deque[T]) Front

func (d *Deque[T]) Front() (v T, ok bool)

Front returns the first value of the deque, if any.

func (*Deque[T]) Grow

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

Grow increases the deque's capacity, if necessary, to guarantee space for another n elements. After Grow(n), at least n elements can be appended to the deque without another allocation. If n is negative, Grow panics.

func (*Deque[T]) Len

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

Len returns the current length of the deque.

func (*Deque[T]) PushBack

func (d *Deque[T]) PushBack(v T)

PushBack adds new value v to the end of the deque.

func (*Deque[T]) PushFront

func (d *Deque[T]) PushFront(v T)

PushFront adds a new value v to the front of the deque.

func (*Deque[T]) RemoveBack

func (d *Deque[T]) RemoveBack() (t T, ok bool)

RemoveBack removes and returns the back of the deque, if any.

func (*Deque[T]) RemoveFront

func (d *Deque[T]) RemoveFront() (t T, ok bool)

RemoveFront removes and returns the front of the deque, if any.

func (*Deque[T]) String

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

String implements fmt.Stringer.

func (*Deque[T]) Swap

func (d *Deque[T]) Swap(i, j int)

Swap swaps the elements with indexes i and j.

type Sortable

type Sortable[T cmp.Ordered] struct {
	*Deque[T]
}

Sortable is a deque that can be sorted with sort.Sort.

func (Sortable[T]) Less

func (sd Sortable[T]) Less(i, j int) bool

Less implements sort.Interface.

Jump to

Keyboard shortcuts

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