-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduler_types.go
68 lines (57 loc) · 1.16 KB
/
scheduler_types.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
63
64
65
66
67
68
package main
import (
"time"
)
type Scheduable interface {
SchedulerAction()
}
type SchedulerData struct {
when time.Time
index int
object Scheduable
}
type ScheduleHeap []*SchedulerData
func (h ScheduleHeap) Len() int {
return len(h)
}
func (h ScheduleHeap) Less(i, j int) bool {
return h[i].when.Before(h[j].when)
}
func (h ScheduleHeap) Swap(i, j int) {
h[i].index, h[j].index = h[j].index, h[i].index
h[i], h[j] = h[j], h[i]
}
func (h *ScheduleHeap) Push(x interface{}) {
// Push and Pop use pointer receivers because they modify the slice's length,
// not just its contents.
*h = append(*h, x.(*SchedulerData))
x.(*SchedulerData).index = len(*h) - 1
}
func (h *ScheduleHeap) Pop() interface{} {
old := *h
n := len(old)
x := old[n-1]
*h = old[0 : n-1]
x.index = -1
return x
}
type SchedUpdateType int
const (
SchedUpdateTypeUnknown SchedUpdateType = iota
SchedUpdateTypeAdd
SchedUpdateTypeRem
)
func (u SchedUpdateType) String() string {
switch u {
case SchedUpdateTypeAdd:
return "Add"
case SchedUpdateTypeRem:
return "Remove"
}
return "Unknown"
}
type SchedUpdate struct {
event SchedUpdateType
d Scheduable
when time.Time
}