Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gammazero/deque
Fast ring-buffer deque (double-ended queue)
https://github.com/gammazero/deque
circular-buffer circular-queue deque queue ring-buffer
Last synced: 3 days ago
JSON representation
Fast ring-buffer deque (double-ended queue)
- Host: GitHub
- URL: https://github.com/gammazero/deque
- Owner: gammazero
- License: mit
- Created: 2018-04-24T02:57:55.000Z (almost 7 years ago)
- Default Branch: main
- Last Pushed: 2024-11-15T08:47:34.000Z (2 months ago)
- Last Synced: 2025-01-16T08:06:40.348Z (10 days ago)
- Topics: circular-buffer, circular-queue, deque, queue, ring-buffer
- Language: Go
- Homepage:
- Size: 57.6 KB
- Stars: 635
- Watchers: 9
- Forks: 59
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - deque - Fast ring-buffer deque (double-ended queue). (Data Structures and Algorithms / Queues)
- awesome-go - deque - Fast ring-buffer deque (double-ended queue). (Data Structures and Algorithms / Queues)
- awesome-go - deque - Fast ring-buffer deque (double-ended queue) - ★ 11 (Data Structures)
- awesome-go-extra - deque - buffer deque (double-ended queue)|364|43|1|2018-04-24T02:57:55Z|2022-08-17T17:51:51Z| (Generators / Queues)
- awesome - gammazero/deque - Fast ring-buffer deque (double-ended queue) (Go)
- awesome - gammazero/deque - Fast ring-buffer deque (double-ended queue) (Go)
README
# deque
[![GoDoc](https://pkg.go.dev/badge/github.com/gammazero/deque)](https://pkg.go.dev/github.com/gammazero/deque)
[![Build Status](https://github.com/gammazero/deque/actions/workflows/go.yml/badge.svg)](https://github.com/gammazero/deque/actions/workflows/go.yml)
[![Go Report Card](https://goreportcard.com/badge/github.com/gammazero/deque)](https://goreportcard.com/report/github.com/gammazero/deque)
[![codecov](https://codecov.io/gh/gammazero/deque/branch/master/graph/badge.svg)](https://codecov.io/gh/gammazero/deque)
[![License](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)Fast ring-buffer deque ([double-ended queue](https://en.wikipedia.org/wiki/Double-ended_queue)) implementation.
For a pictorial description, see the [Deque diagram](https://github.com/gammazero/deque/wiki)
## Installation
```
$ go get github.com/gammazero/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](https://en.wikipedia.org/wiki/Queue_(abstract_data_type)) (FIFO) operations are supported using `PushBack` and `PopFront`. [Stack](https://en.wikipedia.org/wiki/Stack_(abstract_data_type)) (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. A base capacity can be set, with `SetBaseCap`, so that there is no resizing at or below that specified amount. The Deque can also be grown, using `Grow`, to ensure sufficient storage for n additional items, to prevent resizing when adding a number of itmes.
The ring-buffer implementation improves memory and time performance with fewer GC pauses, compared to implementations based on slices or linked lists. By wrapping around the buffer, previously used space is reused, making allocation unnecessary until all buffer capacity is used. The ring buffer implementation performs best when resizes are infrequest, as is the case when items moving in and out of the Deque are balanced or when the base capacity is large enough to rarely require a resize.
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.
## Generics
Deque uses generics to create a Deque that contains items of the type specified. To create a Deque that holds a specific type, provide a type argument with the `Deque` variable declaration. For example:
```go
stringDeque := new(deque.Deque[string])
var intDeque deque.Deque[int]
```## Example
```go
package mainimport (
"fmt"
"github.com/gammazero/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: bazq.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](https://en.wikipedia.org/wiki/Queue_(abstract_data_type)) using `PushBack` and `PopFront`
- [Stack](https://en.wikipedia.org/wiki/Stack_(abstract_data_type)) using `PushBack` and `PopBack`