-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriority_queue.go
More file actions
213 lines (186 loc) · 3.82 KB
/
Copy pathpriority_queue.go
File metadata and controls
213 lines (186 loc) · 3.82 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package axsafe
import (
"errors"
"sort"
"sync"
)
var ErrEmptyPQueue = errors.New("priority queue is empty")
const maxPQCapacity = 0x10000 // 65536 items
type PriorityQueue struct {
mu sync.Mutex
queues map[uint]*queue // priority → FIFO queue
levels []uint // sorted desc (highest first)
total int
maxCapacity int
}
func NewPriorityQueue(capacity int) *PriorityQueue {
if capacity <= 0 {
capacity = 0x1000
}
if capacity > maxPQCapacity {
capacity = maxPQCapacity
}
return &PriorityQueue{
queues: make(map[uint]*queue),
maxCapacity: capacity,
}
}
func (pq *PriorityQueue) Push(priority uint, value interface{}) {
pq.mu.Lock()
if pq.total >= pq.maxCapacity {
pq.evictLowest()
}
q, ok := pq.queues[priority]
if !ok {
q = newQueue()
pq.queues[priority] = q
pq.levels = append(pq.levels, priority)
sort.Slice(pq.levels, func(i, j int) bool {
return pq.levels[i] > pq.levels[j] // descending
})
}
q.push(value)
pq.total++
pq.mu.Unlock()
}
func (pq *PriorityQueue) evictLowest() {
for i := len(pq.levels) - 1; i >= 0; i-- {
q := pq.queues[pq.levels[i]]
if q.len() > 0 {
q.pop()
pq.total--
return
}
}
}
func (pq *PriorityQueue) Pop() (interface{}, error) {
pq.mu.Lock()
defer pq.mu.Unlock()
for _, lvl := range pq.levels {
q := pq.queues[lvl]
if q.len() > 0 {
pq.total--
return q.pop(), nil
}
}
return nil, ErrEmptyPQueue
}
func (pq *PriorityQueue) PopByPriority(priority uint) (interface{}, error) {
pq.mu.Lock()
defer pq.mu.Unlock()
q, ok := pq.queues[priority]
if !ok || q.len() == 0 {
return nil, ErrEmptyPQueue
}
pq.total--
return q.pop(), nil
}
func (pq *PriorityQueue) Len() int {
pq.mu.Lock()
defer pq.mu.Unlock()
return pq.total
}
func (pq *PriorityQueue) LenByPriority(priority uint) int {
pq.mu.Lock()
defer pq.mu.Unlock()
q, ok := pq.queues[priority]
if !ok {
return 0
}
return q.len()
}
func (pq *PriorityQueue) IsEmpty() bool {
pq.mu.Lock()
defer pq.mu.Unlock()
return pq.total == 0
}
func (pq *PriorityQueue) Clear() {
pq.mu.Lock()
pq.queues = make(map[uint]*queue)
pq.levels = pq.levels[:0]
pq.total = 0
pq.mu.Unlock()
}
func (pq *PriorityQueue) RemoveIf(predicate func(v interface{}) bool) (bool, interface{}) {
pq.mu.Lock()
defer pq.mu.Unlock()
for _, lvl := range pq.levels {
q := pq.queues[lvl]
if found, val := q.removeIf(predicate); found {
pq.total--
return true, val
}
}
return false, nil
}
func (pq *PriorityQueue) FindIf(predicate func(v interface{}) bool) (bool, interface{}) {
pq.mu.Lock()
defer pq.mu.Unlock()
for _, lvl := range pq.levels {
q := pq.queues[lvl]
if found, val := q.findIf(predicate); found {
return true, val
}
}
return false, nil
}
type queueNode struct {
value interface{}
next *queueNode
}
type queue struct {
head *queueNode
tail *queueNode
size int
}
func newQueue() *queue {
return &queue{}
}
func (q *queue) push(value interface{}) {
node := &queueNode{value: value}
if q.tail != nil {
q.tail.next = node
} else {
q.head = node
}
q.tail = node
q.size++
}
func (q *queue) pop() interface{} {
node := q.head
q.head = node.next
if q.head == nil {
q.tail = nil
}
q.size--
return node.value
}
func (q *queue) len() int {
return q.size
}
func (q *queue) removeIf(predicate func(v interface{}) bool) (bool, interface{}) {
var prev *queueNode
for cur := q.head; cur != nil; prev, cur = cur, cur.next {
if predicate(cur.value) {
if prev == nil {
q.head = cur.next
} else {
prev.next = cur.next
}
if cur == q.tail {
q.tail = prev
}
q.size--
return true, cur.value
}
}
return false, nil
}
func (q *queue) findIf(predicate func(v interface{}) bool) (bool, interface{}) {
for cur := q.head; cur != nil; cur = cur.next {
if predicate(cur.value) {
return true, cur.value
}
}
return false, nil
}