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
pub const SWITCH_SEQUENTIAL_DEFAULT:usize = 6;
#[inline]
pub fn compute_level_switch_sequential(depth: usize, height: usize) -> Parallel {
let dd = depth;
let gg = if height <= dd { 0 } else { height - dd };
Parallel::new(gg)
}
pub enum ParResult<X,Y>{
Parallel([X;2]),
Sequential([Y;2])
}
pub trait Joiner:Sized+Send+Sync{
fn next(self)->ParResult<Self,Sequential>;
}
#[derive(Copy, Clone)]
pub struct Parallel{
depth_to_switch_at:usize,
current_depth:usize
}
impl Parallel {
pub fn new(depth_to_switch_at: usize) -> Self {
Parallel{depth_to_switch_at,current_depth:0}
}
pub fn get_depth_to_switch_at(&self)->usize{
self.depth_to_switch_at
}
pub fn get_current_depth(&self)->usize{
self.current_depth
}
}
impl Joiner for Parallel {
fn next(mut self)->ParResult<Self,Sequential>{
if self.current_depth >= self.depth_to_switch_at{
ParResult::Sequential([Sequential,Sequential])
}else{
self.current_depth+=1;
ParResult::Parallel([self;2])
}
}
}
#[derive(Copy, Clone)]
pub struct Sequential;
impl Joiner for Sequential {
fn next(self)->ParResult<Self,Sequential>{
ParResult::Sequential([Sequential,Sequential])
}
}