@@ -158,7 +158,9 @@ use {Collection, Mutable, MutableSeq};
158
158
use slice;
159
159
use vec:: Vec ;
160
160
161
- /// A priority queue implemented with a binary heap
161
+ /// A priority queue implemented with a binary heap.
162
+ ///
163
+ /// This will be a max-heap.
162
164
#[ deriving( Clone ) ]
163
165
pub struct PriorityQueue < T > {
164
166
data : Vec < T > ,
@@ -180,35 +182,147 @@ impl<T: Ord> Default for PriorityQueue<T> {
180
182
}
181
183
182
184
impl < T : Ord > PriorityQueue < T > {
185
+ /// Create an empty PriorityQueue as a max-heap.
186
+ ///
187
+ /// # Example
188
+ ///
189
+ /// ```
190
+ /// use std::collections::PriorityQueue;
191
+ /// let pq: PriorityQueue<uint> = PriorityQueue::new();
192
+ /// ```
193
+ pub fn new ( ) -> PriorityQueue < T > { PriorityQueue { data : vec ! ( ) , } }
194
+
195
+ /// Create an empty PriorityQueue with a specific capacity.
196
+ /// This preallocates enough memory for `capacity` elements,
197
+ /// so that the PriorityQueue does not have to be reallocated
198
+ /// until it contains at least that many values.
199
+ ///
200
+ /// # Example
201
+ ///
202
+ /// ```
203
+ /// use std::collections::PriorityQueue;
204
+ /// let pq: PriorityQueue<uint> = PriorityQueue::with_capacity(10u);
205
+ /// ```
206
+ pub fn with_capacity ( capacity : uint ) -> PriorityQueue < T > {
207
+ PriorityQueue { data : Vec :: with_capacity ( capacity) }
208
+ }
209
+
210
+ /// Create a PriorityQueue from a vector. This is sometimes called
211
+ /// `heapifying` the vector.
212
+ ///
213
+ /// # Example
214
+ ///
215
+ /// ```
216
+ /// use std::collections::PriorityQueue;
217
+ /// let pq = PriorityQueue::from_vec(vec![9i, 1, 2, 7, 3, 2]);
218
+ /// ```
219
+ pub fn from_vec ( xs : Vec < T > ) -> PriorityQueue < T > {
220
+ let mut q = PriorityQueue { data : xs, } ;
221
+ let mut n = q. len ( ) / 2 ;
222
+ while n > 0 {
223
+ n -= 1 ;
224
+ q. siftdown ( n)
225
+ }
226
+ q
227
+ }
228
+
183
229
/// An iterator visiting all values in underlying vector, in
184
230
/// arbitrary order.
231
+ ///
232
+ /// # Example
233
+ ///
234
+ /// ```
235
+ /// use std::collections::PriorityQueue;
236
+ /// let pq = PriorityQueue::from_vec(vec![1i, 2, 3, 4]);
237
+ ///
238
+ /// // Print 1, 2, 3, 4 in arbitrary order
239
+ /// for x in pq.iter() {
240
+ /// println!("{}", x);
241
+ /// }
242
+ /// ```
185
243
pub fn iter < ' a > ( & ' a self ) -> Items < ' a , T > {
186
244
Items { iter : self . data . iter ( ) }
187
245
}
188
246
189
- /// Returns the greatest item in a queue or None if it is empty
247
+ /// Returns the greatest item in a queue or `None` if it is empty.
248
+ ///
249
+ /// # Example
250
+ ///
251
+ /// ```
252
+ /// use std::collections::PriorityQueue;
253
+ ///
254
+ /// let mut pq = PriorityQueue::new();
255
+ /// assert_eq!(pq.top(), None);
256
+ ///
257
+ /// pq.push(1i);
258
+ /// pq.push(5i);
259
+ /// pq.push(2i);
260
+ /// assert_eq!(pq.top(), Some(&5i));
261
+ ///
262
+ /// ```
190
263
pub fn top < ' a > ( & ' a self ) -> Option < & ' a T > {
191
264
if self . is_empty ( ) { None } else { Some ( self . data . get ( 0 ) ) }
192
265
}
193
266
194
267
#[ deprecated="renamed to `top`" ]
195
268
pub fn maybe_top < ' a > ( & ' a self ) -> Option < & ' a T > { self . top ( ) }
196
269
197
- /// Returns the number of elements the queue can hold without reallocating
270
+ /// Returns the number of elements the queue can hold without reallocating.
271
+ ///
272
+ /// # Example
273
+ ///
274
+ /// ```
275
+ /// use std::collections::PriorityQueue;
276
+ ///
277
+ /// let pq: PriorityQueue<uint> = PriorityQueue::with_capacity(100u);
278
+ /// assert!(pq.capacity() >= 100u);
279
+ /// ```
198
280
pub fn capacity ( & self ) -> uint { self . data . capacity ( ) }
199
281
200
- /// Reserve capacity for exactly n elements in the PriorityQueue.
282
+ /// Reserve capacity for exactly `n` elements in the PriorityQueue.
201
283
/// Do nothing if the capacity is already sufficient.
284
+ ///
285
+ /// # Example
286
+ ///
287
+ /// ```
288
+ /// use std::collections::PriorityQueue;
289
+ ///
290
+ /// let mut pq: PriorityQueue<uint> = PriorityQueue::new();
291
+ /// pq.reserve_exact(100u);
292
+ /// assert!(pq.capacity() == 100u);
293
+ /// ```
202
294
pub fn reserve_exact ( & mut self , n : uint ) { self . data . reserve_exact ( n) }
203
295
204
- /// Reserve capacity for at least n elements in the PriorityQueue.
296
+ /// Reserve capacity for at least `n` elements in the PriorityQueue.
205
297
/// Do nothing if the capacity is already sufficient.
298
+ ///
299
+ /// # Example
300
+ ///
301
+ /// ```
302
+ /// use std::collections::PriorityQueue;
303
+ ///
304
+ /// let mut pq: PriorityQueue<uint> = PriorityQueue::new();
305
+ /// pq.reserve(100u);
306
+ /// assert!(pq.capacity() >= 100u);
307
+ /// ```
206
308
pub fn reserve ( & mut self , n : uint ) {
207
309
self . data . reserve ( n)
208
310
}
209
311
210
312
/// Remove the greatest item from a queue and return it, or `None` if it is
211
313
/// empty.
314
+ ///
315
+ /// # Example
316
+ ///
317
+ /// ```
318
+ /// use std::collections::PriorityQueue;
319
+ ///
320
+ /// let mut pq = PriorityQueue::from_vec(vec![1i, 3]);
321
+ ///
322
+ /// assert_eq!(pq.pop(), Some(3i));
323
+ /// assert_eq!(pq.pop(), Some(1i));
324
+ /// assert_eq!(pq.pop(), None);
325
+ /// ```
212
326
pub fn pop ( & mut self ) -> Option < T > {
213
327
match self . data . pop ( ) {
214
328
None => { None }
@@ -225,14 +339,43 @@ impl<T: Ord> PriorityQueue<T> {
225
339
#[ deprecated="renamed to `pop`" ]
226
340
pub fn maybe_pop ( & mut self ) -> Option < T > { self . pop ( ) }
227
341
228
- /// Push an item onto the queue
342
+ /// Push an item onto the queue.
343
+ ///
344
+ /// # Example
345
+ ///
346
+ /// ```
347
+ /// use std::collections::PriorityQueue;
348
+ ///
349
+ /// let mut pq = PriorityQueue::new();
350
+ /// pq.push(3i);
351
+ /// pq.push(5i);
352
+ /// pq.push(1i);
353
+ ///
354
+ /// assert_eq!(pq.len(), 3);
355
+ /// assert_eq!(pq.top(), Some(&5i));
356
+ /// ```
229
357
pub fn push ( & mut self , item : T ) {
230
358
self . data . push ( item) ;
231
359
let new_len = self . len ( ) - 1 ;
232
360
self . siftup ( 0 , new_len) ;
233
361
}
234
362
235
- /// Optimized version of a push followed by a pop
363
+ /// Optimized version of a push followed by a pop.
364
+ ///
365
+ /// # Example
366
+ ///
367
+ /// ```
368
+ /// use std::collections::PriorityQueue;
369
+ ///
370
+ /// let mut pq = PriorityQueue::new();
371
+ /// pq.push(1i);
372
+ /// pq.push(5i);
373
+ ///
374
+ /// assert_eq!(pq.push_pop(3i), 5);
375
+ /// assert_eq!(pq.push_pop(9i), 9);
376
+ /// assert_eq!(pq.len(), 2);
377
+ /// assert_eq!(pq.top(), Some(&3i));
378
+ /// ```
236
379
pub fn push_pop ( & mut self , mut item : T ) -> T {
237
380
if !self . is_empty ( ) && * self . top ( ) . unwrap ( ) > item {
238
381
swap ( & mut item, self . data . get_mut ( 0 ) ) ;
@@ -243,6 +386,19 @@ impl<T: Ord> PriorityQueue<T> {
243
386
244
387
/// Optimized version of a pop followed by a push. The push is done
245
388
/// regardless of whether the queue is empty.
389
+ ///
390
+ /// # Example
391
+ ///
392
+ /// ```
393
+ /// use std::collections::PriorityQueue;
394
+ ///
395
+ /// let mut pq = PriorityQueue::new();
396
+ ///
397
+ /// assert_eq!(pq.replace(1i), None);
398
+ /// assert_eq!(pq.replace(3i), Some(1i));
399
+ /// assert_eq!(pq.len(), 1);
400
+ /// assert_eq!(pq.top(), Some(&3i));
401
+ /// ```
246
402
pub fn replace ( & mut self , mut item : T ) -> Option < T > {
247
403
if !self . is_empty ( ) {
248
404
swap ( & mut item, self . data . get_mut ( 0 ) ) ;
@@ -263,10 +419,38 @@ impl<T: Ord> PriorityQueue<T> {
263
419
fn to_sorted_vec ( self ) -> Vec < T > { self . into_sorted_vec ( ) }
264
420
265
421
/// Consume the PriorityQueue and return the underlying vector
422
+ /// in arbitrary order.
423
+ ///
424
+ /// # Example
425
+ ///
426
+ /// ```
427
+ /// use std::collections::PriorityQueue;
428
+ ///
429
+ /// let pq = PriorityQueue::from_vec(vec![1i, 2, 3, 4, 5, 6, 7]);
430
+ /// let vec = pq.into_vec();
431
+ ///
432
+ /// // Will print in some order
433
+ /// for x in vec.iter() {
434
+ /// println!("{}", x);
435
+ /// }
436
+ /// ```
266
437
pub fn into_vec ( self ) -> Vec < T > { let PriorityQueue { data : v} = self ; v }
267
438
268
439
/// Consume the PriorityQueue and return a vector in sorted
269
- /// (ascending) order
440
+ /// (ascending) order.
441
+ ///
442
+ /// # Example
443
+ ///
444
+ /// ```
445
+ /// use std::collections::PriorityQueue;
446
+ ///
447
+ /// let mut pq = PriorityQueue::from_vec(vec![1i, 2, 4, 5, 7]);
448
+ /// pq.push(6);
449
+ /// pq.push(3);
450
+ ///
451
+ /// let vec = pq.into_sorted_vec();
452
+ /// assert_eq!(vec, vec![1i, 2, 3, 4, 5, 6, 7]);
453
+ /// ```
270
454
pub fn into_sorted_vec ( self ) -> Vec < T > {
271
455
let mut q = self ;
272
456
let mut end = q. len ( ) ;
@@ -278,25 +462,6 @@ impl<T: Ord> PriorityQueue<T> {
278
462
q. into_vec ( )
279
463
}
280
464
281
- /// Create an empty PriorityQueue
282
- pub fn new ( ) -> PriorityQueue < T > { PriorityQueue { data : vec ! ( ) , } }
283
-
284
- /// Create an empty PriorityQueue with capacity `capacity`
285
- pub fn with_capacity ( capacity : uint ) -> PriorityQueue < T > {
286
- PriorityQueue { data : Vec :: with_capacity ( capacity) }
287
- }
288
-
289
- /// Create a PriorityQueue from a vector (heapify)
290
- pub fn from_vec ( xs : Vec < T > ) -> PriorityQueue < T > {
291
- let mut q = PriorityQueue { data : xs, } ;
292
- let mut n = q. len ( ) / 2 ;
293
- while n > 0 {
294
- n -= 1 ;
295
- q. siftdown ( n)
296
- }
297
- q
298
- }
299
-
300
465
// The implementations of siftup and siftdown use unsafe blocks in
301
466
// order to move an element out of the vector (leaving behind a
302
467
// zeroed element), shift along the others and move it back into the
@@ -348,7 +513,7 @@ impl<T: Ord> PriorityQueue<T> {
348
513
}
349
514
}
350
515
351
- /// PriorityQueue iterator
516
+ /// PriorityQueue iterator.
352
517
pub struct Items < ' a , T > {
353
518
iter : slice:: Items < ' a , T > ,
354
519
}
0 commit comments