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
pub trait CountElements: Clone {
type Dtype: Clone + Default;
const NUM_ELEMENTS: usize;
const NUM_BYTES: usize = Self::NUM_ELEMENTS * std::mem::size_of::<Self::Dtype>();
fn ref_first_elem(&self) -> &Self::Dtype;
fn mut_first_elem(&mut self) -> &mut Self::Dtype;
}
impl CountElements for f32 {
type Dtype = Self;
const NUM_ELEMENTS: usize = 1;
fn ref_first_elem(&self) -> &Self::Dtype {
self
}
fn mut_first_elem(&mut self) -> &mut Self::Dtype {
self
}
}
impl CountElements for usize {
type Dtype = Self;
const NUM_ELEMENTS: usize = 1;
fn ref_first_elem(&self) -> &Self::Dtype {
self
}
fn mut_first_elem(&mut self) -> &mut Self::Dtype {
self
}
}
impl<T: CountElements, const M: usize> CountElements for [T; M] {
type Dtype = T::Dtype;
const NUM_ELEMENTS: usize = M * T::NUM_ELEMENTS;
fn ref_first_elem(&self) -> &Self::Dtype {
self[0].ref_first_elem()
}
fn mut_first_elem(&mut self) -> &mut Self::Dtype {
self[0].mut_first_elem()
}
}
pub trait HasAxis<const I: isize> {
const SIZE: usize;
}
macro_rules! impl_has_axis {
($SrcTy:tt, $Axis:expr, $Size:expr, {$($Vars:tt),*}) => {
impl<$(const $Vars: usize, )*> HasAxis<$Axis> for $SrcTy {
const SIZE: usize = $Size;
}
};
}
impl_has_axis!(f32, 0, 1, {});
impl_has_axis!(f32, -1, 1, {});
impl_has_axis!([f32; M], 0, M, { M });
impl_has_axis!([f32; M], -1, M, { M });
impl_has_axis!([[f32; N]; M], 0, M, {M, N});
impl_has_axis!([[f32; N]; M], 1, N, {M, N});
impl_has_axis!([[f32; N]; M], -1, N, {M, N});
impl_has_axis!([[[f32; O]; N]; M], 0, M, {M, N, O});
impl_has_axis!([[[f32; O]; N]; M], 1, N, {M, N, O});
impl_has_axis!([[[f32; O]; N]; M], 2, O, {M, N, O});
impl_has_axis!([[[f32; O]; N]; M], -1, O, {M, N, O});
impl_has_axis!([[[[f32; P]; O]; N]; M], 0, M, {M, N, O, P});
impl_has_axis!([[[[f32; P]; O]; N]; M], 1, N, {M, N, O, P});
impl_has_axis!([[[[f32; P]; O]; N]; M], 2, O, {M, N, O, P});
impl_has_axis!([[[[f32; P]; O]; N]; M], 3, P, {M, N, O, P});
impl_has_axis!([[[[f32; P]; O]; N]; M], -1, P, {M, N, O, P});
pub trait ZeroElements {
const ZEROS: Self;
}
impl ZeroElements for f32 {
const ZEROS: Self = 0.0;
}
impl<T: ZeroElements, const M: usize> ZeroElements for [T; M] {
const ZEROS: Self = [T::ZEROS; M];
}
pub trait HasArrayType {
type Dtype;
type Array: 'static
+ Sized
+ Clone
+ CountElements<Dtype = Self::Dtype>
+ ZeroElements
+ HasAxis<0>
+ HasAxis<-1>;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_0d_count() {
assert_eq!(1, f32::NUM_ELEMENTS);
}
#[test]
fn test_1d_count() {
assert_eq!(5, <[f32; 5]>::NUM_ELEMENTS);
}
#[test]
fn test_2d_count() {
assert_eq!(15, <[[f32; 3]; 5]>::NUM_ELEMENTS);
}
#[test]
fn test_3d_count() {
assert_eq!(30, <[[[f32; 2]; 3]; 5]>::NUM_ELEMENTS);
}
#[test]
fn test_first_elem_ref() {
let mut a: [[f32; 2]; 3] = [[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]];
assert_eq!(a.ref_first_elem(), &1.0);
assert_eq!(a.mut_first_elem(), &mut 1.0);
}
}