# README
queue 
Multiple thread-safe generic queue implementations.
A queue is a sequence of entities that is open at both ends where he elements are added(enqueued) to the tail(back) of the queue and removed(dequeued) from the head(front) of the queue.
Benchmarks and Example tests can be found in this package.
All queues implement the Queue
interface
Installation
go get -u github.com/adrianbrad/queue
Blocking Queue
- FIFO Ordering
- Provides blocking and non-blocking methods. The non-blocking methods return an error.
- Implemented using
sync.Cond
from standard library.
Quick start
package main
import (
"fmt"
"github.com/adrianbrad/queue"
)
func main() {
elems := []int{2, 3}
priorityQueue := queue.NewBlocking(elems, queue.WithCapacity(3))
if err := priorityQueue.Offer(1); err != nil {
// handle err
}
elem, err := priorityQueue.Get()
if err != nil {
// handle err
}
fmt.Printf("elem: %d\n", elem) // elem: 2
}
Priority Queue
- Order based on the
less
method provided at construction. - Implemented using
container/heap
standard library package.
Quick Start
package main
import (
"fmt"
"github.com/adrianbrad/queue"
)
func main() {
elems := []int{2, 3, 4}
priorityQueue := queue.NewPriority(elems, func(elem, otherElem int) bool {
return elem < otherElem
})
if err := priorityQueue.Offer(1); err != nil {
// handle err
}
elem, err := priorityQueue.Get()
if err != nil {
// handle err
}
fmt.Printf("elem: %d\n", elem) // elem: 1
}
Benchmarks
Results as of 3rd of February 2023.
BenchmarkBlockingQueue/Peek-12 63275360 19.44 ns/op 0 B/op 0 allocs/op
BenchmarkBlockingQueue/Get_Offer-12 19066974 69.67 ns/op 40 B/op 0 allocs/op
BenchmarkBlockingQueue/Offer-12 36569245 37.86 ns/op 41 B/op 0 allocs/op
BenchmarkPriorityQueue/Peek-12 66765319 15.86 ns/op 0 B/op 0 allocs/op
BenchmarkPriorityQueue/Get_Offer-12 16677442 71.33 ns/op 0 B/op 0 allocs/op
BenchmarkPriorityQueue/Offer-12 20044909 58.58 ns/op 55 B/op 0 allocs/op
# Functions
NewBlocking returns a new Blocking Queue containing the given elements.
NewPriority creates a new Priority Queue containing the given elements.
WithCapacity specifies a fixed capacity for a queue.
# Variables
ErrNoElementsAvailable is an error returned whenever there are no elements available to be extracted from a queue.
ErrQueueIsFull is an error returned whenever the queue is full and there is an attempt to add an element to it.