-
-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathpriority_queue.go
More file actions
35 lines (29 loc) · 662 Bytes
/
Copy pathpriority_queue.go
File metadata and controls
35 lines (29 loc) · 662 Bytes
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
package astar
// A priorityQueue implements heap.Interface and holds Nodes. The
// priorityQueue is used to track open nodes by rank.
type priorityQueue []*node
func (pq priorityQueue) Len() int {
return len(pq)
}
func (pq priorityQueue) Less(i, j int) bool {
return pq[i].rank < pq[j].rank
}
func (pq priorityQueue) Swap(i, j int) {
pq[i], pq[j] = pq[j], pq[i]
pq[i].index = i
pq[j].index = j
}
func (pq *priorityQueue) Push(x interface{}) {
n := len(*pq)
no := x.(*node)
no.index = n
*pq = append(*pq, no)
}
func (pq *priorityQueue) Pop() interface{} {
old := *pq
n := len(old)
no := old[n-1]
no.index = -1
*pq = old[0 : n-1]
return no
}