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
#![forbid(missing_docs)]
#![forbid(unsafe_code)]
extern crate moite_moite;
extern crate rayon;
use rayon::iter::plumbing::{Consumer, ProducerCallback, UnindexedConsumer};
use rayon::iter::{IndexedParallelIterator, ParallelIterator};
mod consumer;
mod producer;
pub trait ParallelIteratorExt: ParallelIterator {
fn mapfold_reduce_into<'t, Output, Target, Mapfold, Reduce>(
self,
target: &'t mut Target,
mapfold: Mapfold,
reduce: Reduce,
) -> MapfoldReduce<'t, Target, Self, Mapfold, Reduce>
where
Output: Send,
Target: Default + Send + 't,
Mapfold: Clone + Fn(&mut Target, Self::Item) -> Output,
Reduce: Clone + Fn(&mut Target, Target),
{
MapfoldReduce {
target: target,
input: self,
mapfold: mapfold,
reduce: reduce,
}
}
}
impl<Input> ParallelIteratorExt for Input where Input: ParallelIterator {}
pub struct MapfoldReduce<'t, Target: 't, Input, Mapfold, Reduce> {
target: &'t mut Target,
input: Input,
mapfold: Mapfold,
reduce: Reduce,
}
impl<'t, Output, Target, Input, Mapfold, Reduce> ParallelIterator
for MapfoldReduce<'t, Target, Input, Mapfold, Reduce>
where
Output: Send,
Target: Default + Send + 't,
Input: ParallelIterator,
Mapfold: Clone + Fn(&mut Target, Input::Item) -> Output + Send,
Reduce: Clone + Fn(&mut Target, Target) + Send,
{
type Item = Output;
fn drive_unindexed<C>(self, consumer: C) -> C::Result
where
C: UnindexedConsumer<Output>,
{
let iter_consumer =
consumer::MapfoldReduceConsumer::new(self.target, self.mapfold, self.reduce, consumer);
self.input.drive_unindexed(iter_consumer).into_output()
}
fn opt_len(&self) -> Option<usize> {
self.input.opt_len()
}
}
impl<'t, Output, Target, Input, Mapfold, Reduce> IndexedParallelIterator
for MapfoldReduce<'t, Target, Input, Mapfold, Reduce>
where
Input: IndexedParallelIterator,
Target: Default + Send + 't,
Output: Send,
Mapfold: Clone + Fn(&mut Target, Input::Item) -> Output + Send,
Reduce: Clone + Fn(&mut Target, Target) + Send,
{
fn len(&self) -> usize {
self.input.len()
}
fn drive<C>(self, consumer: C) -> C::Result
where
C: Consumer<Output>,
{
let iter_consumer =
consumer::MapfoldReduceConsumer::new(self.target, self.mapfold, self.reduce, consumer);
self.input.drive(iter_consumer).into_output()
}
fn with_producer<CB>(self, callback: CB) -> CB::Output
where
CB: ProducerCallback<Self::Item>,
{
self.input
.with_producer(producer::MapfoldReduceCallback::new(
self.target,
self.mapfold,
self.reduce,
callback,
))
}
}
enum Sink<'t, T> {
Borrowed(&'t mut T),
Owned(T),
}
impl<'t, T> Sink<'t, T> {
fn as_mut(&mut self) -> &mut T {
match *self {
Sink::Borrowed(ref mut borrowed) => borrowed,
Sink::Owned(ref mut owned) => owned,
}
}
fn into_owned(self) -> T {
match self {
Sink::Borrowed(_) => panic!("sink is borrowed"),
Sink::Owned(owned) => owned,
}
}
}