generics

package
v0.0.0-...-a76f865 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2025 License: Apache-2.0 Imports: 1 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendIfNotDuplicate

func AppendIfNotDuplicate[Type comparable](list []Type, item Type) []Type

AppendIfNotDuplicate adds item into list if the list does not yet contain the item.

func ContainsItem

func ContainsItem[Type comparable](list []Type, item Type) bool

ContainsItem returns true if the slice contains the given item. This function can be used if slice items are comparable (operator "==" can be used).

func ContainsItemFn

func ContainsItemFn[Type any](list []Type, item Type, equal func(a, b Type) bool) bool

ContainsItemFn returns true if the slice contains the given item. Two slice items are compared using the provided "equal" callback.

func DiffSets

func DiffSets[Type comparable](set1, set2 []Type) (onlyInSet1, onlyInSet2 []Type)

DiffSets returns slice with items that are present in the first slice but not in the second and vice versa. This function can be used if slice items are comparable (operator "==" can be used).

func DiffSetsFn

func DiffSetsFn[Type any](set1, set2 []Type, equal func(a, b Type) bool) (
	onlyInSet1, onlyInSet2 []Type)

DiffSetsFn returns slice with items that are present in the first slice but not in the second and vice versa. Two slice items are compared using the provided "equal" callback.

func EqualLists

func EqualLists[Type comparable](list1, list2 []Type) bool

EqualLists returns true if the two slices representing lists (i.e. order dependent) are equal in size and items they contain. This function can be used if slice items are comparable (operator "==" can be used).

func EqualListsFn

func EqualListsFn[Type any](list1, list2 []Type, equal func(a, b Type) bool) bool

EqualListsFn returns true if the two slices representing lists (i.e. order dependent) are equal in size and items they contain. Two slice items are compared using the provided "equal" callback.

func EqualSets

func EqualSets[Type comparable](set1, set2 []Type) bool

EqualSets returns true if the two slices that represent sets (i.e. order independent) are equal in size and items they contain. This function can be used if slice items are comparable (operator "==" can be used).

func EqualSetsFn

func EqualSetsFn[Type any](set1, set2 []Type, equal func(a, b Type) bool) bool

EqualSetsFn returns true if the two slices that represent sets (i.e. order independent) are equal in size and items they contain. Two slice items are compared using the provided "equal" callback.

func FilterDuplicates

func FilterDuplicates[Type comparable](list []Type) (filtered []Type)

FilterDuplicates return a new slice with duplicate entries removed. This function can be used if slice items are comparable (operator "==" can be used).

func FilterDuplicatesFn

func FilterDuplicatesFn[Type any](list []Type, equal func(a, b Type) bool) (filtered []Type)

FilterDuplicatesFn return a new slice with duplicate entries removed. Two slice items are compared using the provided "equal" callback.

func FilterList

func FilterList[Type any](list []Type, keep func(a Type) bool) (filtered []Type)

FilterList return a new slice with items that pass the "keep" callback.

func MapList

func MapList[Type1, Type2 any](list []Type1, mapFunc func(a Type1) Type2) []Type2

MapList return a new slice with results of applying the given function to every item in the list.

Types

type LockedMap

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

LockedMap is a wrapper on map with locking API is similar to sync.Map difference is that sync.Map is optimized for two common use cases: (1) when the entry for a given key is only ever written once but read many times (2) when multiple goroutines read, write and overwrite entries for disjoint set of keys In these casees use sync.Map, in other cases consider using LockedMap

LockedMap should be created by calling NewLockedMap()

func NewLockedMap

func NewLockedMap[K comparable, V any]() *LockedMap[K, V]

NewLockedMap returns initialized LockedMap struct

func (*LockedMap[K, V]) ApplyOrStore

func (lm *LockedMap[K, V]) ApplyOrStore(key K, applyFn LockedMapApplyFunc[V], defaultVal V) bool

ApplyOrStore is used to perform atomic operations, where the operation is performed by the applyFn function. If the key does not exist in the map then defaultVal will be stored. Otherwise the entry for the key will be updated. returns true if the key existed when the function was called

func (*LockedMap[K, V]) Delete

func (lm *LockedMap[K, V]) Delete(key K)

Delete removes value from map for given key

func (*LockedMap[K, V]) Keys

func (lm *LockedMap[K, V]) Keys() []K

Keys return copy of keys for locked map

func (*LockedMap[K, V]) Load

func (lm *LockedMap[K, V]) Load(key K) (V, bool)

Load returns value for given key; bool shows if map contains variable

func (*LockedMap[K, V]) Range

func (lm *LockedMap[K, V]) Range(callback LockedMapFunc[K, V])

Range iterates over map and applies callback to every element iteration stops if callback returns false. callback function is not allowed to do any changes on map (Store, Delete or Load) since that can deadlock

func (*LockedMap[K, V]) Store

func (lm *LockedMap[K, V]) Store(key K, value V)

Store saves value for given key. Overrites previous value

type LockedMapApplyFunc

type LockedMapApplyFunc[V any] func(V) V

LockedMapApplyFunc is function signature for ApplyOrStore function

type LockedMapFunc

type LockedMapFunc[K comparable, V any] func(K, V) bool

LockedMapFunc is function signature for Range function

Jump to

Keyboard shortcuts

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