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
use super::axis::Axis;
macro_rules! count_idents {
($($idents:ident),* $(,)*) => {
{
#[allow(dead_code, non_camel_case_types)]
enum Idents { $($idents,)* __CountIdentsLast }
const COUNT: usize = Idents::__CountIdentsLast as usize;
COUNT
}
};
}
macro_rules! impl_axes {
() => {
pub trait Axes: Axis {
fn num_dim(&self) -> usize;
}
};
( $type_parameter:ident: $index:tt, ) => {
impl<X: Axis> Axes for (X,) {
fn num_dim(&self) -> usize { 1 }
}
impl<X: Axis> Axis for (X,) {
type Coordinate = X::Coordinate;
type BinInterval = X::BinInterval;
fn index(&self, coordinate: &Self::Coordinate) -> Option<usize> {
self.0.index(coordinate)
}
fn numbins(&self) -> usize {
self.0.numbins()
}
fn bin(&self, index: usize) -> Option<Self::BinInterval> {
self.0.bin(index)
}
}
impl_axes!();
};
( $($nth_type_parameter:ident: $nth_index:tt, )+ ) => {
impl<$($nth_type_parameter: Axis),*> Axes for ($($nth_type_parameter),*) {
fn num_dim(&self) -> usize { count_idents!($($nth_type_parameter),*) }
}
impl<$($nth_type_parameter: Axis),*> Axis for ($($nth_type_parameter),*) {
type Coordinate = ($($nth_type_parameter::Coordinate),*);
type BinInterval = ($($nth_type_parameter::BinInterval),*);
fn index(&self, coordinate: &Self::Coordinate) -> Option<usize> {
let numbins: Vec<_> = [$(self.$nth_index.numbins()),*].iter().scan(1, |acc, nbin| {*acc *= *nbin; Some(*acc)}).collect();
let indices = [$(self.$nth_index.index(&coordinate.$nth_index)?),*];
let index = numbins.iter()
.rev()
.skip(1)
.zip(indices.iter().rev())
.fold(indices[0], |acc, (nbin, idx)| acc + nbin*idx);
Some(index)
}
fn numbins(&self) -> usize {
$(self.$nth_index.numbins()*)* 1
}
fn bin(&self, index: usize) -> Option<Self::BinInterval> {
let numbins = [$(self.$nth_index.numbins()),*];
let product = numbins.iter().scan(1, |acc, it| Some(*acc * *it));
let mut index = index;
let index: Vec<_> = product.map(|nb| {
let v = index % nb;
index /= nb;
v
} ).collect();
Some(
(
$(self.$nth_index.bin(index[$nth_index])?),*
)
)
}
}
impl_axes!(@REMOVELAST $([$nth_index AND $nth_type_parameter],)*);
};
(@REMOVELAST [$index:tt AND $type_parameter:ident], $( [$nth_index:tt AND $nth_type_parameter:ident], )+ ) => {
impl_axes!(@REMOVELAST [$index AND $type_parameter], @SEPARATOR $([$nth_index AND $nth_type_parameter],)*);
};
(@REMOVELAST $( [$first_index:tt AND $first_type_parameter:ident], )+ @SEPARATOR [$index1:tt AND $type_parameter1:ident], [$index2:tt AND $type_parameter2:ident], [$index3:tt AND $type_parameter3:ident], [$index4:tt AND $type_parameter4:ident], $( [$nth_index:tt AND $nth_type_parameter:ident], )+ ) => {
impl_axes!(@REMOVELAST $([$first_index AND $first_type_parameter],)* [$index1 AND $type_parameter1], [$index2 AND $type_parameter2], [$index3 AND $type_parameter3], [$index4 AND $type_parameter4], @SEPARATOR $([$nth_index AND $nth_type_parameter],)*);
};
(@REMOVELAST $( [$first_index:tt AND $first_type_parameter:ident], )+ @SEPARATOR [$index1:tt AND $type_parameter1:ident], [$index2:tt AND $type_parameter2:ident], $( [$nth_index:tt AND $nth_type_parameter:ident], )+ ) => {
impl_axes!(@REMOVELAST $([$first_index AND $first_type_parameter],)* [$index1 AND $type_parameter1], [$index2 AND $type_parameter2], @SEPARATOR $([$nth_index AND $nth_type_parameter],)*);
};
(@REMOVELAST $( [$first_index:tt AND $first_type_parameter:ident], )+ @SEPARATOR [$index:tt AND $type_parameter:ident], $( [$nth_index:tt AND $nth_type_parameter:ident], )+ ) => {
impl_axes!(@REMOVELAST $([$first_index AND $first_type_parameter],)* [$index AND $type_parameter], @SEPARATOR $([$nth_index AND $nth_type_parameter],)*);
};
(@REMOVELAST $( [$first_index:tt AND $first_type_parameter:ident], )+ @SEPARATOR [$index:tt AND $type_parameter:ident], ) => {
impl_axes!($($first_type_parameter: $first_index,)*);
};
}
impl_axes! {
X: 0,
Y: 1,
Z: 2,
T: 3,
D4: 4,
D5: 5,
D6: 6,
D7: 7,
D8: 8,
D9: 9,
D10: 10,
D11: 11,
D12: 12,
D13: 13,
D14: 14,
D15: 15,
D16: 16,
D17: 17,
D18: 18,
D19: 19,
D20: 20,
}