@@ -231,6 +231,20 @@ use super::SpecExtend;
231
231
/// assert_eq!(heap.pop(), Some(Reverse(5)));
232
232
/// assert_eq!(heap.pop(), None);
233
233
/// ```
234
+ ///
235
+ /// # Time complexity
236
+ ///
237
+ /// | [push] | [pop] | [peek]/[peek\_mut] |
238
+ /// |--------|----------|--------------------|
239
+ /// | O(1)~ | O(log n) | O(1) |
240
+ ///
241
+ /// The value for `push` is an expected cost; the method documentation gives a
242
+ /// more detailed analysis.
243
+ ///
244
+ /// [push]: #method.push
245
+ /// [pop]: #method.pop
246
+ /// [peek]: #method.peek
247
+ /// [peek\_mut]: #method.peek_mut
234
248
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
235
249
pub struct BinaryHeap < T > {
236
250
data : Vec < T > ,
@@ -384,6 +398,10 @@ impl<T: Ord> BinaryHeap<T> {
384
398
/// }
385
399
/// assert_eq!(heap.peek(), Some(&2));
386
400
/// ```
401
+ ///
402
+ /// # Time complexity
403
+ ///
404
+ /// Cost is O(1) in the worst case.
387
405
#[ stable( feature = "binary_heap_peek_mut" , since = "1.12.0" ) ]
388
406
pub fn peek_mut ( & mut self ) -> Option < PeekMut < ' _ , T > > {
389
407
if self . is_empty ( ) {
@@ -411,6 +429,11 @@ impl<T: Ord> BinaryHeap<T> {
411
429
/// assert_eq!(heap.pop(), Some(1));
412
430
/// assert_eq!(heap.pop(), None);
413
431
/// ```
432
+ ///
433
+ /// # Time complexity
434
+ ///
435
+ /// The worst case cost of `pop` on a heap containing *n* elements is O(log
436
+ /// n).
414
437
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
415
438
pub fn pop ( & mut self ) -> Option < T > {
416
439
self . data . pop ( ) . map ( |mut item| {
@@ -438,6 +461,22 @@ impl<T: Ord> BinaryHeap<T> {
438
461
/// assert_eq!(heap.len(), 3);
439
462
/// assert_eq!(heap.peek(), Some(&5));
440
463
/// ```
464
+ ///
465
+ /// # Time complexity
466
+ ///
467
+ /// The expected cost of `push`, averaged over every possible ordering of
468
+ /// the elements being pushed, and over a sufficiently large number of
469
+ /// pushes, is O(1). This is the most meaningful cost metric when pushing
470
+ /// elements that are *not* already in any sorted pattern.
471
+ ///
472
+ /// The time complexity degrades if elements are pushed in predominantly
473
+ /// ascending order. In the worst case, elements are pushed in ascending
474
+ /// sorted order and the amortized cost per push is O(log n) against a heap
475
+ /// containing *n* elements.
476
+ ///
477
+ /// The worst case cost of a *single* call to `push` is O(n). The worst case
478
+ /// occurs when capacity is exhausted and needs a resize. The resize cost
479
+ /// has been amortized in the previous figures.
441
480
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
442
481
pub fn push ( & mut self , item : T ) {
443
482
let old_len = self . len ( ) ;
@@ -650,6 +689,10 @@ impl<T> BinaryHeap<T> {
650
689
/// assert_eq!(heap.peek(), Some(&5));
651
690
///
652
691
/// ```
692
+ ///
693
+ /// # Time complexity
694
+ ///
695
+ /// Cost is O(1) in the worst case.
653
696
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
654
697
pub fn peek ( & self ) -> Option < & T > {
655
698
self . data . get ( 0 )
0 commit comments