-
-
Notifications
You must be signed in to change notification settings - Fork 171
/
heap.go
47 lines (37 loc) · 758 Bytes
/
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
package util
import (
hp "container/heap"
)
type path struct {
weight int32
parent int32
nodes []int32
visited map[int32]struct{}
}
type minPath []path
func (h minPath) Len() int { return len(h) }
func (h minPath) Less(i, j int) bool { return h[i].weight < h[j].weight }
func (h minPath) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *minPath) Push(x interface{}) {
*h = append(*h, x.(path))
}
func (h *minPath) Pop() interface{} {
old := *h
n := len(old)
x := old[n-1]
*h = old[:n-1]
return x
}
type heap struct {
paths *minPath
}
func newHeap() *heap {
return &heap{paths: &minPath{}}
}
func (h *heap) push(p path) {
hp.Push(h.paths, p)
}
func (h *heap) pop() path {
i := hp.Pop(h.paths)
return i.(path)
}