priority-queue.go -> priority_queue.go
Vladimir Bauer
8 years ago
| 0 | |
package mpb
|
| 1 | |
|
| 2 | |
import "container/heap"
|
| 3 | |
|
| 4 | |
// A priorityQueue implements heap.Interface and holds Items.
|
| 5 | |
type priorityQueue []*Bar
|
| 6 | |
|
| 7 | |
func (pq priorityQueue) Len() int { return len(pq) }
|
| 8 | |
|
| 9 | |
func (pq priorityQueue) Less(i, j int) bool {
|
| 10 | |
return pq[i].priority < pq[j].priority
|
| 11 | |
}
|
| 12 | |
|
| 13 | |
func (pq priorityQueue) Swap(i, j int) {
|
| 14 | |
pq[i], pq[j] = pq[j], pq[i]
|
| 15 | |
pq[i].index = i
|
| 16 | |
pq[j].index = j
|
| 17 | |
}
|
| 18 | |
|
| 19 | |
func (pq *priorityQueue) Push(x interface{}) {
|
| 20 | |
n := len(*pq)
|
| 21 | |
bar := x.(*Bar)
|
| 22 | |
bar.index = n
|
| 23 | |
*pq = append(*pq, bar)
|
| 24 | |
}
|
| 25 | |
|
| 26 | |
func (pq *priorityQueue) Pop() interface{} {
|
| 27 | |
old := *pq
|
| 28 | |
n := len(old)
|
| 29 | |
bar := old[n-1]
|
| 30 | |
bar.index = -1 // for safety
|
| 31 | |
*pq = old[0 : n-1]
|
| 32 | |
return bar
|
| 33 | |
}
|
| 34 | |
|
| 35 | |
// update modifies the priority of an Bar in the queue.
|
| 36 | |
func (pq *priorityQueue) update(bar *Bar, priority int) {
|
| 37 | |
bar.priority = priority
|
| 38 | |
heap.Fix(pq, bar.index)
|
| 39 | |
}
|
| 40 | |
|
| 41 | |
func (pq priorityQueue) maxNumP() int {
|
| 42 | |
if pq.Len() == 0 {
|
| 43 | |
return 0
|
| 44 | |
}
|
| 45 | |
|
| 46 | |
max := pq[0].NumOfPrependers()
|
| 47 | |
for i := 1; i < pq.Len(); i++ {
|
| 48 | |
n := pq[i].NumOfPrependers()
|
| 49 | |
if n > max {
|
| 50 | |
max = n
|
| 51 | |
}
|
| 52 | |
}
|
| 53 | |
return max
|
| 54 | |
}
|
| 55 | |
|
| 56 | |
func (pq priorityQueue) maxNumA() int {
|
| 57 | |
if pq.Len() == 0 {
|
| 58 | |
return 0
|
| 59 | |
}
|
| 60 | |
|
| 61 | |
max := pq[0].NumOfAppenders()
|
| 62 | |
for i := 1; i < pq.Len(); i++ {
|
| 63 | |
n := pq[i].NumOfAppenders()
|
| 64 | |
if n > max {
|
| 65 | |
max = n
|
| 66 | |
}
|
| 67 | |
}
|
| 68 | |
return max
|
| 69 | |
}
|
|
0 |
package mpb
|
|
1 |
|
|
2 |
import "container/heap"
|
|
3 |
|
|
4 |
// A priorityQueue implements heap.Interface and holds Items.
|
|
5 |
type priorityQueue []*Bar
|
|
6 |
|
|
7 |
func (pq priorityQueue) Len() int { return len(pq) }
|
|
8 |
|
|
9 |
func (pq priorityQueue) Less(i, j int) bool {
|
|
10 |
return pq[i].priority < pq[j].priority
|
|
11 |
}
|
|
12 |
|
|
13 |
func (pq priorityQueue) Swap(i, j int) {
|
|
14 |
pq[i], pq[j] = pq[j], pq[i]
|
|
15 |
pq[i].index = i
|
|
16 |
pq[j].index = j
|
|
17 |
}
|
|
18 |
|
|
19 |
func (pq *priorityQueue) Push(x interface{}) {
|
|
20 |
n := len(*pq)
|
|
21 |
bar := x.(*Bar)
|
|
22 |
bar.index = n
|
|
23 |
*pq = append(*pq, bar)
|
|
24 |
}
|
|
25 |
|
|
26 |
func (pq *priorityQueue) Pop() interface{} {
|
|
27 |
old := *pq
|
|
28 |
n := len(old)
|
|
29 |
bar := old[n-1]
|
|
30 |
bar.index = -1 // for safety
|
|
31 |
*pq = old[0 : n-1]
|
|
32 |
return bar
|
|
33 |
}
|
|
34 |
|
|
35 |
// update modifies the priority of an Bar in the queue.
|
|
36 |
func (pq *priorityQueue) update(bar *Bar, priority int) {
|
|
37 |
bar.priority = priority
|
|
38 |
heap.Fix(pq, bar.index)
|
|
39 |
}
|
|
40 |
|
|
41 |
func (pq priorityQueue) maxNumP() int {
|
|
42 |
if pq.Len() == 0 {
|
|
43 |
return 0
|
|
44 |
}
|
|
45 |
|
|
46 |
max := pq[0].NumOfPrependers()
|
|
47 |
for i := 1; i < pq.Len(); i++ {
|
|
48 |
n := pq[i].NumOfPrependers()
|
|
49 |
if n > max {
|
|
50 |
max = n
|
|
51 |
}
|
|
52 |
}
|
|
53 |
return max
|
|
54 |
}
|
|
55 |
|
|
56 |
func (pq priorityQueue) maxNumA() int {
|
|
57 |
if pq.Len() == 0 {
|
|
58 |
return 0
|
|
59 |
}
|
|
60 |
|
|
61 |
max := pq[0].NumOfAppenders()
|
|
62 |
for i := 1; i < pq.Len(); i++ {
|
|
63 |
n := pq[i].NumOfAppenders()
|
|
64 |
if n > max {
|
|
65 |
max = n
|
|
66 |
}
|
|
67 |
}
|
|
68 |
return max
|
|
69 |
}
|