collections

package
v0.9.3 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2024 License: Apache-2.0 Imports: 7 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrorStopIteration = errors.New("stopped iteration")

Functions

func MapUpdate added in v0.4.6

func MapUpdate[K comparable, V any](curr map[K]V, incom map[K]V)

func SliceContains

func SliceContains[T comparable](data []T, value T) bool

func SliceFilter added in v0.4.3

func SliceFilter[T any](data []T, filterFn func(v T, i int) bool) []T

func SliceFindIndex added in v0.2.2

func SliceFindIndex[T any](data []T, cmp func(v T) bool) int

func SliceMap added in v0.2.2

func SliceMap[T any, U any](data []T, mapFn func(v T, i int) U) []U

func SliceReduce added in v0.2.2

func SliceReduce[T any, U any](data []T, reduceFn func(acc U, curr T, i int) U, initial U) U

func SliceShuffle added in v0.9.2

func SliceShuffle[T any](data []T)

SliceShuffle shuffles a slice in place

Types

type BinTree added in v0.5.0

type BinTree[T Comparable] struct {
	// contains filtered or unexported fields
}

BinTree is a simple unbalanced binary tree implementation for storing sorted values

func (*BinTree[T]) Add added in v0.5.0

func (bt *BinTree[T]) Add(v ...T)

func (*BinTree[T]) ForEach added in v0.5.2

func (bt *BinTree[T]) ForEach(fn func(i int, v T) bool)

func (*BinTree[T]) Get added in v0.5.0

func (bt *BinTree[T]) Get(idx int) T

Get returns an item on i-th index. The function also supports negative indices where -1 is the last item. In case the index does not exist in data the function panics.

func (*BinTree[T]) Len added in v0.5.0

func (bt *BinTree[T]) Len() int

func (*BinTree[T]) Remove added in v0.5.0

func (bt *BinTree[T]) Remove(idx int) T

func (BinTree[T]) ToSlice added in v0.5.0

func (bt BinTree[T]) ToSlice() []T

type CircularList added in v0.3.3

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

CircularList is a structure allowing infinite appending of new items while rewriting the oldest (in terms of order of respective Append() operations) records if needed. It also allows removing oldest records based on a condition - here we expect that the values contain some value representing their order in which they have been added - typically it is a time information.

func NewCircularList added in v0.3.3

func NewCircularList[T any](capacity int) *CircularList[T]

NewCircularList is a recommended factory function for CircularList. The parameter `capacity` defines max. number of items the instance will be able to store.

func (*CircularList[T]) Append added in v0.3.3

func (clist *CircularList[T]) Append(v T)

Append adds a new item to the end of the list. In case the free capacity is depleted, then the oldest item is replaced by this new one.

func (*CircularList[T]) ForEach added in v0.3.3

func (clist *CircularList[T]) ForEach(fn func(i int, item T) bool)

ForEach runs a function fn for all the items starting from the oldest one. The iteration continues until fn returns true.

func (*CircularList[T]) Get added in v0.3.3

func (clist *CircularList[T]) Get(idx int) T

Get returns an item with a specific index. Please note that for CircularList, this method is not very usable as the index does not represent anything specific for the outside world.

func (*CircularList[T]) Head added in v0.3.3

func (clist *CircularList[T]) Head() T

Head returns the oldest item of the list. In case the list is empty, panic() is caused.

func (*CircularList[T]) Last added in v0.3.4

func (clist *CircularList[T]) Last() T

Last returns the most recent item of the list. In case the list is empty, panic() is caused.

func (*CircularList[T]) Len added in v0.3.3

func (clist *CircularList[T]) Len() int

Len returns size of the list

func (*CircularList[T]) Prepend added in v0.3.8

func (clist *CircularList[T]) Prepend(v T)

func (*CircularList[T]) ShiftUntil added in v0.3.3

func (clist *CircularList[T]) ShiftUntil(fn func(item T) bool)

ShiftUntil removes old items starting from the oldest one and moving towards newer ones until 'fn' returns true. In case there are no more items to remove, the function will handle this gracefully without errors. This can be used to e.g. clean old log records.

type Comparable added in v0.5.0

type Comparable interface {
	// Compare should return:
	// *  x > 0 if this item is greater than the `other`,
	// *  x == 0 if items are equal
	// *  x < 0 if this item is lesser than the `other`
	Compare(other Comparable) int
}

type ConcurrentMap added in v0.1.3

type ConcurrentMap[K comparable, T any] struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewConcurrentMap added in v0.1.3

func NewConcurrentMap[K comparable, T any]() *ConcurrentMap[K, T]

func NewConcurrentMapFrom added in v0.1.3

func NewConcurrentMapFrom[K comparable, T any](data map[K]T) *ConcurrentMap[K, T]

func NewConcurrentMapFromJSON added in v0.2.7

func NewConcurrentMapFromJSON[K comparable, T any](data []byte) (*ConcurrentMap[K, T], error)

func (*ConcurrentMap[K, T]) AsMap added in v0.1.6

func (cm *ConcurrentMap[K, T]) AsMap() map[K]T

AsMap creates a shallow copy of a map wrapped by this ConcurrentMap

func (*ConcurrentMap[K, T]) Delete added in v0.1.7

func (cm *ConcurrentMap[K, T]) Delete(k K)

func (*ConcurrentMap[K, T]) Filter added in v0.5.8

func (cm *ConcurrentMap[K, T]) Filter(fn func(k K, v T) bool) *ConcurrentMap[K, T]

func (*ConcurrentMap[K, T]) ForEach added in v0.1.3

func (cm *ConcurrentMap[K, T]) ForEach(fn func(k K, v T))

func (*ConcurrentMap[K, T]) Get added in v0.1.3

func (cm *ConcurrentMap[K, T]) Get(k K) T

func (*ConcurrentMap[K, T]) GetWithTest added in v0.1.3

func (cm *ConcurrentMap[K, T]) GetWithTest(k K) (T, bool)

func (*ConcurrentMap[K, T]) HasKey added in v0.1.3

func (cm *ConcurrentMap[K, T]) HasKey(k K) bool

func (*ConcurrentMap[K, T]) Keys added in v0.1.3

func (cm *ConcurrentMap[K, T]) Keys() []K

func (*ConcurrentMap[K, T]) Len added in v0.1.7

func (cm *ConcurrentMap[K, T]) Len() int

Len returns number of key-value pairs stored in the map

func (*ConcurrentMap[K, T]) MarshalJSON added in v0.2.7

func (cm *ConcurrentMap[K, T]) MarshalJSON() ([]byte, error)

func (*ConcurrentMap[K, T]) Set added in v0.1.3

func (cm *ConcurrentMap[K, T]) Set(k K, v T)

func (*ConcurrentMap[K, T]) Update added in v0.1.3

func (cm *ConcurrentMap[K, T]) Update(fn func(k K, v T) T)

func (*ConcurrentMap[K, T]) Values added in v0.1.3

func (cm *ConcurrentMap[K, T]) Values() []T

type Multidict

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

func NewMultidict

func NewMultidict[T any]() *Multidict[T]

func (*Multidict[T]) Add

func (md *Multidict[T]) Add(k string, v T)

func (*Multidict[T]) ForEach

func (md *Multidict[T]) ForEach(applyFn func(k string, v []T) error) error

func (*Multidict[T]) Get

func (md *Multidict[T]) Get(k string) []T

type Set

type Set[T constraints.Ordered] struct {
	// contains filtered or unexported fields
}

func NewSet

func NewSet[T constraints.Ordered](values ...T) *Set[T]

func (*Set[T]) Add

func (set *Set[T]) Add(value T)

func (*Set[T]) Contains

func (set *Set[T]) Contains(value T) bool

func (*Set[T]) ForEach

func (set *Set[T]) ForEach(fn func(item T))

func (*Set[T]) Intersect added in v0.6.2

func (set *Set[T]) Intersect(other *Set[T]) *Set[T]

func (*Set[T]) Remove

func (set *Set[T]) Remove(value T)

func (*Set[T]) Size added in v0.5.6

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

func (*Set[T]) Sub added in v0.6.2

func (set *Set[T]) Sub(other *Set[T]) *Set[T]

func (*Set[T]) ToOrderedSlice

func (set *Set[T]) ToOrderedSlice() []T

func (*Set[T]) ToSlice

func (set *Set[T]) ToSlice() []T

func (*Set[T]) Union

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

Jump to

Keyboard shortcuts

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