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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
use super::internal::*;
use super::*;

/// Specifies a "map operator", transforming values into something else.
///
/// Implementing this trait is not permitted outside of `rayon`.
pub trait MapOp<In>: Sync {
    type Output: Send;
    fn map(&self, value: In) -> Self::Output;
    private_decl!{}
}

pub struct MapFn<F>(pub F);

impl<F, In, Out> MapOp<In> for MapFn<F>
    where F: Fn(In) -> Out + Sync,
          Out: Send
{
    type Output = Out;
    fn map(&self, value: In) -> Out {
        (self.0)(value)
    }
    private_impl!{}
}

pub struct MapCloned;

impl<'a, T> MapOp<&'a T> for MapCloned
    where T: Clone + Send
{
    type Output = T;
    fn map(&self, value: &'a T) -> T {
        value.clone()
    }
    private_impl!{}
}

pub struct MapInspect<F>(pub F);

impl<F, In> MapOp<In> for MapInspect<F>
    where F: Fn(&In) + Sync,
          In: Send
{
    type Output = In;
    fn map(&self, value: In) -> In {
        (self.0)(&value);
        value
    }
    private_impl!{}
}

/// ////////////////////////////////////////////////////////////////////////

pub struct Map<I: ParallelIterator, F> {
    base: I,
    map_op: F,
}

/// Create a new `Map` iterator.
///
/// NB: a free fn because it is NOT part of the end-user API.
pub fn new<I, F>(base: I, map_op: F) -> Map<I, F>
    where I: ParallelIterator
{
    Map {
        base: base,
        map_op: map_op,
    }
}

impl<I, F> ParallelIterator for Map<I, F>
    where I: ParallelIterator,
          F: MapOp<I::Item>
{
    type Item = F::Output;

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

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

impl<I, F> BoundedParallelIterator for Map<I, F>
    where I: BoundedParallelIterator,
          F: MapOp<I::Item>
{
    fn upper_bound(&mut self) -> usize {
        self.base.upper_bound()
    }

    fn drive<C>(self, consumer: C) -> C::Result
        where C: Consumer<Self::Item>
    {
        let consumer1 = MapConsumer::new(consumer, &self.map_op);
        self.base.drive(consumer1)
    }
}

impl<I, F> ExactParallelIterator for Map<I, F>
    where I: ExactParallelIterator,
          F: MapOp<I::Item>
{
    fn len(&mut self) -> usize {
        self.base.len()
    }
}

impl<I, F> IndexedParallelIterator for Map<I, F>
    where I: IndexedParallelIterator,
          F: MapOp<I::Item>
{
    fn with_producer<CB>(self, callback: CB) -> CB::Output
        where CB: ProducerCallback<Self::Item>
    {
        return self.base.with_producer(Callback {
                                           callback: callback,
                                           map_op: self.map_op,
                                       });

        struct Callback<CB, F> {
            callback: CB,
            map_op: F,
        }

        impl<T, F, CB> ProducerCallback<T> for Callback<CB, F>
            where F: MapOp<T>,
                  CB: ProducerCallback<F::Output>
        {
            type Output = CB::Output;

            fn callback<P>(self, base: P) -> CB::Output
                where P: Producer<Item = T>
            {
                let producer = MapProducer {
                    base: base,
                    map_op: &self.map_op,
                };
                self.callback.callback(producer)
            }
        }
    }
}

/// ////////////////////////////////////////////////////////////////////////

struct MapProducer<'f, P, F: 'f> {
    base: P,
    map_op: &'f F,
}

impl<'f, P, F> Producer for MapProducer<'f, P, F>
    where P: Producer,
          F: MapOp<P::Item>
{
    type Item = F::Output;
    type IntoIter = MapIter<'f, P::IntoIter, F>;

    fn into_iter(self) -> Self::IntoIter {
        MapIter {
            base: self.base.into_iter(),
            map_op: self.map_op,
        }
    }

    fn min_len(&self) -> usize {
        self.base.min_len()
    }
    fn max_len(&self) -> usize {
        self.base.max_len()
    }

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

pub struct MapIter<'f, T, F: 'f> {
    base: T,
    map_op: &'f F,
}

impl<'f, T, F> Iterator for MapIter<'f, T, F>
    where T: Iterator,
          F: MapOp<T::Item>
{
    type Item = F::Output;
    fn next(&mut self) -> Option<Self::Item> {
        self.base.next().map(|value| self.map_op.map(value))
    }
}

impl<'f, T, F> DoubleEndedIterator for MapIter<'f, T, F>
    where T: DoubleEndedIterator,
          F: MapOp<T::Item>
{
    fn next_back(&mut self) -> Option<Self::Item> {
        self.base.next_back().map(|value| self.map_op.map(value))
    }
}

impl<'f, T, F> ExactSizeIterator for MapIter<'f, T, F>
    where T: ExactSizeIterator,
          F: MapOp<T::Item>
{
    fn len(&self) -> usize {
        self.base.len()
    }
}


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

struct MapConsumer<'f, C, F: 'f> {
    base: C,
    map_op: &'f F,
}

impl<'f, C, F> MapConsumer<'f, C, F> {
    fn new(base: C, map_op: &'f F) -> Self {
        MapConsumer {
            base: base,
            map_op: map_op,
        }
    }
}

impl<'f, T, C, F> Consumer<T> for MapConsumer<'f, C, F>
    where C: Consumer<F::Output>,
          F: MapOp<T>
{
    type Folder = MapFolder<'f, C::Folder, F>;
    type Reducer = C::Reducer;
    type Result = C::Result;

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

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

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

impl<'f, T, C, F> UnindexedConsumer<T> for MapConsumer<'f, C, F>
    where C: UnindexedConsumer<F::Output>,
          F: MapOp<T>
{
    fn split_off_left(&self) -> Self {
        MapConsumer::new(self.base.split_off_left(), &self.map_op)
    }

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

struct MapFolder<'f, C, F: 'f> {
    base: C,
    map_op: &'f F,
}

impl<'f, T, C, F> Folder<T> for MapFolder<'f, C, F>
    where C: Folder<F::Output>,
          F: MapOp<T>
{
    type Result = C::Result;

    fn consume(self, item: T) -> Self {
        let map_op = self.map_op;
        let mapped_item = map_op.map(item);
        let base = self.base.consume(mapped_item);
        MapFolder {
            base: base,
            map_op: map_op,
        }
    }

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

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