-
Notifications
You must be signed in to change notification settings - Fork 0
/
priority_queue.go
61 lines (49 loc) · 1.02 KB
/
priority_queue.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
package pathfinding
import (
"container/heap"
//"fmt"
)
type PriorityQueue []*Node
// heap.Interface
func (pq PriorityQueue) Len() int {
return len(pq)
}
// heap.Interface
func (pq PriorityQueue) Less(i, j int) bool {
return pq[i].priority() < pq[j].priority()
}
// heap.Interface
func (pq PriorityQueue) Swap(i, j int) {
pq[i], pq[j] = pq[j], pq[i]
pq[i].heapIndex = i
pq[j].heapIndex = j
}
// heap.Interface
func (pq *PriorityQueue) Push(x interface{}) {
n := len(*pq)
item := x.(*Node)
item.heapIndex = n
*pq = append(*pq, item)
}
// heap.Interface
func (pq *PriorityQueue) Pop() interface{} {
old := *pq
n := len(old)
item := old[n-1]
item.heapIndex = -1 // for safety
*pq = old[0 : n-1]
return item
}
/* Node */
func (pq *PriorityQueue) PushNode(n *Node) {
heap.Push(pq, n)
}
func (pq *PriorityQueue) PopNode() *Node {
return heap.Pop(pq).(*Node)
}
func (pq *PriorityQueue) RemoveNode(n *Node) {
heap.Remove(pq, n.heapIndex)
}
func (pq *PriorityQueue) UpdateNode(n *Node) {
heap.Fix(pq, n.heapIndex)
}