-
Notifications
You must be signed in to change notification settings - Fork 37
/
min_heap_node.go
62 lines (52 loc) · 1.46 KB
/
min_heap_node.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright 2020-2024 guonaihong, antlabs. All rights reserved.
//
// mit license
package timer
import (
"time"
)
type minHeapNode struct {
callback func() // 用户的callback
absExpire time.Time // 绝对时间
userExpire time.Duration // 过期时间段
root *minHeap // 指向最小堆
next Next // 自定义下个触发的时间点, cronex项目用到了
index int32 // 在min heap中的索引,方便删除或者重新推入堆中
isSchedule bool // 是否是周期性任务
}
func (m *minHeapNode) Stop() bool {
m.root.removeTimeNode(m)
return true
}
func (m *minHeapNode) Reset(d time.Duration) bool {
m.root.resetTimeNode(m, d)
return true
}
func (m *minHeapNode) Next(now time.Time) time.Time {
if m.next != nil {
return (m.next).Next(now)
}
return now.Add(m.userExpire)
}
type minHeaps []*minHeapNode
func (m minHeaps) Len() int { return len(m) }
func (m minHeaps) Less(i, j int) bool { return m[i].absExpire.Before(m[j].absExpire) }
func (m minHeaps) Swap(i, j int) {
m[i], m[j] = m[j], m[i]
m[i].index = int32(i)
m[j].index = int32(j)
}
func (m *minHeaps) Push(x any) {
// Push and Pop use pointer receivers because they modify the slice's length,
// not just its contents.
*m = append(*m, x.(*minHeapNode))
lastIndex := int32(len(*m) - 1)
(*m)[lastIndex].index = lastIndex
}
func (m *minHeaps) Pop() any {
old := *m
n := len(old)
x := old[n-1]
*m = old[0 : n-1]
return x
}