-
Notifications
You must be signed in to change notification settings - Fork 501
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
Add cartesian_product to ParallelIterator #1182
Open
nascheinkman
wants to merge
3
commits into
rayon-rs:main
Choose a base branch
from
nascheinkman:main
base: main
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.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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,48 @@ | ||
use super::plumbing::*; | ||
use super::repeat::*; | ||
use super::{IndexedParallelIterator, IntoParallelIterator, ParallelIterator}; | ||
|
||
/// `CartesianProduct` is an iterator that combines `i` and `j` into a single iterator that | ||
/// iterates over the cartesian product of the elements of `i` and `j`. This struct is created by | ||
/// the [`cartesian_product()`] method on [`ParallelIterator`] | ||
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"] | ||
#[derive(Debug, Clone, Copy)] | ||
pub struct CartesianProduct<I, J> | ||
where | ||
I: ParallelIterator, | ||
J: ParallelIterator, | ||
{ | ||
i: I, | ||
j: J, | ||
} | ||
|
||
impl<I, J> CartesianProduct<I, J> | ||
where | ||
I: ParallelIterator, | ||
J: IndexedParallelIterator, | ||
{ | ||
/// Creates a new `CartesianProduct` iterator. | ||
pub(super) fn new(i: I, j: J) -> Self { | ||
CartesianProduct { i, j } | ||
} | ||
} | ||
|
||
impl<I, J> ParallelIterator for CartesianProduct<I, J> | ||
where | ||
I: ParallelIterator, | ||
J: IndexedParallelIterator + Clone + Sync, | ||
I::Item: Clone + Send, | ||
{ | ||
type Item = (I::Item, J::Item); | ||
|
||
fn drive_unindexed<C>(self, consumer: C) -> C::Result | ||
where | ||
C: UnindexedConsumer<Self::Item>, | ||
{ | ||
self.i | ||
.into_par_iter() | ||
.map(|i_item| repeat(i_item.clone()).zip(self.j.clone())) | ||
.flatten() | ||
.drive_unindexed(consumer) | ||
} | ||
} |
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,53 @@ | ||
use rayon::prelude::*; | ||
use std::collections::HashSet; | ||
|
||
#[test] | ||
fn cartesian_ranges() { | ||
let a: Vec<(usize, usize)> = (0..3).into_par_iter().cartesian_product(0..3).collect(); | ||
let b: HashSet<(usize, usize)> = HashSet::from_iter(a.into_iter()); | ||
assert!(b.contains(&(0, 0))); | ||
assert!(b.contains(&(0, 1))); | ||
assert!(b.contains(&(0, 2))); | ||
assert!(b.contains(&(1, 0))); | ||
assert!(b.contains(&(1, 1))); | ||
assert!(b.contains(&(1, 2))); | ||
assert!(b.contains(&(2, 0))); | ||
assert!(b.contains(&(2, 1))); | ||
assert!(b.contains(&(2, 2))); | ||
} | ||
|
||
#[test] | ||
fn cartesian_vecs() { | ||
let a: Vec<(f64, f64)> = vec![0.1, 1.2, 2.4] | ||
.into_par_iter() | ||
.cartesian_product(vec![4.8, 16.32, 32.64]) | ||
.collect(); | ||
assert!(a.contains(&(0.1, 4.8))); | ||
assert!(a.contains(&(0.1, 16.32))); | ||
assert!(a.contains(&(0.1, 32.64))); | ||
assert!(a.contains(&(1.2, 4.8))); | ||
assert!(a.contains(&(1.2, 16.32))); | ||
assert!(a.contains(&(1.2, 32.64))); | ||
assert!(a.contains(&(2.4, 4.8))); | ||
assert!(a.contains(&(2.4, 16.32))); | ||
assert!(a.contains(&(2.4, 32.64))); | ||
} | ||
|
||
#[test] | ||
fn cartesian_chain() { | ||
let a: Vec<(usize, usize, usize)> = (0..2) | ||
.into_par_iter() | ||
.cartesian_product(0..2) | ||
.cartesian_product(0..2) | ||
.map(|((a, b), c)| (a, b, c)) | ||
.collect(); | ||
let b: HashSet<(usize, usize, usize)> = HashSet::from_iter(a.into_iter()); | ||
assert!(b.contains(&(0, 0, 0))); | ||
assert!(b.contains(&(0, 0, 1))); | ||
assert!(b.contains(&(0, 1, 0))); | ||
assert!(b.contains(&(0, 1, 1))); | ||
assert!(b.contains(&(1, 0, 0))); | ||
assert!(b.contains(&(1, 0, 1))); | ||
assert!(b.contains(&(1, 1, 0))); | ||
assert!(b.contains(&(1, 1, 1))); | ||
} |
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.
Are the calls to
into_par_iter
(I
already is aParallelIterator
here) andi_item.clone()
(repeat
will clone it repeatedly, but the first clone seems unnecessary) really required here?I also think we can use
flat_map
here instead ofmap
andflatten
to possibly benefit from additional optimizations in its implementation.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.
Good catch!