-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpq.go
97 lines (81 loc) · 2.45 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package villa
import (
"container/heap"
"fmt"
)
// PriorityQueue is an unbounded priority queue based on a priority heap.
//
// A compare function, typed CmpFunc, needs to be specified. This struct is a better
// encapsulation of the "container/heap" package provided by go standard library.
// Usage:
// pq := villa.NewPriorityQueue(
// func (a, b interface{}) int {
// if a.(int) < b.(int) {
// return -1
// } else if a.(int) < b.(int) {
// return 1
// } // else if
// return 0
// })
// pq.Push(10)
// pq.Push(20)
//
// vl := pq.Pop()
type PriorityQueue struct {
list *pqList
}
type pqList struct {
Slice
cmp CmpFunc
}
// The Push method in heap.Interface.
func (l *pqList) Push(e interface{}) {
l.Add(e)
}
// The Pop method in heap.Interface.
func (l *pqList) Pop() interface{} {
return l.Remove(len(l.Slice) - 1)
}
// The Len method in sort.Interface.
func (l *pqList) Len() int {
return len(l.Slice)
}
// The Less method in sort.Interface
func (l *pqList) Less(i, j int) bool {
return l.cmp(l.Slice[i], l.Slice[j]) <= 0
}
// NewPriorityQueue creates a PriorityQueue instance with a specified compare function.
func NewPriorityQueue(cmp CmpFunc) *PriorityQueue {
return &PriorityQueue{&pqList{cmp: cmp}}
}
// NewPriorityQueue creates a PriorityQueue instance with a specified compare function and a capacity
func NewPriorityQueueCap(cmp CmpFunc, cap int) *PriorityQueue {
return &PriorityQueue{&pqList{Slice: make(Slice, 0, cap), cmp: cmp}}
}
// Push inserts the specified element into this priority queue.
func (pq *PriorityQueue) Push(x interface{}) {
heap.Push(pq.list, x)
}
// Pop retrieves and removes the head of this queue, or returns nil if this queue is empty.
func (pq *PriorityQueue) Pop() interface{} {
return heap.Pop(pq.list)
}
// Peek retrieves the head of this queue, or returns nil if this queue is empty.
func (pq *PriorityQueue) Peek() interface{} {
if pq.list.Len() > 0 {
return pq.list.Slice[0]
} // if
return nil
}
// Remove removes the element at index i from the priority queue.
func (pq *PriorityQueue) Remove(i int) interface{} {
return heap.Remove(pq.list, i)
}
// Len returns the number of elements in this queue.
func (pq *PriorityQueue) Len() int {
return pq.list.Len()
}
// String returns a string with value of "PriorityQueue(Len())"
func (pq *PriorityQueue) String() string {
return fmt.Sprintf("PriorityQueue(%d)", pq.list.Len())
}