Repository navigation
Specialize KMergeBy fold for the final input iterator #1133
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
Open
CollinYuanjieRen
wants to merge
1
commit into
rust-itertools:master
Choose a base branch
from
CollinYuanjieRen:codex/kmerge-fold
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+162
−1
Open
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
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 |
|---|---|---|
|
|
@@ -495,6 +495,95 @@ quickcheck! { | |
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn kmerge_fold_preserves_iterator_events() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this covered by our existing specialization tests? If not, then we should probably add it there and make it generic enough to work for all our specialized |
||
| use std::cell::RefCell; | ||
| use std::rc::Rc; | ||
|
|
||
| #[derive(Clone, Debug, PartialEq)] | ||
| enum Event { | ||
| Next(usize, Option<i32>), | ||
| Compare(i32, i32), | ||
| Consume(i32), | ||
| Drop(usize), | ||
| } | ||
|
|
||
| struct Tracked { | ||
| id: usize, | ||
| items: std::vec::IntoIter<Option<i32>>, | ||
| events: Rc<RefCell<Vec<Event>>>, | ||
| } | ||
|
|
||
| impl Iterator for Tracked { | ||
| type Item = i32; | ||
|
|
||
| fn next(&mut self) -> Option<i32> { | ||
| let item = self.items.next().flatten(); | ||
| self.events.borrow_mut().push(Event::Next(self.id, item)); | ||
| item | ||
| } | ||
| } | ||
|
|
||
| impl Drop for Tracked { | ||
| fn drop(&mut self) { | ||
| self.events.borrow_mut().push(Event::Drop(self.id)); | ||
| } | ||
| } | ||
|
|
||
| fn run( | ||
| sources: &[Vec<Option<i32>>], | ||
| advanced: usize, | ||
| specialized: bool, | ||
| ) -> (Vec<i32>, Vec<Event>) { | ||
| let events = Rc::new(RefCell::new(Vec::new())); | ||
| let mut it = sources | ||
| .iter() | ||
| .enumerate() | ||
| .map(|(id, items)| Tracked { | ||
| id, | ||
| items: items.clone().into_iter(), | ||
| events: events.clone(), | ||
| }) | ||
| .kmerge_by(|&a, &b| { | ||
| events.borrow_mut().push(Event::Compare(a, b)); | ||
| a < b | ||
| }); | ||
| for _ in 0..advanced { | ||
| it.next(); | ||
| } | ||
| let consume = |mut acc: Vec<i32>, item| { | ||
| events.borrow_mut().push(Event::Consume(item)); | ||
| acc.push(item); | ||
| acc | ||
| }; | ||
| let items = if specialized { | ||
| it.fold(Vec::new(), consume) | ||
| } else { | ||
| Unspecialized(it).fold(Vec::new(), consume) | ||
| }; | ||
| let trace = events.borrow().clone(); | ||
| (items, trace) | ||
| } | ||
|
|
||
| let cases = [ | ||
| vec![], | ||
| vec![vec![]], | ||
| vec![vec![Some(1)]], | ||
| vec![vec![Some(1), Some(2), None, Some(99)]], | ||
| vec![ | ||
| vec![Some(0), Some(3), None, Some(98)], | ||
| vec![Some(1)], | ||
| vec![Some(2), Some(4), Some(5)], | ||
| ], | ||
| vec![vec![None, Some(99)], vec![Some(1), Some(2)]], | ||
| ]; | ||
| for sources in &cases { | ||
| for advanced in 0..=3 { | ||
| assert_eq!(run(sources, advanced, true), run(sources, advanced, false)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| quickcheck! { | ||
| // TODO Replace this function by a normal call to test_specializations | ||
| fn process_results(v: Vec<Result<u8, u8>>) -> () { | ||
|
|
||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hope I don't miss the forest for the trees, but counldn't this
f(acc, head),tail.fold(acc, f)?