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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
use std::ops::{Index, IndexMut};

use crate::dim::{DimTrait, GreaterDimTrait, LessDimTrait};

#[derive(Clone, Debug, Copy, Default)]
pub struct Dim0 {}

impl Dim0 {
    pub fn new() -> Self {
        Self {}
    }
}

impl Index<usize> for Dim0 {
    type Output = usize;

    fn index(&self, _: usize) -> &Self::Output {
        &0
    }
}

impl IndexMut<usize> for Dim0 {
    fn index_mut(&mut self, _: usize) -> &mut Self::Output {
        todo!();
    }
}

impl PartialEq for Dim0 {
    fn eq(&self, _: &Self) -> bool {
        true
    }
}

impl Iterator for Dim0 {
    type Item = usize;

    fn next(&mut self) -> Option<Self::Item> {
        None
    }
}

impl DimTrait for Dim0 {
    fn len(&self) -> usize {
        0
    }

    fn is_empty(&self) -> bool {
        todo!();
    }

    fn is_overflow<D: DimTrait>(&self, index: D) -> bool {
        index.len() != 0
    }

    fn slice(&self) -> &[usize] {
        &[]
    }
}

impl From<&[usize]> for Dim0 {
    fn from(dim: &[usize]) -> Self {
        if !dim.is_empty() {
            panic!("Invalid dimension");
        }
        Dim0 {}
    }
}

impl From<&[usize; 0]> for Dim0 {
    fn from(dim: &[usize; 0]) -> Self {
        if dim.is_empty() {
            panic!("Invalid dimension");
        }
        Dim0 {}
    }
}

impl From<[usize; 0]> for Dim0 {
    fn from(dim: [usize; 0]) -> Self {
        if !dim.is_empty() {
            panic!("Invalid dimension");
        }
        Dim0 {}
    }
}

macro_rules! impl_dim {
    ($name:ident, $number_of_elm:expr) => {
        #[derive(Clone, Debug, Copy, Default)]
        pub struct $name {
            dim: [usize; $number_of_elm],
        }

        impl $name {
            pub fn new(dim: [usize; $number_of_elm]) -> Self {
                Self { dim }
            }

            pub fn dim(&self) -> &[usize; $number_of_elm] {
                &self.dim
            }
        }

        impl Index<usize> for $name {
            type Output = usize;

            fn index(&self, index: usize) -> &Self::Output {
                if index >= self.dim.len() {
                    panic!("Index out of range");
                }
                &self.dim[index]
            }
        }

        impl IndexMut<usize> for $name {
            fn index_mut(&mut self, index: usize) -> &mut Self::Output {
                if index >= self.dim.len() {
                    panic!("Index out of range");
                }
                &mut self.dim[index]
            }
        }

        impl IntoIterator for $name {
            type Item = usize;
            type IntoIter = std::array::IntoIter<Self::Item, $number_of_elm>;

            fn into_iter(self) -> Self::IntoIter {
                self.dim.into_iter()
            }
        }

        impl PartialEq for $name {
            fn eq(&self, other: &Self) -> bool {
                self.dim == other.dim
            }
        }

        impl DimTrait for $name {
            fn len(&self) -> usize {
                self.dim.len()
            }

            fn is_empty(&self) -> bool {
                self.dim.iter().all(|&d| d == 0)
            }

            fn slice(&self) -> &[usize] {
                &self.dim
            }
        }

        impl From<&[usize]> for $name {
            fn from(slice: &[usize]) -> Self {
                let mut array = [0; $number_of_elm];
                array[..slice.len()].copy_from_slice(slice);
                $name { dim: array }
            }
        }

        impl From<&[usize; $number_of_elm]> for $name {
            fn from(slice: &[usize; $number_of_elm]) -> Self {
                let mut array = [0; $number_of_elm];
                array.copy_from_slice(slice);
                $name { dim: array }
            }
        }

        impl From<[usize; $number_of_elm]> for $name {
            fn from(array: [usize; $number_of_elm]) -> Self {
                $name { dim: array }
            }
        }
    };
}

impl_dim!(Dim1, 1);
impl_dim!(Dim2, 2);
impl_dim!(Dim3, 3);
impl_dim!(Dim4, 4);

macro_rules! impl_less_dim {
    ($impl_ty:ty, $less_dim:ty) => {
        impl LessDimTrait for $impl_ty {
            type LessDim = $less_dim;
        }
    };
}
impl_less_dim!(Dim1, Dim0);
impl_less_dim!(Dim2, Dim1);
impl_less_dim!(Dim3, Dim2);
impl_less_dim!(Dim4, Dim3);

macro_rules! impl_grater_dim_trait {
    ($impl_ty:ty, $less_dim:ty) => {
        impl GreaterDimTrait for $impl_ty {
            type GreaterDim = $less_dim;
        }
    };
}
impl_grater_dim_trait!(Dim0, Dim1);
impl_grater_dim_trait!(Dim1, Dim2);
impl_grater_dim_trait!(Dim2, Dim3);
impl_grater_dim_trait!(Dim3, Dim4);