deque

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2020 License: MIT Imports: 2 Imported by: 2

README

deque

GoDoc Travis

Implementation of Python deque in Go.

Speed

deque is pretty fast, run make compare to see comparison against some other methods. On my machine I get:

$ make compare
Git head is 765f6b0
cd compare && go test -run NONE -bench . -v
testing: warning: no tests to run
PASS
BenchmarkHistAppend-4	 3000000	       517 ns/op
BenchmarkHistList-4  	 2000000	       702 ns/op
BenchmarkHistQueue-4 	 3000000	       576 ns/op
BenchmarkHistDeque-4 	 3000000	       423 ns/op
ok  	_/home/miki/Projects/go/src/github.com/tebeka/deque/compare	8.505s

Documentation

Overview

Package deque provides a double ended queue modeled after Python's collections.deque

Example:

import (
	"fmt"

	"github.com/tebeka/deque"
)

func Example() {
	// Create a new deque and put some numbers in it.
	dq := deque.New()
	for i := 0; i < 5; i++ {
		dq.Append(i)
	}

	// Pop from the left
	val, _ := dq.PopLeft()
	fmt.Println(val) // 0

	// Get an item
	val, _ = dq.Get(2)
	fmt.Println(val) // 2

	// Set an item
	dq.Set(2, "hello")

	// Rotate
	dq.Rotate(-2)

	// Print
	fmt.Println(dq)

	// Output:
	// Deque{"hello", 4, 1, 2}
}

Index

Constants

View Source
const (

	// Version is the package version
	Version = "0.1.2"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Deque

type Deque struct {
	// contains filtered or unexported fields
}

Deque is a double ended queue

func New

func New() *Deque

New returns a new unbounded Deque

func NewBounded

func NewBounded(maxSize int) (*Deque, error)

NewBounded returns a new bounded Deque A bounded Deque will not grow over maxSize items

func (*Deque) Append

func (dq *Deque) Append(item interface{})

Append appends an item to the right of the deque

func (*Deque) AppendLeft

func (dq *Deque) AppendLeft(item interface{})

AppendLeft appends an item to the left of the deque

func (*Deque) Get

func (dq *Deque) Get(i int) (interface{}, error)

Get return the item at position i

func (*Deque) Len

func (dq *Deque) Len() int

Len returns the number of items in the queue

func (*Deque) Pop

func (dq *Deque) Pop() (interface{}, error)

Pop removes and return the rightmost element

func (*Deque) PopLeft

func (dq *Deque) PopLeft() (interface{}, error)

PopLeft removes and return the leftmost element.

func (*Deque) Rotate

func (dq *Deque) Rotate(n int)

Rotate rotates the queue. If n is positive then rotate right n steps, otherwise rotate left -n steps

func (*Deque) Set

func (dq *Deque) Set(i int, val interface{}) error

Set sets the item at position i to val

func (*Deque) String

func (dq *Deque) String() string

Jump to

Keyboard shortcuts

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