-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
117 additions
and
80 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,34 @@ | ||
/// Extension trait that adds parallel zipping functionality to iterators over iterators. | ||
pub trait ParallelZipExt: Iterator { | ||
/// Zips multiple iterators in parallel, such that the nth invocation yields a | ||
/// [`Vec`] of all nth items of the subiterators. | ||
fn parallel_zip(self) -> ParallelZip<Self::Item> | ||
where | ||
Self: Sized, | ||
Self::Item: Iterator; | ||
} | ||
|
||
/// An iterator similar to [`std::iter::zip`], but instead it zips over *multiple | ||
/// iterators* in parallel, such that the nth invocation yields a [`Vec`] of all | ||
/// nth items of its subiterators. | ||
#[derive(Debug)] | ||
pub struct ParallelZip<I>(Vec<I>); | ||
|
||
impl<I: Iterator> Iterator for ParallelZip<I> { | ||
type Item = Vec<I::Item>; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
self.0.iter_mut().map(Iterator::next).collect() | ||
} | ||
} | ||
|
||
// Implement the extension trait for any iterator whose items are themselves iterators | ||
impl<T> ParallelZipExt for T | ||
where | ||
T: Iterator, | ||
T::Item: Iterator, | ||
{ | ||
fn parallel_zip(self) -> ParallelZip<T::Item> { | ||
ParallelZip(self.collect()) | ||
} | ||
} |
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
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
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