7
7
PriorityQueue{K, V}([ord])
8
8
9
9
Construct a new [`PriorityQueue`](@ref), with keys of type
10
- `K` and values/priorites of type `V`.
10
+ `K` and values/priorities of type `V`.
11
11
If an order is not given, the priority queue is min-ordered using
12
12
the default comparison for `V`.
13
13
14
14
A `PriorityQueue` acts like a `Dict`, mapping values to their
15
- priorities, with the addition of a `dequeue !` function to remove the
16
- lowest priority element .
15
+ priorities. New elements are added using `push !` and retrieved
16
+ using `popfirst!` or `popat!` based on their priority .
17
17
18
18
```jldoctest
19
19
julia> PriorityQueue(Base.Order.Forward, "a" => 2, "b" => 3, "c" => 1)
@@ -117,12 +117,12 @@ Base.isempty(pq::PriorityQueue) = isempty(pq.xs)
117
117
Base. haskey (pq:: PriorityQueue , key) = haskey (pq. index, key)
118
118
119
119
"""
120
- peek (pq)
120
+ first (pq)
121
121
122
122
Return the lowest priority key and value from a priority queue without removing that
123
- key from the queue.
123
+ pair from the queue.
124
124
"""
125
- Base. peek (pq:: PriorityQueue ) = pq. xs[ 1 ]
125
+ Base. first (pq:: PriorityQueue ) = first ( pq. xs)
126
126
127
127
function percolate_down! (pq:: PriorityQueue , i:: Integer )
128
128
x = pq. xs[i]
@@ -183,14 +183,14 @@ end
183
183
function Base. get! (pq:: PriorityQueue , key, default)
184
184
i = get (pq. index, key, 0 )
185
185
if i == 0
186
- enqueue ! (pq, key, default)
186
+ push ! (pq, key=> default)
187
187
return default
188
188
else
189
189
return pq. xs[i]. second
190
190
end
191
191
end
192
192
193
- # Change the priority of an existing element, or equeue it if it isn't present.
193
+ # Change the priority of an existing element, or enqueue it if it isn't present.
194
194
function Base. setindex! (pq:: PriorityQueue{K, V} , value, key) where {K,V}
195
195
i = get (pq. index, key, 0 )
196
196
if i != 0
@@ -202,32 +202,32 @@ function Base.setindex!(pq::PriorityQueue{K, V}, value, key) where {K,V}
202
202
percolate_up! (pq, i)
203
203
end
204
204
else
205
- enqueue ! (pq, key, value)
205
+ push ! (pq, key=> value)
206
206
end
207
207
return value
208
208
end
209
209
210
210
"""
211
- enqueue !(pq, k=>v)
211
+ push !(pq, k=>v)
212
212
213
213
Insert the a key `k` into a priority queue `pq` with priority `v`.
214
214
215
215
```jldoctest
216
- julia> a = PriorityQueue(PriorityQueue( "a"=>1, "b"=>2, "c"=>3) )
216
+ julia> a = PriorityQueue("a"=>1, "b"=>2, "c"=>3)
217
217
PriorityQueue{String, Int64, Base.Order.ForwardOrdering} with 3 entries:
218
218
"a" => 1
219
219
"b" => 2
220
220
"c" => 3
221
221
222
- julia> enqueue !(a, "d"=>4)
222
+ julia> push !(a, "d"=>4)
223
223
PriorityQueue{String, Int64, Base.Order.ForwardOrdering} with 4 entries:
224
224
"a" => 1
225
225
"b" => 2
226
226
"c" => 3
227
227
"d" => 4
228
228
```
229
229
"""
230
- function enqueue ! (pq:: PriorityQueue{K,V} , pair:: Pair{K,V} ) where {K,V}
230
+ function Base . push ! (pq:: PriorityQueue{K,V} , pair:: Pair{K,V} ) where {K,V}
231
231
key = pair. first
232
232
if haskey (pq, key)
233
233
throw (ArgumentError (" PriorityQueue keys must be unique" ))
@@ -239,44 +239,12 @@ function enqueue!(pq::PriorityQueue{K,V}, pair::Pair{K,V}) where {K,V}
239
239
return pq
240
240
end
241
241
242
- """
243
- enqueue!(pq, k, v)
244
-
245
- Insert the a key `k` into a priority queue `pq` with priority `v`.
242
+ Base. push! (pq:: PriorityQueue{K,V} , kv:: Pair ) where {K,V} = push! (pq, Pair {K,V} (kv. first, kv. second))
246
243
247
244
"""
248
- enqueue! (pq:: PriorityQueue , key, value) = enqueue! (pq, key=> value)
249
- enqueue! (pq:: PriorityQueue{K,V} , kv) where {K,V} = enqueue! (pq, Pair {K,V} (kv. first, kv. second))
245
+ popfirst!(pq)
250
246
251
- """
252
- dequeue!(pq)
253
-
254
- Remove and return the lowest priority key from a priority queue.
255
-
256
- ```jldoctest
257
- julia> a = PriorityQueue(Base.Order.Forward, ["a" => 2, "b" => 3, "c" => 1])
258
- PriorityQueue{String, Int64, Base.Order.ForwardOrdering} with 3 entries:
259
- "c" => 1
260
- "a" => 2
261
- "b" => 3
262
-
263
- julia> dequeue!(a)
264
- "c"
265
-
266
- julia> a
267
- PriorityQueue{String, Int64, Base.Order.ForwardOrdering} with 2 entries:
268
- "a" => 2
269
- "b" => 3
270
- ```
271
- """
272
- dequeue! (pq:: PriorityQueue ) = dequeue_pair! (pq). first
273
-
274
- dequeue! (pq:: PriorityQueue , key) = dequeue_pair! (pq, key). first
275
-
276
- """
277
- dequeue_pair!(pq)
278
-
279
- Remove and return a the lowest priority key and value from a priority queue as a pair.
247
+ Remove and return the lowest priority key and value from a priority queue as a pair.
280
248
281
249
```jldoctest
282
250
julia> a = PriorityQueue(Base.Order.Forward, "a" => 2, "b" => 3, "c" => 1)
@@ -285,7 +253,7 @@ PriorityQueue{String, Int64, Base.Order.ForwardOrdering} with 3 entries:
285
253
"a" => 2
286
254
"b" => 3
287
255
288
- julia> dequeue_pair !(a)
256
+ julia> popfirst !(a)
289
257
"c" => 1
290
258
291
259
julia> a
@@ -294,7 +262,7 @@ PriorityQueue{String, Int64, Base.Order.ForwardOrdering} with 2 entries:
294
262
"b" => 3
295
263
```
296
264
"""
297
- function dequeue_pair ! (pq:: PriorityQueue )
265
+ function Base . popfirst ! (pq:: PriorityQueue )
298
266
x = pq. xs[1 ]
299
267
y = pop! (pq. xs)
300
268
if ! isempty (pq)
@@ -306,10 +274,14 @@ function dequeue_pair!(pq::PriorityQueue)
306
274
return x
307
275
end
308
276
309
- function dequeue_pair! (pq:: PriorityQueue , key)
277
+ if isdefined (Base, :popat! ) # We will overload if it is defined, else we define on our own
278
+ import Base: popat!
279
+ end
280
+
281
+ function popat! (pq:: PriorityQueue , key)
310
282
idx = pq. index[key]
311
283
force_up! (pq, idx)
312
- dequeue_pair ! (pq)
284
+ popfirst ! (pq)
313
285
end
314
286
315
287
"""
@@ -330,7 +302,7 @@ PriorityQueue{String, Int64, Base.Order.ForwardOrdering} with 2 entries:
330
302
```
331
303
"""
332
304
function Base. delete! (pq:: PriorityQueue , key)
333
- dequeue_pair ! (pq, key)
305
+ popat ! (pq, key)
334
306
return pq
335
307
end
336
308
@@ -386,15 +358,15 @@ function Base.iterate(pq::PriorityQueue, ordered::Bool=true)
386
358
if ordered
387
359
isempty (pq) && return nothing
388
360
state = _PQIteratorState (PriorityQueue (copy (pq. xs), pq. o, copy (pq. index)))
389
- return dequeue_pair ! (state. pq), state
361
+ return popfirst ! (state. pq), state
390
362
else
391
363
_iterate (pq, iterate (pq. index))
392
364
end
393
365
end
394
366
395
367
function Base. iterate (pq:: PriorityQueue , state:: _PQIteratorState )
396
368
isempty (state. pq) && return nothing
397
- return dequeue_pair ! (state. pq), state
369
+ return popfirst ! (state. pq), state
398
370
end
399
371
400
372
Base. iterate (pq:: PriorityQueue , i) = _iterate (pq, iterate (pq. index, i))
0 commit comments