...

Package collections

import "framework/collections"
Overview
Index

Overview ▾

func AppendToLinkedList

func AppendToLinkedList(dest *list.List, src *list.List)

Appends the elements of src to the end of dest, without making any copies.

func ConvertToLinkedList

func ConvertToLinkedList[T any](elements []T) *list.List

func ConvertToSlice

func ConvertToSlice[T any](linkedList *list.List) []T

func StringLessFn

func StringLessFn(itemA string, itemB string) bool

LessFunc implementation for strings.

func Swap

func Swap[T any](slice []T, i int, j int)

Swaps the elements in the positions i and j.

type ConcurrentObjectStore

Provides a thread-safe store for objects that can be accessed by key or index.

This store is designed for batch access. Thus, its methods obtain a suitable lock and then supply a reader or a writer object, which can be used to make multiple reads or writes while keeping the lock in-between.

Note that access through indices is only possible to allow random access and round-robin access to the stored objects. There is no particular guaranteed order to the objects retrieved by index. The only index-related property that is guaranteed is that the order stays the same as long as no object is added to or removed from the store.

type ConcurrentObjectStore[V any] interface {

    // Obtains a read lock on the store and returns a reader object.
    ReadLock() ConcurrentObjectStoreReader[V]

    // Obtains a write lock on the store and returns a writer object.
    WriteLock() ConcurrentObjectStoreWriter[V]
}

type ConcurrentObjectStoreImpl

Implementation of ConcurrentObjectStore.

type ConcurrentObjectStoreImpl[V any] struct {
    // contains filtered or unexported fields
}

func NewConcurrentObjectStoreImpl

func NewConcurrentObjectStoreImpl[V any]() *ConcurrentObjectStoreImpl[V]

func (*ConcurrentObjectStoreImpl[V]) ReadLock

func (cos *ConcurrentObjectStoreImpl[V]) ReadLock() ConcurrentObjectStoreReader[V]

func (*ConcurrentObjectStoreImpl[V]) WriteLock

func (cos *ConcurrentObjectStoreImpl[V]) WriteLock() ConcurrentObjectStoreWriter[V]

type ConcurrentObjectStoreReader

Provides batch access to a ConcurrentObjectStore locked for reading.

type ConcurrentObjectStoreReader[V any] interface {
    // Gets the object with the specified key.
    GetByKey(key string) (V, bool)

    // Gets the key and the object stored at the specified index.
    //
    // If the index is out of bounds, the last return value is false.
    GetByIndex(index int) (string, V, bool)

    // Returns the number of objects stored.
    Len() int

    // Releases the lock. The store must not be accessed anymore after calling this method.
    Unlock()
}

type ConcurrentObjectStoreWriter

Provides batch access to a ConcurrentObjectStore locked for writing.

type ConcurrentObjectStoreWriter[V any] interface {
    ConcurrentObjectStoreReader[V]

    // Stores or updates the object with the specified key.
    Set(key string, value V)

    // Removes the object with the specified key.
    //
    // Returns the object that was stored or false, if the key did not exist.
    Remove(key string) (V, bool)
}

type EntityLock

Represents ownership of a lock on an entity obtained from an EntityLocker.

type EntityLock interface {
    // The name of the entity that is covered by this lock.
    Name() string

    // Unlocks the entity.
    Unlock()
}

type EntityLocker

Allows obtaining and waiting for locks on entities that do not support locking themselves.

The entities are identified using their names and the actual objects are not needed for this locker. This is useful, e.g., when a node in a cluster needs to be locked - the node can be locked by its name before fetching the actual object.

type EntityLocker interface {
    // Obtains a lock for the entity with the specified name.
    // If the entity is already locked, the goroutine will block until the lock becomes available.
    Lock(name string) EntityLock
}

type EntityLockerImpl

Default implementation of the EntityLocker.

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

func NewEntityLockerImpl

func NewEntityLockerImpl() *EntityLockerImpl

func (*EntityLockerImpl) Lock

func (locker *EntityLockerImpl) Lock(name string) EntityLock

type HeapMap

Defines the interface for a Heap data structure that establishes the heap property based on the item values, but allows accessing items also through keys, which are not related to the heap property. This allows, e.g., updating items.

type HeapMap[K ~int | ~string, V any] interface {

    // Adds the specified item to the heap or updates an existing item, if the key is already present.
    AddOrReplace(key K, item V)

    // Returns the top-most item from the heap and removes it.
    // If there is no item in the heap, the second return value is false.
    Pop() (K, V, bool)

    // Returns the top-most item from the heap and removes it.
    // If there is no item in the heap, the second return value is false.
    Peek() (K, V, bool)

    // Returns the item with the specified key.
    GetByKey(key K) (V, bool)

    // Removes the item with the specified key from the heap and returns it.
    // If there is no item in the heap with that key, the second return value is false.
    RemoveByKey(key K) (V, bool)

    // Gets the number of items currently in the heap.
    Len() int
}

type KeyFunc

Returns the key for the specified value (can be used for maps or similar data structures).

type KeyFunc[K ~int | ~string, V any] func(value V) (K, error)

type LessFunc

Defines a comparison function that returns true iff itemA is less than itemB, otherwise false.

type LessFunc[T any] func(itemA T, itemB T) bool

type MinHeapMap

Implements a HeapMap with the "smallest" item being on top of the heap.

type MinHeapMap[K ~int | ~string, V any] struct {
    // contains filtered or unexported fields
}

func NewMinHeapMap

func NewMinHeapMap[K ~int | ~string, V any](lessFn LessFunc[V], nilValue V) *MinHeapMap[K, V]

Creates a new MinHeap with the specified function LessFunc for comparing items to establish the min heap property.

The nilValue is required, because even though `any` is defined as interface{}, Go allows also non-pointer types types to be used for such generic parameters.

func (*MinHeapMap[K, V]) AddOrReplace

func (mh *MinHeapMap[K, V]) AddOrReplace(key K, item V)

func (*MinHeapMap[K, V]) GetByKey

func (mh *MinHeapMap[K, V]) GetByKey(key K) (V, bool)

func (*MinHeapMap[K, V]) Len

func (mh *MinHeapMap[K, V]) Len() int

func (*MinHeapMap[K, V]) Peek

func (mh *MinHeapMap[K, V]) Peek() (K, V, bool)

func (*MinHeapMap[K, V]) Pop

func (mh *MinHeapMap[K, V]) Pop() (K, V, bool)

func (*MinHeapMap[K, V]) RemoveByKey

func (mh *MinHeapMap[K, V]) RemoveByKey(key K) (V, bool)