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
pub use self::actual::map_reduce;
pub use self::actual::parallel_collect;
pub use self::actual::parallel_into_collect;
#[cfg(not(target_arch = "wasm32"))]
mod actual {
extern crate rayon;
use rayon::prelude::*;
pub fn parallel_collect<T, F, R>(source: &[T], map_op: F) -> Vec<R>
where
T: Send + Sync,
F: Fn(&T) -> R + Sync + Send,
R: Send,
{
source.par_iter().map(map_op).collect()
}
pub fn parallel_into_collect<T, F, R>(source: Vec<T>, map_op: F) -> Vec<R>
where
T: Send + Sync,
F: Fn(T) -> R + Sync + Send,
R: Send,
{
source.into_par_iter().map(map_op).collect()
}
pub fn map_reduce<T, FM, FR, FD, R>(source: &[T], map_op: FM, default_op: FD, reduce_op: FR) -> R
where
T: Send + Sync,
FM: Fn(&T) -> R + Sync + Send,
FR: Fn(R, R) -> R + Sync + Send,
FD: Fn() -> R + Sync + Send,
R: Send,
{
source.par_iter().map(map_op).reduce(default_op, reduce_op)
}
}
#[cfg(target_arch = "wasm32")]
mod actual {
pub fn parallel_collect<T, F, R>(source: &[T], map_op: F) -> Vec<R>
where
T: Send + Sync,
F: Fn(&T) -> R + Sync + Send,
R: Send,
{
source.iter().map(map_op).collect()
}
pub fn parallel_into_collect<T, F, R>(source: Vec<T>, map_op: F) -> Vec<R>
where
T: Send + Sync,
F: Fn(T) -> R + Sync + Send,
R: Send,
{
source.into_iter().map(map_op).collect()
}
pub fn map_reduce<T, FM, FR, FD, R>(source: &[T], map_op: FM, default_op: FD, reduce_op: FR) -> R
where
T: Send + Sync,
FM: Fn(&T) -> R + Sync + Send,
FR: Fn(R, R) -> R + Sync + Send,
FD: Fn() -> R + Sync + Send,
R: Send,
{
source.iter().map(map_op).fold(default_op(), reduce_op)
}
}