Sfoglia il codice sorgente

Implemented 'filter_map' operation

master
Bergmann89 5 anni fa
parent
commit
8b7930cc60
3 ha cambiato i file con 263 aggiunte e 3 eliminazioni
  1. +26
    -0
      asparit/src/core/iterator.rs
  2. +233
    -0
      asparit/src/inner/filter_map.rs
  3. +4
    -3
      asparit/src/inner/mod.rs

+ 26
- 0
asparit/src/core/iterator.rs Vedi File

@@ -8,6 +8,7 @@ use crate::{
collect::Collect,
copied::Copied,
filter::Filter,
filter_map::FilterMap,
for_each::ForEach,
inspect::Inspect,
map::Map,
@@ -515,6 +516,31 @@ pub trait ParallelIterator<'a>: Sized + Send {
Filter::new(self, operation)
}

/// Applies `operation` to each item of this iterator to get an `Option`,
/// producing a new iterator with only the items from `Some` results.
///
/// # Examples
///
/// ```
/// use rayon::prelude::*;
///
/// let mut par_iter = (0..10).into_par_iter()
/// .filter_map(|x| {
/// if x % 2 == 0 { Some(x * 3) }
/// else { None }
/// });
///
/// let even_numbers: Vec<_> = par_iter.collect();
///
/// assert_eq!(&even_numbers[..], &[0, 6, 12, 18, 24]);
/// ```
fn filter_map<O, S>(self, operation: O) -> FilterMap<Self, O>
where
O: Fn(Self::Item) -> Option<S> + Clone + Send + 'a,
{
FilterMap::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


+ 233
- 0
asparit/src/inner/filter_map.rs Vedi File

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

/* FilterMap */

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

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

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

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,
FilterMapConsumer {
base: consumer,
operation: self.operation,
},
)
}

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

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

/* FilterMapConsumer */

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

impl<'a, C, O, T, S> Consumer<T> for FilterMapConsumer<C, O>
where
C: Consumer<S>,
O: Fn(T) -> Option<S> + Clone + Send,
{
type Folder = FilterMapFolder<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 = FilterMapConsumer {
base: left,
operation: self.operation.clone(),
};
let right = FilterMapConsumer {
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 = FilterMapConsumer {
base: left,
operation: self.operation.clone(),
};
let right = FilterMapConsumer {
base: right,
operation: self.operation,
};

(left, right, reducer)
}

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

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

/* FilterMapFolder */

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

impl<F, O, T, S> Folder<T> for FilterMapFolder<F, O>
where
F: Folder<S>,
O: Fn(T) -> Option<S> + Clone,
{
type Result = F::Result;

fn consume(mut self, item: T) -> Self {
if let Some(item) = (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_map(self.operation.clone()));

self
}

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

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

/* FilterMapCallback */

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

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

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

/* FilterMapProducer */

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

impl<'a, P, O, T, S> Producer for FilterMapProducer<P, O>
where
P: Producer<Item = T>,
O: Fn(T) -> Option<S> + Clone + Send,
S: Send,
{
type Item = S;
type IntoIter = std::iter::FilterMap<P::IntoIter, O>;

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

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

let left = FilterMapProducer {
base: left,
operation: operation.clone(),
};
let right = right.map(move |right| FilterMapProducer {
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(FilterMapFolder {
base: folder,
operation: self.operation,
})
.base
}
}

+ 4
- 3
asparit/src/inner/mod.rs Vedi File

@@ -2,6 +2,7 @@ pub mod cloned;
pub mod collect;
pub mod copied;
pub mod filter;
pub mod filter_map;
pub mod for_each;
pub mod inspect;
pub mod map;
@@ -42,11 +43,11 @@ mod tests {
move || i.fetch_add(1, Ordering::Relaxed),
|init, item| (item, *init),
)
.filter(|(_, i)| i % 2 == 0)
.filter_map(|(x, i)| if i % 2 == 0 { Some((i, x)) } else { None })
.try_for_each_init(
move || j.fetch_add(1, Ordering::Relaxed),
|item, init| -> Result<(), ()> {
println!("{:?} - {:?}", item, init);
|init, item| -> Result<(), ()> {
println!("{:?} - {:?}", init, item);

Ok(())
},


Caricamento…
Annulla
Salva