queue

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package deque implements a highly optimized double-ended queue, which is much efficient compared with list.List when adding or removing elements from the beginning or the end.

Example
dq := NewDeque()
dq.PushBack(100)
dq.PushBack(200)
dq.PushBack(300)
for !dq.Empty() {
	fmt.Println(dq.PopFront())
}

dq.PushFront(100)
dq.PushFront(200)
dq.PushFront(300)
for i, n := 0, dq.Len(); i < n; i++ {
	fmt.Println(dq.PopFront())
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NumChunksAllocated

func NumChunksAllocated() int64

NumChunksAllocated returns the number of chunks allocated by now.

Types

type Deque

type Deque interface {
	// PushBack adds a new value v at the back of Deque.
	PushBack(v Elem)
	// PushFront adds a new value v at the front of Deque.
	PushFront(v Elem)
	// PopBack removes a value from the back of Deque and returns
	// the removed value or nil if the Deque is empty.
	PopBack() Elem
	// PopFront removes a value from the front of Deque and returns
	// the removed value or nil if the Deque is empty.
	PopFront() Elem
	// Back returns the last value of Deque or nil if the Deque is empty.
	Back() Elem
	// Front returns the first value of Deque or nil if the Deque is empty.
	Front() Elem
	// Empty returns whether Deque is empty.
	Empty() bool
	// Len returns the number of values in Deque.
	Len() int

	// Enqueue is an alias of PushBack.
	Enqueue(v Elem)
	// Dequeue is an alias of PopFront.
	Dequeue() Elem
	// DequeueMany removes a number of values from the front of Deque and
	// returns the removed values or nil if the Deque is empty.
	// If max <= 0, DequeueMany removes and returns all the values in Deque.
	DequeueMany(max int) []Elem
	// DequeueManyWithBuffer is similar to DequeueMany except that it uses
	// buf to store the removed values as long as it has enough space.
	DequeueManyWithBuffer(max int, buf []Elem) []Elem

	// Range iterates all the values in Deque.
	Range(f func(i int, v Elem) bool)

	// Peek returns the value at idx.
	Peek(idx int) Elem
	// Replace replaces the value at idx.
	Replace(idx int, v Elem)
}

Deque is a highly optimized double-ended queue.

func NewDeque

func NewDeque() Deque

NewDeque creates a new Deque instance.

type Elem

type Elem = interface{}

type Option

type Option[T any] func(option *Options[T])

Option is a function type used to set Options

func WithContainer

func WithContainer[T any](c container.Container[T]) Option[T]

WithContainer is used to set a Queue's underlying container

func WithGoroutineSafe

func WithGoroutineSafe[T any]() Option[T]

WithGoroutineSafe is used to set a Queue goroutine-safe

func WithListContainer

func WithListContainer[T any]() Option[T]

WithListContainer is used to set List as a Queue's underlying container

type Options

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

Options holds Queue's options

type Queue

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

Queue is a first-in-first-out data structure

func New

func New[T any](opts ...Option[T]) *Queue[T]

New creates a new queue

func (*Queue[T]) Back

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

Back returns the back value in the queue

func (*Queue[T]) Clear

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

Clear clears all elements in the queue

func (*Queue[T]) Empty

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

Empty returns true if the queue is empty, otherwise returns false

func (*Queue[T]) Front

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

Front returns the front value in the queue

func (*Queue[T]) Pop

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

Pop removes the the front element in the queue, and returns its value

func (*Queue[T]) Push

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

Push pushes a value to the end of the queue

func (*Queue[T]) Size

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

Size returns the amount of elements in the queue

func (*Queue[T]) String

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

String returns a string representation of the queue

Directories

Path Synopsis
Package deque implements a highly optimized double-ended queue, which is much efficient compared with list.List when adding or removing elements from the beginning or the end.
Package deque implements a highly optimized double-ended queue, which is much efficient compared with list.List when adding or removing elements from the beginning or the end.

Jump to

Keyboard shortcuts

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