/collection

Generic data structures for Go

Primary LanguageGo

collection

CI GoDoc

A few generic data structures for Go. Tested with go1.18beta1.

Work in progress. Expect bugs, poor performance, API instability.

go get github.com/tomyl/collection

Examples

Deque

A double-ended queue based on a double-linked list.

    var q collection.Deque[int]
    q.PushFront(42)
    q.PushFront(17)
    value, ok := q.PopBack() // 42 true

Graph

A directed graph.

    var g collection.Graph[string, string, uint]
    g.SetEdge("a", "b", "ab", 1)
    g.SetEdge("a", "c", "ac", 2)
    g.SetEdge("b", "c", "bc", 3)
    g.SetEdge("b", "d", "bd", 4)
    nodes, edges := g.ShortestPath("a", "d") // [a b d] [ab bd]

Heap

A binary min heap. Trivial to use as a priority queue, just set the key to the negative priority.

    var h collection.Heap[int, string]
    h.Push(42, "foo")
    h.Push(17, "bar")
    key, value, ok := h.Pop()  // 17 "bar" true

Slice

    var s collection.Slice[int]
    s.PushBack(42)
    s.PushBack(17)
    value, ok := s.PopBack() // 17 true