collections

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2023 License: MIT Imports: 7 Imported by: 0

README

collections-go

A library/package of common generic collection data structures in Go

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrEmptyStack is a sentinel error value indicating the operation cannot
	// be performed on an empty stack.
	ErrEmptyStack = errors.New("stack is empty")
)

Functions

This section is empty.

Types

type Deque

type Deque[T any] struct {
	// contains filtered or unexported fields
}

Deque is a linear collection of elements that supports insertion and deletion at both ends. Deque is short for double ended queue.

Deque efficiently handles adding and removing elements at either end with O(1) performance. This is accomplished using a ring buffer to store the data which reduces pressure on garbage collector and more efficiently uses memory when compared to implementations that use linked lists or slices.

Deque can be used as both a queue or a stack.

Stack
	PushBack - Push element to top of stack
	PopBack - Pop top element off the stack
	Back - Peek at the top of the stack

Queue
	PushBack- Add element to end of the queue
	PopFront - Poll the first element at the head of the queue
	Front - Peek at the next element in the queue

This package also provides Queue and Stack types which wraps Deque in a simpler API.

The zero-value of Deque is not usable. New instances of Deque should be created and initialized using NewDeque function.

func NewDeque

func NewDeque[T any]() *Deque[T]

NewDeque creates and initializes a new empty Deque

func (*Deque[T]) AsSlice

func (q *Deque[T]) AsSlice() []T

AsSlice returns an array containing all the elements of the Deque in order of front/head to back/tail.

func (*Deque[T]) Back

func (q *Deque[T]) Back() (T, bool)

Back returns the last element of the Deque. If the Deque is empty the zero-value will be returned with a bool value of false.

func (*Deque[T]) Capacity

func (q *Deque[T]) Capacity() int

Capacity returns the size of the underlying ring-buffer holding the elements.

func (*Deque[T]) Clear

func (q *Deque[T]) Clear()

Clear removes all the elements from the Deque but retains the current capacity.

func (*Deque[T]) Front

func (q *Deque[T]) Front() (T, bool)

Front returns the first element of the Deque. If the Deque is empty the zero-value will be returned with a bool value of false.

func (*Deque[T]) GetAt

func (q *Deque[T]) GetAt(idx int) T

GetAt returns the element at the provided index.

If the index is not valid this will panic.

func (*Deque[T]) IsEmpty

func (q *Deque[T]) IsEmpty() bool

func (*Deque[T]) Len

func (q *Deque[T]) Len() int

Len returns the number of elements in the Deque.

func (*Deque[T]) PopBack

func (q *Deque[T]) PopBack() (T, bool)

PopBack pops the back/last element of the Deque removing it and returning the value. If the Deque is empty the zero-value will be returned with a bool value of false.

func (*Deque[T]) PopFront

func (q *Deque[T]) PopFront() (T, bool)

PopFront pops the front/first element of the Deque removing it and returning the value. If the Deque is empty the zero-value will be returned with a bool value of false.

func (*Deque[T]) PushBack

func (q *Deque[T]) PushBack(elem T)

PushBack pushes a new element to the back of the Deque

func (*Deque[T]) PushFront

func (q *Deque[T]) PushFront(elem T)

PushFront pushes a new element at the front of the Deque

type OrderedMap

type OrderedMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

OrderedMap is a map implementation that maintains the order keys were inserted into the map. Set, Get, and Delete operations remain O(1) and the keys can be iterated in the order they were added using ForEach, or in reverse order using ForEachReverse.

The zero-value of OrderedMap is not usable. NewOrderedMap should be used to create and initialize a new instance of OrderedMap.

func NewOrderedMap

func NewOrderedMap[K comparable, V any]() *OrderedMap[K, V]

NewOrderedMap creates and initializes a new OrderedMap

func (*OrderedMap[K, V]) Contains

func (m *OrderedMap[K, V]) Contains(key K) bool

Contains returns true if the given key exists in the OrderedMap, otherwise returns false.

func (*OrderedMap[K, V]) Delete

func (m *OrderedMap[K, V]) Delete(key K) bool

Delete removes a key/value entry from the OrderedMap. If the key didn't exist returns false so, otherwise returns true if the entry was deleted.

func (*OrderedMap[K, V]) ForEach

func (m *OrderedMap[K, V]) ForEach(fn func(key K, val V))

ForEach iterates through the map entries passing the key/value pair to the provided function/closure in the order they were inserted.

func (*OrderedMap[K, V]) ForEachReverse

func (m *OrderedMap[K, V]) ForEachReverse(fn func(key K, val V))

ForEachReverse iterates through the map entries passing the key/value pairs to the provided function/closure in the reverse order they were inserted (most recently inserted to least recently)

func (*OrderedMap[K, V]) Get

func (m *OrderedMap[K, V]) Get(key K) (V, bool)

Get retrieves the value for a key. It follows the same idioms of the built-in map. If the key doesn't exist the zero value and false value are returned.

func (*OrderedMap[K, V]) GetOrDefault

func (m *OrderedMap[K, V]) GetOrDefault(key K, defaultValue V) V

GetOrDefault retrieves the value for a key and if it doesn't exist returns the provided default value.

func (*OrderedMap[K, V]) Keys

func (m *OrderedMap[K, V]) Keys() []K

Keys returns the keys in the order they were inserted.

func (*OrderedMap[K, V]) Set

func (m *OrderedMap[K, V]) Set(key K, val V) bool

Set inserts a new key/value into the map or replaces the value for an existing key. If the key didn't exist in the map and was inserted true is returned. Otherwise, if they key was already existing returns false.

func (*OrderedMap[K, V]) Size

func (m *OrderedMap[K, V]) Size() int

Size returns the number of entries in the OrderedMap

type PriorityQueue added in v0.2.0

type PriorityQueue[T any] struct {
	// contains filtered or unexported fields
}

PriorityQueue is a queue data structure that orders elements according to priority. When Pop is invoked the item with the highest priority is returned.

The zero-value of PriorityQueue is not usable. Use NewPriorityQueue to create and initialize a new PriorityQueue.

func NewPriorityQueue added in v0.2.0

func NewPriorityQueue[T any]() *PriorityQueue[T]

NewPriorityQueue creates and initializes a new PriorityQueue.

func (*PriorityQueue[T]) IsEmpty added in v0.2.0

func (pq *PriorityQueue[T]) IsEmpty() bool

IsEmpty returns true if the PriorityQueue is empty, otherwise false.

func (*PriorityQueue[T]) Len added in v0.2.0

func (pq *PriorityQueue[T]) Len() int

Len returns the length/size of the PriorityQueue.

func (*PriorityQueue[T]) Peek added in v0.2.0

func (pq *PriorityQueue[T]) Peek() (T, bool)

Peek returns the next element to be polled from the PriorityQueue. If the queue is empty the zero value is returned with a boolean value of false.

func (*PriorityQueue[T]) Poll added in v0.2.0

func (pq *PriorityQueue[T]) Poll() (T, bool)

Poll retrieves the highest priority item from the queue and removes it from the PriorityQueue. If the queue is empty the zero value is returned with a boolean value of false.

func (*PriorityQueue[T]) Push added in v0.2.0

func (pq *PriorityQueue[T]) Push(val T, priority int)

Push adds an element to the PriorityQueue with the specified priority.

type Queue

type Queue[T any] struct {
	// contains filtered or unexported fields
}

Queue represents an unbounded first-in-first-out queue of elements.

Under the hood Queue simply wraps Deque to provide a more friendly API for working with queues.

The zero-value of Queue is not usable. Queue should be created/initialized using NewQueue.

func NewQueue

func NewQueue[T any]() *Queue[T]

NewQueue creates and initializes a new empty Queue.

func (*Queue[T]) Empty

func (q *Queue[T]) Empty() bool

Empty returns a boolean value indicating if the Queue is empty, true if it is empty, otherwise false.

func (*Queue[T]) Offer

func (q *Queue[T]) Offer(val T)

Offer inserts/adds an element at the end of the queue.

func (*Queue[T]) Peek

func (q *Queue[T]) Peek() (val T, ok bool)

Peek retrieves and returns the first element in the queue. Unlike Poll, the element is not removed. If the queue is empty the zero value will be returned along with an ok value of false.

func (*Queue[T]) Poll

func (q *Queue[T]) Poll() (val T, ok bool)

Poll retrieves the first element in the queue, returning the value, proceeding by removing it from the queue. If the queue is empty the zero value will be returned along with an ok value of false.

func (*Queue[T]) Size

func (q *Queue[T]) Size() int

Size returns the number of elements in the queue.

type Set

type Set[T comparable] struct {
	// contains filtered or unexported fields
}

Set is a collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1 == e2. As implied by its name, Set models the mathematical set abstraction.

Set makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time.

The zero-value of Set is not usable. Set should be created and initialized using NewSet function.

Set supports marshaling/unmarshalling for json and msgpack out of the box.

Note: Set is not thread safe.

func NewSet

func NewSet[T comparable]() *Set[T]

NewSet creates and initializes a Set

func (*Set[T]) Add

func (s *Set[T]) Add(vals ...T)

Add adds the elements into the Set. If an element already exists it is effectively a no-op.

func (*Set[T]) AsSlice

func (s *Set[T]) AsSlice() []T

AsSlice converts a Set to a built-in Go slice.

func (*Set[T]) Clone

func (s *Set[T]) Clone() *Set[T]

Clone does a deep copy and returns a new Set with the same elements/deque.

func (*Set[T]) Contains

func (s *Set[T]) Contains(val T) bool

Contains reads through the Set and returns a boolean value indicating if the provided value is in the Set.

func (*Set[T]) Difference

func (s *Set[T]) Difference(other *Set[T]) *Set[T]

Difference returns a new Set containing all the elements in other that did not exists in current Set.

func (*Set[T]) Equals

func (s *Set[T]) Equals(other *Set[T]) bool

Equals returns a boolean indicating if the Set is equal to the provided Set.

func (*Set[T]) ForEach

func (s *Set[T]) ForEach(fn func(val T))

ForEach iterates through the Set passing the value to the provided function.

func (*Set[T]) Iter

func (s *Set[T]) Iter() *SetIterator[T]

Iter returns a stateful iterator for iterator over a Set.

Note: Internally AsSlice is called to populate the SetIterator. In the vast majority of use cases this is unlikely to matter. However, if the Set contains a vast amount of data it may be more efficient to use ForEach with a closure.

func (*Set[T]) MarshalJSON

func (s *Set[T]) MarshalJSON() ([]byte, error)

MarshalJSON marshals a Set into binary JSON representation

func (*Set[T]) MarshalMsgpack

func (s *Set[T]) MarshalMsgpack() ([]byte, error)

MarshalMsgpack marshals a Set into binary msgpack representation.

func (*Set[T]) Remove

func (s *Set[T]) Remove(val T)

Remove removes/deletes an element from the Set. If the element doesn't exist this is a no-op.

func (*Set[T]) Size

func (s *Set[T]) Size() int

Size returns the current number of elements in the Set.

func (*Set[T]) Union

func (s *Set[T]) Union(other *Set[T]) *Set[T]

Union returns a new Set which contains all the elements in both sets.

func (*Set[T]) UnmarshalJSON

func (s *Set[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshalls binary JSON representation of a Set into this instance of Set.

func (*Set[T]) UnmarshalMsgpack

func (s *Set[T]) UnmarshalMsgpack(data []byte) error

UnmarshalMsgpack unmarshalls binary msgpack representation of a Set into this instance of Set.

type SetIterator

type SetIterator[T comparable] struct {
	// contains filtered or unexported fields
}

SetIterator is a stateful iterator for iterating through a Set.

func (*SetIterator[T]) Next

func (s *SetIterator[T]) Next() bool

Next moves the iterator to the next element/value and returns a boolean indicating if there is a valid value.

func (*SetIterator[T]) Value

func (s *SetIterator[T]) Value() T

Value returns the current value.

type Stack

type Stack[T any] struct {
	// contains filtered or unexported fields
}

Stack represents a last-in-first-out (LIFO) stack of elements.

Under the hood Stack simply wraps Deque to provide a more friendly API for working with stacks.

The zero-value of Stack is not usable. In order to properly initialize and use Stack use the NewStack function.

func NewStack

func NewStack[T any]() *Stack[T]

NewStack creates and initializes a new empty Stack.

func (*Stack[T]) Empty

func (s *Stack[T]) Empty() bool

Empty returns a boolean indicating if the stack is empty.

func (*Stack[T]) Peek

func (s *Stack[T]) Peek() (val T, ok bool)

Peek returns the element at the top of the stack but doesn't remove it. Peek additionally returns a boolean indicating if a value was returned. If the stack was empty false we be returned.

func (*Stack[T]) Pop

func (s *Stack[T]) Pop() (val T, err error)

Pop removes and returns the element at the top of the stack. In the event the Stack is empty a non-nil error value of ErrEmptyStack is returned.

func (*Stack[T]) PopEach

func (s *Stack[T]) PopEach(fn func(val T))

PopEach will pop each element off the stack one by one until the stack is empty. As each element is popped off the stack the provided function is executed with the popped off value.

func (*Stack[T]) Push

func (s *Stack[T]) Push(val T)

Push pushes an element to the top of the stack.

func (*Stack[T]) Size

func (s *Stack[T]) Size() int

Size returns the current size (number of elements) on the stack.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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