Pkg.go.dev is a new destination for Go discovery & docs. Check it out at pkg.go.dev/github.com/carlmjohnson/collections and share your feedback.

package collections

import "github.com/carlmjohnson/collections"

Package collections is a convenience package for dealing with collection types.

doc.go keys.go queue.go

SortedKeys takes a map[string]T and returns a sorted slice of keys.

❖ type DequeManager struct { // contains filtered or unexported fields }

DequeManager manages unboxed, type-safe double-ended deques backed by a slice.

Example Code: var ss []string deque := collections.NewDeque(0, func(pivot int) int { ns := make([]string, len(ss)*2+1) copied := copy(ns, (ss)[pivot:]) copy(ns[copied:], (ss)[:pivot]) ss = ns return len(ns) }) fmt.Printf("%q %v

", ss, deque) ss[deque.PushTail()] = "hello" fmt.Printf("%q %v

", ss, deque) ss[deque.PushTail()] = "world" fmt.Printf("%q %v

", ss, deque) fmt.Printf("%q %q %v

", ss[deque.PopHead()], ss, deque) ss[deque.PushHead()] = "," ss[deque.PushHead()] = "Hello" fmt.Printf("%q %v

", ss, deque) ss[deque.PushTail()] = "!" fmt.Printf("%q %v

", ss, deque) Output: [] collections.DequeManager{head: -1, tail: -1, length: 0, pivot: 0} ["hello"] collections.DequeManager{head: 0, tail: 0, length: 1, pivot: 0} ["hello" "world" ""] collections.DequeManager{head: 0, tail: 1, length: 2, pivot: 0} "hello" ["hello" "world" ""] collections.DequeManager{head: 1, tail: 1, length: 1, pivot: 1} ["," "world" "Hello"] collections.DequeManager{head: 2, tail: 1, length: 3, pivot: 2} ["Hello" "," "world" "!" "" "" ""] collections.DequeManager{head: 0, tail: 3, length: 4, pivot: 0}

❖ func NewDeque(capacity int, grow func(pivot int) (capacity int)) *DequeManager

NewDeque returns a DequeManager ready to manage a slice. Initial length of the slice must be given by capacity. Grow function should copy the existing slice around the pivot to a new slice and return the length of the new slice. To signal that the slice cannot be grown (e.g. to use a fixed deque size) return -1.

NewDequeForSlice is a convenience initializer that uses reflection to call NewDeque with an appropriate capacity and growth function.

Example Code: var ss []string deque := collections.NewDequeForSlice(&ss) fmt.Printf("%q %v

", ss, deque) ss[deque.PushTail()] = "hello" fmt.Printf("%q %v

", ss, deque) ss[deque.PushTail()] = "world" fmt.Printf("%q %v

", ss, deque) fmt.Printf("%q %q %v

", ss[deque.PopHead()], ss, deque) ss[deque.PushHead()] = "," ss[deque.PushHead()] = "Hello" fmt.Printf("%q %v

", ss, deque) ss[deque.PushTail()] = "!" fmt.Printf("%q %v

", ss, deque) Output: [] collections.DequeManager{head: -1, tail: -1, length: 0, pivot: 0} ["hello"] collections.DequeManager{head: 0, tail: 0, length: 1, pivot: 0} ["hello" "world" ""] collections.DequeManager{head: 0, tail: 1, length: 2, pivot: 0} "hello" ["hello" "world" ""] collections.DequeManager{head: 1, tail: 1, length: 1, pivot: 1} ["," "world" "Hello"] collections.DequeManager{head: 2, tail: 1, length: 3, pivot: 2} ["Hello" "," "world" "!" "" "" ""] collections.DequeManager{head: 0, tail: 3, length: 4, pivot: 0}

Head returns the index of the current head of the slice or -1 if the deque is empty.

PopHead returns the index of the head of the slice and removes it from the deque. Returns -1 if the deque is empty. Note that actual values in the slice are not affected by deque operations.

PopTail returns the index of the tail of the slice and removes it from the deque. Returns -1 if the deque is empty. Note that actual values in the slice are not affected by deque operations.

PushHead returns the index of the next head of the slice. It may call the grow function if necessary. Returns -1 if the deque needs growth but cannot be grown.

PushTail returns the index of the next tail of the slice. It may call the grow function if necessary. Returns -1 if the deque needs growth but cannot be grown.

String implements fmt.Stringer

Tail returns the index of the current tail of the slice or -1 if the deque is empty.