forked from QuangTung97/memproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap.go
72 lines (58 loc) · 1.26 KB
/
heap.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
package memproxy
type delayedCallHeap struct {
data []delayedCall
}
func heapParent(index int) int {
return (index+1)/2 - 1
}
func heapLeftChild(index int) int {
return index*2 + 1
}
func (h *delayedCallHeap) swap(i, j int) {
h.data[i], h.data[j] = h.data[j], h.data[i]
}
func (h *delayedCallHeap) smaller(i, j int) bool {
return h.data[i].startedAt.Before(h.data[j].startedAt)
}
func (h *delayedCallHeap) push(e delayedCall) {
index := len(h.data)
h.data = append(h.data, e)
for index > 0 {
parent := heapParent(index)
if h.smaller(index, parent) {
h.swap(index, parent)
}
index = parent
}
}
func (h *delayedCallHeap) size() int {
return len(h.data)
}
func (h *delayedCallHeap) top() delayedCall {
return h.data[0]
}
func (h *delayedCallHeap) pop() delayedCall {
result := h.data[0]
last := len(h.data) - 1
h.data[0] = h.data[last]
h.data[last] = delayedCall{} // clear last
h.data = h.data[:last]
index := 0
for {
left := heapLeftChild(index)
right := left + 1
smallest := index
if left < len(h.data) && h.smaller(left, smallest) {
smallest = left
}
if right < len(h.data) && h.smaller(right, smallest) {
smallest = right
}
if smallest == index {
break
}
h.swap(index, smallest)
index = smallest
}
return result
}