deque

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2022 License: MIT Imports: 0 Imported by: 0

README

Deque for generics

GoDoc Build Status Go Report Card codecov License

Fork of gammazero's deque

Fast ring-buffer deque (double-ended queue) implementation.

For a pictorial description, see the Deque diagram

Important note

This package intended to work with only Go's version of 1.18 and above (due the usage of generics).

You can read corresponding information here: https://tip.golang.org/doc/go1.18

Installation

$ go get github.com/LdDl/deque

Deque data structure

Deque generalizes a queue and a stack, to efficiently add and remove items at either end with O(1) performance. Queue (FIFO) operations are supported using PushBack() and PopFront(). Stack (LIFO) operations are supported using PushBack() and PopBack().

Ring-buffer Performance

This deque implementation is optimized for CPU and GC performance. The circular buffer automatically re-sizes by powers of two, growing when additional capacity is needed and shrinking when only a quarter of the capacity is used, and uses bitwise arithmetic for all calculations. Since growth is by powers of two, adding elements will only cause O(log n) allocations.

The ring-buffer implementation improves memory and time performance with fewer GC pauses, compared to implementations based on slices and linked lists. By wrapping around the buffer, previously used space is reused, making allocation unnecessary until all buffer capacity is used. This is particularly efficient when data going into the dequeue is relatively balanced against data coming out. However, if size changes are very large and only fill and then empty then deque, the ring structure offers little benefit for memory reuse. For that usage pattern a different implementation may be preferable.

For maximum speed, this deque implementation leaves concurrency safety up to the application to provide, however the application chooses, if needed at all.

Reading Empty Deque

Since it is OK for the deque to contain a nil value, it is necessary to either panic or return a second boolean value to indicate the deque is empty, when reading or removing an element. This deque panics when reading from an empty deque. This is a run-time check to help catch programming errors, which may be missed if a second return value is ignored. Simply check Deque.Len() before reading from the deque.

Example

package main

import (
    "fmt"
    "github.com/LdDl/deque"
)

func main() {
    var q deque.Deque[string]
    q.PushBack("foo")
    q.PushBack("bar")
    q.PushBack("baz")

    fmt.Println(q.Len())   // Prints: 3
    fmt.Println(q.Front()) // Prints: foo
    fmt.Println(q.Back())  // Prints: baz

    q.PopFront() // remove "foo"
    q.PopBack()  // remove "baz"

    q.PushFront("hello")
    q.PushBack("world")

    // Consume deque and print elements.
    for q.Len() != 0 {
        fmt.Println(q.PopFront())
    }
}

Uses

Deque can be used as both a:

  • Queue using PushBack and PopFront
  • Stack using PushBack and PopBack

Documentation

Overview

Package deque provides a fast ring-buffer deque (double-ended queue) implementation.

Deque generalizes a queue and a stack, to efficiently add and remove items at either end with O(1) performance. Queue (FIFO) operations are supported using PushBack() and PopFront(). Stack (LIFO) operations are supported using PushBack() and PopBack().

Ring-buffer Performance

The ring-buffer automatically resizes by powers of two, growing when additional capacity is needed and shrinking when only a quarter of the capacity is used, and uses bitwise arithmetic for all calculations.

The ring-buffer implementation significantly improves memory and time performance with fewer GC pauses, compared to implementations based on slices and linked lists.

For maximum speed, this deque implementation leaves concurrency safety up to the application to provide, however the application chooses, if needed at all.

Reading Empty Deque

Since it is OK for the deque to contain a nil value, it is necessary to either panic or return a second boolean value to indicate the deque is empty, when reading or removing an element. This deque panics when reading from an empty deque. This is a run-time check to help catch programming errors, which may be missed if a second return value is ignored. Simply check Deque.Len() before reading from the deque.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Zero

func Zero[T any]() T

Types

type Deque

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

Deque represents a single instance of the deque data structure.

func New

func New[T any](size ...int) *Deque[T]

New Returns a new created instance of Deque, optionally setting the current and minimum capacity when non-zero values are given for these.

To create a Deque with capacity to store 2048 items without resizing, and that will not resize below space for 32 items when removing items:

d := deque.New(2048, 32)

To create a Deque that has not yet allocated memory, but after it does will never resize to have space for less than 64 items:

d := deque.New(0, 64)

Note that any values supplied here are rounded up to the nearest power of 2.

provided = 63
    =>
fixed = 64

func (*Deque[T]) At

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

At returns the element at index i in the queue without removing the element from the queue. This method accepts only non-negative index values. At(0) refers to the first element and is the same as Front(). At(Len()-1) refers to the last element and is the same as Back(). If the index is invalid, the call panics.

The purpose of At is to allow Deque to serve as a more general purpose circular buffer, where items are only added to and removed from the ends of the deque, but may be read from any place within the deque. Consider the case of a fixed-size circular log buffer: A new entry is pushed onto one end and when full the oldest is popped from the other end. All the log entries in the buffer must be readable without altering the buffer contents.

func (*Deque[T]) Back

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

Back returns the element at the back of the queue. This is the element that would be returned by PopBack(). This call panics if the queue is empty.

func (*Deque[T]) Cap

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

Cap returns the current capacity of the Deque. If q is nil, q.Cap() is zero.

func (*Deque[T]) Clear

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

Clear removes all elements from the queue, but retains the current capacity. This is useful when repeatedly reusing the queue at high frequency to avoid GC during reuse. The queue will not be resized smaller as long as items are only added. Only when items are removed is the queue subject to getting resized smaller.

func (*Deque[T]) Front

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

Front returns the element at the front of the queue. This is the element that would be returned by PopFront(). This call panics if the queue is empty.

func (*Deque[T]) Index

func (q *Deque[T]) Index(f func(T) bool) int

Index returns the index into the Deque of the first item satisfying f(item), or -1 if none do. If q is nil, then -1 is always returned. Search is linear starting with index 0.

func (*Deque[T]) Insert

func (q *Deque[T]) Insert(at int, item T)

Insert is used to insert an element into the middle of the queue, before the element at the specified index. Insert(0,e) is the same as PushFront(e) and Insert(Len(),e) is the same as PushBack(e). Accepts only non-negative index values, and panics if index is out of range.

Important: Deque is optimized for O(1) operations at the ends of the queue, not for operations in the the middle. Complexity of this function is constant plus linear in the lesser of the distances between the index and either of the ends of the queue.

func (*Deque[T]) Len

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

Len returns the number of elements currently stored in the queue. If q is nil, q.Len() is zero.

func (*Deque[T]) PopBack

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

PopBack removes and returns the element from the back of the queue. Implements LIFO when used with PushBack(). If the queue is empty, the call panics.

func (*Deque[T]) PopFront

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

PopFront removes and returns the element from the front of the queue. Implements FIFO when used with PushBack(). If the queue is empty, the call panics.

func (*Deque[T]) PushBack

func (q *Deque[T]) PushBack(elem T)

PushBack appends an element to the back of the queue. Implements FIFO when elements are removed with PopFront(), and LIFO when elements are removed with PopBack().

func (*Deque[T]) PushFront

func (q *Deque[T]) PushFront(elem T)

PushFront prepends an element to the front of the queue.

func (*Deque[T]) Remove

func (q *Deque[T]) Remove(at int) T

Remove removes and returns an element from the middle of the queue, at the specified index. Remove(0) is the same as PopFront() and Remove(Len()-1) is the same as PopBack(). Accepts only non-negative index values, and panics if index is out of range.

Important: Deque is optimized for O(1) operations at the ends of the queue, not for operations in the the middle. Complexity of this function is constant plus linear in the lesser of the distances between the index and either of the ends of the queue.

func (*Deque[T]) Rotate

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

Rotate rotates the deque n steps front-to-back. If n is negative, rotates back-to-front. Having Deque provide Rotate() avoids resizing that could happen if implementing rotation using only Pop and Push methods. If q.Len() is one or less, or q is nil, then Rotate does nothing.

func (*Deque[T]) Set

func (q *Deque[T]) Set(i int, elem T)

Set puts the element at index i in the queue. Set shares the same purpose than At() but perform the opposite operation. The index i is the same index defined by At(). If the index is invalid, the call panics.

func (*Deque[T]) SetMinCapacity

func (q *Deque[T]) SetMinCapacity(minCapacityExp uint)

SetMinCapacity sets a minimum capacity of 2^minCapacityExp. If the value of the minimum capacity is less than or equal to the minimum allowed, then capacity is set to the minimum allowed. This may be called at anytime to set a new minimum capacity.

Setting a larger minimum capacity may be used to prevent resizing when the number of stored items changes frequently across a wide range.

Jump to

Keyboard shortcuts

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