deque-int/README.md

69 lines
3.1 KiB
Markdown
Raw Permalink Normal View History

2018-04-24 03:06:09 +00:00
# deque
2022-01-27 17:14:58 +00:00
Fork of [github.com/gammazero/deque](https://github.com/gammazero/deque)
2018-04-24 03:06:09 +00:00
2020-07-21 20:26:02 +00:00
Fast ring-buffer deque ([double-ended queue](https://en.wikipedia.org/wiki/Double-ended_queue)) implementation.
2018-04-24 03:06:09 +00:00
2018-05-01 04:44:09 +00:00
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
2018-04-24 03:06:09 +00:00
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.
2020-07-21 20:26:02 +00:00
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.
2018-04-24 03:06:09 +00:00
2018-04-24 03:56:37 +00:00
For maximum speed, this deque implementation leaves concurrency safety up to the application to provide, however the application chooses, if needed at all.
2018-04-24 03:06:09 +00:00
## Reading Empty Deque
2018-04-30 02:59:51 +00:00
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.
2018-04-24 03:06:09 +00:00
## Example
```go
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")
2018-09-20 16:02:04 +00:00
// Consume deque and print elements.
for q.Len() != 0 {
fmt.Println(q.PopFront())
2018-04-24 03:06:09 +00:00
}
}
```
2018-04-24 03:56:37 +00:00
## 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`