collections

package
v0.0.0-...-95ed4ff Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2018 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ChannelQueue

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

ChannelQueue is a threadsafe queue.

func (*ChannelQueue) AsSlice

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

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

func (*ChannelQueue) Clear

func (cq *ChannelQueue) Clear()

Clear clears the queue.

func (*ChannelQueue) Dequeue

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

Dequeue returns the next element in the queue.

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) AsSlice

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

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

func (*LinkedList) Clear

func (q *LinkedList) Clear()

Clear clears the linked list.

func (*LinkedList) Dequeue

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

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

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{}
	AsSlice() []interface{}
	Clear()

	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.

func NewChannelQueue

func NewChannelQueue(maxSize int) Queue

NewChannelQueue returns a new ConcurrentQueue instance.

func NewLinkedList

func NewLinkedList() Queue

NewLinkedList returns a new Queue instance.

type RateLimiter

type RateLimiter struct {
	NumberOfActions int
	Quantum         time.Duration
	Limits          map[string]Queue
}

RateLimiter is a simple implementation of a rate checker.

func NewRateLimiter

func NewRateLimiter(numberOfActions int, quantum time.Duration) *RateLimiter

NewRateLimiter returns a new RateLimiter instance.

func (*RateLimiter) Check

func (rl *RateLimiter) Check(id string) bool

Check returns true if it has been called NumberOfActions times or more in Quantum or smaller duration.

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 NewRingBufferFromSlice

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

NewRingBufferFromSlice createsa ring buffer out of a slice.

func NewRingBufferWithCapacity

func NewRingBufferWithCapacity(capacity int) *RingBuffer

NewRingBufferWithCapacity creates a new RingBuffer pre-allocated with the given capacity.

func (*RingBuffer) AsSlice

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

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

func (*RingBuffer) Clear

func (rb *RingBuffer) Clear()

Clear removes all objects from the RingBuffer.

func (*RingBuffer) Dequeue

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

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

func (*RingBuffer) Drain

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

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

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

func (*RingBuffer) TotalLen

func (rb *RingBuffer) TotalLen() int

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

func (*RingBuffer) TrimExcess

func (rb *RingBuffer) TrimExcess()

TrimExcess resizes the buffer to better fit the contents.

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 StringArray

type StringArray []string

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

func (StringArray) Contains

func (sa StringArray) Contains(elem string) bool

Contains returns if the given string is in the array.

func (StringArray) ContainsLower

func (sa StringArray) ContainsLower(elem string) bool

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

func (StringArray) GetByLower

func (sa StringArray) GetByLower(elem string) string

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

type SynchronizedRingBuffer

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

SynchronizedRingBuffer is a ring buffer wrapper that adds synchronization.

func NewSynchronizedRingBuffer

func NewSynchronizedRingBuffer() *SynchronizedRingBuffer

NewSynchronizedRingBuffer returns a new synchronized ring buffer.

func NewSynchronizedRingBufferWithCapacity

func NewSynchronizedRingBufferWithCapacity(capacity int) *SynchronizedRingBuffer

NewSynchronizedRingBufferWithCapacity retrns a new synchronized ring buffer with a given initial capacity.

func (*SynchronizedRingBuffer) AsSlice

func (srb *SynchronizedRingBuffer) AsSlice() (val []interface{})

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

func (*SynchronizedRingBuffer) Clear

func (srb *SynchronizedRingBuffer) Clear()

Clear removes all objects from the RingBuffer.

func (*SynchronizedRingBuffer) Dequeue

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

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

func (*SynchronizedRingBuffer) Drain

func (srb *SynchronizedRingBuffer) Drain(consumer func(value interface{}))

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

func (*SynchronizedRingBuffer) Each

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

Each calls the consumer for each element in the buffer.

func (*SynchronizedRingBuffer) EachUntil

func (srb *SynchronizedRingBuffer) 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 (*SynchronizedRingBuffer) Enqueue

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

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

func (*SynchronizedRingBuffer) InnerBuffer

func (srb *SynchronizedRingBuffer) InnerBuffer() *RingBuffer

InnerBuffer returns the inner ringbuffer.

func (SynchronizedRingBuffer) Len

func (srb SynchronizedRingBuffer) Len() (val int)

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

func (*SynchronizedRingBuffer) Peek

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

Peek returns but does not remove the first element.

func (*SynchronizedRingBuffer) PeekBack

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

PeekBack returns but does not remove the last element.

func (*SynchronizedRingBuffer) ReverseEachUntil

func (srb *SynchronizedRingBuffer) 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 (*SynchronizedRingBuffer) SyncRoot

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

SyncRoot returns the mutex used to synchronize the collection.

func (*SynchronizedRingBuffer) TotalLen

func (srb *SynchronizedRingBuffer) TotalLen() (val int)

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

func (*SynchronizedRingBuffer) TrimExcess

func (srb *SynchronizedRingBuffer) TrimExcess()

TrimExcess resizes the buffer to better fit the contents.

Jump to

Keyboard shortcuts

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