-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpriority_queue.go
46 lines (38 loc) · 1.03 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
package spellchecker
import "container/heap"
// priorityQueue implements heap.Interface and holds matches.
type priorityQueue struct {
items []match
capacity int
}
// newPriorityQueue initializes a new priorityQueue with a given capacity
func newPriorityQueue(capacity int) *priorityQueue {
return &priorityQueue{
items: make([]match, 0, capacity),
capacity: capacity,
}
}
func (pq priorityQueue) Len() int { return len(pq.items) }
func (pq priorityQueue) Less(i, j int) bool {
return pq.items[j].Score > pq.items[i].Score
}
func (pq priorityQueue) Swap(i, j int) {
pq.items[i], pq.items[j] = pq.items[j], pq.items[i]
}
func (pq *priorityQueue) Push(x interface{}) {
item := x.(match)
if len(pq.items) < pq.capacity {
pq.items = append(pq.items, item)
heap.Fix(pq, len(pq.items)-1)
} else if len(pq.items) > 0 && item.Score >= pq.items[0].Score {
pq.items[0] = item
heap.Fix(pq, 0)
}
}
func (pq *priorityQueue) Pop() interface{} {
old := pq.items
n := len(old)
item := old[n-1]
pq.items = old[0 : n-1]
return item
}