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 ¶
- type Any
- type ChannelQueue
- func (cq *ChannelQueue) Clear()
- func (cq *ChannelQueue) Consume(consumer func(value interface{}))
- func (cq *ChannelQueue) Contents() []interface{}
- func (cq *ChannelQueue) Dequeue() interface{}
- func (cq *ChannelQueue) Drain() []interface{}
- func (cq *ChannelQueue) Each(consumer func(value interface{}))
- func (cq *ChannelQueue) EachUntil(consumer func(value interface{}) bool)
- func (cq *ChannelQueue) Enqueue(item interface{})
- func (cq *ChannelQueue) Len() int
- func (cq *ChannelQueue) Peek() interface{}
- func (cq *ChannelQueue) PeekBack() interface{}
- func (cq *ChannelQueue) ReverseEachUntil(consumer func(value interface{}) bool)
- type Error
- type Labels
- type LinkedList
- func (q *LinkedList) Clear()
- func (q *LinkedList) Consume(consumer func(value interface{}))
- func (q *LinkedList) Contents() []interface{}
- func (q *LinkedList) Dequeue() interface{}
- func (q *LinkedList) Drain() []interface{}
- func (q *LinkedList) Each(consumer func(value interface{}))
- func (q *LinkedList) EachUntil(consumer func(value interface{}) bool)
- func (q *LinkedList) Enqueue(value interface{})
- func (q *LinkedList) Len() int
- func (q *LinkedList) Peek() interface{}
- func (q *LinkedList) PeekBack() interface{}
- func (q *LinkedList) ReverseEachUntil(consumer func(value interface{}) bool)
- type Queue
- type RingBuffer
- func (rb *RingBuffer) Capacity() int
- func (rb *RingBuffer) Clear()
- func (rb *RingBuffer) Consume(consumer func(value interface{}))
- func (rb *RingBuffer) Contents() []interface{}
- func (rb *RingBuffer) Dequeue() interface{}
- func (rb *RingBuffer) Drain() []interface{}
- func (rb *RingBuffer) Each(consumer func(value interface{}))
- func (rb *RingBuffer) EachUntil(consumer func(value interface{}) bool)
- func (rb *RingBuffer) Enqueue(object interface{})
- func (rb *RingBuffer) Len() (len int)
- func (rb *RingBuffer) Peek() interface{}
- func (rb *RingBuffer) PeekBack() interface{}
- func (rb *RingBuffer) ReverseEachUntil(consumer func(value interface{}) bool)
- func (rb *RingBuffer) String() string
- type SetOfInt
- func (si SetOfInt) Add(i int)
- func (si SetOfInt) AsSlice() []int
- func (si SetOfInt) Contains(i int) bool
- func (si SetOfInt) Copy() SetOfInt
- func (si SetOfInt) Difference(other SetOfInt) SetOfInt
- func (si SetOfInt) Intersect(other SetOfInt) SetOfInt
- func (si SetOfInt) IsSubsetOf(other SetOfInt) bool
- func (si SetOfInt) Len() int
- func (si SetOfInt) Remove(i int)
- func (si SetOfInt) String() string
- func (si SetOfInt) Union(other SetOfInt) SetOfInt
- type SetOfString
- func (ss SetOfString) Add(entry string)
- func (ss SetOfString) AsSlice() []string
- func (ss SetOfString) Contains(entry string) bool
- func (ss SetOfString) Copy() SetOfString
- func (ss SetOfString) Difference(other SetOfString) SetOfString
- func (ss SetOfString) Intersect(other SetOfString) SetOfString
- func (ss SetOfString) IsSubsetOf(other SetOfString) bool
- func (ss SetOfString) Len() int
- func (ss SetOfString) Remove(entry string) bool
- func (ss SetOfString) String() string
- func (ss SetOfString) Union(other SetOfString) SetOfString
- type Strings
- type SyncRingBuffer
- func (srb *SyncRingBuffer) Capacity() (val int)
- func (srb *SyncRingBuffer) Clear()
- func (srb *SyncRingBuffer) Consume(consumer func(value interface{}))
- func (srb *SyncRingBuffer) Contents() (val []interface{})
- func (srb *SyncRingBuffer) Dequeue() (val interface{})
- func (srb *SyncRingBuffer) Drain() (val []interface{})
- func (srb *SyncRingBuffer) Each(consumer func(value interface{}))
- func (srb *SyncRingBuffer) EachUntil(consumer func(value interface{}) bool)
- func (srb *SyncRingBuffer) Enqueue(value interface{})
- func (srb SyncRingBuffer) Len() (val int)
- func (srb *SyncRingBuffer) Peek() (val interface{})
- func (srb *SyncRingBuffer) PeekBack() (val interface{})
- func (srb *SyncRingBuffer) ReverseEachUntil(consumer func(value interface{}) bool)
- func (srb *SyncRingBuffer) RingBuffer() *RingBuffer
- func (srb *SyncRingBuffer) SyncRoot() *sync.Mutex
- func (srb *SyncRingBuffer) TrimExcess()
- type Vars
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ChannelQueue ¶
type ChannelQueue struct { Capacity int // contains filtered or unexported fields }
ChannelQueue is a threadsafe queue.
func NewChannelQueueWithCapacity ¶
func NewChannelQueueWithCapacity(capacity int) *ChannelQueue
NewChannelQueueWithCapacity returns a new ConcurrentQueue instance.
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) 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 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 (*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) 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{} 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.
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 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) 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 ¶
SetOfInt is a type alias for map[int]int
func (SetOfInt) Difference ¶
Difference returns non-shared elements between two sets.
func (SetOfInt) IsSubsetOf ¶
IsSubsetOf returns if a given set is a complete subset of another set, i.e. all elements in target set are in other set.
type SetOfString ¶
SetOfString is a set of strings
func NewSetOfString ¶
func NewSetOfString(values ...string) SetOfString
NewSetOfString creates a new SetOfString.
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) 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) ContainsLower ¶
ContainsLower returns true if the `elem` is in the Strings, false otherwise.
func (Strings) GetByLower ¶
GetByLower returns an element from the array that matches the input.
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) 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.