core

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2024 License: MIT Imports: 3 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)

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

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

func (*AsynchronousTemporalQueue) CreateChannel

func (q *AsynchronousTemporalQueue) CreateChannel(key string)

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

func (*AsynchronousTemporalQueue) Head

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

函数执行流程如下:

  1. 初始化结果映射(results)、待处理通道键列表(keys)及当前系统时间对应的NTP时间戳(curNTP)。
  2. 遍历队列(q)中的所有通道项(channelMap),查找最早到期的任务(按NTP时间戳排序): a. 若通道项未关闭且非空,则获取其队列头任务的NTP时间戳。 b. 根据当前系统时间与队列头任务NTP时间戳的关系,更新keys列表和curNTP。
  3. 对于keys列表中的每个通道键,再次检查其对应通道项是否符合条件(未关闭且非空),并尝试获取队首任务数据: a. 获取队首任务数据。 b. 若获取成功,将任务数据添加到结果映射(results)。
  4. 检查结果映射(results)是否为空。若为空,返回nil、0和false;否则返回结果映射、当前NTP时间戳和true。

func (*AsynchronousTemporalQueue) Pop

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

函数执行流程如下:

  1. 初始化结果映射(results)、待处理通道键列表(keys)及当前系统时间对应的NTP时间戳(curNTP)。
  2. 遍历队列(q)中的所有通道项(channelMap),查找最早到期的任务(按NTP时间戳排序): a. 若通道项未关闭且非空,则获取其队列头任务的NTP时间戳。 b. 根据当前系统时间与队列头任务NTP时间戳的关系,更新keys列表和curNTP。
  3. 对于keys列表中的每个通道键,再次检查其对应通道项是否符合条件(未关闭且非空),并尝试弹出任务: a. 增加通道项的_wg计数器,表示开始处理任务。 b. 弹出任务数据并减少通道项的_wg计数器。 c. 若弹出成功,将任务数据添加到结果映射(results)。
  4. 检查结果映射(results)是否为空。若为空,返回nil、0和false;否则返回结果映射、当前NTP时间戳和true。

func (*AsynchronousTemporalQueue) Push

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

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

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