-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeath_row.go
89 lines (66 loc) · 1.5 KB
/
death_row.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package deathrow
import (
"container/heap"
"math"
"time"
)
type deathRow[K comparable] []Item[K]
func newDeathRow[K comparable]() *deathRow[K] {
return &deathRow[K]{}
}
func (dr deathRow[K]) Len() int { return len(dr) }
func (dr deathRow[K]) Less(i, j int) bool {
return dr[i].Deadline().Before(dr[j].Deadline())
}
func (dr deathRow[K]) Swap(i, j int) {
dr[i], dr[j] = dr[j], dr[i]
dr[i].SetIndex(i)
dr[j].SetIndex(j)
}
func (dr *deathRow[K]) Push(x any) {
n := len(*dr)
item := x.(Item[K])
item.SetIndex(n)
*dr = append(*dr, item)
}
func (dr *deathRow[K]) Pop() any {
old := *dr
n := len(old)
item := old[n-1]
old[n-1] = nil // avoid memory leak
item.SetIndex(-1) // for safety
*dr = old[0 : n-1]
return item
}
func (dr *deathRow[K]) Get(idx int) Item[K] {
if idx < 0 || idx >= len(*dr) {
return nil
}
return (*dr)[idx]
}
func (dr *deathRow[K]) GetFirst() Item[K] {
return dr.Get(0)
}
func (dr *deathRow[K]) GetLast() Item[K] {
return dr.Get(dr.Len() - 1)
}
func (dr *deathRow[K]) prolong(item Item[K], ttl time.Duration) {
item.Prolong(ttl)
heap.Fix(dr, item.Index())
}
func (dr *deathRow[K]) drop(item Item[K]) {
// set very low time to keep it at top position
item.Prolong(-math.MaxInt)
// remove will swap it to top and pop it - it should be poppable since its dead
heap.Remove(dr, item.Index())
}
func (dr *deathRow[K]) canPop() bool {
if dr.Len() <= 0 {
return false
}
first := dr.GetFirst()
if first == nil {
return false
}
return first.ShouldExecute()
}