generated from dogmatiq/template-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pheap.go
48 lines (37 loc) · 842 Bytes
/
pheap.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
package kyu
// pheap is an implementation of heap.Interface that stores *Element. It is the
// underlying storage for the priority queue implementations.
type pheap struct {
less func(a, b interface{}) bool
elements []*Element
}
func (h *pheap) Len() int {
return len(h.elements)
}
func (h *pheap) Less(i, j int) bool {
return h.less(
h.elements[i].Value,
h.elements[j].Value,
)
}
func (h *pheap) Swap(i, j int) {
wasI := h.elements[i]
wasJ := h.elements[j]
h.elements[i] = wasJ
wasJ.index = i
h.elements[j] = wasI
wasI.index = j
}
func (h *pheap) Push(x interface{}) {
e := x.(*Element)
e.index = len(h.elements)
h.elements = append(h.elements, e)
}
func (h *pheap) Pop() interface{} {
i := len(h.elements) - 1
e := h.elements[i]
e.index = -1
h.elements[i] = nil
h.elements = h.elements[:i]
return e
}