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
extern crate futures;
extern crate futures_cpupool;
extern crate num_cpus;
use std::collections::VecDeque;
use std::sync::Arc;
use futures::Future;
use futures_cpupool::{CpuPool, CpuFuture};
pub trait ParMap: Iterator + Sized {
fn par_map<B, F>(self, f: F) -> Map<Self, B, F>
where F: Sync + Send + 'static + Fn(Self::Item) -> B,
B: Send + 'static,
Self::Item: Send + 'static
{
let num_threads = num_cpus::get();
let mut res = Map {
pool: CpuPool::new(num_threads),
queue: VecDeque::new(),
iter: self,
f: Arc::new(f),
};
for _ in 0..num_threads * 2 {
res.spawn();
}
res
}
fn par_flat_map<U, F>(self, f: F) -> FlatMap<Self, U, F>
where F: Sync + Send + 'static + Fn(Self::Item) -> U,
U: IntoIterator,
U::Item: Send + 'static,
Self::Item: Send + 'static
{
let num_threads = num_cpus::get();
let mut res = FlatMap {
pool: CpuPool::new(num_threads),
queue: VecDeque::new(),
iter: self,
f: Arc::new(f),
cur_iter: vec![].into_iter(),
};
for _ in 0..num_threads * 2 {
res.spawn();
}
res
}
}
impl<I: Iterator> ParMap for I {}
pub struct Map<I, B, F> {
pool: CpuPool,
queue: VecDeque<CpuFuture<B, ()>>,
iter: I,
f: Arc<F>,
}
impl<I: Iterator, B: Send + 'static, F> Map<I, B, F>
where F: Sync + Send + 'static + Fn(I::Item) -> B,
I::Item: Send + 'static
{
fn spawn(&mut self) {
let future = match self.iter.next() {
None => return,
Some(item) => {
let f = self.f.clone();
self.pool.spawn_fn(move || Ok(f(item)))
}
};
self.queue.push_back(future);
}
}
impl<I: Iterator, B: Send + 'static, F> Iterator for Map<I, B, F>
where F: Sync + Send + 'static + Fn(I::Item) -> B,
I::Item: Send + 'static
{
type Item = B;
fn next(&mut self) -> Option<Self::Item> {
self.queue.pop_front().map(|future| {
let i = future.wait().unwrap();
self.spawn();
i
})
}
}
pub struct FlatMap<I: Iterator, U: IntoIterator, F> {
pool: CpuPool,
queue: VecDeque<CpuFuture<Vec<U::Item>, ()>>,
iter: I,
f: Arc<F>,
cur_iter: ::std::vec::IntoIter<U::Item>,
}
impl<I: Iterator, U: IntoIterator, F> FlatMap<I, U, F>
where F: Sync + Send + 'static + Fn(I::Item) -> U,
U::Item: Send + 'static,
I::Item: Send + 'static
{
fn spawn(&mut self) {
let future = match self.iter.next() {
None => return,
Some(item) => {
let f = self.f.clone();
self.pool.spawn_fn(move || Ok(f(item).into_iter().collect()))
}
};
self.queue.push_back(future);
}
}
impl<I: Iterator, U: IntoIterator, F> Iterator for FlatMap<I, U, F>
where F: Sync + Send + 'static + Fn(I::Item) -> U,
U::Item: Send + 'static,
I::Item: Send + 'static
{
type Item = U::Item;
fn next(&mut self) -> Option<Self::Item> {
loop {
if let Some(item) = self.cur_iter.next() {
return Some(item);
}
let v = match self.queue.pop_front() {
Some(future) => future.wait().unwrap(),
None => return None,
};
self.cur_iter = v.into_iter();
self.spawn();
}
}
}