-
Notifications
You must be signed in to change notification settings - Fork 3
/
priority_queue.go
65 lines (54 loc) · 1.55 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
62
63
64
65
package vibrant
import (
"container/heap"
)
// A PriorityQueue implements heap.Interface and holds items.
type PriorityQueue interface {
Offer(items ...interface{})
Poll() interface{}
Len() int
}
// NewPriorityQueue creates a new PriorityQueue with a given capacity and priority function.
func NewPriorityQueue(initialCapacity uint32, priorityFunction func(interface{}) uint32) PriorityQueue {
return &priorityQueue{
make([]interface{}, 0, initialCapacity),
priorityFunction,
}
}
type priorityQueue struct {
queue []interface{}
priorityFunction func(interface{}) uint32
}
func (pq *priorityQueue) Offer(items ...interface{}) {
for _, item := range items {
heap.Push(pq, item)
}
}
func (pq *priorityQueue) Poll() interface{} {
return heap.Pop(pq)
}
// Satisfy the heap.Interface interface.
func (pq priorityQueue) Len() int {
return len(pq.queue)
}
// Satisfy the heap.Interface interface.
func (pq priorityQueue) Less(i, j int) bool {
// We want Pop to give us the highest, not lowest, priority so we use greater than here.
return pq.priorityFunction(pq.queue[i]) > pq.priorityFunction(pq.queue[j])
}
// Satisfy the heap.Interface interface.
func (pq priorityQueue) Swap(i, j int) {
pq.queue[i], pq.queue[j] = pq.queue[j], pq.queue[i]
}
// Satisfy the heap.Interface interface.
func (pq *priorityQueue) Push(item interface{}) {
pq.queue = append(pq.queue, item)
}
// Satisfy the heap.Interface interface.
func (pq *priorityQueue) Pop() interface{} {
old := pq.queue
n := len(old)
item := old[n-1]
pq.queue = old[0 : n-1]
return item
}