Ver a proveniência

Implemented 'filter' operation

master
Bergmann89 há 5 anos
ascendente
cometimento
a76bb629ed
3 ficheiros alterados com 256 adições e 3 eliminações
  1. +22
    -0
      asparit/src/core/iterator.rs
  2. +229
    -0
      asparit/src/inner/filter.rs
  3. +5
    -3
      asparit/src/inner/mod.rs

+ 22
- 0
asparit/src/core/iterator.rs Ver ficheiro

@@ -7,6 +7,7 @@ use crate::{
cloned::Cloned,
collect::Collect,
copied::Copied,
filter::Filter,
for_each::ForEach,
inspect::Inspect,
map::Map,
@@ -493,6 +494,27 @@ pub trait ParallelIterator<'a>: Sized + Send {
Update::new(self, operation)
}

/// Applies `operation` to each item of this iterator, producing a new
/// iterator with only the items that gave `true` results.
///
/// # Examples
///
/// ```
/// use rayon::prelude::*;
///
/// let mut par_iter = (0..10).into_par_iter().filter(|x| x % 2 == 0);
///
/// let even_numbers: Vec<_> = par_iter.collect();
///
/// assert_eq!(&even_numbers[..], &[0, 2, 4, 6, 8]);
/// ```
fn filter<O>(self, operation: O) -> Filter<Self, O>
where
O: Fn(&Self::Item) -> bool + Clone + Send + 'a,
{
Filter::new(self, operation)
}

/// Reduces the items in the iterator into one item using `operation`.
/// The argument `identity` should be a closure that can produce
/// "identity" value which may be inserted into the sequence as


+ 229
- 0
asparit/src/inner/filter.rs Ver ficheiro

@@ -0,0 +1,229 @@
use crate::{Consumer, Executor, Folder, ParallelIterator, Producer, ProducerCallback, Reducer};

/* Filter */

pub struct Filter<X, O> {
base: X,
operation: O,
}

impl<X, O> Filter<X, O> {
pub fn new(base: X, operation: O) -> Self {
Self { base, operation }
}
}

impl<'a, X, O> ParallelIterator<'a> for Filter<X, O>
where
X: ParallelIterator<'a>,
O: Fn(&X::Item) -> bool + Clone + Send + 'a,
{
type Item = X::Item;

fn drive<E, C, D, R>(self, executor: E, consumer: C) -> E::Result
where
E: Executor<'a, D>,
C: Consumer<Self::Item, Result = D, Reducer = R> + 'a,
D: Send,
R: Reducer<D> + Send,
{
self.base.drive(
executor,
FilterConsumer {
base: consumer,
operation: self.operation,
},
)
}

fn with_producer<CB>(self, callback: CB) -> CB::Output
where
CB: ProducerCallback<'a, Self::Item>,
{
self.base.with_producer(FilterCallback {
base: callback,
operation: self.operation,
})
}

fn len_hint_opt(&self) -> Option<usize> {
self.base.len_hint_opt()
}
}

/* FilterConsumer */

struct FilterConsumer<C, O> {
base: C,
operation: O,
}

impl<'a, C, O, T> Consumer<T> for FilterConsumer<C, O>
where
C: Consumer<T>,
O: Fn(&T) -> bool + Clone + Send,
{
type Folder = FilterFolder<C::Folder, O>;
type Reducer = C::Reducer;
type Result = C::Result;

fn split(self) -> (Self, Self, Self::Reducer) {
let (left, right, reducer) = self.base.split();

let left = FilterConsumer {
base: left,
operation: self.operation.clone(),
};
let right = FilterConsumer {
base: right,
operation: self.operation,
};

(left, right, reducer)
}

fn split_at(self, index: usize) -> (Self, Self, Self::Reducer) {
let (left, right, reducer) = self.base.split_at(index);

let left = FilterConsumer {
base: left,
operation: self.operation.clone(),
};
let right = FilterConsumer {
base: right,
operation: self.operation,
};

(left, right, reducer)
}

fn into_folder(self) -> Self::Folder {
FilterFolder {
base: self.base.into_folder(),
operation: self.operation,
}
}

fn is_full(&self) -> bool {
self.base.is_full()
}
}

/* FilterFolder */

struct FilterFolder<F, O> {
base: F,
operation: O,
}

impl<F, O, T> Folder<T> for FilterFolder<F, O>
where
F: Folder<T>,
O: Fn(&T) -> bool + Clone,
{
type Result = F::Result;

fn consume(mut self, item: T) -> Self {
(self.operation)(&item);

self.base = self.base.consume(item);

self
}

fn consume_iter<X>(mut self, iter: X) -> Self
where
X: IntoIterator<Item = T>,
{
self.base = self
.base
.consume_iter(iter.into_iter().filter(self.operation.clone()));

self
}

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

fn is_full(&self) -> bool {
self.base.is_full()
}
}

/* FilterCallback */

struct FilterCallback<CB, O> {
base: CB,
operation: O,
}

impl<'a, CB, O, T> ProducerCallback<'a, T> for FilterCallback<CB, O>
where
CB: ProducerCallback<'a, T>,
O: Fn(&T) -> bool + Clone + Send + 'a,
{
type Output = CB::Output;

fn callback<P>(self, producer: P) -> Self::Output
where
P: Producer<Item = T> + 'a,
{
self.base.callback(FilterProducer {
base: producer,
operation: self.operation,
})
}
}

/* FilterProducer */

struct FilterProducer<P, O> {
base: P,
operation: O,
}

impl<'a, P, O, T> Producer for FilterProducer<P, O>
where
P: Producer<Item = T>,
O: Fn(&T) -> bool + Clone + Send,
{
type Item = T;
type IntoIter = std::iter::Filter<P::IntoIter, O>;

fn into_iter(self) -> Self::IntoIter {
self.base.into_iter().filter(self.operation)
}

fn split(self) -> (Self, Option<Self>) {
let operation = self.operation;
let (left, right) = self.base.split();

let left = FilterProducer {
base: left,
operation: operation.clone(),
};
let right = right.map(move |right| FilterProducer {
base: right,
operation,
});

(left, right)
}

fn splits(&self) -> Option<usize> {
self.base.splits()
}

fn fold_with<F>(self, folder: F) -> F
where
F: Folder<Self::Item>,
{
self.base
.fold_with(FilterFolder {
base: folder,
operation: self.operation,
})
.base
}
}

+ 5
- 3
asparit/src/inner/mod.rs Ver ficheiro

@@ -1,6 +1,7 @@
pub mod cloned;
pub mod collect;
pub mod copied;
pub mod filter;
pub mod for_each;
pub mod inspect;
pub mod map;
@@ -39,12 +40,13 @@ mod tests {
.update(|x| x.push(5))
.map_init(
move || i.fetch_add(1, Ordering::Relaxed),
|init, item| Some((*init, item)),
|init, item| (item, *init),
)
.filter(|(_, i)| i % 2 == 0)
.try_for_each_init(
move || j.fetch_add(1, Ordering::Relaxed),
|init, item| -> Result<(), ()> {
println!("{:?} {:?}", init, item);
|item, init| -> Result<(), ()> {
println!("{:?} - {:?}", item, init);

Ok(())
},


Carregando…
Cancelar
Guardar