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
use crate::Tensor;
use std::{collections::HashMap, iter, ops::Deref};

pub trait TensorIter {
    fn tensor_iter(&self) -> impl Iterator<Item = &Tensor>;
}

impl<'a, T> TensorIter for &'a T
where
    T: TensorIter,
{
    fn tensor_iter(&self) -> impl Iterator<Item = &Tensor> {
        (*self).tensor_iter()
    }
}

impl TensorIter for () {
    fn tensor_iter(&self) -> impl Iterator<Item = &Tensor> {
        iter::empty()
    }
}

impl TensorIter for Tensor {
    fn tensor_iter(&self) -> impl Iterator<Item = &Tensor> {
        iter::once(self)
    }
}

impl TensorIter for HashMap<usize, Tensor> {
    fn tensor_iter(&self) -> impl Iterator<Item = &Tensor> {
        self.values()
    }
}

impl<const N: usize> TensorIter for [Tensor; N] {
    fn tensor_iter(&self) -> impl Iterator<Item = &Tensor> {
        self.iter()
    }
}

impl<'a, const N: usize> TensorIter for [&'a Tensor; N] {
    fn tensor_iter(&self) -> impl Iterator<Item = &Tensor> {
        self.iter().map(Deref::deref)
    }
}

macro_rules! impl_tuple_tensor_iter {
    ($($T:tt)*) => {
        paste::paste! {
            impl<$($T,)*> TensorIter for ($($T,)*)
            where
                $($T: TensorIter,)*
            {
                fn tensor_iter(&self) -> impl Iterator<Item = &Tensor> {
                    let ($([<$T:lower 1>],)*) = self;
                    iter::empty()$(.chain([<$T:lower 1>].tensor_iter()))*
                }
            }
        }
    };
}

impl_tuple_tensor_iter!(A);
impl_tuple_tensor_iter!(A B);
impl_tuple_tensor_iter!(A B C);
impl_tuple_tensor_iter!(A B C D);
impl_tuple_tensor_iter!(A B C D E);
impl_tuple_tensor_iter!(A B C D E F);
impl_tuple_tensor_iter!(A B C D E F G);
impl_tuple_tensor_iter!(A B C D E F G H);
impl_tuple_tensor_iter!(A B C D E F G H I);
impl_tuple_tensor_iter!(A B C D E F G H I J);
impl_tuple_tensor_iter!(A B C D E F G H I J K);
impl_tuple_tensor_iter!(A B C D E F G H I J K L);