[][src]Trait asparit::ParallelExtend

pub trait ParallelExtend<'a, I, T>: Send + Sized where
    I: Send + 'a,
    T: Send,
    <Self::Consumer as Consumer<I>>::Reducer: Reducer<T>, 
{ type Consumer: Consumer<I, Result = T> + 'a; pub fn into_consumer(self) -> Self::Consumer;
pub fn map_result(inner: <Self::Consumer as Consumer<I>>::Result) -> Self; pub fn par_extend<X>(
        self,
        iter: X
    ) -> ParallelExtendDriver<'a, X::Iter, Self, T>
    where
        X: IntoParallelIterator<'a, Item = I>
, { ... } }

ParallelExtend extends an existing collection with items from a ParallelIterator.

Examples

Implementing ParallelExtend for your type:

use asparit::*;
use std::mem;

struct BlackHole {
    mass: usize,
}

impl<'a, I> ParallelExtend<'a, I, BlackHoleFolder<'a>> for &'a mut BlackHole
where
    I: Send + 'a,
{
    type Consumer = BlackHoleConsumer<'a>;

    fn into_consumer(self) -> Self::Consumer {
        BlackHoleConsumer(Some(self))
    }

    fn map_result(ret: BlackHoleFolder<'a>) -> Self {
        let bh = ret.bh.unwrap();

        bh.mass += ret.mass;

        bh
    }
}

struct BlackHoleConsumer<'a>(Option<&'a mut BlackHole>);

impl WithSetup for BlackHoleConsumer<'_> {}

impl<'a, I> Consumer<I> for BlackHoleConsumer<'a> {
    type Folder = BlackHoleFolder<'a>;
    type Reducer = BlackHoleReducer;
    type Result = BlackHoleFolder<'a>;

    fn split(self) -> (Self, Self, Self::Reducer) {
        (
            BlackHoleConsumer(self.0),
            BlackHoleConsumer(None),
            BlackHoleReducer,
        )
    }

    fn split_at(self, _index: usize) -> (Self, Self, Self::Reducer) {
        (
            BlackHoleConsumer(self.0),
            BlackHoleConsumer(None),
            BlackHoleReducer,
        )
    }

    fn into_folder(self) -> Self::Folder {
        BlackHoleFolder {
            bh: self.0,
            mass: 0,
        }
    }
}

struct BlackHoleFolder<'a> {
    bh: Option<&'a mut BlackHole>,
    mass: usize,
}

impl<'a, I> Folder<I> for BlackHoleFolder<'a> {
    type Result = Self;

    fn consume(mut self, _item: I) -> Self {
        self.mass += std::mem::size_of::<I>();

        self
    }

    fn consume_iter<X>(mut self, iter: X) -> Self
    where
        X: IntoIterator<Item = I>,
    {
        self.mass += iter.into_iter().count() * std::mem::size_of::<I>();

        self
    }

    fn complete(self) -> Self::Result {
        self
    }
}

struct BlackHoleReducer;

impl<'a> Reducer<BlackHoleFolder<'a>> for BlackHoleReducer {
    fn reduce(
        self,
        left: BlackHoleFolder<'a>,
        right: BlackHoleFolder<'a>,
    ) -> BlackHoleFolder<'a> {
        BlackHoleFolder {
            bh: left.bh.or(right.bh),
            mass: left.mass + right.mass,
        }
    }
}

let mut bh = BlackHole { mass: 0 };

bh.par_extend(0i32..1000).exec();
assert_eq!(bh.mass, 4000);

bh.par_extend(0i64..10).exec();
assert_eq!(bh.mass, 4080);

Associated Types

type Consumer: Consumer<I, Result = T> + 'a

Loading content...

Required methods

pub fn into_consumer(self) -> Self::Consumer

Creates a consumer that is used to handle the items from the iterator.

pub fn map_result(inner: <Self::Consumer as Consumer<I>>::Result) -> Self

Converts the result of the consumer into the final type

Loading content...

Provided methods

pub fn par_extend<X>(
    self,
    iter: X
) -> ParallelExtendDriver<'a, X::Iter, Self, T> where
    X: IntoParallelIterator<'a, Item = I>, 

Loading content...

Implementors

Loading content...