collections

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 7, 2019 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

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

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func SortedKeys

func SortedKeys(m interface{}) []string

Types

type DequeManager

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

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

Example
package main

import (
	"fmt"

	"github.com/carlmjohnson/collections"
)

func main() {
	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\n", ss, deque)
	ss[deque.PushTail()] = "hello"
	fmt.Printf("%q %v\n", ss, deque)
	ss[deque.PushTail()] = "world"
	fmt.Printf("%q %v\n", ss, deque)
	fmt.Printf("%q %q %v\n", ss[deque.PopHead()], ss, deque)
	ss[deque.PushHead()] = ","
	ss[deque.PushHead()] = "Hello"
	fmt.Printf("%q %v\n", ss, deque)
	ss[deque.PushTail()] = "!"
	fmt.Printf("%q %v\n", 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

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.

func NewDequeForSlice

func NewDequeForSlice(slicepointer interface{}) *DequeManager

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

Example
package main

import (
	"fmt"

	"github.com/carlmjohnson/collections"
)

func main() {
	var ss []string
	deque := collections.NewDequeForSlice(&ss)

	fmt.Printf("%q %v\n", ss, deque)
	ss[deque.PushTail()] = "hello"
	fmt.Printf("%q %v\n", ss, deque)
	ss[deque.PushTail()] = "world"
	fmt.Printf("%q %v\n", ss, deque)
	fmt.Printf("%q %q %v\n", ss[deque.PopHead()], ss, deque)
	ss[deque.PushHead()] = ","
	ss[deque.PushHead()] = "Hello"
	fmt.Printf("%q %v\n", ss, deque)
	ss[deque.PushTail()] = "!"
	fmt.Printf("%q %v\n", 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 (*DequeManager) Head

func (dm *DequeManager) Head() int

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

func (*DequeManager) PopHead

func (dm *DequeManager) PopHead() int

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.

func (*DequeManager) PopTail

func (dm *DequeManager) PopTail() int

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.

func (*DequeManager) PushHead

func (dm *DequeManager) PushHead() int

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.

func (*DequeManager) PushTail

func (dm *DequeManager) PushTail() int

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.

func (*DequeManager) String

func (dm *DequeManager) String() string

String implements fmt.Stringer

func (*DequeManager) Tail

func (dm *DequeManager) Tail() int

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL