All Projects → gammazero → Deque

gammazero / Deque

Licence: mit
Fast ring-buffer deque (double-ended queue)

Programming Languages

go
31211 projects - #10 most used programming language

Labels

Projects that are alternatives of or similar to Deque

Node Rethinkdb Job Queue
A persistent job or task queue backed by RethinkDB.
Stars: ✭ 158 (-22.17%)
Mutual labels:  queue
Algo Tree
Algo-Tree is a collection of Algorithms and data structures which are fundamentals to efficient code and good software design. Creating and designing excellent algorithms is required for being an exemplary programmer. It contains solutions in various languages such as C++, Python and Java.
Stars: ✭ 166 (-18.23%)
Mutual labels:  queue
Wechatvideocourse
《微信公众号+小程序快速开发》视频教程课件及代码
Stars: ✭ 185 (-8.87%)
Mutual labels:  queue
Phive Queue
$queue->push('I can be popped off after', '10 minutes');
Stars: ✭ 161 (-20.69%)
Mutual labels:  queue
Interviewbit
Collection of Abhishek Agrawal's gists solutions for problems on https://www.interviewbit.com
Stars: ✭ 166 (-18.23%)
Mutual labels:  queue
Leetcode
High-quality LeetCode solutions
Stars: ✭ 178 (-12.32%)
Mutual labels:  queue
Laravel Queue
Laravel Enqueue message queue extension. Supports AMQP, Amazon SQS, Kafka, Google PubSub, Redis, STOMP, Gearman, Beanstalk and others
Stars: ✭ 155 (-23.65%)
Mutual labels:  queue
Stevejobs
A simple jobs queue that just works (for Meteor.js)
Stars: ✭ 195 (-3.94%)
Mutual labels:  queue
Holster
A place to keep useful golang functions and small libraries
Stars: ✭ 166 (-18.23%)
Mutual labels:  queue
Django dramatiq
A Django app that integrates with Dramatiq.
Stars: ✭ 181 (-10.84%)
Mutual labels:  queue
Acl
Server framework and network components written by C/C++ for Linux, Mac, FreeBSD, Solaris(x86), Windows, Android, IOS
Stars: ✭ 2,113 (+940.89%)
Mutual labels:  queue
Message Bus
Go simple async message bus
Stars: ✭ 166 (-18.23%)
Mutual labels:  queue
Opq
A simple, in-memory queue with worker pooling and rate limiting in Elixir.
Stars: ✭ 178 (-12.32%)
Mutual labels:  queue
Sc
Common libraries and data structures for C.
Stars: ✭ 161 (-20.69%)
Mutual labels:  queue
Interview Questions
List of all the Interview questions practiced from online resources and books
Stars: ✭ 187 (-7.88%)
Mutual labels:  queue
Redux Data Structures
Reducer factory functions for common data structures: counters, maps, lists (queues, stacks), sets, etc.
Stars: ✭ 157 (-22.66%)
Mutual labels:  queue
Yocto Queue
Tiny queue data structure
Stars: ✭ 171 (-15.76%)
Mutual labels:  queue
Data Structures
A collection of powerful data structures
Stars: ✭ 2,534 (+1148.28%)
Mutual labels:  queue
C Macro Collections
Easy to use, header only, macro generated, generic and type-safe Data Structures in C
Stars: ✭ 192 (-5.42%)
Mutual labels:  queue
Flask Rq2
A Flask extension for RQ.
Stars: ✭ 176 (-13.3%)
Mutual labels:  queue

deque

Build Status Go Report Card codecov License

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

GoDoc

For a pictorial description, see the Deque diagram

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 (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/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")

    // 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
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].