-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathdim4.rs
100 lines (91 loc) · 2.34 KB
/
dim4.rs
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
use std::fmt;
use std::ops::Index;
/// Dim4 is used to store [Array](./struct.Array.html) dimensions
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct Dim4 {
dims: [u64; 4],
}
/// Default trait for Dim4 returns an Array of dimensions [1, 1, 1, 1]
impl Default for Dim4 {
fn default() -> Self {
Self { dims: [1, 1, 1, 1] }
}
}
/// Enables index operation for Dim4
///
/// # Examples
///
/// ```rust
/// use arrayfire::Dim4;
///
/// let dims = Dim4::new(&[4, 4, 2, 1]);
/// println!("0th Dimension length is {}", dims[0]); // -> 4
/// println!("1th Dimension length is {}", dims[1]); // -> 4
/// println!("2th Dimension length is {}", dims[2]); // -> 2
/// println!("3th Dimension length is {}", dims[3]); // -> 1
/// ```
impl Index<usize> for Dim4 {
type Output = u64;
fn index<'a>(&'a self, _index: usize) -> &'a u64 {
&self.dims[_index]
}
}
/// Enables use of Dim4 objects for printing it to display
///
/// # Examples
///
/// ```rust
/// use arrayfire::Dim4;
///
/// let dims = Dim4::new(&[4, 4, 2, 1]);
/// println!("0th Dimension length is {}", dims[0]); // -> 4
/// ```
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 {
/// Create Dim4 object
///
/// # Examples
///
/// ```rust
/// use arrayfire::Dim4;
/// let dims = Dim4::new(&[4, 4, 2, 1]);
/// ```
pub fn new(dims: &[u64; 4]) -> Self {
Self { dims: dims.clone() }
}
/// Get the number of elements represented by Dim4 object
pub fn elements(&self) -> u64 {
self.dims[0] * self.dims[1] * self.dims[2] * self.dims[3]
}
/// Get the number of dimensions of Dim4
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
}
}
}
}
/// Get the dimensions as a slice of 4 values
pub fn get(&self) -> &[u64; 4] {
&self.dims
}
}