collections

package
v1.20220411.3 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2022 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package collections contains helper data structures.

It is not meant to be comprehensive, but does include common structures like RingBuffer and Queue.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Any

type Any = interface{}

Any is a loose type alias to interface{}.

type BatchIterator added in v1.20210615.7

type BatchIterator struct {
	Items     []interface{}
	BatchSize int
	Cursor    int
}

BatchIterator is an iterator for interface{}

func (*BatchIterator) HasNext added in v1.20210615.7

func (bi *BatchIterator) HasNext() bool

HasNext returns if we should process another batch.

func (*BatchIterator) Next added in v1.20210615.7

func (bi *BatchIterator) Next() []interface{}

Next yields the next batch.

type ChannelQueue

type ChannelQueue struct {
	Capacity int
	// contains filtered or unexported fields
}

ChannelQueue is a threadsafe queue.

func NewChannelQueueFromValues added in v1.20220129.5

func NewChannelQueueFromValues(values []interface{}) *ChannelQueue

NewChannelQueueFromValues returns a new ChannelQueue from a given slice of values.

func NewChannelQueueWithCapacity

func NewChannelQueueWithCapacity(capacity int) *ChannelQueue

NewChannelQueueWithCapacity returns a new ChannelQueue instance.

func (*ChannelQueue) Clear

func (cq *ChannelQueue) Clear()

Clear clears the queue.

func (*ChannelQueue) Consume

func (cq *ChannelQueue) Consume(consumer func(value interface{}))

Consume pulls every value out of the channel, calls consumer on it, effectively clearing the queue.

func (*ChannelQueue) Contents

func (cq *ChannelQueue) Contents() []interface{}

Contents iterates over the queue and returns an array of its contents.

func (*ChannelQueue) Dequeue

func (cq *ChannelQueue) Dequeue() interface{}

Dequeue returns the next element in the queue.

func (*ChannelQueue) DequeueBack added in v1.20220129.5

func (cq *ChannelQueue) DequeueBack() interface{}

DequeueBack iterates over the queue, removing the last element and returning it

func (*ChannelQueue) Drain

func (cq *ChannelQueue) Drain() []interface{}

Drain iterates over the queue and returns an array of its contents, leaving it empty.

func (*ChannelQueue) Each

func (cq *ChannelQueue) Each(consumer func(value interface{}))

Each pulls every value out of the channel, calls consumer on it, and puts it back.

func (*ChannelQueue) EachUntil

func (cq *ChannelQueue) EachUntil(consumer func(value interface{}) bool)

EachUntil pulls every value out of the channel, calls consumer on it, and puts it back and can abort mid process.

func (*ChannelQueue) Enqueue

func (cq *ChannelQueue) Enqueue(item interface{})

Enqueue adds an item to the queue.

func (*ChannelQueue) Len

func (cq *ChannelQueue) Len() int

Len returns the number of items in the queue.

func (*ChannelQueue) Peek

func (cq *ChannelQueue) Peek() interface{}

Peek returns (but does not remove) the first element of the queue.

func (*ChannelQueue) PeekBack

func (cq *ChannelQueue) PeekBack() interface{}

PeekBack returns (but does not remove) the last element of the queue.

func (*ChannelQueue) ReverseEachUntil

func (cq *ChannelQueue) ReverseEachUntil(consumer func(value interface{}) bool)

ReverseEachUntil pulls every value out of the channel, calls consumer on it, and puts it back and can abort mid process.

type Error

type Error string

Error is an error string.

func (Error) Error

func (e Error) Error() string

Error implements error.

type Labels

type Labels = map[string]string

Labels is a loose type alias to map[string]string

type LinkedList

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

LinkedList is an implementation of a fifo buffer using nodes and poitners. Remarks; it is not threadsafe. It is constant(ish) time in all ops.

func NewLinkedList

func NewLinkedList() *LinkedList

NewLinkedList returns a new linked list instance.

func NewLinkedListFromValues added in v1.20220129.5

func NewLinkedListFromValues(values []interface{}) *LinkedList

NewLinkedListFromValues creates a linked list out of a slice.

func (*LinkedList) Clear

func (q *LinkedList) Clear()

Clear clears the linked list.

func (*LinkedList) Consume

func (q *LinkedList) Consume(consumer func(value interface{}))

Consume calls the consumer for each element of the linked list, removing it.

func (*LinkedList) Contents

func (q *LinkedList) Contents() []interface{}

Contents returns the full contents of the queue as a slice.

func (*LinkedList) Dequeue

func (q *LinkedList) Dequeue() interface{}

Dequeue removes an item from the front of the queue and returns it.

func (*LinkedList) DequeueBack added in v1.20220129.5

func (q *LinkedList) DequeueBack() interface{}

DequeueBack pops the _last_ element off the linked list.

func (*LinkedList) Drain

func (q *LinkedList) Drain() []interface{}

Drain calls the consumer for each element of the linked list.

func (*LinkedList) Each

func (q *LinkedList) Each(consumer func(value interface{}))

Each calls the consumer for each element of the linked list.

func (*LinkedList) EachUntil

func (q *LinkedList) EachUntil(consumer func(value interface{}) bool)

EachUntil calls the consumer for each element of the linked list, but can abort.

func (*LinkedList) Enqueue

func (q *LinkedList) Enqueue(value interface{})

Enqueue adds a new value to the queue.

func (*LinkedList) Len

func (q *LinkedList) Len() int

Len returns the length of the queue in constant time.

func (*LinkedList) Peek

func (q *LinkedList) Peek() interface{}

Peek returns the first element of the queue but does not remove it.

func (*LinkedList) PeekBack

func (q *LinkedList) PeekBack() interface{}

PeekBack returns the last element of the queue.

func (*LinkedList) ReverseEachUntil

func (q *LinkedList) ReverseEachUntil(consumer func(value interface{}) bool)

ReverseEachUntil calls the consumer for each element of the linked list, but can abort.

type Queue

type Queue interface {
	Len() int
	Enqueue(value interface{})
	Dequeue() interface{}
	DequeueBack() interface{}
	Peek() interface{}
	PeekBack() interface{}
	Drain() []interface{}
	Contents() []interface{}
	Clear()

	Consume(consumer func(value interface{}))
	Each(consumer func(value interface{}))
	EachUntil(consumer func(value interface{}) bool)
	ReverseEachUntil(consumer func(value interface{}) bool)
}

Queue is an interface for implementations of a FIFO buffer.

With the DequeueBack method, you can also use a Queue as a stack.

type RingBuffer

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

RingBuffer is a fifo buffer that is backed by a pre-allocated array, instead of allocating a whole new node object for each element (which saves GC churn). Enqueue can be O(n), Dequeue can be O(1).

func NewRingBuffer

func NewRingBuffer() *RingBuffer

NewRingBuffer creates a new, empty, RingBuffer.

func NewRingBufferFromValues

func NewRingBufferFromValues(values []interface{}) *RingBuffer

NewRingBufferFromValues creates a new ring buffer out of a slice.

func NewRingBufferWithCapacity

func NewRingBufferWithCapacity(capacity int) *RingBuffer

NewRingBufferWithCapacity creates a new ringbuffer with a given capacity.

func (*RingBuffer) Capacity

func (rb *RingBuffer) Capacity() int

Capacity returns the total size of the ring bufffer, including empty elements.

func (*RingBuffer) Clear

func (rb *RingBuffer) Clear()

Clear removes all objects from the RingBuffer.

func (*RingBuffer) Consume

func (rb *RingBuffer) Consume(consumer func(value interface{}))

Consume calls the consumer for each element in the buffer, while also dequeueing that entry.

func (*RingBuffer) Contents

func (rb *RingBuffer) Contents() []interface{}

Contents returns the ring buffer, in order, as a slice.

func (*RingBuffer) Dequeue

func (rb *RingBuffer) Dequeue() interface{}

Dequeue removes the first (oldest) element from the RingBuffer.

func (*RingBuffer) DequeueBack added in v1.20220129.5

func (rb *RingBuffer) DequeueBack() interface{}

DequeueBack removes the last (newest) element from the RingBuffer.

func (*RingBuffer) Drain

func (rb *RingBuffer) Drain() []interface{}

Drain clears the buffer and removes the contents.

func (*RingBuffer) Each

func (rb *RingBuffer) Each(consumer func(value interface{}))

Each calls the consumer for each element in the buffer.

func (*RingBuffer) EachUntil

func (rb *RingBuffer) EachUntil(consumer func(value interface{}) bool)

EachUntil calls the consumer for each element in the buffer with a stopping condition in head=>tail order.

func (*RingBuffer) Enqueue

func (rb *RingBuffer) Enqueue(object interface{})

Enqueue adds an element to the "back" of the RingBuffer.

func (*RingBuffer) Len

func (rb *RingBuffer) Len() (len int)

Len returns the length of the ring buffer (as it is currently populated). Actual memory footprint may be different.

func (*RingBuffer) Peek

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

Peek returns but does not remove the first element.

func (*RingBuffer) PeekBack

func (rb *RingBuffer) PeekBack() interface{}

PeekBack returns but does not remove the last element.

func (*RingBuffer) ReverseEachUntil

func (rb *RingBuffer) ReverseEachUntil(consumer func(value interface{}) bool)

ReverseEachUntil calls the consumer for each element in the buffer with a stopping condition in tail=>head order.

func (*RingBuffer) String

func (rb *RingBuffer) String() string

type SetOfInt

type SetOfInt map[int]bool

SetOfInt is a type alias for map[int]int

func NewSetOfInt

func NewSetOfInt(values ...int) SetOfInt

NewSetOfInt creates a new SetOfInt.

func (SetOfInt) Add

func (si SetOfInt) Add(i int)

Add adds an element to the set, replaceing a previous value.

func (SetOfInt) AsSlice

func (si SetOfInt) AsSlice() []int

AsSlice returns the set as a slice.

func (SetOfInt) Contains

func (si SetOfInt) Contains(i int) bool

Contains returns if the element is in the set.

func (SetOfInt) Copy

func (si SetOfInt) Copy() SetOfInt

Copy returns a new copy of the set.

func (SetOfInt) Difference

func (si SetOfInt) Difference(other SetOfInt) SetOfInt

Difference returns non-shared elements between two sets.

func (SetOfInt) Intersect

func (si SetOfInt) Intersect(other SetOfInt) SetOfInt

Intersect returns shared elements between two sets.

func (SetOfInt) IsSubsetOf

func (si SetOfInt) IsSubsetOf(other SetOfInt) bool

IsSubsetOf returns if a given set is a complete subset of another set, i.e. all elements in target set are in other set.

func (SetOfInt) Len

func (si SetOfInt) Len() int

Len returns the number of elements in the set.

func (SetOfInt) Remove

func (si SetOfInt) Remove(i int)

Remove removes an element from the set.

func (SetOfInt) String

func (si SetOfInt) String() string

String returns the set as a csv string.

func (SetOfInt) Union

func (si SetOfInt) Union(other SetOfInt) SetOfInt

Union joins two sets together without dupes.

type SetOfString

type SetOfString map[string]bool

SetOfString is a set of strings

func NewSetOfString

func NewSetOfString(values ...string) SetOfString

NewSetOfString creates a new SetOfString.

func (SetOfString) Add

func (ss SetOfString) Add(entry string)

Add adds an element.

func (SetOfString) AsSlice

func (ss SetOfString) AsSlice() []string

AsSlice returns the set as a slice.

func (SetOfString) Contains

func (ss SetOfString) Contains(entry string) bool

Contains returns if an element is in the set.

func (SetOfString) Copy

func (ss SetOfString) Copy() SetOfString

Copy returns a new copy of the set.

func (SetOfString) Difference

func (ss SetOfString) Difference(other SetOfString) SetOfString

Difference returns non-shared elements between two sets.

func (SetOfString) Intersect

func (ss SetOfString) Intersect(other SetOfString) SetOfString

Intersect returns shared elements between two sets.

func (SetOfString) IsSubsetOf

func (ss SetOfString) IsSubsetOf(other SetOfString) bool

IsSubsetOf returns if a given set is a complete subset of another set, i.e. all elements in target set are in other set.

func (SetOfString) Len

func (ss SetOfString) Len() int

Len returns the length of the set.

func (SetOfString) Remove

func (ss SetOfString) Remove(entry string) bool

Remove deletes an element, returns if the element was in the set.

func (SetOfString) String

func (ss SetOfString) String() string

String returns the set as a csv string.

func (SetOfString) Union

func (ss SetOfString) Union(other SetOfString) SetOfString

Union joins two sets together without dupes.

type Strings

type Strings []string

Strings is a type alias for []string with some helper methods.

func (Strings) Contains

func (sa Strings) Contains(elem string) bool

Contains returns if the given string is in the array.

func (Strings) ContainsLower

func (sa Strings) ContainsLower(elem string) bool

ContainsLower returns true if the `elem` is in the Strings, false otherwise.

func (Strings) First

func (sa Strings) First() string

First returns the first element of the array.

func (Strings) GetByLower

func (sa Strings) GetByLower(elem string) string

GetByLower returns an element from the array that matches the input.

func (Strings) Last

func (sa Strings) Last() string

Last returns the last element of the array.

func (Strings) Reverse

func (sa Strings) Reverse() (output Strings)

Reverse reverses the strings array in place.

type SyncRingBuffer

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

SyncRingBuffer is a ring buffer wrapper that adds synchronization.

func NewSyncRingBuffer

func NewSyncRingBuffer() *SyncRingBuffer

NewSyncRingBuffer returns a new synchronized ring buffer.

func NewSyncRingBufferWithCapacity

func NewSyncRingBufferWithCapacity(capacity int) *SyncRingBuffer

NewSyncRingBufferWithCapacity returns a new synchronized ring buffer.

func (*SyncRingBuffer) Capacity

func (srb *SyncRingBuffer) Capacity() (val int)

Capacity returns the total size of the ring bufffer, including empty elements.

func (*SyncRingBuffer) Clear

func (srb *SyncRingBuffer) Clear()

Clear removes all objects from the RingBuffer.

func (*SyncRingBuffer) Consume

func (srb *SyncRingBuffer) Consume(consumer func(value interface{}))

Consume calls the consumer for each element in the buffer, while also dequeueing that entry.

func (*SyncRingBuffer) Contents

func (srb *SyncRingBuffer) Contents() (val []interface{})

Contents returns the ring buffer, in order, as a slice.

func (*SyncRingBuffer) Dequeue

func (srb *SyncRingBuffer) Dequeue() (val interface{})

Dequeue removes the first (oldest) element from the RingBuffer.

func (*SyncRingBuffer) DequeueBack added in v1.20220129.5

func (srb *SyncRingBuffer) DequeueBack() (val interface{})

DequeueBack removes the last (newest) element from the RingBuffer.

func (*SyncRingBuffer) Drain

func (srb *SyncRingBuffer) Drain() (val []interface{})

Drain returns the ring buffer, in order, as a slice and empties it.

func (*SyncRingBuffer) Each

func (srb *SyncRingBuffer) Each(consumer func(value interface{}))

Each calls the consumer for each element in the buffer.

func (*SyncRingBuffer) EachUntil

func (srb *SyncRingBuffer) EachUntil(consumer func(value interface{}) bool)

EachUntil calls the consumer for each element in the buffer with a stopping condition in head=>tail order.

func (*SyncRingBuffer) Enqueue

func (srb *SyncRingBuffer) Enqueue(value interface{})

Enqueue adds an element to the "back" of the RingBuffer.

func (SyncRingBuffer) Len

func (srb SyncRingBuffer) Len() (val int)

Len returns the length of the ring buffer (as it is currently populated). Actual memory footprint may be different.

func (*SyncRingBuffer) Peek

func (srb *SyncRingBuffer) Peek() (val interface{})

Peek returns but does not remove the first element.

func (*SyncRingBuffer) PeekBack

func (srb *SyncRingBuffer) PeekBack() (val interface{})

PeekBack returns but does not remove the last element.

func (*SyncRingBuffer) ReverseEachUntil

func (srb *SyncRingBuffer) ReverseEachUntil(consumer func(value interface{}) bool)

ReverseEachUntil calls the consumer for each element in the buffer with a stopping condition in tail=>head order.

func (*SyncRingBuffer) RingBuffer

func (srb *SyncRingBuffer) RingBuffer() *RingBuffer

RingBuffer returns the inner ringbuffer.

func (*SyncRingBuffer) SyncRoot

func (srb *SyncRingBuffer) SyncRoot() *sync.Mutex

SyncRoot returns the mutex used to synchronize the collection.

func (*SyncRingBuffer) TrimExcess

func (srb *SyncRingBuffer) TrimExcess()

TrimExcess resizes the buffer to better fit the contents.

type Vars

type Vars = map[string]interface{}

Vars is a loose type alias to map[string]string

Jump to

Keyboard shortcuts

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