68
68
//! // dist[node] = current shortest distance from `start` to `node`
69
69
//! let mut dist = Vec::from_elem(adj_list.len(), uint::MAX);
70
70
//!
71
- //! let mut pq = BinaryHeap::new();
71
+ //! let mut heap = BinaryHeap::new();
72
72
//!
73
73
//! // We're at `start`, with a zero cost
74
74
//! dist[start] = 0u;
75
- //! pq .push(State { cost: 0u, position: start });
75
+ //! heap .push(State { cost: 0u, position: start });
76
76
//!
77
77
//! // Examine the frontier with lower cost nodes first (min-heap)
78
78
//! loop {
79
- //! let State { cost, position } = match pq .pop() {
79
+ //! let State { cost, position } = match heap .pop() {
80
80
//! None => break, // empty
81
81
//! Some(s) => s
82
82
//! };
94
94
//!
95
95
//! // If so, add it to the frontier and continue
96
96
//! if next.cost < dist[next.position] {
97
- //! pq .push(next);
97
+ //! heap .push(next);
98
98
//! // Relaxation, we have now found a better way
99
99
//! dist[next.position] = next.cost;
100
100
//! }
@@ -184,7 +184,7 @@ impl<T: Ord> BinaryHeap<T> {
184
184
///
185
185
/// ```
186
186
/// use std::collections::BinaryHeap;
187
- /// let pq : BinaryHeap<uint> = BinaryHeap::new();
187
+ /// let heap : BinaryHeap<uint> = BinaryHeap::new();
188
188
/// ```
189
189
#[ unstable = "matches collection reform specification, waiting for dust to settle" ]
190
190
pub fn new ( ) -> BinaryHeap < T > { BinaryHeap { data : vec ! ( ) , } }
@@ -198,7 +198,7 @@ impl<T: Ord> BinaryHeap<T> {
198
198
///
199
199
/// ```
200
200
/// use std::collections::BinaryHeap;
201
- /// let pq : BinaryHeap<uint> = BinaryHeap::with_capacity(10u);
201
+ /// let heap : BinaryHeap<uint> = BinaryHeap::with_capacity(10u);
202
202
/// ```
203
203
#[ unstable = "matches collection reform specification, waiting for dust to settle" ]
204
204
pub fn with_capacity ( capacity : uint ) -> BinaryHeap < T > {
@@ -212,7 +212,7 @@ impl<T: Ord> BinaryHeap<T> {
212
212
///
213
213
/// ```
214
214
/// use std::collections::BinaryHeap;
215
- /// let pq = BinaryHeap::from_vec(vec![9i, 1, 2, 7, 3, 2]);
215
+ /// let heap = BinaryHeap::from_vec(vec![9i, 1, 2, 7, 3, 2]);
216
216
/// ```
217
217
pub fn from_vec ( xs : Vec < T > ) -> BinaryHeap < T > {
218
218
let mut q = BinaryHeap { data : xs, } ;
@@ -231,10 +231,10 @@ impl<T: Ord> BinaryHeap<T> {
231
231
///
232
232
/// ```
233
233
/// use std::collections::BinaryHeap;
234
- /// let pq = BinaryHeap::from_vec(vec![1i, 2, 3, 4]);
234
+ /// let heap = BinaryHeap::from_vec(vec![1i, 2, 3, 4]);
235
235
///
236
236
/// // Print 1, 2, 3, 4 in arbitrary order
237
- /// for x in pq .iter() {
237
+ /// for x in heap .iter() {
238
238
/// println!("{}", x);
239
239
/// }
240
240
/// ```
@@ -250,13 +250,13 @@ impl<T: Ord> BinaryHeap<T> {
250
250
/// ```
251
251
/// use std::collections::BinaryHeap;
252
252
///
253
- /// let mut pq = BinaryHeap::new();
254
- /// assert_eq!(pq .top(), None);
253
+ /// let mut heap = BinaryHeap::new();
254
+ /// assert_eq!(heap .top(), None);
255
255
///
256
- /// pq .push(1i);
257
- /// pq .push(5i);
258
- /// pq .push(2i);
259
- /// assert_eq!(pq .top(), Some(&5i));
256
+ /// heap .push(1i);
257
+ /// heap .push(5i);
258
+ /// heap .push(2i);
259
+ /// assert_eq!(heap .top(), Some(&5i));
260
260
///
261
261
/// ```
262
262
pub fn top < ' a > ( & ' a self ) -> Option < & ' a T > {
@@ -270,8 +270,8 @@ impl<T: Ord> BinaryHeap<T> {
270
270
/// ```
271
271
/// use std::collections::BinaryHeap;
272
272
///
273
- /// let pq : BinaryHeap<uint> = BinaryHeap::with_capacity(100u);
274
- /// assert!(pq .capacity() >= 100u);
273
+ /// let heap : BinaryHeap<uint> = BinaryHeap::with_capacity(100u);
274
+ /// assert!(heap .capacity() >= 100u);
275
275
/// ```
276
276
#[ unstable = "matches collection reform specification, waiting for dust to settle" ]
277
277
pub fn capacity ( & self ) -> uint { self . data . capacity ( ) }
@@ -292,9 +292,9 @@ impl<T: Ord> BinaryHeap<T> {
292
292
/// ```
293
293
/// use std::collections::BinaryHeap;
294
294
///
295
- /// let mut pq : BinaryHeap<uint> = BinaryHeap::new();
296
- /// pq .reserve_exact(100u);
297
- /// assert!(pq .capacity() >= 100u);
295
+ /// let mut heap : BinaryHeap<uint> = BinaryHeap::new();
296
+ /// heap .reserve_exact(100u);
297
+ /// assert!(heap .capacity() >= 100u);
298
298
/// ```
299
299
#[ unstable = "matches collection reform specification, waiting for dust to settle" ]
300
300
pub fn reserve_exact ( & mut self , additional : uint ) { self . data . reserve_exact ( additional) }
@@ -311,9 +311,9 @@ impl<T: Ord> BinaryHeap<T> {
311
311
/// ```
312
312
/// use std::collections::BinaryHeap;
313
313
///
314
- /// let mut pq : BinaryHeap<uint> = BinaryHeap::new();
315
- /// pq .reserve(100u);
316
- /// assert!(pq .capacity() >= 100u);
314
+ /// let mut heap : BinaryHeap<uint> = BinaryHeap::new();
315
+ /// heap .reserve(100u);
316
+ /// assert!(heap .capacity() >= 100u);
317
317
/// ```
318
318
#[ unstable = "matches collection reform specification, waiting for dust to settle" ]
319
319
pub fn reserve ( & mut self , additional : uint ) {
@@ -334,11 +334,11 @@ impl<T: Ord> BinaryHeap<T> {
334
334
/// ```
335
335
/// use std::collections::BinaryHeap;
336
336
///
337
- /// let mut pq = BinaryHeap::from_vec(vec![1i, 3]);
337
+ /// let mut heap = BinaryHeap::from_vec(vec![1i, 3]);
338
338
///
339
- /// assert_eq!(pq .pop(), Some(3i));
340
- /// assert_eq!(pq .pop(), Some(1i));
341
- /// assert_eq!(pq .pop(), None);
339
+ /// assert_eq!(heap .pop(), Some(3i));
340
+ /// assert_eq!(heap .pop(), Some(1i));
341
+ /// assert_eq!(heap .pop(), None);
342
342
/// ```
343
343
#[ unstable = "matches collection reform specification, waiting for dust to settle" ]
344
344
pub fn pop ( & mut self ) -> Option < T > {
@@ -361,13 +361,13 @@ impl<T: Ord> BinaryHeap<T> {
361
361
/// ```
362
362
/// use std::collections::BinaryHeap;
363
363
///
364
- /// let mut pq = BinaryHeap::new();
365
- /// pq .push(3i);
366
- /// pq .push(5i);
367
- /// pq .push(1i);
364
+ /// let mut heap = BinaryHeap::new();
365
+ /// heap .push(3i);
366
+ /// heap .push(5i);
367
+ /// heap .push(1i);
368
368
///
369
- /// assert_eq!(pq .len(), 3);
370
- /// assert_eq!(pq .top(), Some(&5i));
369
+ /// assert_eq!(heap .len(), 3);
370
+ /// assert_eq!(heap .top(), Some(&5i));
371
371
/// ```
372
372
#[ unstable = "matches collection reform specification, waiting for dust to settle" ]
373
373
pub fn push ( & mut self , item : T ) {
@@ -384,14 +384,14 @@ impl<T: Ord> BinaryHeap<T> {
384
384
/// ```
385
385
/// use std::collections::BinaryHeap;
386
386
///
387
- /// let mut pq = BinaryHeap::new();
388
- /// pq .push(1i);
389
- /// pq .push(5i);
387
+ /// let mut heap = BinaryHeap::new();
388
+ /// heap .push(1i);
389
+ /// heap .push(5i);
390
390
///
391
- /// assert_eq!(pq .push_pop(3i), 5);
392
- /// assert_eq!(pq .push_pop(9i), 9);
393
- /// assert_eq!(pq .len(), 2);
394
- /// assert_eq!(pq .top(), Some(&3i));
391
+ /// assert_eq!(heap .push_pop(3i), 5);
392
+ /// assert_eq!(heap .push_pop(9i), 9);
393
+ /// assert_eq!(heap .len(), 2);
394
+ /// assert_eq!(heap .top(), Some(&3i));
395
395
/// ```
396
396
pub fn push_pop ( & mut self , mut item : T ) -> T {
397
397
if !self . is_empty ( ) && * self . top ( ) . unwrap ( ) > item {
@@ -410,12 +410,12 @@ impl<T: Ord> BinaryHeap<T> {
410
410
/// ```
411
411
/// use std::collections::BinaryHeap;
412
412
///
413
- /// let mut pq = BinaryHeap::new();
413
+ /// let mut heap = BinaryHeap::new();
414
414
///
415
- /// assert_eq!(pq .replace(1i), None);
416
- /// assert_eq!(pq .replace(3i), Some(1i));
417
- /// assert_eq!(pq .len(), 1);
418
- /// assert_eq!(pq .top(), Some(&3i));
415
+ /// assert_eq!(heap .replace(1i), None);
416
+ /// assert_eq!(heap .replace(3i), Some(1i));
417
+ /// assert_eq!(heap .len(), 1);
418
+ /// assert_eq!(heap .top(), Some(&3i));
419
419
/// ```
420
420
pub fn replace ( & mut self , mut item : T ) -> Option < T > {
421
421
if !self . is_empty ( ) {
@@ -436,8 +436,8 @@ impl<T: Ord> BinaryHeap<T> {
436
436
/// ```
437
437
/// use std::collections::BinaryHeap;
438
438
///
439
- /// let pq = BinaryHeap::from_vec(vec![1i, 2, 3, 4, 5, 6, 7]);
440
- /// let vec = pq .into_vec();
439
+ /// let heap = BinaryHeap::from_vec(vec![1i, 2, 3, 4, 5, 6, 7]);
440
+ /// let vec = heap .into_vec();
441
441
///
442
442
/// // Will print in some order
443
443
/// for x in vec.iter() {
@@ -454,11 +454,11 @@ impl<T: Ord> BinaryHeap<T> {
454
454
/// ```
455
455
/// use std::collections::BinaryHeap;
456
456
///
457
- /// let mut pq = BinaryHeap::from_vec(vec![1i, 2, 4, 5, 7]);
458
- /// pq .push(6);
459
- /// pq .push(3);
457
+ /// let mut heap = BinaryHeap::from_vec(vec![1i, 2, 4, 5, 7]);
458
+ /// heap .push(6);
459
+ /// heap .push(3);
460
460
///
461
- /// let vec = pq .into_sorted_vec();
461
+ /// let vec = heap .into_sorted_vec();
462
462
/// assert_eq!(vec, vec![1i, 2, 3, 4, 5, 6, 7]);
463
463
/// ```
464
464
pub fn into_sorted_vec ( self ) -> Vec < T > {
@@ -578,9 +578,9 @@ mod tests {
578
578
fn test_iterator ( ) {
579
579
let data = vec ! ( 5 i, 9 , 3 ) ;
580
580
let iterout = [ 9 i, 5 , 3 ] ;
581
- let pq = BinaryHeap :: from_vec ( data) ;
581
+ let heap = BinaryHeap :: from_vec ( data) ;
582
582
let mut i = 0 ;
583
- for el in pq . iter ( ) {
583
+ for el in heap . iter ( ) {
584
584
assert_eq ! ( * el, iterout[ i] ) ;
585
585
i += 1 ;
586
586
}
0 commit comments