Skip to content

Commit fe8afc6

Browse files
authored
Merge pull request #801 from lfenzo/docs-revamp-priority-queue
Improved Priority Queue Documentation + docstrings
2 parents 98ad0f2 + 9b75600 commit fe8afc6

File tree

2 files changed

+146
-65
lines changed

2 files changed

+146
-65
lines changed

docs/src/priority-queue.md

Lines changed: 74 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,95 @@
11
```@meta
22
DocTestSetup = :(using DataStructures)
33
```
4-
54
# Priority Queue
65

76
The `PriorityQueue` type provides a basic priority queue implementation
87
allowing for arbitrary key and priority types. Multiple identical keys
98
are not permitted, but the priority of existing keys can be changed
109
efficiently.
1110

12-
Usage:
11+
## Constructors
1312

14-
```julia
15-
PriorityQueue{K, V}() # construct a new priority queue with keys of type K and priorities of type V (forward ordering by default)
16-
PriorityQueue{K, V}(ord) # construct a new priority queue with the given types and ordering ord (Base.Order.Forward or Base.Order.Reverse)
17-
push!(pq, k=>v) # insert the key k into pq with priority v
18-
popfirst!(pq) # remove and return the lowest priority key and value
19-
first(pq) # return the lowest priority key and value without removing it
20-
delete!(pq, k) # delete the mapping for the given key in a priority queue, and return the priority queue.
13+
```@autodocs
14+
Modules = [DataStructures]
15+
Pages = ["src/priorityqueue.jl"]
16+
Order = [:type]
2117
```
2218

23-
`PriorityQueue` also behaves similarly to a `Dict` in that keys can be
24-
inserted and priorities accessed or changed using indexing notation.
19+
## Usage
2520

26-
Examples:
21+
The `PriorityQueue` type implements the following methods:
2722

28-
```jldoctest
29-
julia> pq = PriorityQueue();
23+
- [`delete!(pd::PriorityQueue, key)`](@ref)
24+
- [`empty!(pd::PriorityQueue)`](@ref)
25+
- [`first(pd::PriorityQueue)`](@ref)
26+
- [`haskey(pd::PriorityQueue, key)`](@ref)
27+
- [`isempty(pd::PriorityQueue)`](@ref)
28+
- [`length(pd::PriorityQueue)`](@ref)
29+
- [`popfirst!(pd::PriorityQueue)`](@ref)
30+
- [`push!(pd::PriorityQueue)`](@ref)
3031

31-
julia> # Insert keys with associated priorities
32-
pq["a"] = 10; pq["b"] = 5; pq["c"] = 15; pq
33-
PriorityQueue{Any, Any, Base.Order.ForwardOrdering} with 3 entries:
34-
"b" => 5
35-
"a" => 10
36-
"c" => 15
32+
!!! note
33+
`PriorityQueue` also behaves similarly to a `Dict` in that keys can be
34+
inserted and priorities accessed or changed using indexing notation.
3735

38-
julia> # Change the priority of an existing key
39-
pq["a"] = 0; pq
40-
PriorityQueue{Any, Any, Base.Order.ForwardOrdering} with 3 entries:
41-
"a" => 0
42-
"b" => 5
43-
"c" => 15
44-
```
36+
Examples:
37+
38+
```jldoctest
39+
julia> pq = PriorityQueue();
40+
41+
julia> # Insert keys with associated priorities
42+
pq["a"] = 10; pq["b"] = 5; pq["c"] = 15; pq
43+
PriorityQueue{Any, Any, Base.Order.ForwardOrdering} with 3 entries:
44+
"b" => 5
45+
"a" => 10
46+
"c" => 15
47+
48+
julia> # Change the priority of an existing key
49+
pq["a"] = 0; pq
50+
PriorityQueue{Any, Any, Base.Order.ForwardOrdering} with 3 entries:
51+
"a" => 0
52+
"b" => 5
53+
"c" => 15
54+
```
55+
56+
It is also possible to iterate over the priorities and elements of the queue in sorted order.
57+
58+
```jldoctest
59+
julia> pq = PriorityQueue("a" => 2, "b" => 1, "c" => 3)
60+
PriorityQueue{String, Int64, Base.Order.ForwardOrdering} with 3 entries:
61+
"b" => 1
62+
"a" => 2
63+
"c" => 3
64+
65+
julia> for priority in values(pq)
66+
println(priority)
67+
end
68+
1
69+
2
70+
3
71+
72+
julia> for element in keys(pq)
73+
println(element)
74+
end
75+
b
76+
a
77+
c
78+
79+
julia> for (element, priority) in pq
80+
println("$element $priority")
81+
end
82+
b 1
83+
a 2
84+
c 3
85+
```
86+
87+
------
4588

46-
It is also possible to iterate over the priorities and elements of the queue in sorted order.
47-
48-
```jldoctest
49-
julia> pq = PriorityQueue("a"=>2, "b"=>1, "c"=>3)
50-
PriorityQueue{String, Int64, Base.Order.ForwardOrdering} with 3 entries:
51-
"b" => 1
52-
"a" => 2
53-
"c" => 3
54-
55-
julia> for priority in values(pq)
56-
println(priority)
57-
end
58-
1
59-
2
60-
3
61-
62-
julia> for element in keys(pq)
63-
println(element)
64-
end
65-
b
66-
a
67-
c
89+
```@autodocs
90+
Modules = [DataStructures]
91+
Pages = ["src/priorityqueue.jl"]
92+
Order = [:function]
6893
```
6994

7095
```@meta

src/priorityqueue.jl

Lines changed: 72 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,24 @@
66
"""
77
PriorityQueue{K, V}([ord])
88
9-
Construct a new [`PriorityQueue`](@ref), with keys of type
10-
`K` and values/priorities of type `V`.
11-
If an order is not given, the priority queue is min-ordered using
9+
Construct a new `PriorityQueue`, with keys of type `K` and values/priorities
10+
of type `V`. If an order is not given, the priority queue is min-ordered using
1211
the default comparison for `V`.
1312
1413
A `PriorityQueue` acts like a `Dict`, mapping values to their
1514
priorities. New elements are added using `push!` and retrieved
1615
using `popfirst!` or `popat!` based on their priority.
1716
17+
Parameters
18+
---------
19+
20+
`K::Type` Data type for the keys
21+
22+
`V::Type` Data type for the values/priorities
23+
24+
`ord::Base.Ordering` Priority queue ordering
25+
26+
# Examples
1827
```jldoctest
1928
julia> PriorityQueue(Base.Order.Forward, "a" => 2, "b" => 3, "c" => 1)
2029
PriorityQueue{String, Int64, Base.Order.ForwardOrdering} with 3 entries:
@@ -112,15 +121,49 @@ _priority_queue_with_eltype(o::Ord, kv, ::Type ) where { Ord} = Pr
112121
## If deemed possible, please create a test and uncomment this definition.
113122
# _priority_queue_with_eltype{ D,Ord}(o::Ord, ps, ::Type{Pair{K,V} where K}) = PriorityQueue{Any, D,Ord}(o, ps)
114123

124+
125+
"""
126+
length(pq::PriorityQueue)
127+
128+
Return the number of pairs (`k`, `v`) in the priority queue `pq`.
129+
"""
115130
Base.length(pq::PriorityQueue) = length(pq.xs)
131+
132+
"""
133+
isempty(pq::PriorityQueue)
134+
135+
Verify if priority queue `pq` is empty.
136+
"""
116137
Base.isempty(pq::PriorityQueue) = isempty(pq.xs)
138+
139+
"""
140+
haskey(pq::PriorityQueue, key)
141+
142+
Verify if priority queue `pq` has `key` in its keys.
143+
144+
# Example
145+
146+
```jldoctest
147+
ulia> pq = PriorityQueue("a" => 1, "b" => 2, "c" => 3)
148+
PriorityQueue{String, Int64, Base.Order.ForwardOrdering} with 3 entries:
149+
"a" => 1
150+
"b" => 2
151+
"c" => 3
152+
153+
julia> haskey(pq, "a")
154+
true
155+
156+
julia> haskey(pq, "e")
157+
false
158+
```
159+
"""
117160
Base.haskey(pq::PriorityQueue, key) = haskey(pq.index, key)
118161

119162
"""
120-
first(pq)
163+
first(pq::PriorityQueue)
121164
122-
Return the lowest priority key and value from a priority queue without removing that
123-
pair from the queue.
165+
Return the lowest priority pair (`k`, `v`) from `pq` without removing it from the
166+
priority queue.
124167
"""
125168
Base.first(pq::PriorityQueue) = first(pq.xs)
126169

@@ -208,23 +251,27 @@ function Base.setindex!(pq::PriorityQueue{K, V}, value, key) where {K,V}
208251
end
209252

210253
"""
211-
push!(pq, k=>v)
254+
push!(pq::PriorityQueue{K,V}, pair::Pair{K,V}) where {K,V}
212255
213256
Insert the a key `k` into a priority queue `pq` with priority `v`.
214257
258+
# Examples
259+
215260
```jldoctest
216-
julia> a = PriorityQueue("a"=>1, "b"=>2, "c"=>3)
217-
PriorityQueue{String, Int64, Base.Order.ForwardOrdering} with 3 entries:
261+
julia> a = PriorityQueue("a" => 1, "b" => 2, "c" => 3, "e" => 5)
262+
PriorityQueue{String, Int64, Base.Order.ForwardOrdering} with 4 entries:
218263
"a" => 1
219264
"b" => 2
220265
"c" => 3
266+
"e" => 5
221267
222-
julia> push!(a, "d"=>4)
223-
PriorityQueue{String, Int64, Base.Order.ForwardOrdering} with 4 entries:
268+
julia> push!(a, "d" => 4)
269+
PriorityQueue{String, Int64, Base.Order.ForwardOrdering} with 5 entries:
224270
"a" => 1
225271
"b" => 2
226272
"c" => 3
227273
"d" => 4
274+
"e" => 5
228275
```
229276
"""
230277
function Base.push!(pq::PriorityQueue{K,V}, pair::Pair{K,V}) where {K,V}
@@ -242,9 +289,11 @@ end
242289
Base.push!(pq::PriorityQueue{K,V}, kv::Pair) where {K,V} = push!(pq, Pair{K,V}(kv.first, kv.second))
243290

244291
"""
245-
popfirst!(pq)
292+
popfirst!(pq::PriorityQueue)
246293
247-
Remove and return the lowest priority key and value from a priority queue as a pair.
294+
Remove and return the lowest priority key and value from a priority queue `pq` as a pair.
295+
296+
# Examples
248297
249298
```jldoctest
250299
julia> a = PriorityQueue(Base.Order.Forward, "a" => 2, "b" => 3, "c" => 1)
@@ -285,12 +334,14 @@ function popat!(pq::PriorityQueue, key)
285334
end
286335

287336
"""
288-
delete!(pq, key)
337+
delete!(pq::PriorityQueue, key)
338+
339+
Delete the mapping for the given `key` in a priority queue `pq` and return the priority queue.
289340
290-
Delete the mapping for the given key in a priority queue, and return the priority queue.
291341
# Examples
342+
292343
```jldoctest
293-
julia> q = PriorityQueue(Base.Order.Forward, "a"=>2, "b"=>3, "c"=>1)
344+
julia> q = PriorityQueue(Base.Order.Forward, "a" => 2, "b" => 3, "c" => 1)
294345
PriorityQueue{String, Int64, Base.Order.ForwardOrdering} with 3 entries:
295346
"c" => 1
296347
"a" => 2
@@ -306,6 +357,11 @@ function Base.delete!(pq::PriorityQueue, key)
306357
return pq
307358
end
308359

360+
"""
361+
empty!(pq::PriorityQueue)
362+
363+
Reset priority queue `pq`.
364+
"""
309365
function Base.empty!(pq::PriorityQueue)
310366
empty!(pq.xs)
311367
empty!(pq.index)

0 commit comments

Comments
 (0)