collections

package
v1.20240719.1 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package generic contains generic 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

func Contains added in v1.20240719.1

func Contains[T comparable](s []T, target T) bool

Contains returns if the given string is in the slice.

func First added in v1.20240719.1

func First[T any](s []T) T

First returns the first element of the slice.

func Last added in v1.20240719.1

func Last[T any](s []T) T

Last returns the last element of the slice.

func Reverse added in v1.20240719.1

func Reverse[T any](s []T) []T

Reverse returns a new slice with the elements reversed. Reverse returns nil if the slice is empty.

func ReverseInPlace added in v1.20240719.1

func ReverseInPlace[T any](s []T)

ReverseInPlace reverses the generic slice in place.

Types

type BatchIterator added in v1.20210615.7

type BatchIterator[T any] struct {
	Items     []T
	BatchSize int
	Cursor    int
}

BatchIterator is an iterator for T

func (*BatchIterator[T]) HasNext added in v1.20210615.7

func (bi *BatchIterator[T]) HasNext() bool

HasNext returns if we should process another batch.

func (*BatchIterator[T]) Next added in v1.20210615.7

func (bi *BatchIterator[T]) Next() []T

Next yields the next batch.

type ChannelQueue

type ChannelQueue[T any] struct {
	Capacity int
	// contains filtered or unexported fields
}

ChannelQueue is a thread safe queue.

func NewChannelQueueFromValues added in v1.20220129.5

func NewChannelQueueFromValues[T any](values []T) *ChannelQueue[T]

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

func NewChannelQueueWithCapacity

func NewChannelQueueWithCapacity[T any](capacity int) *ChannelQueue[T]

NewChannelQueueWithCapacity returns a new ChannelQueue instance.

func (*ChannelQueue[T]) Clear

func (cq *ChannelQueue[T]) Clear()

Clear clears the queue.

func (*ChannelQueue[T]) Consume

func (cq *ChannelQueue[T]) Consume(consumer func(value T))

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

func (*ChannelQueue[T]) Contents

func (cq *ChannelQueue[T]) Contents() []T

Contents iterates over the queue and returns a slice of its contents.

func (*ChannelQueue[T]) Dequeue

func (cq *ChannelQueue[T]) Dequeue() T

Dequeue returns the next element in the queue.

func (*ChannelQueue[T]) DequeueBack added in v1.20220129.5

func (cq *ChannelQueue[T]) DequeueBack() T

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

func (*ChannelQueue[T]) Drain

func (cq *ChannelQueue[T]) Drain() []T

Drain iterates over the queue and returns a slice of its contents, leaving it empty.

func (*ChannelQueue[T]) Each

func (cq *ChannelQueue[T]) Each(consumer func(value T))

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

func (*ChannelQueue[T]) EachUntil

func (cq *ChannelQueue[T]) EachUntil(consumer func(value T) bool)

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

func (*ChannelQueue[T]) Enqueue

func (cq *ChannelQueue[T]) Enqueue(item T)

Enqueue adds an item to the queue.

func (*ChannelQueue[T]) Len

func (cq *ChannelQueue[T]) Len() int

Len returns the number of items in the queue.

func (*ChannelQueue[T]) Peek

func (cq *ChannelQueue[T]) Peek() T

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

func (*ChannelQueue[T]) PeekBack

func (cq *ChannelQueue[T]) PeekBack() T

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

func (*ChannelQueue[T]) ReverseEachUntil

func (cq *ChannelQueue[T]) ReverseEachUntil(consumer func(value T) 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[T any] struct {
	// contains filtered or unexported fields
}

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

func NewLinkedList

func NewLinkedList[T any]() *LinkedList[T]

NewLinkedList returns a new linked list instance.

func NewLinkedListFromValues added in v1.20220129.5

func NewLinkedListFromValues[T any](values []T) *LinkedList[T]

NewLinkedListFromValues creates a linked list out of a slice.

func (*LinkedList[T]) Clear

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

Clear clears the linked list.

func (*LinkedList[T]) Consume

func (q *LinkedList[T]) Consume(consumer func(value T))

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

func (*LinkedList[T]) Contents

func (q *LinkedList[T]) Contents() []T

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

func (*LinkedList[T]) Dequeue

func (q *LinkedList[T]) Dequeue() T

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

func (*LinkedList[T]) DequeueBack added in v1.20220129.5

func (q *LinkedList[T]) DequeueBack() T

DequeueBack pops the _last_ element off the linked list.

func (*LinkedList[T]) Drain

func (q *LinkedList[T]) Drain() []T

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

func (*LinkedList[T]) Each

func (q *LinkedList[T]) Each(consumer func(value T))

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

func (*LinkedList[T]) EachUntil

func (q *LinkedList[T]) EachUntil(consumer func(value T) bool)

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

func (*LinkedList[T]) Enqueue

func (q *LinkedList[T]) Enqueue(value T)

Enqueue adds a new value to the queue.

func (*LinkedList[T]) Len

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

Len returns the length of the queue in constant time.

func (*LinkedList[T]) Peek

func (q *LinkedList[T]) Peek() T

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

func (*LinkedList[T]) PeekBack

func (q *LinkedList[T]) PeekBack() T

PeekBack returns the last element of the queue.

func (*LinkedList[T]) ReverseEachUntil

func (q *LinkedList[T]) ReverseEachUntil(consumer func(value T) bool)

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

type Queue

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

	Consume(consumer func(value T))
	Each(consumer func(value T))
	EachUntil(consumer func(value T) bool)
	ReverseEachUntil(consumer func(value T) bool)
}

Queue is an interface for implementations of a FIFO buffer.

type RingBuffer

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

RingBuffer is a fifo buffer that is backed by a pre-allocated slice, 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[T any]() *RingBuffer[T]

NewRingBuffer creates a new, empty, RingBuffer.

func NewRingBufferFromValues

func NewRingBufferFromValues[T any](values []T) *RingBuffer[T]

NewRingBufferFromValues creates a new ring buffer out of a slice.

func NewRingBufferWithCapacity

func NewRingBufferWithCapacity[T any](capacity int) *RingBuffer[T]

NewRingBufferWithCapacity creates a new ring buffer with a given capacity.

func (*RingBuffer[T]) Capacity

func (rb *RingBuffer[T]) Capacity() int

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

func (*RingBuffer[T]) Clear

func (rb *RingBuffer[T]) Clear()

Clear removes all objects from the RingBuffer.

func (*RingBuffer[T]) Consume

func (rb *RingBuffer[T]) Consume(consumer func(value T))

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

func (*RingBuffer[T]) Contents

func (rb *RingBuffer[T]) Contents() []T

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

func (*RingBuffer[T]) Dequeue

func (rb *RingBuffer[T]) Dequeue() T

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

func (*RingBuffer[T]) DequeueBack added in v1.20220129.5

func (rb *RingBuffer[T]) DequeueBack() T

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

func (*RingBuffer[T]) Drain

func (rb *RingBuffer[T]) Drain() []T

Drain clears the buffer and removes the contents.

func (*RingBuffer[T]) Each

func (rb *RingBuffer[T]) Each(consumer func(value T))

Each calls the consumer for each element in the buffer.

func (*RingBuffer[T]) EachUntil

func (rb *RingBuffer[T]) EachUntil(consumer func(value T) bool)

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

func (*RingBuffer[T]) Enqueue

func (rb *RingBuffer[T]) Enqueue(value T)

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

func (*RingBuffer[T]) Len

func (rb *RingBuffer[T]) Len() (len int)

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

func (*RingBuffer[T]) Peek

func (rb *RingBuffer[T]) Peek() T

Peek returns but does not remove the first element.

func (*RingBuffer[T]) PeekBack

func (rb *RingBuffer[T]) PeekBack() T

PeekBack returns but does not remove the last element.

func (*RingBuffer[T]) ReverseEachUntil

func (rb *RingBuffer[T]) ReverseEachUntil(consumer func(value T) bool)

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

func (*RingBuffer[T]) String

func (rb *RingBuffer[T]) String() string

type Set added in v1.20240719.1

type Set[T comparable] map[T]struct{}

Set is a generic implementation of a set data structure.

func NewSet added in v1.20240719.1

func NewSet[T comparable](values ...T) Set[T]

NewSet creates a new set from a list of values.

func (Set[T]) Add added in v1.20240719.1

func (s Set[T]) Add(i T)

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

func (Set[T]) AsSlice added in v1.20240719.1

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

AsSlice returns the set as a slice.

func (Set[T]) Contains added in v1.20240719.1

func (s Set[T]) Contains(i T) bool

Contains returns if the element is in the set.

func (Set[T]) Copy added in v1.20240719.1

func (s Set[T]) Copy() Set[T]

Copy returns a new copy of the set.

func (Set[T]) Difference added in v1.20240719.1

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

Difference returns non-shared elements between two sets.

func (Set[T]) Intersect added in v1.20240719.1

func (s Set[T]) Intersect(other Set[T]) Set[T]

Intersect returns shared elements between two sets.

func (Set[T]) IsSubsetOf added in v1.20240719.1

func (s Set[T]) IsSubsetOf(other Set[T]) 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 (Set[T]) Len added in v1.20240719.1

func (s Set[T]) Len() int

Len returns the number of elements in the set.

func (Set[T]) Remove added in v1.20240719.1

func (s Set[T]) Remove(i T)

Remove removes an element from the set.

func (Set[T]) String added in v1.20240719.1

func (s Set[T]) String() string

String returns the set as a csv string.

func (Set[T]) Subtract added in v1.20240719.1

func (s Set[T]) Subtract(other Set[T]) Set[T]

Subtract removes all elements of `other` set from `s`.

func (Set[T]) Union added in v1.20240719.1

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

Union joins two sets together without dupes.

type Strings

type Strings []string

Strings is a type alias for []string with some helper methods. Deprecated: Use collections/generic version.

func (Strings) Contains

func (sa Strings) Contains(elem string) bool

Contains returns if the given string is in the array. Deprecated: Use collections/generic version.

func (Strings) ContainsLower

func (sa Strings) ContainsLower(elem string) bool

ContainsLower returns true if the `elem` is in the Strings, false otherwise. Deprecated: Use collections/generic version.

func (Strings) First

func (sa Strings) First() string

First returns the first element of the array. Deprecated: Use collections/generic version.

func (Strings) GetByLower

func (sa Strings) GetByLower(elem string) string

GetByLower returns an element from the array that matches the input. Deprecated: Use collections/generic version.

func (Strings) Last

func (sa Strings) Last() string

Last returns the last element of the array. Deprecated: Use collections/generic version.

func (Strings) Reverse

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

Reverse reverses the strings array in place. Deprecated: Use collections/generic version.

type SyncRingBuffer

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

SyncRingBuffer is a ring buffer wrapper that adds synchronization.

func NewSyncRingBuffer

func NewSyncRingBuffer[T any]() *SyncRingBuffer[T]

NewSyncRingBuffer returns a new synchronized ring buffer.

func NewSyncRingBufferWithCapacity

func NewSyncRingBufferWithCapacity[T any](capacity int) *SyncRingBuffer[T]

NewSyncRingBufferWithCapacity returns a new synchronized ring buffer.

func (*SyncRingBuffer[T]) Capacity

func (srb *SyncRingBuffer[T]) Capacity() int

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

func (*SyncRingBuffer[T]) Clear

func (srb *SyncRingBuffer[T]) Clear()

Clear removes all objects from the ring buffer.

func (*SyncRingBuffer[T]) Consume

func (srb *SyncRingBuffer[T]) Consume(consumer func(value T))

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

func (*SyncRingBuffer[T]) Contents

func (srb *SyncRingBuffer[T]) Contents() []T

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

func (*SyncRingBuffer[T]) Dequeue

func (srb *SyncRingBuffer[T]) Dequeue() T

Dequeue removes the first (oldest) element from the ring buffer.

func (*SyncRingBuffer[T]) DequeueBack added in v1.20220129.5

func (srb *SyncRingBuffer[T]) DequeueBack() T

DequeueBack removes the last (newest) element from the ring buffer.

func (*SyncRingBuffer[T]) Drain

func (srb *SyncRingBuffer[T]) Drain() []T

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

func (*SyncRingBuffer[T]) Each

func (srb *SyncRingBuffer[T]) Each(consumer func(value T))

Each calls the consumer for each element in the buffer.

func (*SyncRingBuffer[T]) EachUntil

func (srb *SyncRingBuffer[T]) EachUntil(consumer func(value T) bool)

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

func (*SyncRingBuffer[T]) Enqueue

func (srb *SyncRingBuffer[T]) Enqueue(value T)

Enqueue adds an element to the "back" of the ring buffer.

func (SyncRingBuffer[T]) Len

func (srb SyncRingBuffer[T]) Len() int

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

func (*SyncRingBuffer[T]) Peek

func (srb *SyncRingBuffer[T]) Peek() T

Peek returns but does not remove the first element.

func (*SyncRingBuffer[T]) PeekBack

func (srb *SyncRingBuffer[T]) PeekBack() T

PeekBack returns but does not remove the last element.

func (*SyncRingBuffer[T]) ReverseEachUntil

func (srb *SyncRingBuffer[T]) ReverseEachUntil(consumer func(value T) bool)

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

func (*SyncRingBuffer[T]) RingBuffer

func (srb *SyncRingBuffer[T]) RingBuffer() *RingBuffer[T]

RingBuffer returns the inner ring buffer.

func (*SyncRingBuffer[T]) SyncRoot

func (srb *SyncRingBuffer[T]) SyncRoot() *sync.Mutex

SyncRoot returns the mutex used to synchronize the collection.

func (*SyncRingBuffer[T]) TrimExcess

func (srb *SyncRingBuffer[T]) 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