Fast ring-buffer deque (double-ended queue), for integers only. Fork of https://github.com/gammazero/deque
Go to file
2018-04-28 20:29:52 -04:00
.gitignore initial commit 2018-04-23 23:06:09 -04:00
.travis.yml initial commit 2018-04-23 23:06:09 -04:00
deque.go Add Insert and Remove. No negative indexing. 2018-04-28 20:29:52 -04:00
deque_test.go Add Insert and Remove. No negative indexing. 2018-04-28 20:29:52 -04:00
go.test.sh initial commit 2018-04-23 23:06:09 -04:00
LICENSE Create LICENSE 2018-04-23 20:10:58 -07:00
README.md comments 2018-04-24 17:51:08 -04:00

deque

Build Status Go Report Card codecov License

Extremely fast ring-buffer deque (double-ended queue) implementation.

GoDoc

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.

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.

Installation

$ go get github.com/gammazero/deque

Example

package main

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

func main() {
    var q deque.Deque
    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")

    // Print: hello bar world
    for i := 0; i < q.Len(); i++ {
        fmt.Print(q.PeekAt(i), " ")
    }
    fmt.Println()
}

Uses

Deque can be used as both a:

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