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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
use {
std::{
fmt,
iter::{
self,
FromIterator,
FusedIterator
},
slice
},
smallvec::SmallVec
};
/// A structure which represents an n-dimensional shape.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Shape( SmallVec< [usize; 4] > );
impl Shape {
/// Constructs a new empty shape.
#[inline]
pub fn empty() -> Self {
[].into_iter().cloned().collect()
}
/// Constructs a new one dimensional shape.
#[inline]
pub fn new_1d( x: usize ) -> Self {
[x].into_iter().cloned().collect()
}
/// Constructs a new two dimensional shape.
#[inline]
pub fn new_2d( x: usize, y: usize ) -> Self {
[x, y].into_iter().cloned().collect()
}
/// Constructs a new three dimensional shape.
#[inline]
pub fn new_3d( x: usize, y: usize, z: usize ) -> Self {
[x, y, z].into_iter().cloned().collect()
}
/// Constructs a new four dimensional shape.
#[inline]
pub fn new_4d( x: usize, y: usize, z: usize, w: usize ) -> Self {
[x, y, z, w].into_iter().cloned().collect()
}
/// Checks whenever the product of all of the dimensions is zero.
#[inline]
pub fn is_zero( &self ) -> bool {
self.product() == 0
}
/// Returns the 1st dimension; will return `0` if the number of dimensions is zero.
#[inline]
pub fn x( &self ) -> usize {
if self.dimension_count() == 0 {
return 0;
}
self.0.get( 0 ).cloned().unwrap_or( 0 )
}
/// Returns the 2nd dimension; will return `0` if the number of dimensions is zero, `1` if it has less than two dimensions.
#[inline]
pub fn y( &self ) -> usize {
if self.dimension_count() == 0 {
return 0;
}
self.0.get( 1 ).cloned().unwrap_or( 1 )
}
/// Returns the 3rd dimension; will return `0` if the number of dimensions is zero, `1` if it has less than three dimensions.
#[inline]
pub fn z( &self ) -> usize {
if self.dimension_count() == 0 {
return 0;
}
self.0.get( 2 ).cloned().unwrap_or( 1 )
}
/// Returns the 4rd dimension; will return `0` if the number of dimensions is zero, `1` if it has less than four dimensions.
#[inline]
pub fn w( &self ) -> usize {
if self.dimension_count() == 0 {
return 0;
}
self.0.get( 3 ).cloned().unwrap_or( 1 )
}
/// Multiplies every dimension with each other and returns the result.
///
/// ```rust
/// # use sarek::Shape;
/// let shape = Shape::new_2d( 2, 3 );
/// assert_eq!( shape.product(), 6 );
/// ```
#[inline]
pub fn product( &self ) -> usize {
if self.dimension_count() == 0 {
return 0;
}
self.iter().product()
}
/// Returns the number of dimensions.
///
/// ```rust
/// # use sarek::Shape;
/// let shape = Shape::new_2d( 2, 3 );
/// assert_eq!( shape.dimension_count(), 2 );
/// ```
#[inline]
pub fn dimension_count( &self ) -> usize {
self.iter().len()
}
/// Returns an iterator over the dimensions.
///
/// ```rust
/// # use sarek::Shape;
/// let shape = Shape::new_2d( 2, 3 );
/// let mut iter = shape.iter();
/// assert_eq!( iter.next(), Some( 2 ) );
/// assert_eq!( iter.next(), Some( 3 ) );
/// assert_eq!( iter.next(), None );
/// ```
#[inline]
pub fn iter< 'a >( &'a self ) -> impl ExactSizeIterator< Item = usize > + FusedIterator + 'a {
self.0.iter().cloned()
}
/// Prepends an extra dimension to the shape.
///
/// ```rust
/// # use sarek::Shape;
/// let shape = Shape::new_2d( 2, 3 );
/// assert_eq!( shape.prepend( 1 ), Shape::new_3d( 1, 2, 3 ) );
/// ```
pub fn prepend( &self, value: usize ) -> Shape {
iter::once( value ).chain( self.iter() ).collect()
}
/// Appends an extra dimension to the shape.
///
/// ```rust
/// # use sarek::Shape;
/// let shape = Shape::new_2d( 1, 2 );
/// assert_eq!( shape.append( 3 ), Shape::new_3d( 1, 2, 3 ) );
/// ```
pub fn append( &self, value: usize ) -> Shape {
self.iter().chain( iter::once( value ) ).collect()
}
}
impl fmt::Display for Shape {
fn fmt( &self, fmt: &mut fmt::Formatter ) -> fmt::Result {
write!( fmt, "(" )?;
let mut iter = self.iter().peekable();
while let Some( value ) = iter.next() {
write!( fmt, "{}", value )?;
let is_last = iter.peek().is_none();
if !is_last {
write!( fmt, ", " )?;
}
}
write!( fmt, ")" )?;
Ok(())
}
}
impl FromIterator< usize > for Shape {
#[inline]
fn from_iter< T >( iter: T ) -> Self where T: IntoIterator< Item = usize > {
Shape( iter.into_iter().collect() )
}
}
pub struct Iter< 'a >( slice::Iter< 'a, usize > );
impl< 'a > Iterator for Iter< 'a > {
type Item = usize;
#[inline]
fn next( &mut self ) -> Option< Self::Item > {
self.0.next().cloned()
}
}
impl< 'a > ExactSizeIterator for Iter< 'a > {
#[inline]
fn len( &self ) -> usize {
self.0.len()
}
}
impl< 'a > FusedIterator for Iter< 'a > {}
impl< 'a > IntoIterator for &'a Shape {
type Item = usize;
type IntoIter = Iter< 'a >;
#[inline]
fn into_iter( self ) -> Self::IntoIter {
Iter( self.0.iter() )
}
}
impl From< usize > for Shape {
#[inline]
fn from( x: usize ) -> Self {
Shape::new_1d( x )
}
}
impl From< (usize,) > for Shape {
#[inline]
fn from( (x,): (usize,) ) -> Self {
Shape::new_1d( x )
}
}
impl From< (usize, usize) > for Shape {
#[inline]
fn from( (x, y): (usize, usize) ) -> Self {
Shape::new_2d( x, y )
}
}
impl From< (usize, usize, usize) > for Shape {
#[inline]
fn from( (x, y, z): (usize, usize, usize) ) -> Self {
Shape::new_3d( x, y, z )
}
}
impl From< (usize, usize, usize, usize) > for Shape {
#[inline]
fn from( (x, y, z, w): (usize, usize, usize, usize) ) -> Self {
Shape::new_4d( x, y, z, w )
}
}
#[test]
fn test_format_shape() {
assert_eq!(
format!( "{}", Shape::new_1d( 123 ) ),
"(123)"
);
assert_eq!(
format!( "{}", Shape::new_2d( 123, 456 ) ),
"(123, 456)"
);
}