]> git.lizzy.rs Git - rust.git/blob - library/alloc/src/collections/binary_heap.rs
3706300dcfeb717e53897d01fca200d0b9b8eb62
[rust.git] / library / alloc / src / collections / binary_heap.rs
1 //! A priority queue implemented with a binary heap.
2 //!
3 //! Insertion and popping the largest element have *O*(log(*n*)) time complexity.
4 //! Checking the largest element is *O*(1). Converting a vector to a binary heap
5 //! can be done in-place, and has *O*(*n*) complexity. A binary heap can also be
6 //! converted to a sorted vector in-place, allowing it to be used for an *O*(*n* * log(*n*))
7 //! in-place heapsort.
8 //!
9 //! # Examples
10 //!
11 //! This is a larger example that implements [Dijkstra's algorithm][dijkstra]
12 //! to solve the [shortest path problem][sssp] on a [directed graph][dir_graph].
13 //! It shows how to use [`BinaryHeap`] with custom types.
14 //!
15 //! [dijkstra]: https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
16 //! [sssp]: https://en.wikipedia.org/wiki/Shortest_path_problem
17 //! [dir_graph]: https://en.wikipedia.org/wiki/Directed_graph
18 //!
19 //! ```
20 //! use std::cmp::Ordering;
21 //! use std::collections::BinaryHeap;
22 //!
23 //! #[derive(Copy, Clone, Eq, PartialEq)]
24 //! struct State {
25 //!     cost: usize,
26 //!     position: usize,
27 //! }
28 //!
29 //! // The priority queue depends on `Ord`.
30 //! // Explicitly implement the trait so the queue becomes a min-heap
31 //! // instead of a max-heap.
32 //! impl Ord for State {
33 //!     fn cmp(&self, other: &Self) -> Ordering {
34 //!         // Notice that the we flip the ordering on costs.
35 //!         // In case of a tie we compare positions - this step is necessary
36 //!         // to make implementations of `PartialEq` and `Ord` consistent.
37 //!         other.cost.cmp(&self.cost)
38 //!             .then_with(|| self.position.cmp(&other.position))
39 //!     }
40 //! }
41 //!
42 //! // `PartialOrd` needs to be implemented as well.
43 //! impl PartialOrd for State {
44 //!     fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
45 //!         Some(self.cmp(other))
46 //!     }
47 //! }
48 //!
49 //! // Each node is represented as a `usize`, for a shorter implementation.
50 //! struct Edge {
51 //!     node: usize,
52 //!     cost: usize,
53 //! }
54 //!
55 //! // Dijkstra's shortest path algorithm.
56 //!
57 //! // Start at `start` and use `dist` to track the current shortest distance
58 //! // to each node. This implementation isn't memory-efficient as it may leave duplicate
59 //! // nodes in the queue. It also uses `usize::MAX` as a sentinel value,
60 //! // for a simpler implementation.
61 //! fn shortest_path(adj_list: &Vec<Vec<Edge>>, start: usize, goal: usize) -> Option<usize> {
62 //!     // dist[node] = current shortest distance from `start` to `node`
63 //!     let mut dist: Vec<_> = (0..adj_list.len()).map(|_| usize::MAX).collect();
64 //!
65 //!     let mut heap = BinaryHeap::new();
66 //!
67 //!     // We're at `start`, with a zero cost
68 //!     dist[start] = 0;
69 //!     heap.push(State { cost: 0, position: start });
70 //!
71 //!     // Examine the frontier with lower cost nodes first (min-heap)
72 //!     while let Some(State { cost, position }) = heap.pop() {
73 //!         // Alternatively we could have continued to find all shortest paths
74 //!         if position == goal { return Some(cost); }
75 //!
76 //!         // Important as we may have already found a better way
77 //!         if cost > dist[position] { continue; }
78 //!
79 //!         // For each node we can reach, see if we can find a way with
80 //!         // a lower cost going through this node
81 //!         for edge in &adj_list[position] {
82 //!             let next = State { cost: cost + edge.cost, position: edge.node };
83 //!
84 //!             // If so, add it to the frontier and continue
85 //!             if next.cost < dist[next.position] {
86 //!                 heap.push(next);
87 //!                 // Relaxation, we have now found a better way
88 //!                 dist[next.position] = next.cost;
89 //!             }
90 //!         }
91 //!     }
92 //!
93 //!     // Goal not reachable
94 //!     None
95 //! }
96 //!
97 //! fn main() {
98 //!     // This is the directed graph we're going to use.
99 //!     // The node numbers correspond to the different states,
100 //!     // and the edge weights symbolize the cost of moving
101 //!     // from one node to another.
102 //!     // Note that the edges are one-way.
103 //!     //
104 //!     //                  7
105 //!     //          +-----------------+
106 //!     //          |                 |
107 //!     //          v   1        2    |  2
108 //!     //          0 -----> 1 -----> 3 ---> 4
109 //!     //          |        ^        ^      ^
110 //!     //          |        | 1      |      |
111 //!     //          |        |        | 3    | 1
112 //!     //          +------> 2 -------+      |
113 //!     //           10      |               |
114 //!     //                   +---------------+
115 //!     //
116 //!     // The graph is represented as an adjacency list where each index,
117 //!     // corresponding to a node value, has a list of outgoing edges.
118 //!     // Chosen for its efficiency.
119 //!     let graph = vec![
120 //!         // Node 0
121 //!         vec![Edge { node: 2, cost: 10 },
122 //!              Edge { node: 1, cost: 1 }],
123 //!         // Node 1
124 //!         vec![Edge { node: 3, cost: 2 }],
125 //!         // Node 2
126 //!         vec![Edge { node: 1, cost: 1 },
127 //!              Edge { node: 3, cost: 3 },
128 //!              Edge { node: 4, cost: 1 }],
129 //!         // Node 3
130 //!         vec![Edge { node: 0, cost: 7 },
131 //!              Edge { node: 4, cost: 2 }],
132 //!         // Node 4
133 //!         vec![]];
134 //!
135 //!     assert_eq!(shortest_path(&graph, 0, 1), Some(1));
136 //!     assert_eq!(shortest_path(&graph, 0, 3), Some(3));
137 //!     assert_eq!(shortest_path(&graph, 3, 0), Some(7));
138 //!     assert_eq!(shortest_path(&graph, 0, 4), Some(5));
139 //!     assert_eq!(shortest_path(&graph, 4, 0), None);
140 //! }
141 //! ```
142
143 #![allow(missing_docs)]
144 #![stable(feature = "rust1", since = "1.0.0")]
145
146 use core::fmt;
147 use core::iter::{FromIterator, FusedIterator, InPlaceIterable, SourceIter, TrustedLen};
148 use core::mem::{self, swap, ManuallyDrop};
149 use core::ops::{Deref, DerefMut};
150 use core::ptr;
151
152 use crate::collections::TryReserveError;
153 use crate::slice;
154 use crate::vec::{self, AsVecIntoIter, Vec};
155
156 use super::SpecExtend;
157
158 #[cfg(test)]
159 mod tests;
160
161 /// A priority queue implemented with a binary heap.
162 ///
163 /// This will be a max-heap.
164 ///
165 /// It is a logic error for an item to be modified in such a way that the
166 /// item's ordering relative to any other item, as determined by the [`Ord`]
167 /// trait, changes while it is in the heap. This is normally only possible
168 /// through [`Cell`], [`RefCell`], global state, I/O, or unsafe code. The
169 /// behavior resulting from such a logic error is not specified, but will
170 /// be encapsulated to the `BinaryHeap` that observed the logic error and not
171 /// result in undefined behavior. This could include panics, incorrect results,
172 /// aborts, memory leaks, and non-termination.
173 ///
174 /// # Examples
175 ///
176 /// ```
177 /// use std::collections::BinaryHeap;
178 ///
179 /// // Type inference lets us omit an explicit type signature (which
180 /// // would be `BinaryHeap<i32>` in this example).
181 /// let mut heap = BinaryHeap::new();
182 ///
183 /// // We can use peek to look at the next item in the heap. In this case,
184 /// // there's no items in there yet so we get None.
185 /// assert_eq!(heap.peek(), None);
186 ///
187 /// // Let's add some scores...
188 /// heap.push(1);
189 /// heap.push(5);
190 /// heap.push(2);
191 ///
192 /// // Now peek shows the most important item in the heap.
193 /// assert_eq!(heap.peek(), Some(&5));
194 ///
195 /// // We can check the length of a heap.
196 /// assert_eq!(heap.len(), 3);
197 ///
198 /// // We can iterate over the items in the heap, although they are returned in
199 /// // a random order.
200 /// for x in &heap {
201 ///     println!("{x}");
202 /// }
203 ///
204 /// // If we instead pop these scores, they should come back in order.
205 /// assert_eq!(heap.pop(), Some(5));
206 /// assert_eq!(heap.pop(), Some(2));
207 /// assert_eq!(heap.pop(), Some(1));
208 /// assert_eq!(heap.pop(), None);
209 ///
210 /// // We can clear the heap of any remaining items.
211 /// heap.clear();
212 ///
213 /// // The heap should now be empty.
214 /// assert!(heap.is_empty())
215 /// ```
216 ///
217 /// A `BinaryHeap` with a known list of items can be initialized from an array:
218 ///
219 /// ```
220 /// use std::collections::BinaryHeap;
221 ///
222 /// let heap = BinaryHeap::from([1, 5, 2]);
223 /// ```
224 ///
225 /// ## Min-heap
226 ///
227 /// Either [`core::cmp::Reverse`] or a custom [`Ord`] implementation can be used to
228 /// make `BinaryHeap` a min-heap. This makes `heap.pop()` return the smallest
229 /// value instead of the greatest one.
230 ///
231 /// ```
232 /// use std::collections::BinaryHeap;
233 /// use std::cmp::Reverse;
234 ///
235 /// let mut heap = BinaryHeap::new();
236 ///
237 /// // Wrap values in `Reverse`
238 /// heap.push(Reverse(1));
239 /// heap.push(Reverse(5));
240 /// heap.push(Reverse(2));
241 ///
242 /// // If we pop these scores now, they should come back in the reverse order.
243 /// assert_eq!(heap.pop(), Some(Reverse(1)));
244 /// assert_eq!(heap.pop(), Some(Reverse(2)));
245 /// assert_eq!(heap.pop(), Some(Reverse(5)));
246 /// assert_eq!(heap.pop(), None);
247 /// ```
248 ///
249 /// # Time complexity
250 ///
251 /// | [push]  | [pop]         | [peek]/[peek\_mut] |
252 /// |---------|---------------|--------------------|
253 /// | *O*(1)~ | *O*(log(*n*)) | *O*(1)             |
254 ///
255 /// The value for `push` is an expected cost; the method documentation gives a
256 /// more detailed analysis.
257 ///
258 /// [`core::cmp::Reverse`]: core::cmp::Reverse
259 /// [`Ord`]: core::cmp::Ord
260 /// [`Cell`]: core::cell::Cell
261 /// [`RefCell`]: core::cell::RefCell
262 /// [push]: BinaryHeap::push
263 /// [pop]: BinaryHeap::pop
264 /// [peek]: BinaryHeap::peek
265 /// [peek\_mut]: BinaryHeap::peek_mut
266 #[stable(feature = "rust1", since = "1.0.0")]
267 #[cfg_attr(not(test), rustc_diagnostic_item = "BinaryHeap")]
268 pub struct BinaryHeap<T> {
269     data: Vec<T>,
270 }
271
272 /// Structure wrapping a mutable reference to the greatest item on a
273 /// `BinaryHeap`.
274 ///
275 /// This `struct` is created by the [`peek_mut`] method on [`BinaryHeap`]. See
276 /// its documentation for more.
277 ///
278 /// [`peek_mut`]: BinaryHeap::peek_mut
279 #[stable(feature = "binary_heap_peek_mut", since = "1.12.0")]
280 pub struct PeekMut<'a, T: 'a + Ord> {
281     heap: &'a mut BinaryHeap<T>,
282     sift: bool,
283 }
284
285 #[stable(feature = "collection_debug", since = "1.17.0")]
286 impl<T: Ord + fmt::Debug> fmt::Debug for PeekMut<'_, T> {
287     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
288         f.debug_tuple("PeekMut").field(&self.heap.data[0]).finish()
289     }
290 }
291
292 #[stable(feature = "binary_heap_peek_mut", since = "1.12.0")]
293 impl<T: Ord> Drop for PeekMut<'_, T> {
294     fn drop(&mut self) {
295         if self.sift {
296             // SAFETY: PeekMut is only instantiated for non-empty heaps.
297             unsafe { self.heap.sift_down(0) };
298         }
299     }
300 }
301
302 #[stable(feature = "binary_heap_peek_mut", since = "1.12.0")]
303 impl<T: Ord> Deref for PeekMut<'_, T> {
304     type Target = T;
305     fn deref(&self) -> &T {
306         debug_assert!(!self.heap.is_empty());
307         // SAFE: PeekMut is only instantiated for non-empty heaps
308         unsafe { self.heap.data.get_unchecked(0) }
309     }
310 }
311
312 #[stable(feature = "binary_heap_peek_mut", since = "1.12.0")]
313 impl<T: Ord> DerefMut for PeekMut<'_, T> {
314     fn deref_mut(&mut self) -> &mut T {
315         debug_assert!(!self.heap.is_empty());
316         self.sift = true;
317         // SAFE: PeekMut is only instantiated for non-empty heaps
318         unsafe { self.heap.data.get_unchecked_mut(0) }
319     }
320 }
321
322 impl<'a, T: Ord> PeekMut<'a, T> {
323     /// Removes the peeked value from the heap and returns it.
324     #[stable(feature = "binary_heap_peek_mut_pop", since = "1.18.0")]
325     pub fn pop(mut this: PeekMut<'a, T>) -> T {
326         let value = this.heap.pop().unwrap();
327         this.sift = false;
328         value
329     }
330 }
331
332 #[stable(feature = "rust1", since = "1.0.0")]
333 impl<T: Clone> Clone for BinaryHeap<T> {
334     fn clone(&self) -> Self {
335         BinaryHeap { data: self.data.clone() }
336     }
337
338     fn clone_from(&mut self, source: &Self) {
339         self.data.clone_from(&source.data);
340     }
341 }
342
343 #[stable(feature = "rust1", since = "1.0.0")]
344 impl<T: Ord> Default for BinaryHeap<T> {
345     /// Creates an empty `BinaryHeap<T>`.
346     #[inline]
347     fn default() -> BinaryHeap<T> {
348         BinaryHeap::new()
349     }
350 }
351
352 #[stable(feature = "binaryheap_debug", since = "1.4.0")]
353 impl<T: fmt::Debug> fmt::Debug for BinaryHeap<T> {
354     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
355         f.debug_list().entries(self.iter()).finish()
356     }
357 }
358
359 impl<T: Ord> BinaryHeap<T> {
360     /// Creates an empty `BinaryHeap` as a max-heap.
361     ///
362     /// # Examples
363     ///
364     /// Basic usage:
365     ///
366     /// ```
367     /// use std::collections::BinaryHeap;
368     /// let mut heap = BinaryHeap::new();
369     /// heap.push(4);
370     /// ```
371     #[stable(feature = "rust1", since = "1.0.0")]
372     #[must_use]
373     pub fn new() -> BinaryHeap<T> {
374         BinaryHeap { data: vec![] }
375     }
376
377     /// Creates an empty `BinaryHeap` with a specific capacity.
378     /// This preallocates enough memory for `capacity` elements,
379     /// so that the `BinaryHeap` does not have to be reallocated
380     /// until it contains at least that many values.
381     ///
382     /// # Examples
383     ///
384     /// Basic usage:
385     ///
386     /// ```
387     /// use std::collections::BinaryHeap;
388     /// let mut heap = BinaryHeap::with_capacity(10);
389     /// heap.push(4);
390     /// ```
391     #[stable(feature = "rust1", since = "1.0.0")]
392     #[must_use]
393     pub fn with_capacity(capacity: usize) -> BinaryHeap<T> {
394         BinaryHeap { data: Vec::with_capacity(capacity) }
395     }
396
397     /// Returns a mutable reference to the greatest item in the binary heap, or
398     /// `None` if it is empty.
399     ///
400     /// Note: If the `PeekMut` value is leaked, the heap may be in an
401     /// inconsistent state.
402     ///
403     /// # Examples
404     ///
405     /// Basic usage:
406     ///
407     /// ```
408     /// use std::collections::BinaryHeap;
409     /// let mut heap = BinaryHeap::new();
410     /// assert!(heap.peek_mut().is_none());
411     ///
412     /// heap.push(1);
413     /// heap.push(5);
414     /// heap.push(2);
415     /// {
416     ///     let mut val = heap.peek_mut().unwrap();
417     ///     *val = 0;
418     /// }
419     /// assert_eq!(heap.peek(), Some(&2));
420     /// ```
421     ///
422     /// # Time complexity
423     ///
424     /// If the item is modified then the worst case time complexity is *O*(log(*n*)),
425     /// otherwise it's *O*(1).
426     #[stable(feature = "binary_heap_peek_mut", since = "1.12.0")]
427     pub fn peek_mut(&mut self) -> Option<PeekMut<'_, T>> {
428         if self.is_empty() { None } else { Some(PeekMut { heap: self, sift: false }) }
429     }
430
431     /// Removes the greatest item from the binary heap and returns it, or `None` if it
432     /// is empty.
433     ///
434     /// # Examples
435     ///
436     /// Basic usage:
437     ///
438     /// ```
439     /// use std::collections::BinaryHeap;
440     /// let mut heap = BinaryHeap::from([1, 3]);
441     ///
442     /// assert_eq!(heap.pop(), Some(3));
443     /// assert_eq!(heap.pop(), Some(1));
444     /// assert_eq!(heap.pop(), None);
445     /// ```
446     ///
447     /// # Time complexity
448     ///
449     /// The worst case cost of `pop` on a heap containing *n* elements is *O*(log(*n*)).
450     #[stable(feature = "rust1", since = "1.0.0")]
451     pub fn pop(&mut self) -> Option<T> {
452         self.data.pop().map(|mut item| {
453             if !self.is_empty() {
454                 swap(&mut item, &mut self.data[0]);
455                 // SAFETY: !self.is_empty() means that self.len() > 0
456                 unsafe { self.sift_down_to_bottom(0) };
457             }
458             item
459         })
460     }
461
462     /// Pushes an item onto the binary heap.
463     ///
464     /// # Examples
465     ///
466     /// Basic usage:
467     ///
468     /// ```
469     /// use std::collections::BinaryHeap;
470     /// let mut heap = BinaryHeap::new();
471     /// heap.push(3);
472     /// heap.push(5);
473     /// heap.push(1);
474     ///
475     /// assert_eq!(heap.len(), 3);
476     /// assert_eq!(heap.peek(), Some(&5));
477     /// ```
478     ///
479     /// # Time complexity
480     ///
481     /// The expected cost of `push`, averaged over every possible ordering of
482     /// the elements being pushed, and over a sufficiently large number of
483     /// pushes, is *O*(1). This is the most meaningful cost metric when pushing
484     /// elements that are *not* already in any sorted pattern.
485     ///
486     /// The time complexity degrades if elements are pushed in predominantly
487     /// ascending order. In the worst case, elements are pushed in ascending
488     /// sorted order and the amortized cost per push is *O*(log(*n*)) against a heap
489     /// containing *n* elements.
490     ///
491     /// The worst case cost of a *single* call to `push` is *O*(*n*). The worst case
492     /// occurs when capacity is exhausted and needs a resize. The resize cost
493     /// has been amortized in the previous figures.
494     #[stable(feature = "rust1", since = "1.0.0")]
495     pub fn push(&mut self, item: T) {
496         let old_len = self.len();
497         self.data.push(item);
498         // SAFETY: Since we pushed a new item it means that
499         //  old_len = self.len() - 1 < self.len()
500         unsafe { self.sift_up(0, old_len) };
501     }
502
503     /// Consumes the `BinaryHeap` and returns a vector in sorted
504     /// (ascending) order.
505     ///
506     /// # Examples
507     ///
508     /// Basic usage:
509     ///
510     /// ```
511     /// use std::collections::BinaryHeap;
512     ///
513     /// let mut heap = BinaryHeap::from([1, 2, 4, 5, 7]);
514     /// heap.push(6);
515     /// heap.push(3);
516     ///
517     /// let vec = heap.into_sorted_vec();
518     /// assert_eq!(vec, [1, 2, 3, 4, 5, 6, 7]);
519     /// ```
520     #[must_use = "`self` will be dropped if the result is not used"]
521     #[stable(feature = "binary_heap_extras_15", since = "1.5.0")]
522     pub fn into_sorted_vec(mut self) -> Vec<T> {
523         let mut end = self.len();
524         while end > 1 {
525             end -= 1;
526             // SAFETY: `end` goes from `self.len() - 1` to 1 (both included),
527             //  so it's always a valid index to access.
528             //  It is safe to access index 0 (i.e. `ptr`), because
529             //  1 <= end < self.len(), which means self.len() >= 2.
530             unsafe {
531                 let ptr = self.data.as_mut_ptr();
532                 ptr::swap(ptr, ptr.add(end));
533             }
534             // SAFETY: `end` goes from `self.len() - 1` to 1 (both included) so:
535             //  0 < 1 <= end <= self.len() - 1 < self.len()
536             //  Which means 0 < end and end < self.len().
537             unsafe { self.sift_down_range(0, end) };
538         }
539         self.into_vec()
540     }
541
542     // The implementations of sift_up and sift_down use unsafe blocks in
543     // order to move an element out of the vector (leaving behind a
544     // hole), shift along the others and move the removed element back into the
545     // vector at the final location of the hole.
546     // The `Hole` type is used to represent this, and make sure
547     // the hole is filled back at the end of its scope, even on panic.
548     // Using a hole reduces the constant factor compared to using swaps,
549     // which involves twice as many moves.
550
551     /// # Safety
552     ///
553     /// The caller must guarantee that `pos < self.len()`.
554     unsafe fn sift_up(&mut self, start: usize, pos: usize) -> usize {
555         // Take out the value at `pos` and create a hole.
556         // SAFETY: The caller guarantees that pos < self.len()
557         let mut hole = unsafe { Hole::new(&mut self.data, pos) };
558
559         while hole.pos() > start {
560             let parent = (hole.pos() - 1) / 2;
561
562             // SAFETY: hole.pos() > start >= 0, which means hole.pos() > 0
563             //  and so hole.pos() - 1 can't underflow.
564             //  This guarantees that parent < hole.pos() so
565             //  it's a valid index and also != hole.pos().
566             if hole.element() <= unsafe { hole.get(parent) } {
567                 break;
568             }
569
570             // SAFETY: Same as above
571             unsafe { hole.move_to(parent) };
572         }
573
574         hole.pos()
575     }
576
577     /// Take an element at `pos` and move it down the heap,
578     /// while its children are larger.
579     ///
580     /// # Safety
581     ///
582     /// The caller must guarantee that `pos < end <= self.len()`.
583     unsafe fn sift_down_range(&mut self, pos: usize, end: usize) {
584         // SAFETY: The caller guarantees that pos < end <= self.len().
585         let mut hole = unsafe { Hole::new(&mut self.data, pos) };
586         let mut child = 2 * hole.pos() + 1;
587
588         // Loop invariant: child == 2 * hole.pos() + 1.
589         while child <= end.saturating_sub(2) {
590             // compare with the greater of the two children
591             // SAFETY: child < end - 1 < self.len() and
592             //  child + 1 < end <= self.len(), so they're valid indexes.
593             //  child == 2 * hole.pos() + 1 != hole.pos() and
594             //  child + 1 == 2 * hole.pos() + 2 != hole.pos().
595             // FIXME: 2 * hole.pos() + 1 or 2 * hole.pos() + 2 could overflow
596             //  if T is a ZST
597             child += unsafe { hole.get(child) <= hole.get(child + 1) } as usize;
598
599             // if we are already in order, stop.
600             // SAFETY: child is now either the old child or the old child+1
601             //  We already proven that both are < self.len() and != hole.pos()
602             if hole.element() >= unsafe { hole.get(child) } {
603                 return;
604             }
605
606             // SAFETY: same as above.
607             unsafe { hole.move_to(child) };
608             child = 2 * hole.pos() + 1;
609         }
610
611         // SAFETY: && short circuit, which means that in the
612         //  second condition it's already true that child == end - 1 < self.len().
613         if child == end - 1 && hole.element() < unsafe { hole.get(child) } {
614             // SAFETY: child is already proven to be a valid index and
615             //  child == 2 * hole.pos() + 1 != hole.pos().
616             unsafe { hole.move_to(child) };
617         }
618     }
619
620     /// # Safety
621     ///
622     /// The caller must guarantee that `pos < self.len()`.
623     unsafe fn sift_down(&mut self, pos: usize) {
624         let len = self.len();
625         // SAFETY: pos < len is guaranteed by the caller and
626         //  obviously len = self.len() <= self.len().
627         unsafe { self.sift_down_range(pos, len) };
628     }
629
630     /// Take an element at `pos` and move it all the way down the heap,
631     /// then sift it up to its position.
632     ///
633     /// Note: This is faster when the element is known to be large / should
634     /// be closer to the bottom.
635     ///
636     /// # Safety
637     ///
638     /// The caller must guarantee that `pos < self.len()`.
639     unsafe fn sift_down_to_bottom(&mut self, mut pos: usize) {
640         let end = self.len();
641         let start = pos;
642
643         // SAFETY: The caller guarantees that pos < self.len().
644         let mut hole = unsafe { Hole::new(&mut self.data, pos) };
645         let mut child = 2 * hole.pos() + 1;
646
647         // Loop invariant: child == 2 * hole.pos() + 1.
648         while child <= end.saturating_sub(2) {
649             // SAFETY: child < end - 1 < self.len() and
650             //  child + 1 < end <= self.len(), so they're valid indexes.
651             //  child == 2 * hole.pos() + 1 != hole.pos() and
652             //  child + 1 == 2 * hole.pos() + 2 != hole.pos().
653             // FIXME: 2 * hole.pos() + 1 or 2 * hole.pos() + 2 could overflow
654             //  if T is a ZST
655             child += unsafe { hole.get(child) <= hole.get(child + 1) } as usize;
656
657             // SAFETY: Same as above
658             unsafe { hole.move_to(child) };
659             child = 2 * hole.pos() + 1;
660         }
661
662         if child == end - 1 {
663             // SAFETY: child == end - 1 < self.len(), so it's a valid index
664             //  and child == 2 * hole.pos() + 1 != hole.pos().
665             unsafe { hole.move_to(child) };
666         }
667         pos = hole.pos();
668         drop(hole);
669
670         // SAFETY: pos is the position in the hole and was already proven
671         //  to be a valid index.
672         unsafe { self.sift_up(start, pos) };
673     }
674
675     /// Rebuild assuming data[0..start] is still a proper heap.
676     fn rebuild_tail(&mut self, start: usize) {
677         if start == self.len() {
678             return;
679         }
680
681         let tail_len = self.len() - start;
682
683         #[inline(always)]
684         fn log2_fast(x: usize) -> usize {
685             (usize::BITS - x.leading_zeros() - 1) as usize
686         }
687
688         // `rebuild` takes O(self.len()) operations
689         // and about 2 * self.len() comparisons in the worst case
690         // while repeating `sift_up` takes O(tail_len * log(start)) operations
691         // and about 1 * tail_len * log_2(start) comparisons in the worst case,
692         // assuming start >= tail_len. For larger heaps, the crossover point
693         // no longer follows this reasoning and was determined empirically.
694         let better_to_rebuild = if start < tail_len {
695             true
696         } else if self.len() <= 2048 {
697             2 * self.len() < tail_len * log2_fast(start)
698         } else {
699             2 * self.len() < tail_len * 11
700         };
701
702         if better_to_rebuild {
703             self.rebuild();
704         } else {
705             for i in start..self.len() {
706                 // SAFETY: The index `i` is always less than self.len().
707                 unsafe { self.sift_up(0, i) };
708             }
709         }
710     }
711
712     fn rebuild(&mut self) {
713         let mut n = self.len() / 2;
714         while n > 0 {
715             n -= 1;
716             // SAFETY: n starts from self.len() / 2 and goes down to 0.
717             //  The only case when !(n < self.len()) is if
718             //  self.len() == 0, but it's ruled out by the loop condition.
719             unsafe { self.sift_down(n) };
720         }
721     }
722
723     /// Moves all the elements of `other` into `self`, leaving `other` empty.
724     ///
725     /// # Examples
726     ///
727     /// Basic usage:
728     ///
729     /// ```
730     /// use std::collections::BinaryHeap;
731     ///
732     /// let mut a = BinaryHeap::from([-10, 1, 2, 3, 3]);
733     /// let mut b = BinaryHeap::from([-20, 5, 43]);
734     ///
735     /// a.append(&mut b);
736     ///
737     /// assert_eq!(a.into_sorted_vec(), [-20, -10, 1, 2, 3, 3, 5, 43]);
738     /// assert!(b.is_empty());
739     /// ```
740     #[stable(feature = "binary_heap_append", since = "1.11.0")]
741     pub fn append(&mut self, other: &mut Self) {
742         if self.len() < other.len() {
743             swap(self, other);
744         }
745
746         let start = self.data.len();
747
748         self.data.append(&mut other.data);
749
750         self.rebuild_tail(start);
751     }
752
753     /// Clears the binary heap, returning an iterator over the removed elements
754     /// in heap order. If the iterator is dropped before being fully consumed,
755     /// it drops the remaining elements in heap order.
756     ///
757     /// The returned iterator keeps a mutable borrow on the heap to optimize
758     /// its implementation.
759     ///
760     /// Note:
761     /// * `.drain_sorted()` is *O*(*n* \* log(*n*)); much slower than `.drain()`.
762     ///   You should use the latter for most cases.
763     ///
764     /// # Examples
765     ///
766     /// Basic usage:
767     ///
768     /// ```
769     /// #![feature(binary_heap_drain_sorted)]
770     /// use std::collections::BinaryHeap;
771     ///
772     /// let mut heap = BinaryHeap::from([1, 2, 3, 4, 5]);
773     /// assert_eq!(heap.len(), 5);
774     ///
775     /// drop(heap.drain_sorted()); // removes all elements in heap order
776     /// assert_eq!(heap.len(), 0);
777     /// ```
778     #[inline]
779     #[unstable(feature = "binary_heap_drain_sorted", issue = "59278")]
780     pub fn drain_sorted(&mut self) -> DrainSorted<'_, T> {
781         DrainSorted { inner: self }
782     }
783
784     /// Retains only the elements specified by the predicate.
785     ///
786     /// In other words, remove all elements `e` for which `f(&e)` returns
787     /// `false`. The elements are visited in unsorted (and unspecified) order.
788     ///
789     /// # Examples
790     ///
791     /// Basic usage:
792     ///
793     /// ```
794     /// #![feature(binary_heap_retain)]
795     /// use std::collections::BinaryHeap;
796     ///
797     /// let mut heap = BinaryHeap::from([-10, -5, 1, 2, 4, 13]);
798     ///
799     /// heap.retain(|x| x % 2 == 0); // only keep even numbers
800     ///
801     /// assert_eq!(heap.into_sorted_vec(), [-10, 2, 4])
802     /// ```
803     #[unstable(feature = "binary_heap_retain", issue = "71503")]
804     pub fn retain<F>(&mut self, mut f: F)
805     where
806         F: FnMut(&T) -> bool,
807     {
808         let mut first_removed = self.len();
809         let mut i = 0;
810         self.data.retain(|e| {
811             let keep = f(e);
812             if !keep && i < first_removed {
813                 first_removed = i;
814             }
815             i += 1;
816             keep
817         });
818         // data[0..first_removed] is untouched, so we only need to rebuild the tail:
819         self.rebuild_tail(first_removed);
820     }
821 }
822
823 impl<T> BinaryHeap<T> {
824     /// Returns an iterator visiting all values in the underlying vector, in
825     /// arbitrary order.
826     ///
827     /// # Examples
828     ///
829     /// Basic usage:
830     ///
831     /// ```
832     /// use std::collections::BinaryHeap;
833     /// let heap = BinaryHeap::from([1, 2, 3, 4]);
834     ///
835     /// // Print 1, 2, 3, 4 in arbitrary order
836     /// for x in heap.iter() {
837     ///     println!("{x}");
838     /// }
839     /// ```
840     #[stable(feature = "rust1", since = "1.0.0")]
841     pub fn iter(&self) -> Iter<'_, T> {
842         Iter { iter: self.data.iter() }
843     }
844
845     /// Returns an iterator which retrieves elements in heap order.
846     /// This method consumes the original heap.
847     ///
848     /// # Examples
849     ///
850     /// Basic usage:
851     ///
852     /// ```
853     /// #![feature(binary_heap_into_iter_sorted)]
854     /// use std::collections::BinaryHeap;
855     /// let heap = BinaryHeap::from([1, 2, 3, 4, 5]);
856     ///
857     /// assert_eq!(heap.into_iter_sorted().take(2).collect::<Vec<_>>(), [5, 4]);
858     /// ```
859     #[unstable(feature = "binary_heap_into_iter_sorted", issue = "59278")]
860     pub fn into_iter_sorted(self) -> IntoIterSorted<T> {
861         IntoIterSorted { inner: self }
862     }
863
864     /// Returns the greatest item in the binary heap, or `None` if it is empty.
865     ///
866     /// # Examples
867     ///
868     /// Basic usage:
869     ///
870     /// ```
871     /// use std::collections::BinaryHeap;
872     /// let mut heap = BinaryHeap::new();
873     /// assert_eq!(heap.peek(), None);
874     ///
875     /// heap.push(1);
876     /// heap.push(5);
877     /// heap.push(2);
878     /// assert_eq!(heap.peek(), Some(&5));
879     ///
880     /// ```
881     ///
882     /// # Time complexity
883     ///
884     /// Cost is *O*(1) in the worst case.
885     #[must_use]
886     #[stable(feature = "rust1", since = "1.0.0")]
887     pub fn peek(&self) -> Option<&T> {
888         self.data.get(0)
889     }
890
891     /// Returns the number of elements the binary heap can hold without reallocating.
892     ///
893     /// # Examples
894     ///
895     /// Basic usage:
896     ///
897     /// ```
898     /// use std::collections::BinaryHeap;
899     /// let mut heap = BinaryHeap::with_capacity(100);
900     /// assert!(heap.capacity() >= 100);
901     /// heap.push(4);
902     /// ```
903     #[must_use]
904     #[stable(feature = "rust1", since = "1.0.0")]
905     pub fn capacity(&self) -> usize {
906         self.data.capacity()
907     }
908
909     /// Reserves the minimum capacity for exactly `additional` more elements to be inserted in the
910     /// given `BinaryHeap`. Does nothing if the capacity is already sufficient.
911     ///
912     /// Note that the allocator may give the collection more space than it requests. Therefore
913     /// capacity can not be relied upon to be precisely minimal. Prefer [`reserve`] if future
914     /// insertions are expected.
915     ///
916     /// # Panics
917     ///
918     /// Panics if the new capacity overflows `usize`.
919     ///
920     /// # Examples
921     ///
922     /// Basic usage:
923     ///
924     /// ```
925     /// use std::collections::BinaryHeap;
926     /// let mut heap = BinaryHeap::new();
927     /// heap.reserve_exact(100);
928     /// assert!(heap.capacity() >= 100);
929     /// heap.push(4);
930     /// ```
931     ///
932     /// [`reserve`]: BinaryHeap::reserve
933     #[stable(feature = "rust1", since = "1.0.0")]
934     pub fn reserve_exact(&mut self, additional: usize) {
935         self.data.reserve_exact(additional);
936     }
937
938     /// Reserves capacity for at least `additional` more elements to be inserted in the
939     /// `BinaryHeap`. The collection may reserve more space to avoid frequent reallocations.
940     ///
941     /// # Panics
942     ///
943     /// Panics if the new capacity overflows `usize`.
944     ///
945     /// # Examples
946     ///
947     /// Basic usage:
948     ///
949     /// ```
950     /// use std::collections::BinaryHeap;
951     /// let mut heap = BinaryHeap::new();
952     /// heap.reserve(100);
953     /// assert!(heap.capacity() >= 100);
954     /// heap.push(4);
955     /// ```
956     #[stable(feature = "rust1", since = "1.0.0")]
957     pub fn reserve(&mut self, additional: usize) {
958         self.data.reserve(additional);
959     }
960
961     /// Tries to reserve the minimum capacity for exactly `additional`
962     /// elements to be inserted in the given `BinaryHeap<T>`. After calling
963     /// `try_reserve_exact`, capacity will be greater than or equal to
964     /// `self.len() + additional` if it returns `Ok(())`.
965     /// Does nothing if the capacity is already sufficient.
966     ///
967     /// Note that the allocator may give the collection more space than it
968     /// requests. Therefore, capacity can not be relied upon to be precisely
969     /// minimal. Prefer [`try_reserve`] if future insertions are expected.
970     ///
971     /// [`try_reserve`]: BinaryHeap::try_reserve
972     ///
973     /// # Errors
974     ///
975     /// If the capacity overflows, or the allocator reports a failure, then an error
976     /// is returned.
977     ///
978     /// # Examples
979     ///
980     /// ```
981     /// use std::collections::BinaryHeap;
982     /// use std::collections::TryReserveError;
983     ///
984     /// fn find_max_slow(data: &[u32]) -> Result<Option<u32>, TryReserveError> {
985     ///     let mut heap = BinaryHeap::new();
986     ///
987     ///     // Pre-reserve the memory, exiting if we can't
988     ///     heap.try_reserve_exact(data.len())?;
989     ///
990     ///     // Now we know this can't OOM in the middle of our complex work
991     ///     heap.extend(data.iter());
992     ///
993     ///     Ok(heap.pop())
994     /// }
995     /// # find_max_slow(&[1, 2, 3]).expect("why is the test harness OOMing on 12 bytes?");
996     /// ```
997     #[stable(feature = "try_reserve_2", since = "1.63.0")]
998     pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError> {
999         self.data.try_reserve_exact(additional)
1000     }
1001
1002     /// Tries to reserve capacity for at least `additional` more elements to be inserted
1003     /// in the given `BinaryHeap<T>`. The collection may reserve more space to avoid
1004     /// frequent reallocations. After calling `try_reserve`, capacity will be
1005     /// greater than or equal to `self.len() + additional`. Does nothing if
1006     /// capacity is already sufficient.
1007     ///
1008     /// # Errors
1009     ///
1010     /// If the capacity overflows, or the allocator reports a failure, then an error
1011     /// is returned.
1012     ///
1013     /// # Examples
1014     ///
1015     /// ```
1016     /// use std::collections::BinaryHeap;
1017     /// use std::collections::TryReserveError;
1018     ///
1019     /// fn find_max_slow(data: &[u32]) -> Result<Option<u32>, TryReserveError> {
1020     ///     let mut heap = BinaryHeap::new();
1021     ///
1022     ///     // Pre-reserve the memory, exiting if we can't
1023     ///     heap.try_reserve(data.len())?;
1024     ///
1025     ///     // Now we know this can't OOM in the middle of our complex work
1026     ///     heap.extend(data.iter());
1027     ///
1028     ///     Ok(heap.pop())
1029     /// }
1030     /// # find_max_slow(&[1, 2, 3]).expect("why is the test harness OOMing on 12 bytes?");
1031     /// ```
1032     #[stable(feature = "try_reserve_2", since = "1.63.0")]
1033     pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
1034         self.data.try_reserve(additional)
1035     }
1036
1037     /// Discards as much additional capacity as possible.
1038     ///
1039     /// # Examples
1040     ///
1041     /// Basic usage:
1042     ///
1043     /// ```
1044     /// use std::collections::BinaryHeap;
1045     /// let mut heap: BinaryHeap<i32> = BinaryHeap::with_capacity(100);
1046     ///
1047     /// assert!(heap.capacity() >= 100);
1048     /// heap.shrink_to_fit();
1049     /// assert!(heap.capacity() == 0);
1050     /// ```
1051     #[stable(feature = "rust1", since = "1.0.0")]
1052     pub fn shrink_to_fit(&mut self) {
1053         self.data.shrink_to_fit();
1054     }
1055
1056     /// Discards capacity with a lower bound.
1057     ///
1058     /// The capacity will remain at least as large as both the length
1059     /// and the supplied value.
1060     ///
1061     /// If the current capacity is less than the lower limit, this is a no-op.
1062     ///
1063     /// # Examples
1064     ///
1065     /// ```
1066     /// use std::collections::BinaryHeap;
1067     /// let mut heap: BinaryHeap<i32> = BinaryHeap::with_capacity(100);
1068     ///
1069     /// assert!(heap.capacity() >= 100);
1070     /// heap.shrink_to(10);
1071     /// assert!(heap.capacity() >= 10);
1072     /// ```
1073     #[inline]
1074     #[stable(feature = "shrink_to", since = "1.56.0")]
1075     pub fn shrink_to(&mut self, min_capacity: usize) {
1076         self.data.shrink_to(min_capacity)
1077     }
1078
1079     /// Returns a slice of all values in the underlying vector, in arbitrary
1080     /// order.
1081     ///
1082     /// # Examples
1083     ///
1084     /// Basic usage:
1085     ///
1086     /// ```
1087     /// #![feature(binary_heap_as_slice)]
1088     /// use std::collections::BinaryHeap;
1089     /// use std::io::{self, Write};
1090     ///
1091     /// let heap = BinaryHeap::from([1, 2, 3, 4, 5, 6, 7]);
1092     ///
1093     /// io::sink().write(heap.as_slice()).unwrap();
1094     /// ```
1095     #[must_use]
1096     #[unstable(feature = "binary_heap_as_slice", issue = "83659")]
1097     pub fn as_slice(&self) -> &[T] {
1098         self.data.as_slice()
1099     }
1100
1101     /// Consumes the `BinaryHeap` and returns the underlying vector
1102     /// in arbitrary order.
1103     ///
1104     /// # Examples
1105     ///
1106     /// Basic usage:
1107     ///
1108     /// ```
1109     /// use std::collections::BinaryHeap;
1110     /// let heap = BinaryHeap::from([1, 2, 3, 4, 5, 6, 7]);
1111     /// let vec = heap.into_vec();
1112     ///
1113     /// // Will print in some order
1114     /// for x in vec {
1115     ///     println!("{x}");
1116     /// }
1117     /// ```
1118     #[must_use = "`self` will be dropped if the result is not used"]
1119     #[stable(feature = "binary_heap_extras_15", since = "1.5.0")]
1120     pub fn into_vec(self) -> Vec<T> {
1121         self.into()
1122     }
1123
1124     /// Returns the length of the binary heap.
1125     ///
1126     /// # Examples
1127     ///
1128     /// Basic usage:
1129     ///
1130     /// ```
1131     /// use std::collections::BinaryHeap;
1132     /// let heap = BinaryHeap::from([1, 3]);
1133     ///
1134     /// assert_eq!(heap.len(), 2);
1135     /// ```
1136     #[must_use]
1137     #[stable(feature = "rust1", since = "1.0.0")]
1138     pub fn len(&self) -> usize {
1139         self.data.len()
1140     }
1141
1142     /// Checks if the binary heap is empty.
1143     ///
1144     /// # Examples
1145     ///
1146     /// Basic usage:
1147     ///
1148     /// ```
1149     /// use std::collections::BinaryHeap;
1150     /// let mut heap = BinaryHeap::new();
1151     ///
1152     /// assert!(heap.is_empty());
1153     ///
1154     /// heap.push(3);
1155     /// heap.push(5);
1156     /// heap.push(1);
1157     ///
1158     /// assert!(!heap.is_empty());
1159     /// ```
1160     #[must_use]
1161     #[stable(feature = "rust1", since = "1.0.0")]
1162     pub fn is_empty(&self) -> bool {
1163         self.len() == 0
1164     }
1165
1166     /// Clears the binary heap, returning an iterator over the removed elements
1167     /// in arbitrary order. If the iterator is dropped before being fully
1168     /// consumed, it drops the remaining elements in arbitrary order.
1169     ///
1170     /// The returned iterator keeps a mutable borrow on the heap to optimize
1171     /// its implementation.
1172     ///
1173     /// # Examples
1174     ///
1175     /// Basic usage:
1176     ///
1177     /// ```
1178     /// use std::collections::BinaryHeap;
1179     /// let mut heap = BinaryHeap::from([1, 3]);
1180     ///
1181     /// assert!(!heap.is_empty());
1182     ///
1183     /// for x in heap.drain() {
1184     ///     println!("{x}");
1185     /// }
1186     ///
1187     /// assert!(heap.is_empty());
1188     /// ```
1189     #[inline]
1190     #[stable(feature = "drain", since = "1.6.0")]
1191     pub fn drain(&mut self) -> Drain<'_, T> {
1192         Drain { iter: self.data.drain(..) }
1193     }
1194
1195     /// Drops all items from the binary heap.
1196     ///
1197     /// # Examples
1198     ///
1199     /// Basic usage:
1200     ///
1201     /// ```
1202     /// use std::collections::BinaryHeap;
1203     /// let mut heap = BinaryHeap::from([1, 3]);
1204     ///
1205     /// assert!(!heap.is_empty());
1206     ///
1207     /// heap.clear();
1208     ///
1209     /// assert!(heap.is_empty());
1210     /// ```
1211     #[stable(feature = "rust1", since = "1.0.0")]
1212     pub fn clear(&mut self) {
1213         self.drain();
1214     }
1215 }
1216
1217 /// Hole represents a hole in a slice i.e., an index without valid value
1218 /// (because it was moved from or duplicated).
1219 /// In drop, `Hole` will restore the slice by filling the hole
1220 /// position with the value that was originally removed.
1221 struct Hole<'a, T: 'a> {
1222     data: &'a mut [T],
1223     elt: ManuallyDrop<T>,
1224     pos: usize,
1225 }
1226
1227 impl<'a, T> Hole<'a, T> {
1228     /// Create a new `Hole` at index `pos`.
1229     ///
1230     /// Unsafe because pos must be within the data slice.
1231     #[inline]
1232     unsafe fn new(data: &'a mut [T], pos: usize) -> Self {
1233         debug_assert!(pos < data.len());
1234         // SAFE: pos should be inside the slice
1235         let elt = unsafe { ptr::read(data.get_unchecked(pos)) };
1236         Hole { data, elt: ManuallyDrop::new(elt), pos }
1237     }
1238
1239     #[inline]
1240     fn pos(&self) -> usize {
1241         self.pos
1242     }
1243
1244     /// Returns a reference to the element removed.
1245     #[inline]
1246     fn element(&self) -> &T {
1247         &self.elt
1248     }
1249
1250     /// Returns a reference to the element at `index`.
1251     ///
1252     /// Unsafe because index must be within the data slice and not equal to pos.
1253     #[inline]
1254     unsafe fn get(&self, index: usize) -> &T {
1255         debug_assert!(index != self.pos);
1256         debug_assert!(index < self.data.len());
1257         unsafe { self.data.get_unchecked(index) }
1258     }
1259
1260     /// Move hole to new location
1261     ///
1262     /// Unsafe because index must be within the data slice and not equal to pos.
1263     #[inline]
1264     unsafe fn move_to(&mut self, index: usize) {
1265         debug_assert!(index != self.pos);
1266         debug_assert!(index < self.data.len());
1267         unsafe {
1268             let ptr = self.data.as_mut_ptr();
1269             let index_ptr: *const _ = ptr.add(index);
1270             let hole_ptr = ptr.add(self.pos);
1271             ptr::copy_nonoverlapping(index_ptr, hole_ptr, 1);
1272         }
1273         self.pos = index;
1274     }
1275 }
1276
1277 impl<T> Drop for Hole<'_, T> {
1278     #[inline]
1279     fn drop(&mut self) {
1280         // fill the hole again
1281         unsafe {
1282             let pos = self.pos;
1283             ptr::copy_nonoverlapping(&*self.elt, self.data.get_unchecked_mut(pos), 1);
1284         }
1285     }
1286 }
1287
1288 /// An iterator over the elements of a `BinaryHeap`.
1289 ///
1290 /// This `struct` is created by [`BinaryHeap::iter()`]. See its
1291 /// documentation for more.
1292 ///
1293 /// [`iter`]: BinaryHeap::iter
1294 #[must_use = "iterators are lazy and do nothing unless consumed"]
1295 #[stable(feature = "rust1", since = "1.0.0")]
1296 pub struct Iter<'a, T: 'a> {
1297     iter: slice::Iter<'a, T>,
1298 }
1299
1300 #[stable(feature = "collection_debug", since = "1.17.0")]
1301 impl<T: fmt::Debug> fmt::Debug for Iter<'_, T> {
1302     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1303         f.debug_tuple("Iter").field(&self.iter.as_slice()).finish()
1304     }
1305 }
1306
1307 // FIXME(#26925) Remove in favor of `#[derive(Clone)]`
1308 #[stable(feature = "rust1", since = "1.0.0")]
1309 impl<T> Clone for Iter<'_, T> {
1310     fn clone(&self) -> Self {
1311         Iter { iter: self.iter.clone() }
1312     }
1313 }
1314
1315 #[stable(feature = "rust1", since = "1.0.0")]
1316 impl<'a, T> Iterator for Iter<'a, T> {
1317     type Item = &'a T;
1318
1319     #[inline]
1320     fn next(&mut self) -> Option<&'a T> {
1321         self.iter.next()
1322     }
1323
1324     #[inline]
1325     fn size_hint(&self) -> (usize, Option<usize>) {
1326         self.iter.size_hint()
1327     }
1328
1329     #[inline]
1330     fn last(self) -> Option<&'a T> {
1331         self.iter.last()
1332     }
1333 }
1334
1335 #[stable(feature = "rust1", since = "1.0.0")]
1336 impl<'a, T> DoubleEndedIterator for Iter<'a, T> {
1337     #[inline]
1338     fn next_back(&mut self) -> Option<&'a T> {
1339         self.iter.next_back()
1340     }
1341 }
1342
1343 #[stable(feature = "rust1", since = "1.0.0")]
1344 impl<T> ExactSizeIterator for Iter<'_, T> {
1345     fn is_empty(&self) -> bool {
1346         self.iter.is_empty()
1347     }
1348 }
1349
1350 #[stable(feature = "fused", since = "1.26.0")]
1351 impl<T> FusedIterator for Iter<'_, T> {}
1352
1353 /// An owning iterator over the elements of a `BinaryHeap`.
1354 ///
1355 /// This `struct` is created by [`BinaryHeap::into_iter()`]
1356 /// (provided by the [`IntoIterator`] trait). See its documentation for more.
1357 ///
1358 /// [`into_iter`]: BinaryHeap::into_iter
1359 /// [`IntoIterator`]: core::iter::IntoIterator
1360 #[stable(feature = "rust1", since = "1.0.0")]
1361 #[derive(Clone)]
1362 pub struct IntoIter<T> {
1363     iter: vec::IntoIter<T>,
1364 }
1365
1366 #[stable(feature = "collection_debug", since = "1.17.0")]
1367 impl<T: fmt::Debug> fmt::Debug for IntoIter<T> {
1368     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1369         f.debug_tuple("IntoIter").field(&self.iter.as_slice()).finish()
1370     }
1371 }
1372
1373 #[stable(feature = "rust1", since = "1.0.0")]
1374 impl<T> Iterator for IntoIter<T> {
1375     type Item = T;
1376
1377     #[inline]
1378     fn next(&mut self) -> Option<T> {
1379         self.iter.next()
1380     }
1381
1382     #[inline]
1383     fn size_hint(&self) -> (usize, Option<usize>) {
1384         self.iter.size_hint()
1385     }
1386 }
1387
1388 #[stable(feature = "rust1", since = "1.0.0")]
1389 impl<T> DoubleEndedIterator for IntoIter<T> {
1390     #[inline]
1391     fn next_back(&mut self) -> Option<T> {
1392         self.iter.next_back()
1393     }
1394 }
1395
1396 #[stable(feature = "rust1", since = "1.0.0")]
1397 impl<T> ExactSizeIterator for IntoIter<T> {
1398     fn is_empty(&self) -> bool {
1399         self.iter.is_empty()
1400     }
1401 }
1402
1403 #[stable(feature = "fused", since = "1.26.0")]
1404 impl<T> FusedIterator for IntoIter<T> {}
1405
1406 // In addition to the SAFETY invariants of the following three unsafe traits
1407 // also refer to the vec::in_place_collect module documentation to get an overview
1408 #[unstable(issue = "none", feature = "inplace_iteration")]
1409 #[doc(hidden)]
1410 unsafe impl<T> SourceIter for IntoIter<T> {
1411     type Source = IntoIter<T>;
1412
1413     #[inline]
1414     unsafe fn as_inner(&mut self) -> &mut Self::Source {
1415         self
1416     }
1417 }
1418
1419 #[unstable(issue = "none", feature = "inplace_iteration")]
1420 #[doc(hidden)]
1421 unsafe impl<I> InPlaceIterable for IntoIter<I> {}
1422
1423 unsafe impl<I> AsVecIntoIter for IntoIter<I> {
1424     type Item = I;
1425
1426     fn as_into_iter(&mut self) -> &mut vec::IntoIter<Self::Item> {
1427         &mut self.iter
1428     }
1429 }
1430
1431 #[must_use = "iterators are lazy and do nothing unless consumed"]
1432 #[unstable(feature = "binary_heap_into_iter_sorted", issue = "59278")]
1433 #[derive(Clone, Debug)]
1434 pub struct IntoIterSorted<T> {
1435     inner: BinaryHeap<T>,
1436 }
1437
1438 #[unstable(feature = "binary_heap_into_iter_sorted", issue = "59278")]
1439 impl<T: Ord> Iterator for IntoIterSorted<T> {
1440     type Item = T;
1441
1442     #[inline]
1443     fn next(&mut self) -> Option<T> {
1444         self.inner.pop()
1445     }
1446
1447     #[inline]
1448     fn size_hint(&self) -> (usize, Option<usize>) {
1449         let exact = self.inner.len();
1450         (exact, Some(exact))
1451     }
1452 }
1453
1454 #[unstable(feature = "binary_heap_into_iter_sorted", issue = "59278")]
1455 impl<T: Ord> ExactSizeIterator for IntoIterSorted<T> {}
1456
1457 #[unstable(feature = "binary_heap_into_iter_sorted", issue = "59278")]
1458 impl<T: Ord> FusedIterator for IntoIterSorted<T> {}
1459
1460 #[unstable(feature = "trusted_len", issue = "37572")]
1461 unsafe impl<T: Ord> TrustedLen for IntoIterSorted<T> {}
1462
1463 /// A draining iterator over the elements of a `BinaryHeap`.
1464 ///
1465 /// This `struct` is created by [`BinaryHeap::drain()`]. See its
1466 /// documentation for more.
1467 ///
1468 /// [`drain`]: BinaryHeap::drain
1469 #[stable(feature = "drain", since = "1.6.0")]
1470 #[derive(Debug)]
1471 pub struct Drain<'a, T: 'a> {
1472     iter: vec::Drain<'a, T>,
1473 }
1474
1475 #[stable(feature = "drain", since = "1.6.0")]
1476 impl<T> Iterator for Drain<'_, T> {
1477     type Item = T;
1478
1479     #[inline]
1480     fn next(&mut self) -> Option<T> {
1481         self.iter.next()
1482     }
1483
1484     #[inline]
1485     fn size_hint(&self) -> (usize, Option<usize>) {
1486         self.iter.size_hint()
1487     }
1488 }
1489
1490 #[stable(feature = "drain", since = "1.6.0")]
1491 impl<T> DoubleEndedIterator for Drain<'_, T> {
1492     #[inline]
1493     fn next_back(&mut self) -> Option<T> {
1494         self.iter.next_back()
1495     }
1496 }
1497
1498 #[stable(feature = "drain", since = "1.6.0")]
1499 impl<T> ExactSizeIterator for Drain<'_, T> {
1500     fn is_empty(&self) -> bool {
1501         self.iter.is_empty()
1502     }
1503 }
1504
1505 #[stable(feature = "fused", since = "1.26.0")]
1506 impl<T> FusedIterator for Drain<'_, T> {}
1507
1508 /// A draining iterator over the elements of a `BinaryHeap`.
1509 ///
1510 /// This `struct` is created by [`BinaryHeap::drain_sorted()`]. See its
1511 /// documentation for more.
1512 ///
1513 /// [`drain_sorted`]: BinaryHeap::drain_sorted
1514 #[unstable(feature = "binary_heap_drain_sorted", issue = "59278")]
1515 #[derive(Debug)]
1516 pub struct DrainSorted<'a, T: Ord> {
1517     inner: &'a mut BinaryHeap<T>,
1518 }
1519
1520 #[unstable(feature = "binary_heap_drain_sorted", issue = "59278")]
1521 impl<'a, T: Ord> Drop for DrainSorted<'a, T> {
1522     /// Removes heap elements in heap order.
1523     fn drop(&mut self) {
1524         struct DropGuard<'r, 'a, T: Ord>(&'r mut DrainSorted<'a, T>);
1525
1526         impl<'r, 'a, T: Ord> Drop for DropGuard<'r, 'a, T> {
1527             fn drop(&mut self) {
1528                 while self.0.inner.pop().is_some() {}
1529             }
1530         }
1531
1532         while let Some(item) = self.inner.pop() {
1533             let guard = DropGuard(self);
1534             drop(item);
1535             mem::forget(guard);
1536         }
1537     }
1538 }
1539
1540 #[unstable(feature = "binary_heap_drain_sorted", issue = "59278")]
1541 impl<T: Ord> Iterator for DrainSorted<'_, T> {
1542     type Item = T;
1543
1544     #[inline]
1545     fn next(&mut self) -> Option<T> {
1546         self.inner.pop()
1547     }
1548
1549     #[inline]
1550     fn size_hint(&self) -> (usize, Option<usize>) {
1551         let exact = self.inner.len();
1552         (exact, Some(exact))
1553     }
1554 }
1555
1556 #[unstable(feature = "binary_heap_drain_sorted", issue = "59278")]
1557 impl<T: Ord> ExactSizeIterator for DrainSorted<'_, T> {}
1558
1559 #[unstable(feature = "binary_heap_drain_sorted", issue = "59278")]
1560 impl<T: Ord> FusedIterator for DrainSorted<'_, T> {}
1561
1562 #[unstable(feature = "trusted_len", issue = "37572")]
1563 unsafe impl<T: Ord> TrustedLen for DrainSorted<'_, T> {}
1564
1565 #[stable(feature = "binary_heap_extras_15", since = "1.5.0")]
1566 impl<T: Ord> From<Vec<T>> for BinaryHeap<T> {
1567     /// Converts a `Vec<T>` into a `BinaryHeap<T>`.
1568     ///
1569     /// This conversion happens in-place, and has *O*(*n*) time complexity.
1570     fn from(vec: Vec<T>) -> BinaryHeap<T> {
1571         let mut heap = BinaryHeap { data: vec };
1572         heap.rebuild();
1573         heap
1574     }
1575 }
1576
1577 #[stable(feature = "std_collections_from_array", since = "1.56.0")]
1578 impl<T: Ord, const N: usize> From<[T; N]> for BinaryHeap<T> {
1579     /// ```
1580     /// use std::collections::BinaryHeap;
1581     ///
1582     /// let mut h1 = BinaryHeap::from([1, 4, 2, 3]);
1583     /// let mut h2: BinaryHeap<_> = [1, 4, 2, 3].into();
1584     /// while let Some((a, b)) = h1.pop().zip(h2.pop()) {
1585     ///     assert_eq!(a, b);
1586     /// }
1587     /// ```
1588     fn from(arr: [T; N]) -> Self {
1589         Self::from_iter(arr)
1590     }
1591 }
1592
1593 #[stable(feature = "binary_heap_extras_15", since = "1.5.0")]
1594 impl<T> From<BinaryHeap<T>> for Vec<T> {
1595     /// Converts a `BinaryHeap<T>` into a `Vec<T>`.
1596     ///
1597     /// This conversion requires no data movement or allocation, and has
1598     /// constant time complexity.
1599     fn from(heap: BinaryHeap<T>) -> Vec<T> {
1600         heap.data
1601     }
1602 }
1603
1604 #[stable(feature = "rust1", since = "1.0.0")]
1605 impl<T: Ord> FromIterator<T> for BinaryHeap<T> {
1606     fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> BinaryHeap<T> {
1607         BinaryHeap::from(iter.into_iter().collect::<Vec<_>>())
1608     }
1609 }
1610
1611 #[stable(feature = "rust1", since = "1.0.0")]
1612 impl<T> IntoIterator for BinaryHeap<T> {
1613     type Item = T;
1614     type IntoIter = IntoIter<T>;
1615
1616     /// Creates a consuming iterator, that is, one that moves each value out of
1617     /// the binary heap in arbitrary order. The binary heap cannot be used
1618     /// after calling this.
1619     ///
1620     /// # Examples
1621     ///
1622     /// Basic usage:
1623     ///
1624     /// ```
1625     /// use std::collections::BinaryHeap;
1626     /// let heap = BinaryHeap::from([1, 2, 3, 4]);
1627     ///
1628     /// // Print 1, 2, 3, 4 in arbitrary order
1629     /// for x in heap.into_iter() {
1630     ///     // x has type i32, not &i32
1631     ///     println!("{x}");
1632     /// }
1633     /// ```
1634     fn into_iter(self) -> IntoIter<T> {
1635         IntoIter { iter: self.data.into_iter() }
1636     }
1637 }
1638
1639 #[stable(feature = "rust1", since = "1.0.0")]
1640 impl<'a, T> IntoIterator for &'a BinaryHeap<T> {
1641     type Item = &'a T;
1642     type IntoIter = Iter<'a, T>;
1643
1644     fn into_iter(self) -> Iter<'a, T> {
1645         self.iter()
1646     }
1647 }
1648
1649 #[stable(feature = "rust1", since = "1.0.0")]
1650 impl<T: Ord> Extend<T> for BinaryHeap<T> {
1651     #[inline]
1652     fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
1653         <Self as SpecExtend<I>>::spec_extend(self, iter);
1654     }
1655
1656     #[inline]
1657     fn extend_one(&mut self, item: T) {
1658         self.push(item);
1659     }
1660
1661     #[inline]
1662     fn extend_reserve(&mut self, additional: usize) {
1663         self.reserve(additional);
1664     }
1665 }
1666
1667 impl<T: Ord, I: IntoIterator<Item = T>> SpecExtend<I> for BinaryHeap<T> {
1668     default fn spec_extend(&mut self, iter: I) {
1669         self.extend_desugared(iter.into_iter());
1670     }
1671 }
1672
1673 impl<T: Ord> SpecExtend<Vec<T>> for BinaryHeap<T> {
1674     fn spec_extend(&mut self, ref mut other: Vec<T>) {
1675         let start = self.data.len();
1676         self.data.append(other);
1677         self.rebuild_tail(start);
1678     }
1679 }
1680
1681 impl<T: Ord> SpecExtend<BinaryHeap<T>> for BinaryHeap<T> {
1682     fn spec_extend(&mut self, ref mut other: BinaryHeap<T>) {
1683         self.append(other);
1684     }
1685 }
1686
1687 impl<T: Ord> BinaryHeap<T> {
1688     fn extend_desugared<I: IntoIterator<Item = T>>(&mut self, iter: I) {
1689         let iterator = iter.into_iter();
1690         let (lower, _) = iterator.size_hint();
1691
1692         self.reserve(lower);
1693
1694         iterator.for_each(move |elem| self.push(elem));
1695     }
1696 }
1697
1698 #[stable(feature = "extend_ref", since = "1.2.0")]
1699 impl<'a, T: 'a + Ord + Copy> Extend<&'a T> for BinaryHeap<T> {
1700     fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) {
1701         self.extend(iter.into_iter().cloned());
1702     }
1703
1704     #[inline]
1705     fn extend_one(&mut self, &item: &'a T) {
1706         self.push(item);
1707     }
1708
1709     #[inline]
1710     fn extend_reserve(&mut self, additional: usize) {
1711         self.reserve(additional);
1712     }
1713 }