utilds

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: May 2, 2025 License: Apache-2.0 Imports: 3 Imported by: 3

Documentation

Index

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

func MakeCirBuf[T any](maxSize int) *CirBuf[T]

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

func (cb *CirBuf[T]) FilterItems(filter func(item T, index int) bool) []T

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

func (cb *CirBuf[T]) GetAll() ([]T, int)

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

func (cb *CirBuf[T]) GetLast() (T, int, bool)

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]) GetRange

func (cb *CirBuf[T]) GetRange(start int, end int) ([]T, int, bool)

returns items, true-offset, eof

func (*CirBuf[T]) GetTotalCountAndHeadOffset

func (cb *CirBuf[T]) GetTotalCountAndHeadOffset() (int, int)

func (*CirBuf[T]) IsEmpty

func (cb *CirBuf[T]) IsEmpty() bool

IsEmpty returns true if the buffer is empty.

func (*CirBuf[T]) IsFull

func (cb *CirBuf[T]) IsFull() bool

IsFull returns true if the buffer is full.

func (*CirBuf[T]) Read

func (cb *CirBuf[T]) Read() (T, bool)

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.

func (*CirBuf[T]) Size

func (cb *CirBuf[T]) Size() int

Size returns the current number of elements in the buffer.

func (*CirBuf[T]) Write

func (cb *CirBuf[T]) Write(element T) *T

Write adds an element to the circular buffer. If the buffer is full, the oldest element will be overwritten. Returns a pointer to the element that was kicked out, or nil if no element was kicked out.

type ExpMap added in v0.1.2

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

func MakeExpMap added in v0.1.2

func MakeExpMap[T any]() *ExpMap[T]

func (*ExpMap[T]) Get added in v0.1.2

func (em *ExpMap[T]) Get(key string) (T, bool)

func (*ExpMap[T]) Set added in v0.1.2

func (em *ExpMap[T]) Set(key string, value T, exp time.Time)

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]) Delete

func (sm *SyncMap[K, T]) Delete(key K)

func (*SyncMap[K, T]) Get

func (sm *SyncMap[K, T]) Get(key K) T

func (*SyncMap[K, T]) GetEx

func (sm *SyncMap[K, T]) GetEx(key K) (T, bool)

func (*SyncMap[K, T]) GetOrCreate

func (sm *SyncMap[K, T]) GetOrCreate(key K, createFn func() T) (T, bool)

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

func (*SyncMap[K, T]) Len

func (sm *SyncMap[K, T]) Len() int

Len returns the number of items in the map

func (*SyncMap[K, T]) Set

func (sm *SyncMap[K, T]) Set(key K, value T)

Jump to

Keyboard shortcuts

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