Documentation
¶
Index ¶
- type CirBuf
- func (cb *CirBuf[T]) FilterItems(filter func(item T, index int) bool) []T
- func (cb *CirBuf[T]) GetAll() ([]T, int)
- func (cb *CirBuf[T]) GetLast() (T, int, bool)
- func (cb *CirBuf[T]) GetRange(start int, end int) ([]T, int, bool)
- func (cb *CirBuf[T]) GetTotalCountAndHeadOffset() (int, int)
- func (cb *CirBuf[T]) IsEmpty() bool
- func (cb *CirBuf[T]) IsFull() bool
- func (cb *CirBuf[T]) Read() (T, bool)
- func (cb *CirBuf[T]) Size() int
- func (cb *CirBuf[T]) Write(element T) *T
- type SetOnceConfig
- type SyncMap
- func (sm *SyncMap[K, T]) Delete(key K)
- func (sm *SyncMap[K, T]) Get(key K) T
- func (sm *SyncMap[K, T]) GetEx(key K) (T, bool)
- func (sm *SyncMap[K, T]) GetOrCreate(key K, createFn func() T) (T, bool)
- func (sm *SyncMap[K, T]) Keys() []K
- func (sm *SyncMap[K, T]) Len() int
- func (sm *SyncMap[K, T]) Set(key K, value T)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CirBuf ¶
type CirBuf[T any] struct { Lock *sync.Mutex MaxSize int TotalCount int HeadOffset int Buf []T Head int Tail int }
CirBuf is a generic circular buffer implementation that is thread-safe. It dynamically grows until it reaches MaxSize and can reclaim memory when emptied.
func MakeCirBuf ¶
MakeCirBuf creates a new circular buffer with the specified maximum size. The buffer is initially empty and will grow dynamically as elements are added.
func (*CirBuf[T]) FilterItems ¶
FilterItems returns a slice of items for which the provided filter function returns true. The filter function takes the item and its absolute index (TotalCount-based) and returns a boolean. This is useful for filtering items based on custom criteria, such as timestamp.
func (*CirBuf[T]) GetAll ¶
GetAll returns a slice containing all elements in the buffer in order from oldest to newest. This does not remove elements from the buffer. It also returns the HeadOffset, which is the offset of the first element in the buffer.
func (*CirBuf[T]) GetLast ¶
GetLast returns the last element in the buffer, its offset, and a boolean indicating whether the buffer has any elements. If the buffer is empty, the zero value of T, 0, and false are returned.
func (*CirBuf[T]) GetTotalCountAndHeadOffset ¶
func (*CirBuf[T]) Read ¶
Read removes and returns the oldest element from the circular buffer. If the buffer is empty, the zero value of T and false are returned.
type SetOnceConfig ¶ added in v0.8.1
type SetOnceConfig[T any] struct { // contains filtered or unexported fields }
SetOnceConfig provides a thread-safe way to set a configuration value exactly once with fallback to a default configuration if nil is provided
func NewSetOnceConfig ¶ added in v0.8.1
func NewSetOnceConfig[T any](defaultCfg T) *SetOnceConfig[T]
NewSetOnceConfig creates a new SetOnceConfig with the provided default configuration The default configuration is immediately stored and available via Get()
func (*SetOnceConfig[T]) Get ¶ added in v0.8.1
func (soc *SetOnceConfig[T]) Get() T
Get returns the current configuration value Always safe to call as default is set during NewSetOnceConfig
func (*SetOnceConfig[T]) SetOnce ¶ added in v0.8.1
func (soc *SetOnceConfig[T]) SetOnce(cfg *T) bool
SetOnce attempts to set the configuration value exactly once, overriding the default If cfg is nil, keeps the default configuration Returns true if the configuration was set, false if it was already set
type SyncMap ¶
type SyncMap[K comparable, T any] struct { // contains filtered or unexported fields }
func MakeSyncMap ¶
func MakeSyncMap[K comparable, T any]() *SyncMap[K, T]
func (*SyncMap[K, T]) GetOrCreate ¶
GetOrCreate gets a value by key. If the key doesn't exist, it calls the provided function to create a new value, sets it in the map, and returns it. Returns the value and a boolean indicating if the key was found (true) or created (false).
func (*SyncMap[K, T]) Keys ¶
func (sm *SyncMap[K, T]) Keys() []K
Keys returns a slice of all keys in the map