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
use join;
use super::IndexedParallelIterator;
use super::len::*;
pub trait ProducerCallback<ITEM> {
type Output;
fn callback<P>(self, producer: P) -> Self::Output
where P: Producer<Item=ITEM>;
}
pub trait Producer: IntoIterator + Send {
fn cost(&mut self, len: usize) -> f64;
fn split_at(self, index: usize) -> (Self, Self);
}
pub trait Consumer<Item>: Send {
type Folder: Folder<Item, Result=Self::Result>;
type Reducer: Reducer<Self::Result>;
type Result: Send;
fn cost(&mut self, producer_cost: f64) -> f64;
fn split_at(self, index: usize) -> (Self, Self, Self::Reducer);
fn into_folder(self) -> Self::Folder;
}
pub trait Folder<Item> {
type Result;
fn consume(self, item: Item) -> Self;
fn complete(self) -> Self::Result;
}
pub trait Reducer<Result> {
fn reduce(self, left: Result, right: Result) -> Result;
}
pub trait UnindexedConsumer<ITEM>: Consumer<ITEM> {
fn split_off(&self) -> Self;
fn to_reducer(&self) -> Self::Reducer;
}
pub fn bridge<PAR_ITER,C>(mut par_iter: PAR_ITER,
consumer: C)
-> C::Result
where PAR_ITER: IndexedParallelIterator, C: Consumer<PAR_ITER::Item>
{
let len = par_iter.len();
return par_iter.with_producer(Callback { len: len,
consumer: consumer, });
struct Callback<C> {
len: usize,
consumer: C,
}
impl<C, ITEM> ProducerCallback<ITEM> for Callback<C>
where C: Consumer<ITEM>
{
type Output = C::Result;
fn callback<P>(mut self, mut producer: P) -> C::Result
where P: Producer<Item=ITEM>
{
let producer_cost = producer.cost(self.len);
let cost = self.consumer.cost(producer_cost);
bridge_producer_consumer(self.len, cost, producer, self.consumer)
}
}
}
fn bridge_producer_consumer<P,C>(len: usize,
cost: f64,
producer: P,
consumer: C)
-> C::Result
where P: Producer, C: Consumer<P::Item>
{
if len > 1 && cost > THRESHOLD {
let mid = len / 2;
let (left_producer, right_producer) = producer.split_at(mid);
let (left_consumer, right_consumer, reducer) = consumer.split_at(mid);
let (left_result, right_result) =
join(|| bridge_producer_consumer(mid, cost / 2.0,
left_producer, left_consumer),
|| bridge_producer_consumer(len - mid, cost / 2.0,
right_producer, right_consumer));
reducer.reduce(left_result, right_result)
} else {
let mut folder = consumer.into_folder();
for item in producer {
folder = folder.consume(item);
}
folder.complete()
}
}
pub struct NoopReducer;
impl Reducer<()> for NoopReducer {
fn reduce(self, _left: (), _right: ()) { }
}