diff options
| author | Dylan DPC <dylan.dpc@gmail.com> | 2020-04-25 01:35:59 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-04-25 01:35:59 +0200 |
| commit | e20ca112cc821136f2bf314c6b61fdb73a2e8d8d (patch) | |
| tree | 7a51803c06f52b42d1306ef848044227f10ba4b8 /src/liballoc/collections | |
| parent | a23d8ec8a7525ae90e7625312cc2bee83dbb7493 (diff) | |
| parent | 787eddc1ab49766204c35d2a60c3d75b6ea7413c (diff) | |
| download | rust-e20ca112cc821136f2bf314c6b61fdb73a2e8d8d.tar.gz rust-e20ca112cc821136f2bf314c6b61fdb73a2e8d8d.zip | |
Rollup merge of #71485 - arlopurcell:binary_heap_retain, r=Amanieu
Add BinaryHeap::retain as suggested in #42849 This PR implements retain for BinaryHeap as suggested in #42849. This is my first PR for Rust, so please let me know if I should be doing anything differently, thanks!
Diffstat (limited to 'src/liballoc/collections')
| -rw-r--r-- | src/liballoc/collections/binary_heap.rs | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/src/liballoc/collections/binary_heap.rs b/src/liballoc/collections/binary_heap.rs index 03c9164fb90..8e170d970bc 100644 --- a/src/liballoc/collections/binary_heap.rs +++ b/src/liballoc/collections/binary_heap.rs @@ -665,6 +665,34 @@ impl<T: Ord> BinaryHeap<T> { pub fn drain_sorted(&mut self) -> DrainSorted<'_, T> { DrainSorted { inner: self } } + + /// Retains only the elements specified by the predicate. + /// + /// In other words, remove all elements `e` such that `f(&e)` returns + /// `false`. The elements are visited in unsorted (and unspecified) order. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// #![feature(binary_heap_retain)] + /// use std::collections::BinaryHeap; + /// + /// let mut heap = BinaryHeap::from(vec![-10, -5, 1, 2, 4, 13]); + /// + /// heap.retain(|x| x % 2 == 0); // only keep even numbers + /// + /// assert_eq!(heap.into_sorted_vec(), [-10, 2, 4]) + /// ``` + #[unstable(feature = "binary_heap_retain", issue = "71503")] + pub fn retain<F>(&mut self, f: F) + where + F: FnMut(&T) -> bool, + { + self.data.retain(f); + self.rebuild(); + } } impl<T> BinaryHeap<T> { |
