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
use super::super::Collate;
use super::TorchCollate;
use itertools::Itertools;

// Maybe an implementation passing the length and the index of elements to the macro could be more efficient than with the
// `Iterttols::multiunzip`.

/// `tuple` implementation, up to 16 elements.
macro_rules! tuple_impl {
    ($($name:ident)+) => {
        impl<$($name),+> Collate<($($name,)+)> for TorchCollate
        where
            $($name: Clone,)+
            $(TorchCollate: Collate<$name>,)+

        {
            type Output = ($(<TorchCollate as Collate<$name>>::Output,)+);

            #[allow(non_snake_case)]
            fn collate(&self, batch: Vec<($($name,)+)>) -> Self::Output {
                let copy = batch.to_vec();
                let ($($name,)+) = copy.into_iter().multiunzip();
                (
                    $(TorchCollate::default().collate($name),)+
                )

            }
        }
    };
}

tuple_impl! { A }
tuple_impl! { A B }
tuple_impl! { A B C }
tuple_impl! { A B C D }
tuple_impl! { A B C D E }
tuple_impl! { A B C D E F }
tuple_impl! { A B C D E F G }
tuple_impl! { A B C D E F G H }
tuple_impl! { A B C D E F G H I }
tuple_impl! { A B C D E F G H I J }
tuple_impl! { A B C D E F G H I J K }
tuple_impl! { A B C D E F G H I J K L }

#[cfg(test)]
mod tests {
    use super::*;

    use tch::Tensor;

    #[test]
    fn vec_of_tuple() {
        assert_eq!(
            TorchCollate::default().collate(vec![(1, 2)]),
            (Tensor::of_slice(&[1]), Tensor::of_slice(&[2]))
        );
        assert_eq!(
            TorchCollate::default().collate(vec![(1.0, 2.0), (3.0, 4.0)]),
            (Tensor::of_slice(&[1.0, 3.0]), Tensor::of_slice(&[2.0, 4.0]))
        );
        assert_eq!(
            TorchCollate::default().collate(vec![(1, 2), (3, 4)]),
            (Tensor::of_slice(&[1, 3]), Tensor::of_slice(&[2, 4]))
        );
        assert_eq!(
            TorchCollate::default().collate(vec![(-1, 2), (3, 4)]),
            (Tensor::of_slice(&[-1, 3]), Tensor::of_slice(&[2, 4]))
        );
        assert_eq!(
            TorchCollate::default().collate(vec![(1.0, 2.0), (3.0, 4.0), (5.0, 6.0)]),
            (
                Tensor::of_slice(&[1.0, 3.0, 5.0]),
                Tensor::of_slice(&[2.0, 4.0, 6.0])
            )
        );
    }
    #[test]
    fn vec_of_tuple_with_len_1() {
        assert_eq!(
            TorchCollate::default().collate(vec![(1,)]),
            (Tensor::of_slice(&[1]),)
        );
    }

    #[test]
    fn vec_of_tuple_with_len_2() {
        assert_eq!(
            TorchCollate::default().collate(vec![(1, 2.0)]),
            (Tensor::of_slice(&[1]), Tensor::of_slice(&[2.0]))
        );
        assert_eq!(
            TorchCollate::default().collate(vec![(1, 2.0), (3, 4.0)]),
            (Tensor::of_slice(&[1, 3]), Tensor::of_slice(&[2.0, 4.0]))
        );
        assert_eq!(
            TorchCollate::default().collate(vec![(-1, true), (-3, false)]),
            (
                Tensor::of_slice(&[-1, -3]),
                Tensor::of_slice(&[true, false])
            )
        );
        assert_eq!(
            TorchCollate::default().collate(vec![(-1, true), (3, false)]),
            (Tensor::of_slice(&[-1, 3]), Tensor::of_slice(&[true, false]))
        );
        assert_eq!(
            TorchCollate::default().collate(vec![(1, 2.0), (3, 4.0), (5, 6.0)]),
            (
                Tensor::of_slice(&[1, 3, 5]),
                Tensor::of_slice(&[2.0, 4.0, 6.0])
            )
        );
    }
    #[test]
    fn vec_of_tuple_with_len_3() {
        assert_eq!(
            TorchCollate::default().collate(vec![(1, 2.0, true)]),
            (
                Tensor::of_slice(&[1]),
                Tensor::of_slice(&[2.0]),
                Tensor::of_slice(&[true])
            )
        );
        assert_eq!(
            TorchCollate::default().collate(vec![(1, 2.0, true), (3, 4.0, true)]),
            (
                Tensor::of_slice(&[1, 3]),
                Tensor::of_slice(&[2.0, 4.0]),
                Tensor::of_slice(&[true, true])
            )
        );
        assert_eq!(
            TorchCollate::default().collate(vec![(1, 2.0, true), (3, 4.0, false), (5, 6.0, true)]),
            (
                Tensor::of_slice(&[1, 3, 5]),
                Tensor::of_slice(&[2.0, 4.0, 6.0]),
                Tensor::of_slice(&[true, false, true])
            )
        );
    }
}