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
use std::fmt;
use std::ops::{Index, IndexMut};
#[cfg(feature = "afserde")]
use serde::{Deserialize, Serialize};
#[derive(Copy, Clone, PartialEq, Debug)]
#[cfg_attr(feature = "afserde", derive(Serialize, Deserialize))]
pub struct Dim4 {
dims: [u64; 4],
}
impl Default for Dim4 {
fn default() -> Self {
Self { dims: [1, 1, 1, 1] }
}
}
impl Index<usize> for Dim4 {
type Output = u64;
fn index(&self, _index: usize) -> &u64 {
&self.dims[_index]
}
}
impl IndexMut<usize> for Dim4 {
fn index_mut(&mut self, _index: usize) -> &mut Self::Output {
&mut self.dims[_index]
}
}
impl fmt::Display for Dim4 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"[{} {} {} {}]",
self.dims[0], self.dims[1], self.dims[2], self.dims[3]
)
}
}
impl Dim4 {
pub fn new(dims: &[u64; 4]) -> Self {
Self {
dims: [dims[0], dims[1], dims[2], dims[3]],
}
}
pub fn elements(&self) -> u64 {
self.dims[0] * self.dims[1] * self.dims[2] * self.dims[3]
}
pub fn ndims(&self) -> usize {
let nelems = self.elements();
match nelems {
0 => 0,
1 => 1,
_ => {
if self.dims[3] != 1 {
4
} else if self.dims[2] != 1 {
3
} else if self.dims[1] != 1 {
2
} else {
1
}
}
}
}
pub fn get(&self) -> &[u64; 4] {
&self.dims
}
}
#[cfg(test)]
mod tests {
#[cfg(feature = "afserde")]
mod serde_tests {
use super::super::Dim4;
use crate::dim4;
#[test]
fn dim4_serde() {
let dims = dim4!(4, 4);
let serd = match serde_json::to_string(&dims) {
Ok(serialized_str) => serialized_str,
Err(e) => e.to_string(),
};
assert_eq!(serd, "{\"dims\":[4,4,1,1]}");
let deserd: Dim4 = serde_json::from_str(&serd).unwrap();
assert_eq!(deserd, dims);
}
}
}