@@ -167,6 +167,49 @@ use vec::{self, Vec};
167
167
/// item's ordering relative to any other item, as determined by the `Ord`
168
168
/// trait, changes while it is in the heap. This is normally only possible
169
169
/// through `Cell`, `RefCell`, global state, I/O, or unsafe code.
170
+ ///
171
+ /// # Examples
172
+ ///
173
+ /// ```
174
+ /// use std::collections::BinaryHeap;
175
+ ///
176
+ /// // type inference lets us omit an explicit type signature (which
177
+ /// // would be `BinaryHeap<i32>` in this example).
178
+ /// let mut heap = BinaryHeap::new();
179
+ ///
180
+ /// // We can use peek to look at the next item in the heap. In this case,
181
+ /// // there's no items in there yet so we get None.
182
+ /// assert_eq!(heap.peek(), None);
183
+ ///
184
+ /// // Let's add some scores...
185
+ /// heap.push(1);
186
+ /// heap.push(5);
187
+ /// heap.push(2);
188
+ ///
189
+ /// // Now peek shows the most important item in the heap.
190
+ /// assert_eq!(heap.peek(), Some(&5));
191
+ ///
192
+ /// // We can check the length of a heap.
193
+ /// assert_eq!(heap.len(), 3);
194
+ ///
195
+ /// // We can iterate over the items in the heap, although they are returned in
196
+ /// // a random order.
197
+ /// for x in heap.iter() {
198
+ /// println!("{}", x);
199
+ /// }
200
+ ///
201
+ /// // If we instead pop these scores, they should come back in order.
202
+ /// assert_eq!(heap.pop(), Some(5));
203
+ /// assert_eq!(heap.pop(), Some(2));
204
+ /// assert_eq!(heap.pop(), Some(1));
205
+ /// assert_eq!(heap.pop(), None);
206
+ ///
207
+ /// // We can clear the heap of any remaining items.
208
+ /// heap.clear();
209
+ ///
210
+ /// // The heap should now be empty.
211
+ /// assert!(heap.is_empty())
212
+ /// ```
170
213
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
171
214
pub struct BinaryHeap < T > {
172
215
data : Vec < T > ,
@@ -331,6 +374,17 @@ impl<T: Ord> BinaryHeap<T> {
331
374
}
332
375
333
376
/// Discards as much additional capacity as possible.
377
+ ///
378
+ /// # Examples
379
+ ///
380
+ /// ```
381
+ /// use std::collections::BinaryHeap;
382
+ /// let mut heap: BinaryHeap<i32> = BinaryHeap::with_capacity(100);
383
+ ///
384
+ /// assert!(heap.capacity() >= 100);
385
+ /// heap.shrink_to_fit();
386
+ /// assert!(heap.capacity() == 0);
387
+ /// ```
334
388
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
335
389
pub fn shrink_to_fit ( & mut self ) {
336
390
self . data . shrink_to_fit ( ) ;
@@ -571,12 +625,36 @@ impl<T: Ord> BinaryHeap<T> {
571
625
}
572
626
573
627
/// Returns the length of the binary heap.
628
+ ///
629
+ /// # Examples
630
+ ///
631
+ /// ```
632
+ /// use std::collections::BinaryHeap;
633
+ /// let mut heap = BinaryHeap::from(vec![1, 3]);
634
+ ///
635
+ /// assert_eq!(heap.len(), 2);
636
+ /// ```
574
637
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
575
638
pub fn len ( & self ) -> usize {
576
639
self . data . len ( )
577
640
}
578
641
579
642
/// Checks if the binary heap is empty.
643
+ ///
644
+ /// # Examples
645
+ ///
646
+ /// ```
647
+ /// use std::collections::BinaryHeap;
648
+ /// let mut heap = BinaryHeap::new();
649
+ ///
650
+ /// assert!(heap.is_empty());
651
+ ///
652
+ /// heap.push(3);
653
+ /// heap.push(5);
654
+ /// heap.push(1);
655
+ ///
656
+ /// assert!(!heap.is_empty());
657
+ /// ```
580
658
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
581
659
pub fn is_empty ( & self ) -> bool {
582
660
self . len ( ) == 0
@@ -585,13 +663,41 @@ impl<T: Ord> BinaryHeap<T> {
585
663
/// Clears the binary heap, returning an iterator over the removed elements.
586
664
///
587
665
/// The elements are removed in arbitrary order.
666
+ ///
667
+ /// # Examples
668
+ ///
669
+ /// ```
670
+ /// use std::collections::BinaryHeap;
671
+ /// let mut heap = BinaryHeap::from(vec![1, 3]);
672
+ ///
673
+ /// assert!(!heap.is_empty());
674
+ ///
675
+ /// for x in heap.drain() {
676
+ /// println!("{}", x);
677
+ /// }
678
+ ///
679
+ /// assert!(heap.is_empty());
680
+ /// ```
588
681
#[ inline]
589
682
#[ stable( feature = "drain" , since = "1.6.0" ) ]
590
683
pub fn drain ( & mut self ) -> Drain < T > {
591
684
Drain { iter : self . data . drain ( ..) }
592
685
}
593
686
594
687
/// Drops all items from the binary heap.
688
+ ///
689
+ /// # Examples
690
+ ///
691
+ /// ```
692
+ /// use std::collections::BinaryHeap;
693
+ /// let mut heap = BinaryHeap::from(vec![1, 3]);
694
+ ///
695
+ /// assert!(!heap.is_empty());
696
+ ///
697
+ /// heap.clear();
698
+ ///
699
+ /// assert!(heap.is_empty());
700
+ /// ```
595
701
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
596
702
pub fn clear ( & mut self ) {
597
703
self . drain ( ) ;
0 commit comments