Skip to content

Commit

Permalink
Merge commit 'refs/pull/21/head' of https://github.com/eggyal/copse
Browse files Browse the repository at this point in the history
  • Loading branch information
eggyal committed Apr 4, 2023
2 parents 849b228 + 6636a9f commit bc34674
Showing 1 changed file with 19 additions and 60 deletions.
79 changes: 19 additions & 60 deletions src/liballoc/collections/binary_heap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ use alloc::slice;
use alloc::vec::{self, Vec};
use cfg_if::cfg_if;

use super::SpecExtend;
use crate::{
default::{OrdStoredKey, OrdTotalOrder},
SortableBy, SortableByWithOrder, TotalOrder,
Expand Down Expand Up @@ -408,6 +407,17 @@ impl<T: fmt::Debug, O> fmt::Debug for BinaryHeap<T, O> {
}
}

struct RebuildOnDrop<'a, T: SortableByWithOrder<O>, O: TotalOrder> {
heap: &'a mut BinaryHeap<T, O>,
rebuild_from: usize,
}

impl<'a, T: SortableByWithOrder<O>, O: TotalOrder> Drop for RebuildOnDrop<'a, T, O> {
fn drop(&mut self) {
self.heap.rebuild_tail(self.rebuild_from);
}
}

impl<T: SortableByWithOrder<O>, O: TotalOrder> BinaryHeap<T, O> {
/// Creates an empty `BinaryHeap` as a max-heap.
///
Expand Down Expand Up @@ -849,30 +859,19 @@ impl<T: SortableByWithOrder<O>, O: TotalOrder> BinaryHeap<T, O> {
where
F: FnMut(&T) -> bool,
{
struct RebuildOnDrop<'a, T: SortableByWithOrder<O>, O: TotalOrder> {
heap: &'a mut BinaryHeap<T, O>,
first_removed: usize,
}

let mut guard = RebuildOnDrop { first_removed: self.len(), heap: self };

// rebuild_start will be updated to the first touched element below, and the rebuild will
// only be done for the tail.
let mut guard = RebuildOnDrop { rebuild_from: self.len(), heap: self };
let mut i = 0;

guard.heap.data.retain(|e| {
let keep = f(e);
if !keep && i < guard.first_removed {
guard.first_removed = i;
if !keep && i < guard.rebuild_from {
guard.rebuild_from = i;
}
i += 1;
keep
});

impl<'a, T: SortableByWithOrder<O>, O: TotalOrder> Drop for RebuildOnDrop<'a, T, O> {
fn drop(&mut self) {
// data[..first_removed] is untouched, so we only need to
// rebuild the tail:
self.heap.rebuild_tail(self.first_removed);
}
}
}

#[doc(hidden)]
Expand Down Expand Up @@ -1739,7 +1738,8 @@ impl<'a, T, O> IntoIterator for &'a BinaryHeap<T, O> {
impl<T: SortableByWithOrder<O>, O: TotalOrder> Extend<T> for BinaryHeap<T, O> {
#[inline]
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
<Self as SpecExtend<I>>::spec_extend(self, iter);
let guard = RebuildOnDrop { rebuild_from: self.len(), heap: self };
guard.heap.data.extend(iter);
}

#[inline]
Expand All @@ -1755,47 +1755,6 @@ impl<T: SortableByWithOrder<O>, O: TotalOrder> Extend<T> for BinaryHeap<T, O> {
}
}

cfg_if! {
if #[cfg(feature = "specialization")] {
impl<T: SortableByWithOrder<O>, O: TotalOrder, I: IntoIterator<Item = T>> SpecExtend<I> for BinaryHeap<T, O> {
default fn spec_extend(&mut self, iter: I) {
self.extend_desugared(iter.into_iter());
}
}

impl<T: SortableByWithOrder<O>, O: TotalOrder> SpecExtend<Vec<T>> for BinaryHeap<T, O> {
fn spec_extend(&mut self, ref mut other: Vec<T>) {
let start = self.data.len();
self.data.append(other);
self.rebuild_tail(start);
}
}

impl<T: SortableByWithOrder<O>, O: TotalOrder> SpecExtend<BinaryHeap<T, O>> for BinaryHeap<T, O> {
fn spec_extend(&mut self, ref mut other: BinaryHeap<T, O>) {
self.append(other);
}
}
} else {
impl<T: SortableByWithOrder<O>, O: TotalOrder, I: IntoIterator<Item = T>> SpecExtend<I> for BinaryHeap<T, O> {
fn spec_extend(&mut self, iter: I) {
self.extend_desugared(iter.into_iter());
}
}
}
}

impl<T: SortableByWithOrder<O>, O: TotalOrder> BinaryHeap<T, O> {
fn extend_desugared<I: IntoIterator<Item = T>>(&mut self, iter: I) {
let iterator = iter.into_iter();
let (lower, _) = iterator.size_hint();

self.reserve(lower);

iterator.for_each(move |elem| self.push(elem));
}
}

impl<'a, T: 'a + SortableByWithOrder<O> + Copy, O: TotalOrder> Extend<&'a T> for BinaryHeap<T, O> {
fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) {
self.extend(iter.into_iter().cloned());
Expand Down

0 comments on commit bc34674

Please sign in to comment.