haar_lib/misc/
transpose.rs1pub trait Transpose {
5 type Output;
7 fn transpose(self) -> Self::Output;
9}
10
11macro_rules! impl_transpose {
12 ($($t:tt),+; $($index:tt),+) => {
13 impl<$($t),+> Transpose for Vec<($($t),+)> {
14 type Output = ($(Vec<$t>),+);
15 fn transpose(self) -> Self::Output {
16 let mut ret = ($(Vec::<$t>::new()),+);
17
18 for x in self {
19 $(
20 ret.$index.push(x.$index);
21 )+
22 }
23
24 ret
25 }
26 }
27 };
28}
29
30impl_transpose!(T0, T1; 0, 1);
31impl_transpose!(T0, T1, T2; 0, 1, 2);
32impl_transpose!(T0, T1, T2, T3; 0, 1, 2, 3);
33impl_transpose!(T0, T1, T2, T3, T4; 0, 1, 2, 3, 4);
34
35#[cfg(test)]
36mod tests {
37 use super::*;
38
39 #[test]
40 fn test() {
41 let a = vec![(1, "b", 0.4), (2, "aa", 0.3), (3, "ccc", -0.2)];
42
43 assert_eq!(
44 a.transpose(),
45 (vec![1, 2, 3], vec!["b", "aa", "ccc"], vec![0.4, 0.3, -0.2])
46 );
47 }
48}