collections

package
Version: v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2020 License: BSD-3-Clause Imports: 3 Imported by: 2

Documentation

Overview

Package collections provides some typical and often used collection types like a ring buffer, stacks, sets and trees. They are implemented as generic collections managing empty interfaces as well as typed ones, e.g. for strings. They are not synchronized, so this has to be done by the user.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Changer

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

Changer defines the interface to perform changes on a tree node. It is returned by the addressing operations like At() and Create() of the Tree.

func (*Changer) Add

func (c *Changer) Add(v interface{}) error

Add sets a child value.

func (*Changer) Error

func (c *Changer) Error() error

Error returns a potential error of the changer.

func (*Changer) List

func (c *Changer) List() ([]interface{}, error)

List returns the values of the children of the changer node.

func (*Changer) Remove

func (c *Changer) Remove() error

Remove deletes this changer node.

func (*Changer) SetValue

func (c *Changer) SetValue(v interface{}) (interface{}, error)

SetValue sets the changer node value. It also returns the previous value.

func (*Changer) Value

func (c *Changer) Value() (interface{}, error)

Value returns the changer node value.

type KeyStringValue

type KeyStringValue struct {
	Key   string
	Value string
}

KeyStringValue carries a combination of key and string value.

type KeyStringValueChanger

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

KeyStringValueChanger defines the interface to perform changes on a key/string value tree node. It is returned by the addressing operations like At() and Create() of the KeyStringValueTree.

func (*KeyStringValueChanger) Add

func (c *KeyStringValueChanger) Add(k, v string) error

Add sets a child key/value. If the key already exists the value will be overwritten.

func (*KeyStringValueChanger) Error

func (c *KeyStringValueChanger) Error() error

Error returns a potential error of the changer.

func (*KeyStringValueChanger) Key

func (c *KeyStringValueChanger) Key() (string, error)

Key returns the changer node key.

func (*KeyStringValueChanger) List

List returns the keys and values of the children of the changer node.

func (*KeyStringValueChanger) Remove

func (c *KeyStringValueChanger) Remove() error

Remove deletes this changer node.

func (*KeyStringValueChanger) SetKey

func (c *KeyStringValueChanger) SetKey(key string) (string, error)

SetKey sets the changer node key. Its checks if duplicate keys are allowed and returns the previous key.

func (*KeyStringValueChanger) SetValue

func (c *KeyStringValueChanger) SetValue(value string) (string, error)

SetValue sets the changer node value. It also returns the previous value.

func (*KeyStringValueChanger) Value

func (c *KeyStringValueChanger) Value() (string, error)

Value returns the changer node value.

type KeyStringValueTree

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

KeyStringValueTree defines the interface for a tree able to store key/string value pairs.

func NewKeyStringValueTree

func NewKeyStringValueTree(k, v string, duplicates bool) *KeyStringValueTree

NewKeyStringValueTree creates a new key/value tree with or without duplicate values for children and strings as values.

func (*KeyStringValueTree) At

At returns the changer of the path defined by the given values. If it does not exist it will not be created. Use Create() here. So to set a child at a given node path do

err := tree.At("path", "one", "to", "use").Set(12345)

func (*KeyStringValueTree) Copy

Copy creates a copy of the tree.

func (*KeyStringValueTree) CopyAt

func (t *KeyStringValueTree) CopyAt(keys ...string) (*KeyStringValueTree, error)

CopyAt creates a copy of a subtree.

func (*KeyStringValueTree) Create

func (t *KeyStringValueTree) Create(keys ...string) *KeyStringValueChanger

Create returns the changer of the path defined by the given keys. If it does not exist it will be created, but at least the root key has to be correct.

func (*KeyStringValueTree) Deflate

func (t *KeyStringValueTree) Deflate(k, v string)

Deflate cleans the tree with a new root value.

func (*KeyStringValueTree) DoAll

func (t *KeyStringValueTree) DoAll(f func(k, v string) error) error

DoAll executes the passed function on all nodes.

func (*KeyStringValueTree) DoAllDeep

func (t *KeyStringValueTree) DoAllDeep(f func(ks []string, v string) error) error

DoAllDeep executes the passed function on all nodes passing a deep list of keys ordered top-down.

func (*KeyStringValueTree) FindAll

func (t *KeyStringValueTree) FindAll(f func(k, v string) (bool, error)) []*KeyStringValueChanger

FindAll returns all changers for the nodes found by the passed function.

func (*KeyStringValueTree) FindFirst

func (t *KeyStringValueTree) FindFirst(f func(k, v string) (bool, error)) *KeyStringValueChanger

FindFirst returns the changer for the first node found by the passed function.

func (*KeyStringValueTree) Len

func (t *KeyStringValueTree) Len() int

Len returns the number of nodes of the tree.

func (*KeyStringValueTree) Root

Root returns the top level changer.

func (*KeyStringValueTree) String

func (t *KeyStringValueTree) String() string

String implements the fmt.Stringer interface.

type KeyValue

type KeyValue struct {
	Keys  string
	Value interface{}
}

KeyValue wraps a key and a value for the key/value iterator.

type KeyValueChanger

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

KeyValueChanger defines the interface to perform changes on a key/value tree node. It is returned by the addressing operations like At() and Create() of the KeyValueTree.

func (*KeyValueChanger) Add

func (c *KeyValueChanger) Add(k string, v interface{}) error

Add sets a child key/value. If the key already exists the value will be overwritten.

func (*KeyValueChanger) Error

func (c *KeyValueChanger) Error() error

Error returns a potential error of the changer.

func (*KeyValueChanger) Key

func (c *KeyValueChanger) Key() (string, error)

Key returns the changer node key.

func (*KeyValueChanger) List

func (c *KeyValueChanger) List() ([]KeyValue, error)

List returns the keys and values of the children of the changer node.

func (*KeyValueChanger) Remove

func (c *KeyValueChanger) Remove() error

Remove deletes this changer node.

func (*KeyValueChanger) SetKey

func (c *KeyValueChanger) SetKey(key string) (string, error)

SetKey sets the changer node key. Its checks if duplicate keys are allowed and returns the previous key.

func (*KeyValueChanger) SetValue

func (c *KeyValueChanger) SetValue(value interface{}) (interface{}, error)

SetValue sets the changer node value. It also returns the previous value.

func (*KeyValueChanger) Value

func (c *KeyValueChanger) Value() (interface{}, error)

Value returns the changer node value.

type KeyValueTree

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

KeyValueTree defines the interface for a tree able to store key/value pairs.

func NewKeyValueTree

func NewKeyValueTree(k string, v interface{}, duplicates bool) *KeyValueTree

NewKeyValueTree creates a new key/value tree with or without duplicate values for children.

func (*KeyValueTree) At

func (t *KeyValueTree) At(keys ...string) *KeyValueChanger

At returns the changer of the path defined by the given values. If it does not exist it will not be created. Use Create() here. So to set a child at a given node path do

err := tree.At("path", "one", "to", "use").Set(12345)

func (*KeyValueTree) Copy

func (t *KeyValueTree) Copy() *KeyValueTree

Copy creates a copy of the tree.

func (*KeyValueTree) CopyAt

func (t *KeyValueTree) CopyAt(keys ...string) (*KeyValueTree, error)

CopyAt creates a copy of a subtree.

func (*KeyValueTree) Create

func (t *KeyValueTree) Create(keys ...string) *KeyValueChanger

Create returns the changer of the path defined by the given keys. If it does not exist it will be created, but at least the root key has to be correct.

func (*KeyValueTree) Deflate

func (t *KeyValueTree) Deflate(k string, v interface{})

Deflate cleans the tree with a new root value.

func (*KeyValueTree) DoAll

func (t *KeyValueTree) DoAll(f func(k string, v interface{}) error) error

DoAll executes the passed function on all nodes.

func (*KeyValueTree) DoAllDeep

func (t *KeyValueTree) DoAllDeep(f func(ks []string, v interface{}) error) error

DoAllDeep executes the passed function on all nodes passing a deep list of keys ordered top-down.

func (*KeyValueTree) FindAll

func (t *KeyValueTree) FindAll(f func(k string, v interface{}) (bool, error)) []*KeyValueChanger

FindAll returns all changers for the nodes found by the passed function.

func (*KeyValueTree) FindFirst

func (t *KeyValueTree) FindFirst(f func(k string, v interface{}) (bool, error)) *KeyValueChanger

FindFirst returns the changer for the first node found by the passed function.

func (*KeyValueTree) Len

func (t *KeyValueTree) Len() int

Len returns the number of nodes of the tree.

func (*KeyValueTree) Root

func (t *KeyValueTree) Root() *KeyValueChanger

Root returns the top level changer.

func (*KeyValueTree) String

func (t *KeyValueTree) String() string

String implements the fmt.Stringer interface.

type RingBuffer

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

RingBuffer defines a buffer which is connected end-to-end. It grows if needed.

func NewRingBuffer

func NewRingBuffer(size int) *RingBuffer

NewRingBuffer creates a new ring buffer.

func (*RingBuffer) Cap

func (rb *RingBuffer) Cap() int

Cap returns the capacity of the buffer.

func (*RingBuffer) Len

func (rb *RingBuffer) Len() int

Len returns the number of values in the buffer.

func (*RingBuffer) Peek

func (rb *RingBuffer) Peek() (interface{}, bool)

Peek returns the first value of the buffer. If the buffer is empty the second return value is false.

func (*RingBuffer) Pop

func (rb *RingBuffer) Pop() (interface{}, bool)

Pop removes and returns the first value of the buffer. If the buffer is empty the second return value is false.

func (*RingBuffer) Push

func (rb *RingBuffer) Push(values ...interface{})

Push adds values to the end of the buffer.

func (*RingBuffer) String

func (rb *RingBuffer) String() string

String implements the fmt.Stringer interface.

type Set

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

Set defines a set containing any kind of values.

func NewSet

func NewSet(vs ...interface{}) *Set

NewSet creates a set with the passed values as initial content.

func (*Set) Add

func (s *Set) Add(vs ...interface{})

Add adds values to the set.

func (*Set) All

func (s *Set) All() []interface{}

All returns all values.

func (*Set) Contains

func (s *Set) Contains(v interface{}) bool

Contains checks if the set contains a given value.

func (*Set) Deflate

func (s *Set) Deflate()

Deflate cleans the stack.

func (*Set) DoAll

func (s *Set) DoAll(f func(v interface{}) error) error

DoAll executes the passed function on all values.

func (*Set) FindAll

func (s *Set) FindAll(f func(v interface{}) (bool, error)) ([]interface{}, error)

FindAll returns all values found by the passed function.

func (*Set) Len

func (s *Set) Len() int

Len returns the number of entries in the set.

func (*Set) Remove

func (s *Set) Remove(vs ...interface{})

Remove removes values out if the set. It doesn't matter if the set does not contain them.

func (*Set) String

func (s *Set) String() string

String implements the fmt.Stringer interface.

type Stack

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

Stack defines a stack containing any kind of values.

func NewStack

func NewStack(vs ...interface{}) *Stack

NewStack creates a stack with the passed values as initial content.

func (*Stack) All

func (s *Stack) All() []interface{}

All returns all values bottom-up.

func (*Stack) AllReverse

func (s *Stack) AllReverse() []interface{}

AllReverse returns all values top-down.

func (*Stack) Deflate

func (s *Stack) Deflate()

Deflate cleans the stack.

func (*Stack) Len

func (s *Stack) Len() int

Len returns the number of entries in the stack.

func (*Stack) Peek

func (s *Stack) Peek() (interface{}, error)

Peek returns the top value of the stack.

func (*Stack) Pop

func (s *Stack) Pop() (interface{}, error)

Pop removes and returns the top value of the stack.

func (*Stack) Push

func (s *Stack) Push(vs ...interface{})

Push adds values to the top of the stack.

func (*Stack) String

func (s *Stack) String() string

String implements the fmt.Stringer interface.

type StringChanger

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

StringChanger defines the interface to perform changes on a string tree node. It is returned by the addressing operations like At() and Create() of the StringTree.

func (*StringChanger) Add

func (c *StringChanger) Add(v string) error

Add sets a child value. If it already exists it will be overwritten.

func (*StringChanger) Error

func (c *StringChanger) Error() error

Error returns a potential error of the changer.

func (*StringChanger) List

func (c *StringChanger) List() ([]string, error)

List returns the values of the children of the changer node.

func (*StringChanger) Remove

func (c *StringChanger) Remove() error

Remove deletes this changer node.

func (*StringChanger) SetValue

func (c *StringChanger) SetValue(v string) (string, error)

SetValue sets the changer node value. It also returns the previous value.

func (*StringChanger) Value

func (c *StringChanger) Value() (string, error)

Value returns the changer node value.

type StringSet

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

StringSet defines a set containing string values.

func NewStringSet

func NewStringSet(vs ...string) *StringSet

NewStringSet creates a string set with the passed strings as initial content.

func (*StringSet) Add

func (s *StringSet) Add(vs ...string)

Add adds strings to the set.

func (*StringSet) All

func (s *StringSet) All() []string

All returns all strings.

func (*StringSet) Contains

func (s *StringSet) Contains(v string) bool

Contains checks if the set contains a given string.

func (*StringSet) Deflate

func (s *StringSet) Deflate()

Deflate cleans the stack.

func (*StringSet) DoAll

func (s *StringSet) DoAll(f func(v string) error) error

DoAll executes the passed function on all strings.

func (*StringSet) FindAll

func (s *StringSet) FindAll(f func(v string) (bool, error)) ([]string, error)

FindAll returns all strings found by the passed function.

func (*StringSet) Len

func (s *StringSet) Len() int

Len returns the number of entries in the set.

func (*StringSet) Remove

func (s *StringSet) Remove(vs ...string)

Remove removes strings out if the set. It doesn't matter if the set does not contain them.

func (*StringSet) String

func (s *StringSet) String() string

String implements the fmt.Stringer interface.

type StringStack

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

StringStack defines a stack containing string values.

func NewStringStack

func NewStringStack(vs ...string) *StringStack

NewStringStack creates a string stack with the passed strings as initial content.

func (*StringStack) All

func (s *StringStack) All() []string

All returns all strings bottom-up.

func (*StringStack) AllReverse

func (s *StringStack) AllReverse() []string

AllReverse returns all strings top-down.

func (*StringStack) Deflate

func (s *StringStack) Deflate()

Deflate cleans the stack.

func (*StringStack) Len

func (s *StringStack) Len() int

Len returns the number of entries in the stack.

func (*StringStack) Peek

func (s *StringStack) Peek() (string, error)

Peek returns the top string of the stack.

func (*StringStack) Pop

func (s *StringStack) Pop() (string, error)

Pop removes and returns the top string of the stack.

func (*StringStack) Push

func (s *StringStack) Push(vs ...string)

Push adds strings to the top of the stack.

func (*StringStack) String

func (s *StringStack) String() string

String implements the fmt.Stringer interface.

type StringTree

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

StringTree defines the interface for a tree able to store strings.

func NewStringTree

func NewStringTree(v string, duplicates bool) *StringTree

NewStringTree creates a new string tree with or without duplicate values for children.

func (*StringTree) At

func (t *StringTree) At(values ...string) *StringChanger

At returns the changer of the path defined by the given values. If it does not exist it will not be created. Use Create() here. So to set a child at a given node path do

err := tree.At("path", "one", "to", "use").Set("12345")

func (*StringTree) Copy

func (t *StringTree) Copy() *StringTree

Copy creates a copy of the tree.

func (*StringTree) Create

func (t *StringTree) Create(values ...string) *StringChanger

Create returns the changer of the path defined by the given keys. If it does not exist it will be created, but at least the root key has to be correct.

func (*StringTree) Deflate

func (t *StringTree) Deflate(v string)

Deflate cleans the tree with a new root value.

func (*StringTree) DoAll

func (t *StringTree) DoAll(f func(v string) error) error

DoAll executes the passed function on all nodes.

func (*StringTree) DoAllDeep

func (t *StringTree) DoAllDeep(f func(vs []string) error) error

DoAllDeep executes the passed function on all nodes passing a deep list of values ordered top-down.

func (*StringTree) FindAll

func (t *StringTree) FindAll(f func(v string) (bool, error)) []*StringChanger

FindAll returns all changers for the nodes found by the passed function.

func (*StringTree) FindFirst

func (t *StringTree) FindFirst(f func(v string) (bool, error)) *StringChanger

FindFirst returns the changer for the first node found by the passed function.

func (*StringTree) Len

func (t *StringTree) Len() int

Len returns the number of nodes of the tree.

func (*StringTree) Root

func (t *StringTree) Root() *StringChanger

Root returns the top level changer.

func (*StringTree) String

func (t *StringTree) String() string

String implements the fmt.Stringer interface.

type Tree

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

Tree defines the interface for a tree able to store any type of values.

func NewTree

func NewTree(v interface{}, duplicates bool) *Tree

NewTree creates a new tree with or without duplicate values for children.

func (*Tree) At

func (t *Tree) At(values ...interface{}) *Changer

At returns the changer of the path defined by the given values. If it does not exist it will not be created. Use Create() here. So to set a child at a given node path do

err := tree.At("path", 1, "to", "use").Set(12345)

func (*Tree) Copy

func (t *Tree) Copy() *Tree

Copy creates a copy of the tree.

func (*Tree) Create

func (t *Tree) Create(values ...interface{}) *Changer

Create returns the changer of the path defined by the given keys. If it does not exist it will be created, but at least the root key has to be correct.

func (*Tree) Deflate

func (t *Tree) Deflate(v interface{})

Deflate cleans the tree with a new root value.

func (*Tree) DoAll

func (t *Tree) DoAll(f func(v interface{}) error) error

DoAll executes the passed function on all nodes.

func (*Tree) DoAllDeep

func (t *Tree) DoAllDeep(f func(vs []interface{}) error) error

DoAllDeep executes the passed function on all nodes passing a deep list of values ordered top-down.

func (*Tree) FindAll

func (t *Tree) FindAll(f func(v interface{}) (bool, error)) []*Changer

FindAll returns all changers for the nodes found by the passed function.

func (*Tree) FindFirst

func (t *Tree) FindFirst(f func(v interface{}) (bool, error)) *Changer

FindFirst returns the changer for the first node found by the passed function.

func (*Tree) Len

func (t *Tree) Len() int

Len returns the number of nodes of the tree.

func (*Tree) Root

func (t *Tree) Root() *Changer

Root returns the top level changer.

func (*Tree) String

func (t *Tree) String() string

String implements the fmt.Stringer interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL