-
Notifications
You must be signed in to change notification settings - Fork 567
/
priority_queue.go
160 lines (145 loc) · 3.11 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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package stream
import (
"io"
"sort"
"github.com/pachyderm/pachyderm/v2/src/internal/errors"
)
// Stream is a sorted stream that can be iterated.
// DEPRECATED: use Iterator instead
type Stream interface {
Next() error
}
// CompareFunc is a comparision function for two streams.
// DEPRECATED
type CompareFunc func(Stream, Stream) int
type stream struct {
stream Stream
priority int
}
// PriorityQueue implements a priority queue that operates on streams.
// DEPRECATED: use Merger instead
type PriorityQueue struct {
queue []*stream
size int
ss []*stream
cmp CompareFunc
}
// NewPriorityQueue creates a new priority queue.
// DEPRECATED: use Merger instead
func NewPriorityQueue(ss []Stream, cmp CompareFunc) *PriorityQueue {
var streams []*stream
for _, s := range ss {
streams = append(streams, &stream{
stream: s,
priority: len(streams),
})
}
return &PriorityQueue{
queue: make([]*stream, len(ss)+1),
ss: streams,
cmp: cmp,
}
}
// Iterate iterates through the priority queue.
func (pq *PriorityQueue) Iterate(cb func([]Stream) error) error {
for {
ss, err := pq.next()
if err != nil {
if errors.Is(err, io.EOF) {
return nil
}
return err
}
if err := cb(collectStreams(ss)); err != nil {
return err
}
}
}
func collectStreams(ss []*stream) []Stream {
var streams []Stream
for _, s := range ss {
streams = append(streams, s.stream)
}
return streams
}
func (pq *PriorityQueue) compare(i, j int) int {
si := pq.queue[i]
sj := pq.queue[j]
return pq.cmp(si.stream, sj.stream)
}
func (pq *PriorityQueue) empty() bool {
return len(pq.queue) == 1 || pq.queue[1] == nil
}
func (pq *PriorityQueue) insert(s *stream) error {
// Get next in stream and insert it.
if err := s.stream.Next(); err != nil {
if errors.Is(err, io.EOF) {
return nil
}
return errors.EnsureStack(err)
}
pq.queue[pq.size+1] = s
pq.size++
// Propagate insert up the queue
i := pq.size
for i > 1 {
if pq.compare(i/2, i) < 0 {
break
}
pq.swap(i/2, i)
i /= 2
}
return nil
}
func (pq *PriorityQueue) next() ([]*stream, error) {
// Re-insert streams
if pq.ss != nil {
for _, s := range pq.ss {
if err := pq.insert(s); err != nil {
return nil, err
}
}
}
if pq.empty() {
return nil, io.EOF
}
ss := []*stream{pq.queue[1]}
pq.fill()
// Keep popping streams off the queue if they are equal.
for pq.queue[1] != nil && pq.cmp(pq.queue[1].stream, ss[0].stream) == 0 {
ss = append(ss, pq.queue[1])
pq.fill()
}
sort.SliceStable(ss, func(i, j int) bool {
return ss[i].priority < ss[j].priority
})
pq.ss = ss
return ss, nil
}
func (pq *PriorityQueue) fill() {
// Replace first stream with last
pq.queue[1] = pq.queue[pq.size]
pq.queue[pq.size] = nil
pq.size--
// Propagate last stream down the queue
i := 1
var next int
for {
left, right := i*2, i*2+1
if left > pq.size {
break
} else if right > pq.size || pq.compare(left, right) < 0 {
next = left
} else {
next = right
}
if pq.compare(i, next) < 0 {
break
}
pq.swap(i, next)
i = next
}
}
func (pq *PriorityQueue) swap(i, j int) {
pq.queue[i], pq.queue[j] = pq.queue[j], pq.queue[i]
}