Skip to content

Commit b980ab9

Browse files
authored
Merge pull request #509 from jturner314/dimension-zeros
Rename zero_index_with_ndim -> zeros, make it public, and remove zero_index
2 parents d8dccdc + a760d6e commit b980ab9

File tree

3 files changed

+15
-31
lines changed

3 files changed

+15
-31
lines changed

src/dimension/dimension_trait.rs

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -151,20 +151,13 @@ pub trait Dimension : Clone + Eq + Debug + Send + Sync + Default +
151151
strides
152152
}
153153

154-
#[doc(hidden)]
155-
// Return an index of same dimensionality
156-
fn zero_index(&self) -> Self {
157-
Self::default()
158-
}
159-
160-
#[doc(hidden)]
161-
/// Return an index of same type and with the specified dimensionality.
154+
/// Creates a dimension of all zeros with the specified ndim.
162155
///
163156
/// This method is useful for generalizing over fixed-size and
164157
/// variable-size dimension representations.
165158
///
166159
/// **Panics** if `Self` has a fixed size that is not `ndim`.
167-
fn zero_index_with_ndim(ndim: usize) -> Self;
160+
fn zeros(ndim: usize) -> Self;
168161

169162
#[doc(hidden)]
170163
#[inline]
@@ -174,11 +167,7 @@ pub trait Dimension : Clone + Eq + Debug + Send + Sync + Default +
174167
return None;
175168
}
176169
}
177-
let mut index = self.clone();
178-
for rr in index.slice_mut().iter_mut() {
179-
*rr = 0;
180-
}
181-
Some(index)
170+
Some(Self::zeros(self.ndim()))
182171
}
183172

184173
#[doc(hidden)]
@@ -381,7 +370,7 @@ impl Dimension for Dim<[Ix; 0]> {
381370
#[inline]
382371
fn into_pattern(self) -> Self::Pattern { }
383372
#[inline]
384-
fn zero_index_with_ndim(ndim: usize) -> Self {
373+
fn zeros(ndim: usize) -> Self {
385374
assert_eq!(ndim, 0);
386375
Self::default()
387376
}
@@ -417,7 +406,7 @@ impl Dimension for Dim<[Ix; 1]> {
417406
get!(&self, 0)
418407
}
419408
#[inline]
420-
fn zero_index_with_ndim(ndim: usize) -> Self {
409+
fn zeros(ndim: usize) -> Self {
421410
assert_eq!(ndim, 1);
422411
Self::default()
423412
}
@@ -511,7 +500,7 @@ impl Dimension for Dim<[Ix; 2]> {
511500
#[inline]
512501
fn slice_mut(&mut self) -> &mut [Ix] { self.ixm() }
513502
#[inline]
514-
fn zero_index_with_ndim(ndim: usize) -> Self {
503+
fn zeros(ndim: usize) -> Self {
515504
assert_eq!(ndim, 2);
516505
Self::default()
517506
}
@@ -656,7 +645,7 @@ impl Dimension for Dim<[Ix; 3]> {
656645
}
657646

658647
#[inline]
659-
fn zero_index_with_ndim(ndim: usize) -> Self {
648+
fn zeros(ndim: usize) -> Self {
660649
assert_eq!(ndim, 3);
661650
Self::default()
662651
}
@@ -765,7 +754,7 @@ macro_rules! large_dim {
765754
#[inline]
766755
fn slice_mut(&mut self) -> &mut [Ix] { self.ixm() }
767756
#[inline]
768-
fn zero_index_with_ndim(ndim: usize) -> Self {
757+
fn zeros(ndim: usize) -> Self {
769758
assert_eq!(ndim, $n);
770759
Self::default()
771760
}
@@ -818,12 +807,7 @@ impl Dimension for IxDyn
818807
}
819808

820809
#[inline]
821-
fn zero_index(&self) -> Self {
822-
IxDyn::zeros(self.ndim())
823-
}
824-
825-
#[inline]
826-
fn zero_index_with_ndim(ndim: usize) -> Self {
810+
fn zeros(ndim: usize) -> Self {
827811
IxDyn::zeros(ndim)
828812
}
829813

src/impl_methods.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,8 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
318318

319319
// Copy the dim and strides that remain after removing the subview axes.
320320
let out_ndim = info.out_ndim();
321-
let mut new_dim = Do::zero_index_with_ndim(out_ndim);
322-
let mut new_strides = Do::zero_index_with_ndim(out_ndim);
321+
let mut new_dim = Do::zeros(out_ndim);
322+
let mut new_strides = Do::zeros(out_ndim);
323323
izip!(self.dim.slice(), self.strides.slice(), indices)
324324
.filter_map(|(d, s, slice_or_index)| match slice_or_index {
325325
&SliceOrIndex::Slice {..} => Some((d, s)),
@@ -1401,7 +1401,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
14011401
{
14021402
let axes = axes.into_dimension();
14031403
// Ensure that each axis is used exactly once.
1404-
let mut usage_counts = D::zero_index_with_ndim(self.ndim());
1404+
let mut usage_counts = D::zeros(self.ndim());
14051405
for axis in axes.slice() {
14061406
usage_counts[*axis] += 1;
14071407
}
@@ -1410,7 +1410,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
14101410
}
14111411
// Determine the new shape and strides.
14121412
let mut new_dim = usage_counts; // reuse to avoid an allocation
1413-
let mut new_strides = D::zero_index_with_ndim(self.ndim());
1413+
let mut new_strides = D::zeros(self.ndim());
14141414
{
14151415
let dim = self.dim.slice();
14161416
let strides = self.strides.slice();

src/indexes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub fn indices<E>(shape: E) -> Indices<E::Dim>
3131
{
3232
let dim = shape.into_dimension();
3333
Indices {
34-
start: dim.zero_index(),
34+
start: E::Dim::zeros(dim.ndim()),
3535
dim: dim,
3636
}
3737
}
@@ -210,7 +210,7 @@ pub fn indices_iter_f<E>(shape: E) -> IndicesIterF<E::Dim>
210210
where E: IntoDimension,
211211
{
212212
let dim = shape.into_dimension();
213-
let zero = dim.zero_index();
213+
let zero = E::Dim::zeros(dim.ndim());
214214
IndicesIterF {
215215
has_remaining: dim.size_checked() != Some(0),
216216
index: zero,

0 commit comments

Comments
 (0)