Skip to content

Commit

Permalink
IntoParallelIterator for Option and Result
Browse files Browse the repository at this point in the history
  • Loading branch information
cuviper committed Sep 7, 2016
1 parent 4123054 commit fbc8b47
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/par_iter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub mod weight;
pub mod zip;
pub mod range;
pub mod vec;
pub mod option;

#[cfg(test)]
mod test;
Expand Down
125 changes: 125 additions & 0 deletions src/par_iter/option.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
use super::*;
use super::internal::*;
use std;

pub struct OptionIter<T: Send> {
opt: Option<T>
}

impl<T: Send> IntoParallelIterator for Option<T> {
type Item = T;
type Iter = OptionIter<Self::Item>;

fn into_par_iter(self) -> Self::Iter {
OptionIter { opt: self }
}
}

impl<T: Send, E> IntoParallelIterator for Result<T, E> {
type Item = T;
type Iter = OptionIter<Self::Item>;

fn into_par_iter(self) -> Self::Iter {
self.ok().into_par_iter()
}
}

impl<'a, T: Sync> IntoParallelIterator for &'a Option<T> {
type Item = &'a T;
type Iter = OptionIter<Self::Item>;

fn into_par_iter(self) -> Self::Iter {
self.as_ref().into_par_iter()
}
}

impl<'a, T: Sync, E> IntoParallelIterator for &'a Result<T, E> {
type Item = &'a T;
type Iter = OptionIter<Self::Item>;

fn into_par_iter(self) -> Self::Iter {
self.as_ref().ok().into_par_iter()
}
}

impl<'a, T: Send> IntoParallelIterator for &'a mut Option<T> {
type Item = &'a mut T;
type Iter = OptionIter<Self::Item>;

fn into_par_iter(self) -> Self::Iter {
self.as_mut().into_par_iter()
}
}

impl<'a, T: Send, E> IntoParallelIterator for &'a mut Result<T, E> {
type Item = &'a mut T;
type Iter = OptionIter<Self::Item>;

fn into_par_iter(self) -> Self::Iter {
self.as_mut().ok().into_par_iter()
}
}

///////////////////////////////////////////////////////////////////////////

impl<T: Send> ParallelIterator for OptionIter<T> {
type Item = T;

fn drive_unindexed<C>(self, consumer: C) -> C::Result
where C: UnindexedConsumer<Self::Item>
{
bridge(self, consumer)
}
}

impl<T: Send> BoundedParallelIterator for OptionIter<T> {
fn upper_bound(&mut self) -> usize {
ExactParallelIterator::len(self)
}

fn drive<C>(self, consumer: C) -> C::Result
where C: Consumer<Self::Item>
{
bridge(self, consumer)
}
}

impl<T: Send> ExactParallelIterator for OptionIter<T> {
fn len(&mut self) -> usize {
match self.opt { Some(_) => 1, None => 0 }
}
}

impl<T: Send> IndexedParallelIterator for OptionIter<T> {
fn with_producer<CB>(self, callback: CB) -> CB::Output
where CB: ProducerCallback<Self::Item>
{
callback.callback(OptionProducer { opt: self.opt })
}
}

///////////////////////////////////////////////////////////////////////////

pub struct OptionProducer<T: Send> {
opt: Option<T>
}

impl<T: Send> Producer for OptionProducer<T> {
fn cost(&mut self, len: usize) -> f64 {
len as f64
}

fn split_at(self, index: usize) -> (Self, Self) {
let none = OptionProducer { opt: None };
if index == 0 { (none, self) } else { (self, none) }
}
}

impl<T: Send> IntoIterator for OptionProducer<T> {
type Item = T;
type IntoIter = std::option::IntoIter<T>;

fn into_iter(self) -> Self::IntoIter {
self.opt.into_iter()
}
}
25 changes: 25 additions & 0 deletions src/par_iter/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,3 +420,28 @@ pub fn check_chunks_mut() {
assert_eq!(a, &[6, 2, 3, 15, 5, 6, 24, 8, 9, 10]);
assert_eq!(a, b);
}

#[test]
pub fn check_options() {
let mut a = vec![None, Some(1), None, None, Some(2), Some(4)];

assert_eq!(7, a.par_iter().flat_map(|opt| opt).cloned().sum());
assert_eq!(7, a.par_iter().cloned().flat_map(|opt| opt).sum());

a.par_iter_mut().flat_map(|opt| opt)
.for_each(|x| *x = *x * *x);

assert_eq!(21, a.into_par_iter().flat_map(|opt| opt).sum());
}

#[test]
pub fn check_results() {
let mut a = vec![Err(()), Ok(1), Err(()), Err(()), Ok(2), Ok(4)];

assert_eq!(7, a.par_iter().flat_map(|res| res).cloned().sum());

a.par_iter_mut().flat_map(|res| res)
.for_each(|x| *x = *x * *x);

assert_eq!(21, a.into_par_iter().flat_map(|res| res).sum());
}

0 comments on commit fbc8b47

Please sign in to comment.