datastructure

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2020 License: Apache-2.0 Imports: 5 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrStopIteration causes a ForEach() or ForEachInto() loop to terminate
	// early.
	ErrStopIteration = errors.New("datastructure: stop iteration")

	// ErrInvalidFuncSignature is raised in a panic() if a function passed to a
	// ForEachInto() method does not conform to the expected interface.
	ErrInvalidFuncSignature = errors.New("datastructure: invalid function signature")
)

Functions

This section is empty.

Types

type Container

type Container interface {
	// Empty returns true if this container is empty; false otherwise.
	//
	// The result of this function is equivalent to the test Size() == 0, but
	// certain data structures that do not have an O(1) Size() implementation
	// may optimize this function.
	Empty() bool

	// Size returns the number of values in this container.
	Size() int

	// Clear resets this container to its zero-value state.
	Clear()

	// Values returns the values from this container as a slice of interface{}.
	Values() []interface{}

	// ValuesInto inserts the values from this container into the given slice.
	// The type of the into parameter must be a pointer to a slice for which
	// each value must be assignable by the type of every value in this
	// container.
	//
	// If the requirements for the into parameter are not met, this function
	// will panic.
	ValuesInto(into interface{})
}

Container represents any type with elements, such as a map, list, or set.

type HashMap

type HashMap map[interface{}]interface{}

HashMap is a simple hash map implementation, backed by the built-in map type in Go.

func NewHashMap

func NewHashMap() *HashMap

NewHashMap creates a new hash map with the default initial capacity of this Go implementation.

func NewHashMapWithCapacity

func NewHashMapWithCapacity(capacity int) *HashMap

NewHashMapWithCapacity creates a new hash map with the specified initial capacity.

func (*HashMap) Clear

func (m *HashMap) Clear()

func (HashMap) Contains

func (m HashMap) Contains(key interface{}) (found bool)

func (HashMap) Empty

func (m HashMap) Empty() bool

func (HashMap) ForEach

func (m HashMap) ForEach(fn MapIterationFunc) error

func (*HashMap) ForEachInto

func (m *HashMap) ForEachInto(fn interface{}) error

func (HashMap) Get

func (m HashMap) Get(key interface{}) (value interface{}, found bool)

func (*HashMap) GetInto

func (m *HashMap) GetInto(key, into interface{}) bool

func (*HashMap) Keys

func (m *HashMap) Keys() []interface{}

func (*HashMap) KeysInto

func (m *HashMap) KeysInto(into interface{})

func (HashMap) Put

func (m HashMap) Put(key, value interface{}) (found bool)

func (HashMap) Remove

func (m HashMap) Remove(key interface{}) (found bool)

func (HashMap) Size

func (m HashMap) Size() int

func (*HashMap) Values

func (m *HashMap) Values() []interface{}

func (*HashMap) ValuesInto

func (m *HashMap) ValuesInto(into interface{})

type LinkedHashMap

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

LinkedHashMap is hash map that iterates its entries in the order they were inserted into the map. Calls to Put() for a key that already exists in the map will not change its insertion order.

func NewLinkedHashMap

func NewLinkedHashMap() *LinkedHashMap

NewLinkedHashMap creates a new linked hash map with the default initial capacity of this Go implementation.

func NewLinkedHashMapWithCapacity

func NewLinkedHashMapWithCapacity(capacity int) *LinkedHashMap

NewLinkedHashMapWithCapacity creates a new linked hash map with the specified initial capacity.

func (*LinkedHashMap) Clear

func (m *LinkedHashMap) Clear()

func (*LinkedHashMap) Contains

func (m *LinkedHashMap) Contains(key interface{}) (found bool)

func (*LinkedHashMap) Empty

func (m *LinkedHashMap) Empty() bool

func (*LinkedHashMap) ForEach

func (m *LinkedHashMap) ForEach(fn MapIterationFunc) error

func (*LinkedHashMap) ForEachInto

func (m *LinkedHashMap) ForEachInto(fn interface{}) error

func (*LinkedHashMap) Get

func (m *LinkedHashMap) Get(key interface{}) (value interface{}, found bool)

func (*LinkedHashMap) GetInto

func (m *LinkedHashMap) GetInto(key, into interface{}) bool

func (*LinkedHashMap) Keys

func (m *LinkedHashMap) Keys() []interface{}

func (*LinkedHashMap) KeysInto

func (m *LinkedHashMap) KeysInto(into interface{})

func (*LinkedHashMap) Put

func (m *LinkedHashMap) Put(key, value interface{}) (found bool)

func (*LinkedHashMap) Remove

func (m *LinkedHashMap) Remove(key interface{}) (found bool)

func (*LinkedHashMap) Size

func (m *LinkedHashMap) Size() int

func (*LinkedHashMap) Values

func (m *LinkedHashMap) Values() []interface{}

func (*LinkedHashMap) ValuesInto

func (m *LinkedHashMap) ValuesInto(into interface{})

type Map

type Map interface {
	Container

	// Contains returns true if the given key exists in this map.
	Contains(key interface{}) bool

	// Put adds the given key and value to the map. If the value already exists
	// in the map, the old value is overwritten by the specified value.
	//
	// Returns true if this map already contained an entry for this key, and
	// false otherwise.
	Put(key, value interface{}) bool

	// Get retrieves the value associated with the given key from the map.
	//
	// Returns the value, or nil if the key does not exist in the map, and a
	// boolean indicating whether the key exists.
	Get(key interface{}) (interface{}, bool)

	// GetInto retrieves the value associated with the given key from the map
	// and stores it in the into parameter passed to this function.
	//
	// The into parameter must be a pointer to a type assignable by the stored
	// value. If the given key does not exist in the map, the into parameter is
	// not modified.
	//
	// If the into parameter is not compatible with the stored value, this
	// function will panic.
	//
	// Returns true if the key exists, and false otherwise.
	GetInto(key interface{}, into interface{}) bool

	// Remove eliminates the given key from the map, and returns true if the key
	// existed in the map.
	Remove(key interface{}) bool

	// Keys returns the keys from this map as a slice of interface{}.
	Keys() []interface{}

	// KeysInto inserts the keys from this map into the given slice. The type of
	// the into parameter must be a pointer to a slice for which each value must
	// be assignable by the type of every key in this map.
	//
	// If the requirements for the into parameter are not met, this function
	// will panic.
	KeysInto(into interface{})

	// ForEach iterates each key-value pair in the map and executes the given
	// callback function. If the callback function returns an error, this
	// function will return the same error and immediately stop iteration.
	//
	// To stop iteration without returning an error, return ErrStopIteration.
	ForEach(fn MapIterationFunc) error

	// ForEachInto iterates each key-value pair in the map and executes the
	// given callback function, which must be of a type similar to
	// MapIterationFunc, except that the key and value parameters may be any
	// type assignable by every key and value in the map, respectively.
	//
	// If the requirements for the fn parameter are not met, this function will
	// panic.
	ForEachInto(fn interface{}) error
}

Map represents a key-to-value mapping, where keys are unique.

type MapBackedSet

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

MapBackedSet is a set that stores its elements as keys in a given map.

func NewMapBackedSet

func NewMapBackedSet(storage Map) *MapBackedSet

NewMapBackedSet creates a new map-backed set with the given map for storage.

func (*MapBackedSet) Add

func (s *MapBackedSet) Add(elements ...interface{})

func (*MapBackedSet) AddAll

func (s *MapBackedSet) AddAll(other Container)

func (*MapBackedSet) Clear

func (s *MapBackedSet) Clear()

func (*MapBackedSet) Contains

func (s *MapBackedSet) Contains(elements ...interface{}) bool

func (*MapBackedSet) Empty

func (s *MapBackedSet) Empty() bool

func (*MapBackedSet) ForEach

func (s *MapBackedSet) ForEach(fn SetIterationFunc) error

func (*MapBackedSet) ForEachInto

func (s *MapBackedSet) ForEachInto(fn interface{}) error

func (*MapBackedSet) Remove

func (s *MapBackedSet) Remove(elements ...interface{})

func (*MapBackedSet) RemoveAll

func (s *MapBackedSet) RemoveAll(other Set)

func (*MapBackedSet) RetainAll

func (s *MapBackedSet) RetainAll(other Set)

func (*MapBackedSet) Size

func (s *MapBackedSet) Size() int

func (*MapBackedSet) Values

func (s *MapBackedSet) Values() []interface{}

func (*MapBackedSet) ValuesInto

func (s *MapBackedSet) ValuesInto(into interface{})

type MapIterationFunc

type MapIterationFunc func(key, value interface{}) error

type PriorityQueue

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

func NewPriorityQueue

func NewPriorityQueue() *PriorityQueue

NewPriorityQueue creates a new priority queue backed by a heap.

func (*PriorityQueue) Add

func (pq *PriorityQueue) Add(v interface{}, priority float64)

Add inserts a new item to the priority queue with the given priority.

func (*PriorityQueue) Clear

func (pq *PriorityQueue) Clear()

func (*PriorityQueue) Empty

func (pq *PriorityQueue) Empty() bool

func (*PriorityQueue) Poll

func (pq *PriorityQueue) Poll() (interface{}, bool)

Poll retrieves and removes the item with the highest priority from the queue.

If no items are currently in the queue, this function returns a nil value and false. Otherwise, it returns the value and true.

func (*PriorityQueue) PollInto

func (pq *PriorityQueue) PollInto(into interface{}) bool

PollInto retrieves and removes the item with the highest priority from the queue, and stores the item value in the into parameter. The into parameter must be of a type assignable by the stored value. If there are no items in the queue, the into parameter is not modified and the function returns false. Otherwise, the function returns true.

If the into parameter is not compatible with the stored value, this function will panic.

func (*PriorityQueue) Size

func (pq *PriorityQueue) Size() int

type Set

type Set interface {
	Container

	// Contains returns true if the set contains all of the elements specified,
	// and false otherwise.
	Contains(elements ...interface{}) bool

	// Adds inserts all of the elements specified to the set. If an element
	// already exists in the set, it will not be duplicated.
	Add(elements ...interface{})

	// AddAll inserts the values from the given container to this set. If an
	// element already exists in the set, it will not be duplicated. Equivalent
	// to a set union operation.
	AddAll(other Container)

	// Remove eliminates all of the elements specified from the set.
	Remove(elements ...interface{})

	// RemoveAll removes the elements from the given set from this set.
	// Equivalent to a set difference operation.
	RemoveAll(other Set)

	// ForEach each element in the set and executes the given callback function.
	// If the callback function returns an error, this function will return the
	// same error and immediately stop iteration.
	//
	// To stop iteration without returning an error, return ErrStopIteration.
	ForEach(fn SetIterationFunc) error

	// ForEachInto iterates each element in the set and executes the given
	// callback function, which must be of a type similar to SetIterationFunc,
	// except that the element parameter may be any type assignable by every
	// element in the set.
	//
	// If the requirements for the fn parameter are not met, this function will
	// panic.
	ForEachInto(fn interface{}) error

	// RetailAll removes any elements that are not shared between this set and
	// the given set. Equivalent to a set intersection operation.
	RetainAll(other Set)
}

A Set is a well-defined collection of distinct objects.

func NewHashSet

func NewHashSet() Set

NewHashSet creates a new set backed by a HashMap.

func NewHashSetWithCapacity

func NewHashSetWithCapacity(capacity int) Set

NewHashSetWithCapacity creates a new set backed by a HashMap with the given initial capacity.

func NewLinkedHashSet

func NewLinkedHashSet() Set

NewLinkedHashSet creates a new set backed by a LinkedHashMap.

func NewLinkedHashSetWithCapacity

func NewLinkedHashSetWithCapacity(capacity int) Set

NewLinkedHashSetWithCapacity creates a new set backed by a LinkedHashMap with the given initial capacity.

type SetIterationFunc

type SetIterationFunc func(element interface{}) error

type SynchronizedMap

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

SynchronizedMap is a synchronization layer for a Map. It exposes the complete Map interface and passes all calls through to a given delegate, guarding them with a mutual exclusion lock.

SynchronizedMap makes no assumptions about the underlying storage. In particular, it does not assume that read operations are concurrently safe.

func NewSynchronizedMap

func NewSynchronizedMap(storage Map) *SynchronizedMap

NewSynchronizedMap creates a synchronization layer over a given map.

func (*SynchronizedMap) Clear

func (sm *SynchronizedMap) Clear()

func (*SynchronizedMap) CompareAndPut

func (sm *SynchronizedMap) CompareAndPut(key, value, expected interface{}) bool

func (*SynchronizedMap) CompareAndRemove

func (sm *SynchronizedMap) CompareAndRemove(key, expected interface{}) bool

func (*SynchronizedMap) Contains

func (sm *SynchronizedMap) Contains(key interface{}) bool

func (*SynchronizedMap) Empty

func (sm *SynchronizedMap) Empty() bool

func (*SynchronizedMap) ForEach

func (sm *SynchronizedMap) ForEach(fn MapIterationFunc) error

func (*SynchronizedMap) ForEachInto

func (sm *SynchronizedMap) ForEachInto(fn interface{}) error

func (*SynchronizedMap) Get

func (sm *SynchronizedMap) Get(key interface{}) (interface{}, bool)

func (*SynchronizedMap) GetInto

func (sm *SynchronizedMap) GetInto(key interface{}, into interface{}) bool

func (*SynchronizedMap) Keys

func (sm *SynchronizedMap) Keys() []interface{}

func (*SynchronizedMap) KeysInto

func (sm *SynchronizedMap) KeysInto(into interface{})

func (*SynchronizedMap) Put

func (sm *SynchronizedMap) Put(key, value interface{}) bool

func (*SynchronizedMap) Remove

func (sm *SynchronizedMap) Remove(key interface{}) bool

func (*SynchronizedMap) Size

func (sm *SynchronizedMap) Size() int

func (*SynchronizedMap) Values

func (sm *SynchronizedMap) Values() []interface{}

func (*SynchronizedMap) ValuesInto

func (sm *SynchronizedMap) ValuesInto(into interface{})

type SynchronizedSet

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

SynchronizedSet is a synchronization layer for a Set. It exposes the complete Set interface and passes all calls through to a given delegate, guarding them with a mutual exclusion lock.

SynchronizedSet makes no assumptions about the underlying storage. In particular, it does not assume that read operations are concurrently safe.

func NewSynchronizedSet

func NewSynchronizedSet(storage Set) *SynchronizedSet

NewSynchronizedSet creates a synchronization layer over a given set.

func (*SynchronizedSet) Add

func (ss *SynchronizedSet) Add(elements ...interface{})

func (*SynchronizedSet) AddAll

func (ss *SynchronizedSet) AddAll(other Container)

func (*SynchronizedSet) Clear

func (ss *SynchronizedSet) Clear()

func (*SynchronizedSet) Contains

func (ss *SynchronizedSet) Contains(elements ...interface{}) bool

func (*SynchronizedSet) Empty

func (ss *SynchronizedSet) Empty() bool

func (*SynchronizedSet) ForEach

func (ss *SynchronizedSet) ForEach(fn SetIterationFunc) error

func (*SynchronizedSet) ForEachInto

func (ss *SynchronizedSet) ForEachInto(fn interface{}) error

func (*SynchronizedSet) Remove

func (ss *SynchronizedSet) Remove(elements ...interface{})

func (*SynchronizedSet) RemoveAll

func (ss *SynchronizedSet) RemoveAll(other Set)

func (*SynchronizedSet) RetainAll

func (ss *SynchronizedSet) RetainAll(other Set)

func (*SynchronizedSet) Size

func (ss *SynchronizedSet) Size() int

func (*SynchronizedSet) Values

func (ss *SynchronizedSet) Values() []interface{}

func (*SynchronizedSet) ValuesInto

func (ss *SynchronizedSet) ValuesInto(into interface{})

Jump to

Keyboard shortcuts

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