Skip to content

Commit 3b3cb17

Browse files
committed
Refactor popfirst!(::PriorityQueue)
`PriorityQueue` has been introduced as "acting like a `Dict`". Therefore, it may be considered as a collection of `key => priority` pairs, as indicated by `first(pq)` or iteration over it. However, `popfirst!(pq)` has returned only the key but not the pair, such that the return type of `popfirst!` did not match `eltype(pq)`. This commit fixes that by replacing `popfirst!` with `dequeue_pair!`. This is a breaking change.
1 parent 48cb833 commit 3b3cb17

File tree

4 files changed

+27
-49
lines changed

4 files changed

+27
-49
lines changed

src/DataStructures.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module DataStructures
1515
export complement, complement!
1616

1717
export Deque, Stack, Queue, CircularDeque
18-
export dequeue_pair!, update!
18+
export update!
1919
export capacity, num_blocks, top_with_handle, sizehint!
2020

2121
export Accumulator, counter, reset!, inc!, dec!

src/deprecations.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,7 @@ Base.@deprecate_binding IntDisjointSets IntDisjointSet
1212
@deprecate enqueue!(q::PriorityQueue, x) Base.push!(q, x)
1313
@deprecate enqueue!(q::PriorityQueue, k, v) Base.push!(q, k=>v)
1414
@deprecate dequeue!(q::Queue) Base.popfirst!(q)
15-
@deprecate dequeue!(q::PriorityQueue) Base.popfirst!(q)
16-
@deprecate dequeue!(q::PriorityQueue, x) Base.popat!(q, x)
15+
@deprecate dequeue!(q::PriorityQueue) Base.popfirst!(q).first # maybe better: `val, _ = popfirst!(pq)`
16+
@deprecate dequeue!(q::PriorityQueue, x) Base.popat!(q, x).first
17+
@deprecate dequeue_pair!(q::PriorityQueue) Base.popfirst!(q)
18+
@deprecate dequeue_pair!(q::PriorityQueue, key) Base.popat!(q, key)

src/priorityqueue.jl

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -244,32 +244,7 @@ Base.push!(pq::PriorityQueue{K,V}, kv::Pair) where {K,V} = push!(pq, Pair{K,V}(k
244244
"""
245245
popfirst!(pq)
246246
247-
Remove and return the lowest priority key from a priority queue.
248-
249-
```jldoctest
250-
julia> a = PriorityQueue(Base.Order.Forward, ["a" => 2, "b" => 3, "c" => 1])
251-
PriorityQueue{String, Int64, Base.Order.ForwardOrdering} with 3 entries:
252-
"c" => 1
253-
"a" => 2
254-
"b" => 3
255-
256-
julia> popfirst!(a)
257-
"c"
258-
259-
julia> a
260-
PriorityQueue{String, Int64, Base.Order.ForwardOrdering} with 2 entries:
261-
"a" => 2
262-
"b" => 3
263-
```
264-
"""
265-
Base.popfirst!(pq::PriorityQueue) = dequeue_pair!(pq).first
266-
267-
Base.popat!(pq::PriorityQueue, key) = dequeue_pair!(pq, key).first
268-
269-
"""
270-
dequeue_pair!(pq)
271-
272-
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.
273248
274249
```jldoctest
275250
julia> a = PriorityQueue(Base.Order.Forward, "a" => 2, "b" => 3, "c" => 1)
@@ -278,7 +253,7 @@ PriorityQueue{String, Int64, Base.Order.ForwardOrdering} with 3 entries:
278253
"a" => 2
279254
"b" => 3
280255
281-
julia> dequeue_pair!(a)
256+
julia> popfirst!(a)
282257
"c" => 1
283258
284259
julia> a
@@ -287,7 +262,7 @@ PriorityQueue{String, Int64, Base.Order.ForwardOrdering} with 2 entries:
287262
"b" => 3
288263
```
289264
"""
290-
function dequeue_pair!(pq::PriorityQueue)
265+
function Base.popfirst!(pq::PriorityQueue)
291266
x = pq.xs[1]
292267
y = pop!(pq.xs)
293268
if !isempty(pq)
@@ -299,10 +274,10 @@ function dequeue_pair!(pq::PriorityQueue)
299274
return x
300275
end
301276

302-
function dequeue_pair!(pq::PriorityQueue, key)
277+
function Base.popat!(pq::PriorityQueue, key)
303278
idx = pq.index[key]
304279
force_up!(pq, idx)
305-
dequeue_pair!(pq)
280+
popfirst!(pq)
306281
end
307282

308283
"""
@@ -323,7 +298,7 @@ PriorityQueue{String, Int64, Base.Order.ForwardOrdering} with 2 entries:
323298
```
324299
"""
325300
function Base.delete!(pq::PriorityQueue, key)
326-
dequeue_pair!(pq, key)
301+
popat!(pq, key)
327302
return pq
328303
end
329304

@@ -379,15 +354,15 @@ function Base.iterate(pq::PriorityQueue, ordered::Bool=true)
379354
if ordered
380355
isempty(pq) && return nothing
381356
state = _PQIteratorState(PriorityQueue(copy(pq.xs), pq.o, copy(pq.index)))
382-
return dequeue_pair!(state.pq), state
357+
return popfirst!(state.pq), state
383358
else
384359
_iterate(pq, iterate(pq.index))
385360
end
386361
end
387362

388363
function Base.iterate(pq::PriorityQueue, state::_PQIteratorState)
389364
isempty(state.pq) && return nothing
390-
return dequeue_pair!(state.pq), state
365+
return popfirst!(state.pq), state
391366
end
392367

393368
Base.iterate(pq::PriorityQueue, i) = _iterate(pq, iterate(pq.index, i))

test/test_priority_queue.jl

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import Base.Order.Reverse
77

88
# Test dequeing in sorted order.
99
function test_issorted!(pq::PriorityQueue, priorities, rev=false)
10-
last = popfirst!(pq)
10+
last, _ = popfirst!(pq)
1111
while !isempty(pq)
12-
value = popfirst!(pq)
12+
value, _ = popfirst!(pq)
1313
if !rev
1414
@test priorities[last] <= priorities[value]
1515
else
@@ -23,7 +23,7 @@ import Base.Order.Reverse
2323
i = 0
2424
while !isempty(pq)
2525
krqst = keys[i+=1]
26-
krcvd = popat!(pq, krqst)
26+
krcvd, _ = popat!(pq, krqst)
2727
@test krcvd == krqst
2828
end
2929
end
@@ -115,8 +115,8 @@ import Base.Order.Reverse
115115
@test get(pq1, 'a', 0) == 1
116116
@test get(pq1, 'c', 0) == 0
117117
@test get!(pq1, 'b', 20) == 2
118-
@test popfirst!(pq1) == 'a'
119-
@test popfirst!(pq1) == 'b'
118+
@test popfirst!(pq1).first == 'a'
119+
@test popfirst!(pq1).first == 'b'
120120
@test get!(pq1, 'c', 0) == 0
121121
@test peek(pq1) == ('c'=>0)
122122
@test get!(pq1, 'c', 3) == 0
@@ -192,21 +192,22 @@ import Base.Order.Reverse
192192
pq = PriorityQueue(priorities)
193193
@test_throws KeyError popat!(pq, 0)
194194

195-
@test 10 == popat!(pq, 10)
195+
v, _ = popat!(pq, 10)
196+
@test v == 10
196197
while !isempty(pq)
197-
@test 10 != popfirst!(pq)
198+
v, _ = popfirst!(pq)
199+
@test v != 10
198200
end
199201

200202
pq = PriorityQueue(1.0 => 1)
201-
@test popat!(pq, 1.0f0) isa Float64
202-
end
203+
@test popat!(pq, 1.0f0) isa eltype(pq)
204+
@test eltype(pq) == Pair{Float64, Int64}
203205

204-
@testset "dequeuing pair" begin
205206
priorities2 = Dict(zip('a':'e', 5:-1:1))
206207
pq = PriorityQueue(priorities2)
207-
@test_throws KeyError dequeue_pair!(pq, 'g')
208-
@test dequeue_pair!(pq) == ('e'=> 1)
209-
@test dequeue_pair!(pq, 'b') == ('b'=>4)
208+
@test_throws KeyError popat!(pq, 'g')
209+
@test popfirst!(pq) == ('e'=> 1)
210+
@test popat!(pq, 'b') == ('b'=>4)
210211
@test length(pq) == 3
211212
end
212213

0 commit comments

Comments
 (0)