-
Notifications
You must be signed in to change notification settings - Fork 13.4k
btree: merge the implementations of MergeIter #78015
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.