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 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 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