core

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Maximum

func Maximum[T constraints.Ordered](lhs, rhs T) bool

Maximum returns whether `rhs` is greater than `lhs`.

Use it as a comparison heuristic during a PriorityQueue's instantiation.

func Minimum

func Minimum[T constraints.Ordered](lhs, rhs T) bool

Minimum returns whether `rhs` is less than `lhs`.

Use it as a comparison heuristic during a PriorityQueue's instantiation.

func NewAsynchronousTemporalQueueItem

func NewAsynchronousTemporalQueueItem() *asynchronousTemporalQueueItem

Types

type AsynchronousTemporalQueue

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

func NewAsynchronousTemporalQueue

func NewAsynchronousTemporalQueue() *AsynchronousTemporalQueue

NewAsynchronousTemporalQueue 创建一个新的异步时间队列实例。

返回值 *AsynchronousTemporalQueue: 返回一个初始化好的异步时间队列指针。

这个函数不接受任何参数。

func (*AsynchronousTemporalQueue) CloseChannel

func (q *AsynchronousTemporalQueue) CloseChannel(key string)

(q *AsynchronousTemporalQueue) CloseChannel 关闭异步时间队列(q)中与给定键(key)关联的通道。

参数 key string: 要关闭的通道的字符串键。

函数首先从队列的channelMap中加载与键key对应的值(通道项)。若该键存在且加载成功(ok为true),执行以下操作:

  1. 将通道项的_close标志设置为true,表示该通道应被关闭。
  2. 启动一个新的goroutine,用于等待当前正在处理的所有任务完成,并最终删除已关闭的通道。此goroutine执行如下逻辑: a. 无限循环,直到满足退出条件。 b. 使用_item._wg等待所有正在执行的任务完成。 c. 检查通道项的queue是否为空。若为空,表示所有任务已完成,此时从队列的channelMap中删除键key,并退出goroutine。

func (*AsynchronousTemporalQueue) CloseSample added in v0.0.2

func (q *AsynchronousTemporalQueue) CloseSample()

func (*AsynchronousTemporalQueue) CreateChannel

func (q *AsynchronousTemporalQueue) CreateChannel(key string)

(q *AsynchronousTemporalQueue) CreateChannel 根据给定的键(key)在异步时间队列(q)中创建一个新的通道。

参数 key string: 用于唯一标识新通道的字符串键。

函数首先检查队列中是否已存在与给定键关联的通道。如果不存在(即ok为false),则创建一个新的AsynchronousTemporalQueueItem,并将其存储到队列的channelMap中,以键key作为索引。

func (*AsynchronousTemporalQueue) Empty added in v0.0.2

func (q *AsynchronousTemporalQueue) Empty() bool

func (*AsynchronousTemporalQueue) Head

func (q *AsynchronousTemporalQueue) Head() (values map[string]any, NTP int64, ok bool)

func (*AsynchronousTemporalQueue) Pop

func (q *AsynchronousTemporalQueue) Pop() (values map[string]any, NTP int64, ok bool)

func (*AsynchronousTemporalQueue) Push

func (q *AsynchronousTemporalQueue) Push(key string, value any, NTP int64)

(q *AsynchronousTemporalQueue) Push 向异步时间队列(q)中与给定键(key)关联的通道添加一个带有NTP时间戳的新任务(value)。

参数:

key string: 目标通道的字符串键。
value any: 要添加到通道的任务数据。
NTP int64: 任务关联的NTP时间戳(单位:纳秒)。

函数首先从队列的channelMap中加载与键key对应的值(通道项)。若该键存在且加载成功(ok为true),执行以下操作: 1. 检查通道项的_close标志,确保通道未被关闭。若通道未关闭,继续执行。 2. 增加通道项的_wg计数器,表示开始一个新任务。 3. 将任务数据(value)及其NTP时间戳(NTP)推入通道项的queue中。 4. 减少通道项的_wg计数器,表示新任务添加完毕。

注意:若给定键对应的通道已关闭,此函数将不会向其添加任务。

func (*AsynchronousTemporalQueue) StartSample added in v0.0.2

func (q *AsynchronousTemporalQueue) StartSample(sampleRate int, sampleWeights sync.Map)

type PriorityQueue

type PriorityQueue[T any, P constraints.Ordered] struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

PriorityQueue is a heap-based priority-queue data structure implementation.

It can either be min (ascending) or max (descending) oriented/ordered. Its type parameters `T` and `P`, respectively specify the underlying value type and the underlying priority type.

Every operation on PriorityQueues are goroutine-safe.

func NewMaxPriorityQueue

func NewMaxPriorityQueue[T any, P constraints.Ordered]() *PriorityQueue[T, P]

NewMaxPriorityQueue instantiates a new maximum oriented PriorityQueue.

func NewMinPriorityQueue

func NewMinPriorityQueue[T any, P constraints.Ordered]() *PriorityQueue[T, P]

NewMinPriorityQueue instantiates a new minimum oriented PriorityQueue.

func NewPriorityQueue

func NewPriorityQueue[T any, P constraints.Ordered](heuristic func(lhs, rhs P) bool) *PriorityQueue[T, P]

NewPriorityQueue instantiates a new PriorityQueue with the provided comparison heuristic. The package defines the `Max` and `Min` heuristic to define a max-oriented or min-oriented heuristics, respectively.

func (*PriorityQueue[T, P]) Empty

func (pq *PriorityQueue[T, P]) Empty() bool

Empty returns whether the PriorityQueue is empty.

func (*PriorityQueue[T, P]) Head

func (pq *PriorityQueue[T, P]) Head() (value T, priority P, ok bool)

Head returns the highest or lowest priority item (depending on the comparison heuristic of your PriorityQueue) from the PriorityQueue in *O(1)* complexity.

func (*PriorityQueue[T, P]) Pop

func (pq *PriorityQueue[T, P]) Pop() (value T, priority P, ok bool)

Pop and return the highest or lowest priority item (depending on the comparison heuristic of your PriorityQueue) from the PriorityQueue in at most *O(log n)* complexity.

func (*PriorityQueue[T, P]) Push

func (pq *PriorityQueue[T, P]) Push(value T, priority P)

Push inserts the value in the PriorityQueue with the provided priority in at most *O(log n)* time complexity.

func (*PriorityQueue[T, P]) Size

func (pq *PriorityQueue[T, P]) Size() uint

Size returns the number of elements present in the PriorityQueue.

Jump to

Keyboard shortcuts

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