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