forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BTreeMap/Set: merge the implementations of MergeIter
- Loading branch information
Showing
4 changed files
with
111 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
use core::cmp::Ordering; | ||
use core::fmt::{self, Debug}; | ||
use core::iter::FusedIterator; | ||
|
||
/// Core of an iterator that merges the output of two ascending iterators, | ||
/// for instance a union or a symmetric difference. | ||
pub struct MergeIterInner<I> | ||
where | ||
I: Iterator, | ||
{ | ||
a: I, | ||
b: I, | ||
peeked: Option<Peeked<I>>, | ||
} | ||
|
||
/// Benchmarks faster than wrapping both iterators in a Peekable. | ||
#[derive(Clone, Debug)] | ||
enum Peeked<I: Iterator> { | ||
A(I::Item), | ||
B(I::Item), | ||
} | ||
|
||
impl<I> Clone for MergeIterInner<I> | ||
where | ||
I: Clone + Iterator, | ||
I::Item: Clone, | ||
{ | ||
fn clone(&self) -> Self { | ||
Self { a: self.a.clone(), b: self.b.clone(), peeked: self.peeked.clone() } | ||
} | ||
} | ||
|
||
impl<I> Debug for MergeIterInner<I> | ||
where | ||
I: Iterator + Debug, | ||
I::Item: Debug, | ||
{ | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.debug_tuple("MergeIterInner").field(&self.a).field(&self.b).finish() | ||
} | ||
} | ||
|
||
impl<I> MergeIterInner<I> | ||
where | ||
I: ExactSizeIterator + FusedIterator, | ||
{ | ||
/// Creates a new core for an iterator merging a pair of sources. | ||
pub fn new(a: I, b: I) -> Self { | ||
MergeIterInner { a, b, peeked: None } | ||
} | ||
|
||
/// Returns the next pair of items stemming from the pair of sources | ||
/// being merged. If both returned options contain a value, that value | ||
/// is equal and occurs in both sources. If one of the returned options | ||
/// contains a value, that value doesn't occur in the other source. | ||
/// If neither returned option contains a value, iteration has finished | ||
/// and subsequent calls will return the same empty pair. | ||
pub fn nexts<Cmp: Fn(&I::Item, &I::Item) -> Ordering>( | ||
&mut self, | ||
cmp: Cmp, | ||
) -> (Option<I::Item>, Option<I::Item>) { | ||
let mut a_next; | ||
let mut b_next; | ||
match self.peeked.take() { | ||
Some(Peeked::A(next)) => { | ||
a_next = Some(next); | ||
b_next = self.b.next(); | ||
} | ||
Some(Peeked::B(next)) => { | ||
b_next = Some(next); | ||
a_next = self.a.next(); | ||
} | ||
None => { | ||
a_next = self.a.next(); | ||
b_next = self.b.next(); | ||
} | ||
} | ||
if let (Some(ref a1), Some(ref b1)) = (&a_next, &b_next) { | ||
match cmp(a1, b1) { | ||
Ordering::Less => self.peeked = b_next.take().map(Peeked::B), | ||
Ordering::Greater => self.peeked = a_next.take().map(Peeked::A), | ||
Ordering::Equal => (), | ||
} | ||
} | ||
(a_next, b_next) | ||
} | ||
|
||
/// Returns a pair of upper bounds for the `size_hint` of the final iterator. | ||
pub fn lens(&self) -> (usize, usize) { | ||
match self.peeked { | ||
Some(Peeked::A(_)) => (1 + self.a.len(), self.b.len()), | ||
Some(Peeked::B(_)) => (self.a.len(), 1 + self.b.len()), | ||
_ => (self.a.len(), self.b.len()), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
mod borrow; | ||
pub mod map; | ||
mod merge_iter; | ||
mod navigate; | ||
mod node; | ||
mod remove; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters