6
6
"""
7
7
PriorityQueue{K, V}([ord])
8
8
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
12
11
the default comparison for `V`.
13
12
14
13
A `PriorityQueue` acts like a `Dict`, mapping values to their
15
14
priorities. New elements are added using `push!` and retrieved
16
15
using `popfirst!` or `popat!` based on their priority.
17
16
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
18
27
```jldoctest
19
28
julia> PriorityQueue(Base.Order.Forward, "a" => 2, "b" => 3, "c" => 1)
20
29
PriorityQueue{String, Int64, Base.Order.ForwardOrdering} with 3 entries:
@@ -112,15 +121,49 @@ _priority_queue_with_eltype(o::Ord, kv, ::Type ) where { Ord} = Pr
112
121
# # If deemed possible, please create a test and uncomment this definition.
113
122
# _priority_queue_with_eltype{ D,Ord}(o::Ord, ps, ::Type{Pair{K,V} where K}) = PriorityQueue{Any, D,Ord}(o, ps)
114
123
124
+
125
+ """
126
+ length(pq::PriorityQueue)
127
+
128
+ Return the number of pairs (`k`, `v`) in the priority queue `pq`.
129
+ """
115
130
Base. length (pq:: PriorityQueue ) = length (pq. xs)
131
+
132
+ """
133
+ isempty(pq::PriorityQueue)
134
+
135
+ Verify if priority queue `pq` is empty.
136
+ """
116
137
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
+ """
117
160
Base. haskey (pq:: PriorityQueue , key) = haskey (pq. index, key)
118
161
119
162
"""
120
- first(pq)
163
+ first(pq::PriorityQueue )
121
164
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.
124
167
"""
125
168
Base. first (pq:: PriorityQueue ) = first (pq. xs)
126
169
@@ -208,23 +251,27 @@ function Base.setindex!(pq::PriorityQueue{K, V}, value, key) where {K,V}
208
251
end
209
252
210
253
"""
211
- push!(pq, k=>v)
254
+ push!(pq::PriorityQueue{K,V}, pair::Pair{K,V}) where {K,V}
212
255
213
256
Insert the a key `k` into a priority queue `pq` with priority `v`.
214
257
258
+ # Examples
259
+
215
260
```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:
218
263
"a" => 1
219
264
"b" => 2
220
265
"c" => 3
266
+ "e" => 5
221
267
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:
224
270
"a" => 1
225
271
"b" => 2
226
272
"c" => 3
227
273
"d" => 4
274
+ "e" => 5
228
275
```
229
276
"""
230
277
function Base. push! (pq:: PriorityQueue{K,V} , pair:: Pair{K,V} ) where {K,V}
242
289
Base. push! (pq:: PriorityQueue{K,V} , kv:: Pair ) where {K,V} = push! (pq, Pair {K,V} (kv. first, kv. second))
243
290
244
291
"""
245
- popfirst!(pq)
292
+ popfirst!(pq::PriorityQueue )
246
293
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
248
297
249
298
```jldoctest
250
299
julia> a = PriorityQueue(Base.Order.Forward, "a" => 2, "b" => 3, "c" => 1)
@@ -285,12 +334,14 @@ function popat!(pq::PriorityQueue, key)
285
334
end
286
335
287
336
"""
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.
289
340
290
- Delete the mapping for the given key in a priority queue, and return the priority queue.
291
341
# Examples
342
+
292
343
```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)
294
345
PriorityQueue{String, Int64, Base.Order.ForwardOrdering} with 3 entries:
295
346
"c" => 1
296
347
"a" => 2
@@ -306,6 +357,11 @@ function Base.delete!(pq::PriorityQueue, key)
306
357
return pq
307
358
end
308
359
360
+ """
361
+ empty!(pq::PriorityQueue)
362
+
363
+ Reset priority queue `pq`.
364
+ """
309
365
function Base. empty! (pq:: PriorityQueue )
310
366
empty! (pq. xs)
311
367
empty! (pq. index)
0 commit comments