Skip to content

Commit 2448c15

Browse files
committed
Deprecate Base.peek(::PriorityQueue) in favor of Base.first
This also changes the error for empty queues from an `ArgumentError` to a `BoundsError`, so this is technically a breaking change. Previously, `Base.first` has been defined based on the `AbstractArray` interface via `iterate`. This commit defines a shortcut, thereby changing the error type.
1 parent 783dd9b commit 2448c15

File tree

5 files changed

+31
-14
lines changed

5 files changed

+31
-14
lines changed

src/DataStructures.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ module DataStructures
102102
export status
103103
export deref_key, deref_value, deref, advance, regress
104104

105-
export PriorityQueue, peek
105+
export PriorityQueue
106106

107107
include("priorityqueue.jl")
108108
include("sparse_int_set.jl")

src/deprecations.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,9 @@ Base.@deprecate_binding IntDisjointSets IntDisjointSet
1616
@deprecate dequeue!(q::PriorityQueue, x) popat!(q, x).first
1717
@deprecate dequeue_pair!(q::PriorityQueue) Base.popfirst!(q)
1818
@deprecate dequeue_pair!(q::PriorityQueue, key) popat!(q, key)
19+
20+
function Base.peek(q::PriorityQueue)
21+
Expr(:meta, :noinline)
22+
Base.depwarn("`peek(q::PriorityQueue)` is deprecated, use `first(q)` instead.", :peek)
23+
first(q)
24+
end

src/priorityqueue.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,12 @@ Base.isempty(pq::PriorityQueue) = isempty(pq.xs)
117117
Base.haskey(pq::PriorityQueue, key) = haskey(pq.index, key)
118118

119119
"""
120-
peek(pq)
120+
first(pq)
121121
122122
Return the lowest priority key and value from a priority queue without removing that
123-
key from the queue.
123+
pair from the queue.
124124
"""
125-
Base.peek(pq::PriorityQueue) = pq.xs[1]
125+
Base.first(pq::PriorityQueue) = first(pq.xs)
126126

127127
function percolate_down!(pq::PriorityQueue, i::Integer)
128128
x = pq.xs[i]

test/test_deprecations.jl

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ end
8383

8484
@test length(s) == 0
8585
@test isempty(s)
86-
@test_throws ArgumentError first(s)
86+
@test_throws BoundsError first(s)
8787
@test_throws BoundsError dequeue!(s)
8888

8989
for i = 1 : n
@@ -99,9 +99,20 @@ end
9999
if i < n
100100
@test first(s) == (i+1 => i+1)
101101
else
102-
@test_throws ArgumentError first(s)
102+
@test_throws BoundsError first(s)
103103
end
104104
@test isempty(s) == (i == n)
105105
@test length(s) == n - i
106106
end
107107
end
108+
109+
@testset "peek" begin
110+
pq = PriorityQueue{Int,Int}()
111+
push!(pq, 1 => 1)
112+
push!(pq, 2 => 2)
113+
@test peek(pq) == (1=>1)
114+
pq = PriorityQueue{Int,Int}()
115+
push!(pq, 2 => 2)
116+
push!(pq, 1 => 1)
117+
@test peek(pq) == (1=>1)
118+
end

test/test_priority_queue.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,17 @@ import Base.Order.Reverse
7777
@testset "construction from pairs" begin
7878
@testset "pq9" begin
7979
pq9 = PriorityQueue('a'=>1, 'b'=>2)
80-
@test peek(pq9) == ('a'=>1)
80+
@test first(pq9) == ('a'=>1)
8181
end
8282

8383
@testset "pq10" begin
8484
pq10 = PriorityQueue(Reverse, 'a'=>1, 'b'=>2)
85-
@test peek(pq10) == ('b'=>2)
85+
@test first(pq10) == ('b'=>2)
8686
end
8787

8888
@testset "pq11" begin
8989
pq11 = PriorityQueue(Pair{Char}['a'=>1,'b'=>2])
90-
@test peek(pq11) == ('a'=>1)
90+
@test first(pq11) == ('a'=>1)
9191
end
9292
end
9393

@@ -110,15 +110,15 @@ import Base.Order.Reverse
110110
@testset "PriorityQueueMethods" begin
111111
pq1 = PriorityQueue('a'=>1, 'b'=>2)
112112

113-
@testset "peek/get/popfirst!/get!" begin
114-
@test peek(pq1) == ('a'=>1)
113+
@testset "first/get/popfirst!/get!" begin
114+
@test first(pq1) == ('a'=>1)
115115
@test get(pq1, 'a', 0) == 1
116116
@test get(pq1, 'c', 0) == 0
117117
@test get!(pq1, 'b', 20) == 2
118118
@test popfirst!(pq1).first == 'a'
119119
@test popfirst!(pq1).first == 'b'
120120
@test get!(pq1, 'c', 0) == 0
121-
@test peek(pq1) == ('c'=>0)
121+
@test first(pq1) == ('c'=>0)
122122
@test get!(pq1, 'c', 3) == 0
123123
end
124124

@@ -127,10 +127,10 @@ import Base.Order.Reverse
127127
ks, vs = 1:n, rand(1:pmax, n)
128128
priorities = Dict(zip(ks, vs))
129129

130-
@testset "peek" begin
130+
@testset "first" begin
131131
pq1 = PriorityQueue(priorities)
132132
lowpri = findmin(vs)
133-
@test peek(pq1)[2] == pq1[ks[lowpri[2]]]
133+
@test first(pq1)[2] == pq1[ks[lowpri[2]]]
134134
end
135135

136136
@testset "enqueue error throw" begin

0 commit comments

Comments
 (0)