utils

package
v0.0.0-...-1513131 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	STOCK_DATE_FORMAT_LAYOUT = "2006-01-02"
	RFC3339_TIME_LAYOUT      = "2006-01-02T15:04:05Z07:00" // RFC3339 format
)

Variables

This section is empty.

Functions

func All

func All[T any](slice []T, predicate func(T) bool) bool

All function checks if all elements in the slice satisfy the predicate.

func Any

func Any[T any](slice []T, predicate func(T) bool) bool

Any function checks if any element in the slice satisfies the predicate.

func B2F

func B2F(b bool) float64

B2F converts a boolean to a float

func B2I

func B2I(b bool) int

B2I converts a boolean to an integer

func CopySlice

func CopySlice[T any](src []T) []T

CopySlice creates a copy of the given slice of any type.

func ExtractStringValue

func ExtractStringValue(pattern, input string) (string, error)

Utility function to extract a string value using a regex pattern

func IsLineIntersect

func IsLineIntersect(line1Points, line2Points []float64) bool

func IsLineIntersectCyclic

func IsLineIntersectCyclic(line1Points, line2Points *CyclicSlice[float64]) bool

func MapKeys

func MapKeys[K comparable, V any](m map[K]V) []K

MapKeys extracts the keys of a map into a slice.

func MapValues

func MapValues[K comparable, V any](m map[K]V) []V

MapValues extracts the values of a map into a slice.

func Max

func Max[T Ordered](a, b T) T

Min returns the minimum of two values of an ordered type.

func Min

func Min[T Ordered](a, b T) T

Min returns the minimum of two values of an ordered type.

func ParseList

func ParseList[T any](input, pattern, delimiter string) ([]T, error)

Utility function to extract a list of values from a delimited string

func ParseValue

func ParseValue[T any](input, pattern string) (T, error)

Utility function to extract a value using a regex pattern and convert it to type T

Types

type BackwardIterator

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

BackwardIterator represents an iterator for traversing the list in reverse order.

func (*BackwardIterator[T]) HasPrev

func (it *BackwardIterator[T]) HasPrev() bool

HasPrev checks if there is a previous node available in the list.

func (*BackwardIterator[T]) Prev

func (it *BackwardIterator[T]) Prev() *DoublyLinkedNode[T]

Prev moves the iterator to the previous node and returns the current node.

func (*BackwardIterator[T]) Remove

func (it *BackwardIterator[T]) Remove()

Remove removes the current node from the list during backward iteration.

type Batcher

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

Batcher is a struct that batches data using a cyclic slice and returns batches in real-time.

func NewBatcher

func NewBatcher[T any](batchSize int) *Batcher[T]

NewBatcherWithCyclicSlice creates a new Batcher with a specified batch size and cyclic buffer size.

func (*Batcher[T]) AddAndGet

func (b *Batcher[T]) AddAndGet(item T) []T

Add adds a new item to the batcher and returns a batch if the batch size is met.

func (*Batcher[T]) GetLast

func (b *Batcher[T]) GetLast() []T

func (*Batcher[T]) ProcessorViaChannel

func (b *Batcher[T]) ProcessorViaChannel(items gr_variable.ReadOnlyGrChannel[T], processFunc func(batch []T))

type Color

type Color bool

Color is a type representing the color of a node in the Red-Black Tree.

const (
	Red   Color = true
	Black Color = false
)

type Comparator

type Comparator[T any] func(a, b T) bool

Comparator is a function type that compares two values of type T and returns true if the first is less than the second.

type CyclicSlice

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

func NewCyclicSlice

func NewCyclicSlice[T any](size int) *CyclicSlice[T]

func (*CyclicSlice[T]) Add

func (cs *CyclicSlice[T]) Add(value T)

func (*CyclicSlice[T]) GetAt

func (cs *CyclicSlice[T]) GetAt(index int) T

func (*CyclicSlice[T]) GetCurrentSize

func (cs *CyclicSlice[T]) GetCurrentSize() int

func (*CyclicSlice[T]) GetData

func (cs *CyclicSlice[T]) GetData() []T

func (*CyclicSlice[T]) GetLast

func (cs *CyclicSlice[T]) GetLast() T

func (*CyclicSlice[T]) GetPrevious

func (cs *CyclicSlice[T]) GetPrevious() T

func (*CyclicSlice[T]) IsFull

func (cs *CyclicSlice[T]) IsFull() bool

func (*CyclicSlice[T]) Reset

func (cs *CyclicSlice[T]) Reset()

func (*CyclicSlice[T]) SetAt

func (cs *CyclicSlice[T]) SetAt(index int, value T)

type DayCacheManager

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

DayCacheManager is a generic structure to manage caching for an indicator adaptor

func NewDayCacheManager

func NewDayCacheManager[T any](dayInterval, hourInterval int64, defaultValue T, dayIntervalPool *ObjectPool[TimeIntervalCache[T]], dayPool *ObjectPool[TimeIntervalCache[*TimeIntervalCache[T]]]) *DayCacheManager[T]

NewDayCacheManager creates a new DayCacheManager instance

func (*DayCacheManager[T]) CleanUp

func (dcm *DayCacheManager[T]) CleanUp()

CleanUp cleans up all the caches

func (*DayCacheManager[T]) Get

func (dcm *DayCacheManager[T]) Get(timestamp int64) T

Get retrieves a value from the cache based on the timestamp

func (*DayCacheManager[T]) Set

func (dcm *DayCacheManager[T]) Set(timestamp int64, value T)

Set adds a value to the appropriate time cache within the day

type DaySingleBigCacheManager

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

DaySingleBigCacheManager is a generic structure to manage caching for an indicator adaptor

func NewDaySingleBigCacheManager

func NewDaySingleBigCacheManager[T any](hourInterval int64, defaultValue T, dayIntervalPool *ObjectPool[TimeIntervalCache[T]]) *DaySingleBigCacheManager[T]

NewDaySingleBigCacheManager creates a new DaySingleBigCacheManager instance

func (*DaySingleBigCacheManager[T]) CleanUp

func (dcm *DaySingleBigCacheManager[T]) CleanUp()

CleanUp cleans up all the caches

func (*DaySingleBigCacheManager[T]) Get

func (dcm *DaySingleBigCacheManager[T]) Get(timestamp int64) T

Get retrieves a value from the cache based on the timestamp

func (*DaySingleBigCacheManager[T]) Set

func (dcm *DaySingleBigCacheManager[T]) Set(timestamp int64, value T)

Set adds a value to the appropriate time cache within the day

type DoublyLinkedList

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

DoublyLinkedList represents the doubly linked list structure.

func NewDoublyLinkedListWithPool

func NewDoublyLinkedListWithPool[T any](dllnPool *ObjectPool[DoublyLinkedNode[T]], dllPool *ObjectPool[DoublyLinkedList[T]]) *DoublyLinkedList[T]

NewDoublyLinkedListWithPool creates a new DoublyLinkedList with a specific dllnPool.

func (*DoublyLinkedList[T]) Add

func (ll *DoublyLinkedList[T]) Add(value T) *DoublyLinkedNode[T]

Add appends a new element to the end of the list.

func (*DoublyLinkedList[T]) AddSorted

func (ll *DoublyLinkedList[T]) AddSorted(value T, less func(a, b T) bool) *DoublyLinkedNode[T]

AddSorted inserts a value into the list while maintaining sorted order. The 'less' function defines the comparison logic.

func (*DoublyLinkedList[T]) Cleanup

func (ll *DoublyLinkedList[T]) Cleanup(valueCleanup func(T))

Cleanup releases all nodes in the list back to the dllnPool.

func (*DoublyLinkedList[T]) Merge

func (ll *DoublyLinkedList[T]) Merge(other *DoublyLinkedList[T])

Merge appends another doubly linked list into the current list.

func (*DoublyLinkedList[T]) MergeSort

func (ll *DoublyLinkedList[T]) MergeSort(comparator func(a, b T) bool)

MergeSort sorts the doubly linked list using the Merge MergeSort algorithm and the provided comparator function.

func (*DoublyLinkedList[T]) NewBackwardIterator

func (ll *DoublyLinkedList[T]) NewBackwardIterator() *BackwardIterator[T]

NewBackwardIterator creates a new backward iterator for the linked list, starting from the tail.

func (*DoublyLinkedList[T]) NewIterator

func (ll *DoublyLinkedList[T]) NewIterator() *Iterator[T]

NewIterator creates a new iterator for the linked list.

func (*DoublyLinkedList[T]) Remove

func (ll *DoublyLinkedList[T]) Remove(node *DoublyLinkedNode[T])

Remove removes the given node from the list in O(1) time and returns it to the dllnPool.

func (*DoublyLinkedList[T]) Reset

func (ll *DoublyLinkedList[T]) Reset()

func (*DoublyLinkedList[T]) ResetWithCleanup

func (ll *DoublyLinkedList[T]) ResetWithCleanup()

func (*DoublyLinkedList[T]) Size

func (ll *DoublyLinkedList[T]) Size() int

Size returns the number of elements in the list.

func (*DoublyLinkedList[T]) Tail

func (ll *DoublyLinkedList[T]) Tail() *DoublyLinkedNode[T]

Tail returns the number of elements in the list.

type DoublyLinkedNode

type DoublyLinkedNode[T any] struct {
	Value T
	Next  *DoublyLinkedNode[T]
	Prev  *DoublyLinkedNode[T]
}

DoublyLinkedNode represents a node in the doubly linked list.

type Item

type Item[T any] struct {
	Value T
}

Item represents a single item in the heap

type Iterator

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

Iterator represents an iterator for the doubly linked list.

func (*Iterator[T]) HasNext

func (it *Iterator[T]) HasNext() bool

HasNext checks if there is a next node available in the list.

func (*Iterator[T]) Next

func (it *Iterator[T]) Next() *DoublyLinkedNode[T]

Next moves the iterator to the next node and returns the current node.

func (*Iterator[T]) Remove

func (it *Iterator[T]) Remove()

Remove removes the current node from the list during iteration.

type ObjectPool

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

ObjectPool is a wrapper around sync.Pool that provides type-safe pooling for any type T.

func NewObjectPool

func NewObjectPool[T any]() *ObjectPool[T]

NewObjectPool creates a new ObjectPool for a given type T.

func (*ObjectPool[T]) Get

func (p *ObjectPool[T]) Get() *T

Get retrieves a node from the pool, or creates a new one if none are available.

func (*ObjectPool[T]) Put

func (p *ObjectPool[T]) Put(node *T)

Put returns a node to the pool.

type Ordered

type Ordered interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | ~float32 | ~float64
}

type PriorityQueueComparator

type PriorityQueueComparator[T any] func(a, b T) bool

PriorityQueueComparator function type, returns true if a should come before b.

type PriorityQueueInterface

type PriorityQueueInterface[T any] interface {
	Insert(value T)
	Remove() T
}

PriorityQueueInterface defines the public methods for the priority queue

func NewPriorityQueue

func NewPriorityQueue[T any](comparator PriorityQueueComparator[T]) PriorityQueueInterface[T]

NewPriorityQueue creates a new PriorityQueue with the given comparator.

type RBNode

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

RBNode represents a node in the Red-Black Tree.

func (*RBNode[T]) CleanUp

func (node *RBNode[T]) CleanUp(cleanUpValue func(*RBNode[T]))

func (*RBNode[T]) GetValue

func (node *RBNode[T]) GetValue() T

func (*RBNode[T]) InOrder

func (node *RBNode[T]) InOrder(f func(T))

func (*RBNode[T]) PostOrder

func (node *RBNode[T]) PostOrder(f func(T))

type RBTree

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

RBTree represents a Red-Black Tree with a pool for nodes and trees.

func NewRBTree

func NewRBTree[T any](comparator Comparator[T], nodePool *ObjectPool[RBNode[T]], treePool *ObjectPool[RBTree[T]]) *RBTree[T]

NewRBTree creates a new instance of a Red-Black Tree with a custom comparator and node and tree pools.

func (*RBTree[T]) CleanUp

func (tree *RBTree[T]) CleanUp(f func(T))

CleanUpTree resets the tree's properties and returns it to the tree pool.

func (*RBTree[T]) InOrder

func (tree *RBTree[T]) InOrder(f func(T))

InOrder traverses the tree in-order and applies the provided function to each node.

func (*RBTree[T]) Insert

func (tree *RBTree[T]) Insert(value T)

Insert inserts a value into the Red-Black Tree.

func (*RBTree[T]) PrintInOrder

func (tree *RBTree[T]) PrintInOrder()

PrintInOrder is a utility function to print the tree in-order (for debugging).

func (*RBTree[T]) Search

func (tree *RBTree[T]) Search(value T) bool

Search looks for a node with the given value in the RBTree and returns true if found.

func (*RBTree[T]) SearchNode

func (tree *RBTree[T]) SearchNode(value T) *RBNode[T]

type Semaphore

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

Semaphore is a struct that encapsulates a semaphore pattern for concurrency control.

func NewSemaphore

func NewSemaphore(maxGoroutines int) *Semaphore

NewSemaphore creates a new Semaphore with a specified maximum number of concurrent goroutines.

func (*Semaphore) Acquire

func (s *Semaphore) Acquire()

Acquire acquires a semaphore slot. Blocks if no slots are available.

func (*Semaphore) ProcessAndRelease

func (s *Semaphore) ProcessAndRelease(fn func())

ProcessAndRelease processes a given function with arguments and releases the semaphore.

func (*Semaphore) ProcessAndReleaseReflect

func (s *Semaphore) ProcessAndReleaseReflect(fn interface{}, args ...interface{})

ProcessAndReleaseReflect processes a given function with arguments and releases the semaphore.

func (*Semaphore) Release

func (s *Semaphore) Release()

Release releases a semaphore slot.

func (*Semaphore) Wait

func (s *Semaphore) Wait()

Wait waits for all goroutines to complete.

type Task

type Task func()

Task is a type that represents a function to be executed by a worker.

type TimeIntervalCache

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

Define a generic TimeIntervalCache structure with childInterval included

func NewTimeIntervalCache

func NewTimeIntervalCache[T any](s int64, interval int64, defaultValue T, pool *ObjectPool[TimeIntervalCache[T]], cleanupFunc func(T)) *TimeIntervalCache[T]

NewTimeIntervalCache is a generic function to create a new TimeIntervalCache instance with childInterval and cleanup function

func (*TimeIntervalCache[T]) CleanUp

func (dc *TimeIntervalCache[T]) CleanUp()

CleanUp returns the TimeIntervalCache to the pool, invoking the cleanup function for each value

func (*TimeIntervalCache[T]) Get

func (dc *TimeIntervalCache[T]) Get(time int64) T

Get retrieves a value from the TimeIntervalCache based on the time

func (*TimeIntervalCache[T]) GetIfExist

func (dc *TimeIntervalCache[T]) GetIfExist(time int64) T

Get retrieves a value from the TimeIntervalCache based on the time

func (*TimeIntervalCache[T]) IsOutOfIndex

func (dc *TimeIntervalCache[T]) IsOutOfIndex(time int64) bool

IsOutOfIndex checks if the given time is outside the index bounds of the cache

func (*TimeIntervalCache[T]) Set

func (dc *TimeIntervalCache[T]) Set(time int64, s T)

AddInCache adds a value to the TimeIntervalCache

type TimeUtil

type TimeUtil struct {
	time.Time
}

func DayTimeFromTimeStamp

func DayTimeFromTimeStamp(t int64) TimeUtil

func NewTimeUtil

func NewTimeUtil(t time.Time) TimeUtil

func NewTimeUtilFromFormat

func NewTimeUtilFromFormat(dateString string) (TimeUtil, error)

func TimeFromTimeStamp

func TimeFromTimeStamp(t int64) TimeUtil

func (TimeUtil) AddDate

func (tu TimeUtil) AddDate(years int, months int, days int) TimeUtil

func (TimeUtil) After

func (tu TimeUtil) After(u TimeUtil) bool

func (TimeUtil) Before

func (tu TimeUtil) Before(u TimeUtil) bool

func (TimeUtil) Equal

func (tu TimeUtil) Equal(u TimeUtil) bool

func (TimeUtil) RFC3339FormatDate

func (tu TimeUtil) RFC3339FormatDate() string

func (TimeUtil) StockFormatDate

func (tu TimeUtil) StockFormatDate() string

func (TimeUtil) String

func (tu TimeUtil) String() string

func (TimeUtil) Unix

func (tu TimeUtil) Unix() int64

type WorkerPool

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

WorkerPool is a struct that manages a pool of workers to execute tasks concurrently.

func NewWorkerPool

func NewWorkerPool(numWorkers int, maxTasks int) *WorkerPool

NewWorkerPool creates a new WorkerPool with a specified number of workers and a maximum number of tasks in the queue.

func (*WorkerPool) AddTask

func (wp *WorkerPool) AddTask(task Task)

AddTask adds a new task to the worker pool for execution.

func (*WorkerPool) Wait

func (wp *WorkerPool) Wait()

Wait blocks until all tasks have been completed and all workers have stopped.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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