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
129
130
131
132
133
134
use super::*;
use super::internal::*;
use std::f64;

pub struct FlatMap<M, MAP_OP> {
    base: M,
    map_op: MAP_OP,
}

impl<M, MAP_OP> FlatMap<M, MAP_OP> {
    pub fn new(base: M, map_op: MAP_OP) -> FlatMap<M, MAP_OP> {
        FlatMap { base: base, map_op: map_op }
    }
}

impl<M, MAP_OP, PI> ParallelIterator for FlatMap<M, MAP_OP>
    where M: ParallelIterator,
          MAP_OP: Fn(M::Item) -> PI + Sync,
          PI: ParallelIterator,
{
    type Item = PI::Item;

    fn drive_unindexed<C>(self, consumer: C) -> C::Result
        where C: UnindexedConsumer<Self::Item>
    {
        let consumer = FlatMapConsumer { base: consumer,
                                         map_op: &self.map_op };
        self.base.drive_unindexed(consumer)
    }
}

///////////////////////////////////////////////////////////////////////////
// Consumer implementation

struct FlatMapConsumer<'m, C, MAP_OP: 'm> {
    base: C,
    map_op: &'m MAP_OP,
}

impl<'m, C, MAP_OP> FlatMapConsumer<'m, C, MAP_OP> {
    fn new(base: C, map_op: &'m MAP_OP) -> Self {
        FlatMapConsumer { base: base, map_op: map_op }
    }
}

impl<'m, ITEM, MAPPED_ITEM, C, MAP_OP> Consumer<ITEM>
    for FlatMapConsumer<'m, C, MAP_OP>
    where C: UnindexedConsumer<MAPPED_ITEM::Item>,
          MAP_OP: Fn(ITEM) -> MAPPED_ITEM + Sync,
          MAPPED_ITEM: ParallelIterator,
{
    type Folder = FlatMapFolder<'m, C, MAP_OP, C::Result>;
    type Reducer = C::Reducer;
    type Result = C::Result;

    fn cost(&mut self, _cost: f64) -> f64 {
        // We have no idea how many items we will produce, so ramp up
        // the cost, so as to encourage the producer to do a
        // fine-grained divison. This is not necessarily a good
        // policy.
        f64::INFINITY
    }

    fn split_at(self, _index: usize) -> (Self, Self, C::Reducer) {
        (FlatMapConsumer::new(self.base.split_off(), self.map_op),
         FlatMapConsumer::new(self.base.split_off(), self.map_op),
         self.base.to_reducer())
    }

    fn into_folder(self) -> Self::Folder {
        FlatMapFolder {
            base: self.base,
            map_op: self.map_op,
            previous: None,
        }
    }
}

impl<'m, ITEM, MAPPED_ITEM, C, MAP_OP> UnindexedConsumer<ITEM>
    for FlatMapConsumer<'m, C, MAP_OP>
    where C: UnindexedConsumer<MAPPED_ITEM::Item>,
          MAP_OP: Fn(ITEM) -> MAPPED_ITEM + Sync,
          MAPPED_ITEM: ParallelIterator,
{
    fn split_off(&self) -> Self {
        FlatMapConsumer::new(self.base.split_off(), self.map_op)
    }

    fn to_reducer(&self) -> Self::Reducer {
        self.base.to_reducer()
    }
}


struct FlatMapFolder<'m, C, MAP_OP: 'm, R> {
    base: C,
    map_op: &'m MAP_OP,
    previous: Option<R>,
}

impl<'m, ITEM, MAPPED_ITEM, C, MAP_OP> Folder<ITEM>
    for FlatMapFolder<'m, C, MAP_OP, C::Result>
    where C: UnindexedConsumer<MAPPED_ITEM::Item>,
          MAP_OP: Fn(ITEM) -> MAPPED_ITEM + Sync,
          MAPPED_ITEM: ParallelIterator,
{
    type Result = C::Result;

    fn consume(self, item: ITEM) -> Self {
        let map_op = self.map_op;
        let par_iter = map_op(item).into_par_iter();
        let result = par_iter.drive_unindexed(self.base.split_off());

        // We expect that `previous` is `None`, because we drive
        // the cost up so high, but just in case.
        let previous = match self.previous {
            None => Some(result),
            Some(previous) => {
                let reducer = self.base.to_reducer();
                Some(reducer.reduce(result, previous))
            }
        };

        FlatMapFolder { base: self.base,
                        map_op: map_op,
                        previous: previous }
    }

    fn complete(self) -> Self::Result {
        // should have processed at least one item -- but is this
        // really a fair assumption?
        self.previous.unwrap()
    }
}