forked from arrayfire/arrayfire-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdim4.rs
More file actions
119 lines (109 loc) · 2.79 KB
/
dim4.rs
File metadata and controls
119 lines (109 loc) · 2.79 KB
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
use std::fmt;
use std::ops::{Index, IndexMut};
/// 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(&self, _index: usize) -> &u64 {
&self.dims[_index]
}
}
/// Enables index operation for Dim4 to modify dimensions
///
/// # Examples
///
/// ```rust
/// use arrayfire::Dim4;
///
/// let mut dims = Dim4::new(&[4, 4, 2, 1]);
/// dims[2] = 4;
/// println!("Dimensions: {}", dims); // note that third dimension changed to 4
/// ```
impl IndexMut<usize> for Dim4 {
fn index_mut(&mut self, _index: usize) -> &mut Self::Output {
&mut 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[0], dims[1], dims[2], dims[3]],
}
}
/// 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
}
}