-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriorityqueue.go
More file actions
149 lines (131 loc) · 3.04 KB
/
Copy pathpriorityqueue.go
File metadata and controls
149 lines (131 loc) · 3.04 KB
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
// Package priorityqueue implements a generic binary heap (min-heap by default)
// with concurrency-safe push and pop operations.
//
// Zero external dependencies; uses a manual heap implementation for
// predictable performance.
package priorityqueue
import (
"context"
"sync"
)
// Item holds a value with an associated priority. Lower Priority values
// have higher precedence in a min-heap (the default).
type Item[T any] struct {
Value T
Priority int
}
// Queue is a generic priority queue backed by a binary heap.
// A zero-value Queue is NOT usable; use New to create one.
type Queue[T any] struct {
mu sync.Mutex
cond *sync.Cond
items []Item[T]
maxHeap bool
}
// New returns an empty priority queue. By default it is a min-heap; use
// WithMaxHeap to reverse the ordering.
func New[T any](opts ...Option) *Queue[T] {
cfg := defaultConfig()
for _, o := range opts {
o(&cfg)
}
q := &Queue[T]{maxHeap: cfg.maxHeap}
q.cond = sync.NewCond(&q.mu)
return q
}
// Push adds an item to the queue.
func (q *Queue[T]) Push(item Item[T]) {
q.mu.Lock()
defer q.mu.Unlock()
q.items = append(q.items, item)
q.siftUp(len(q.items) - 1)
q.cond.Signal()
}
// Pop removes and returns the highest-priority item (smallest priority in
// a min-heap, largest in a max-heap). Returns false if the queue is empty.
func (q *Queue[T]) Pop() (Item[T], bool) {
q.mu.Lock()
defer q.mu.Unlock()
if len(q.items) == 0 {
return Item[T]{}, false
}
v := q.items[0]
q.items[0] = q.items[len(q.items)-1]
q.items = q.items[:len(q.items)-1]
if len(q.items) > 0 {
q.siftDown(0)
}
return v, true
}
// PopContext blocks until an item is available and pops it, or returns
// the zero value and false if ctx is cancelled.
func (q *Queue[T]) PopContext(ctx context.Context) (Item[T], bool) {
q.mu.Lock()
defer q.mu.Unlock()
if len(q.items) == 0 {
done := make(chan struct{})
defer close(done)
go func() {
select {
case <-ctx.Done():
q.cond.Broadcast()
case <-done:
}
}()
for len(q.items) == 0 {
if ctx.Err() != nil {
return Item[T]{}, false
}
q.cond.Wait()
}
}
v := q.items[0]
q.items[0] = q.items[len(q.items)-1]
q.items = q.items[:len(q.items)-1]
if len(q.items) > 0 {
q.siftDown(0)
}
return v, true
}
// Len returns the number of items in the queue.
func (q *Queue[T]) Len() int {
q.mu.Lock()
defer q.mu.Unlock()
return len(q.items)
}
// --- binary heap helpers ---
func (q *Queue[T]) less(i, j int) bool {
if q.maxHeap {
return q.items[i].Priority > q.items[j].Priority
}
return q.items[i].Priority < q.items[j].Priority
}
func (q *Queue[T]) siftUp(i int) {
for i > 0 {
p := (i - 1) / 2
if !q.less(i, p) {
break
}
q.items[i], q.items[p] = q.items[p], q.items[i]
i = p
}
}
func (q *Queue[T]) siftDown(i int) {
n := len(q.items)
for {
smallest := i
l := 2*i + 1
r := 2*i + 2
if l < n && q.less(l, smallest) {
smallest = l
}
if r < n && q.less(r, smallest) {
smallest = r
}
if smallest == i {
break
}
q.items[i], q.items[smallest] = q.items[smallest], q.items[i]
i = smallest
}
}