lane
Lane package provides queue, priority queue, stack and deque data structures
implementations. Its was designed with simplicity, performance, and concurrent
usage in mind.
Installation
go get github.com/oleiade/lane
Usage
Priority Queue
Pqueue is a heap priority queue data structure implementation. It can be whether max or min ordered, is synchronized and is safe for concurrent operations. It performs insertion and max/min removal in O(log N) time.
Example
var priorityQueue *PQueue = NewPQueue(MINPQ)
priorityQueue.Push("easy as", 3)
priorityQueue.Push("123", 2)
priorityQueue.Push("do re mi", 4)
priorityQueue.Push("abc", 1)
headValue, headPriority := priorityQueue.Head()
fmt.Println(headValue)
fmt.Println(headPriority)
var jacksonFive []string = make([]string, priorityQueue.Size())
for i := 0; i < len(jacksonFive); i++ {
value, _ := priorityQueue.Pop()
jacksonFive[i] = value.(string)
}
fmt.Println(strings.Join(jacksonFive, " "))
Deque
Deque is a head-tail linked list data structure implementation. It is based on a doubly-linked list container, so that every operations time complexity is O(1). All operations over an instiated Deque are synchronized and safe for concurrent usage.
Deques can optionally be created with a limited capacity, whereby the return value of the Append
and Prepend
return false if the Deque was full and the item was not added.
Example
var deque *Deque = NewDeque()
deque.Append("easy as")
deque.Prepend("123")
deque.Append("do re mi")
deque.Prepend("abc")
firstValue := deque.First()
lastValue := deque.Last()
fmt.Println(firstValue)
fmt.Println(lastValue)
var jacksonFive []string = make([]string, deque.Size())
for i := 0; i < len(jacksonFive); i++ {
value := deque.Shift()
jacksonFive[i] = value.(string)
}
fmt.Println(strings.Join(jacksonFive, " "))
quartet := NewCappedDeque(4)
musicians := []string{"John", "Paul", "George", "Ringo", "Stuart"}
for _, name := range musicians {
if quartet.Append(name) {
fmt.Printf("%s is in the band!\n", name)
} else {
fmt.Printf("Sorry - %s is not in the band.\n", name)
}
}
var beatles = make([]string, quartet.Size())
for i := 0; i < len(beatles); i++ {
beatles[i] = quartet.Shift().(string)
}
fmt.Println("The Beatles are:", strings.Join(beatles, ", "))
Queue
Queue is a FIFO ( First in first out ) data structure implementation. It is based on a deque container and focuses its API on core functionalities: Enqueue, Dequeue, Head, Size, Empty. Every operations time complexity is O(1). As it is implemented using a Deque container, every operations over an instiated Queue are synchronized and safe for concurrent usage.
Example
import (
"fmt"
"github.com/oleiade/lane"
"sync"
)
func worker(item interface{}, wg *sync.WaitGroup) {
fmt.Println(item)
wg.Done()
}
func main() {
queue := lane.NewQueue()
queue.Enqueue("grumpyClient")
queue.Enqueue("happyClient")
queue.Enqueue("ecstaticClient")
var wg sync.WaitGroup
for queue.Head() != nil {
item := queue.Dequeue()
wg.Add(1)
go worker(item, &wg)
}
wg.Wait()
}
Stack
Stack is a LIFO ( Last in first out ) data structure implementation. It is based on a deque container and focuses its API on core functionalities: Push, Pop, Head, Size, Empty. Every operations time complexity is O(1). As it is implemented using a Deque container, every operations over an instiated Stack are synchronized and safe for concurrent usage.
Example
var stack *Stack = NewStack()
stack.Push("redPlate")
stack.Push("bluePlate")
stack.Push("greenPlate")
fmt.Println(stack.Head)
value := stack.Pop()
fmt.Println(value.(string))
stack.Push("yellowPlate")
value = stack.Pop()
fmt.Println(value.(string))
value = stack.Pop()
fmt.Println(value.(string))
value = stack.Pop()
fmt.Println(value.(string))
Documentation
For a more detailled overview of lane, please refer to Documentation