1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use crate::{ParallelMap, Scope};

trait FilterMapFn<Item>: FnMut(Item) -> Option<Item> + Send {
    fn clone_box<'a>(&self) -> Box<dyn 'a + FilterMapFn<Item> + Send>
    where
        Self: 'a;
}

impl<F, Item> FilterMapFn<Item> for F
where
    F: FnMut(Item) -> Option<Item> + Clone + Send,
{
    fn clone_box<'a>(&self) -> Box<dyn 'a + FilterMapFn<Item> + Send>
    where
        Self: 'a,
    {
        Box::new(self.clone())
    }
}

impl<'a, Item: 'a> Clone for Box<dyn 'a + FilterMapFn<Item> + Send> {
    fn clone(&self) -> Self {
        (**self).clone_box()
    }
}

/// Like [`std::iter::Filter`] but multi-threaded
pub struct ParallelFilter<'env, 'scope, I>
where
    I: Iterator,
{
    // the iterator we wrapped
    iter:
        ParallelMap<'env, 'scope, I, Option<I::Item>, Box<dyn FilterMapFn<I::Item> + Send + 'env>>,
}

impl<I> ParallelFilter<'static, 'static, I>
where
    I: Iterator,
{
    pub fn new<F>(iter: I, mut f: F) -> ParallelFilter<'static, 'static, I>
    where
        F: FnMut(&I::Item) -> bool + Send + 'static + Clone,
        I::Item: Send + 'static,
    {
        ParallelFilter {
            iter: ParallelMap::new(
                iter,
                Box::new(move |item| if (f)(&item) { Some(item) } else { None })
                    as Box<dyn FilterMapFn<I::Item> + Send + 'static>,
            ),
        }
    }
}

impl<'env, 'scope, I> ParallelFilter<'env, 'scope, I>
where
    I: Iterator + 'env + 'scope,
    I::Item: Send + 'env + 'scope,
    'env: 'scope,
{
    pub fn new_scoped<F>(
        iter: I,
        scope: &'scope Scope<'env>,
        mut f: F,
    ) -> ParallelFilter<'env, 'scope, I>
    where
        F: FnMut(&I::Item) -> bool + Send + 'env + 'scope + Clone,
    {
        ParallelFilter {
            iter: ParallelMap::new_scoped(
                iter,
                scope,
                Box::new(move |item| if (f)(&item) { Some(item) } else { None })
                    as Box<dyn FilterMapFn<I::Item> + Send + 'env>,
            ),
        }
    }
    /// Start the background workers eagerly, without waiting for a first [`Iterator::next`] call.
    ///
    /// See [`ParallelMap::started`].
    pub fn started(self) -> Self {
        Self {
            iter: self.iter.started(),
        }
    }

    /// Set number of threads to use manually.
    ///
    /// See [`ParallelMap::threads`].
    pub fn threads(self, num_threads: usize) -> Self {
        Self {
            iter: self.iter.threads(num_threads),
        }
    }

    /// Set max number of items in flight
    ///
    /// See [`ParallelMap::max_in_flight`].
    pub fn max_in_flight(self, max_in_flight: usize) -> Self {
        Self {
            iter: self.iter.max_in_flight(max_in_flight),
        }
    }
}

impl<'env, 'scope, I> Iterator for ParallelFilter<'env, 'scope, I>
where
    I: Iterator + 'env + 'scope,
    I::Item: Send + 'env + 'scope,
    'env: 'scope,
{
    type Item = I::Item;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            match self.iter.next() {
                Some(Some(item)) => return Some(item),
                Some(None) => continue,
                None => return None,
            }
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.iter.size_hint()
    }
}