-
Notifications
You must be signed in to change notification settings - Fork 2
/
intheap.go
43 lines (35 loc) · 1.02 KB
/
intheap.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
package nject
import (
"container/heap"
)
// Code below originated with the container/heap documentation
//nolint:recvcheck // some methods of intsHeap use a pointer, some do not
type intsHeap [][2]int
func (h intsHeap) Len() int { return len(h) }
func (h intsHeap) Less(i, j int) bool { return h[i][0] < h[j][0] }
func (h intsHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *intsHeap) Push(x interface{}) {
// Push and Pop use pointer receivers because they modify the slice's length,
// not just its contents.
//nolint:errcheck // if you Push something other than [2]int, expect a panic
*h = append(*h, x.([2]int))
}
func (h *intsHeap) Pop() interface{} {
old := *h
n := len(old)
x := old[n-1]
*h = old[0 : n-1]
return x
}
func push(h *intsHeap, funcs []*provider, i int) {
priority := i
if i < len(funcs) && funcs[i].reorder {
priority -= len(funcs)
}
heap.Push(h, [2]int{priority, i})
}
func pop(h *intsHeap) int {
//nolint:errcheck // we trust the type
x := heap.Pop(h).([2]int)
return x[1]
}