/
pq.go
50 lines (42 loc) · 1.21 KB
/
pq.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
package ladder
// A node in a priorityQueue.
type node struct {
word *Word
fScore int
gScore int
prev *node // Previous node.
// The index is maintained by the heap.Interface methods.
index int // The index of the item in the heap.
}
// A priorityQueue implements heap.Interface and holds slice of *node.
type priorityQueue []*node
// Len is the number of elements in the collection.
func (pq priorityQueue) Len() int { return len(pq) }
// Less reports whether the element with
// index i should sort before the element with index j.
func (pq priorityQueue) Less(i, j int) bool {
return pq[i].gScore < pq[j].gScore
}
// Swap swaps the elements with indexes i and j.
func (pq priorityQueue) Swap(i, j int) {
pq[i], pq[j] = pq[j], pq[i]
pq[i].index = i
pq[j].index = j
}
// Push pushes the element x into priorityQueue.
func (pq *priorityQueue) Push(x interface{}) {
n := len(*pq)
item := x.(*node)
item.index = n
*pq = append(*pq, item)
}
// Pop removes and returns the highest priority element from the priorityQueue.
func (pq *priorityQueue) Pop() interface{} {
old := *pq
n := len(old)
item := old[n-1]
old[n-1] = nil // avoid memory leak
item.index = -1 // for safety
*pq = old[0 : n-1]
return item
}