-
Notifications
You must be signed in to change notification settings - Fork 111
/
schedule.go
70 lines (61 loc) · 1.55 KB
/
schedule.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
69
70
package schedule
import (
"time"
"github.com/rilldata/rill/runtime/pkg/priorityqueue"
)
// Schedule tracks unique values ordered by time.
// It is not thread-safe.
type Schedule[K comparable, V any] struct {
hash func(V) K
pq *priorityqueue.PriorityQueue[V]
items map[K]*priorityqueue.Item[V]
}
// New creates a new Schedule. The hash function is used to determine a comparable key for a value.
func New[K comparable, V any](hash func(V) K) *Schedule[K, V] {
return &Schedule[K, V]{
hash: hash,
pq: priorityqueue.New[V](true),
items: make(map[K]*priorityqueue.Item[V]),
}
}
// Set adds or updates the time of a value.
func (s Schedule[K, V]) Set(v V, t time.Time) {
k := s.hash(v)
i, ok := s.items[k]
if ok {
s.pq.Remove(i)
}
p := int(t.Unix())
i = s.pq.Push(v, p)
s.items[k] = i
}
// Remove removes a value from the schedule.
func (s Schedule[K, V]) Remove(v V) {
k := s.hash(v)
i, ok := s.items[k]
if ok {
s.pq.Remove(i)
delete(s.items, k)
}
}
// Pop removes the value with the earliest time from the schedule and returns it.
// It will panic if the schedule is empty.
func (s Schedule[K, V]) Pop() V {
v := s.pq.Pop()
delete(s.items, s.hash(v))
return v
}
// Peek returns the value with the earliest time from the schedule.
func (s Schedule[K, V]) Peek() (V, time.Time) {
if s.pq.Len() == 0 {
var null V
return null, time.Time{}
}
v := s.pq.Peek()
i := s.items[s.hash(v)]
return v, time.Unix(int64(i.Priority()), 0)
}
// Len returns the number of values in the schedule.
func (s Schedule[K, V]) Len() int {
return len(s.items)
}