Skip to content

Commit 13c9215

Browse files
committed
Add Dimension::zero_index_with_ndim()
1 parent d2837d1 commit 13c9215

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/dimension/dimension_trait.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,15 @@ pub trait Dimension : Clone + Eq + Debug + Send + Sync + Default +
156156
Self::default()
157157
}
158158

159+
#[doc(hidden)]
160+
/// Return an index of same type and with the specified dimensionality.
161+
///
162+
/// This method is useful for generalizing over fixed-size and
163+
/// variable-size dimension representations.
164+
///
165+
/// **Panics** if `Self` has a fixed size that is not `ndim`.
166+
fn zero_index_with_ndim(ndim: usize) -> Self;
167+
159168
#[doc(hidden)]
160169
#[inline]
161170
fn first_index(&self) -> Option<Self> {
@@ -371,6 +380,11 @@ impl Dimension for Dim<[Ix; 0]> {
371380
#[inline]
372381
fn into_pattern(self) -> Self::Pattern { }
373382
#[inline]
383+
fn zero_index_with_ndim(ndim: usize) -> Self {
384+
assert_eq!(ndim, 0);
385+
Self::default()
386+
}
387+
#[inline]
374388
fn next_for(&self, _index: Self) -> Option<Self> {
375389
None
376390
}
@@ -402,6 +416,11 @@ impl Dimension for Dim<[Ix; 1]> {
402416
get!(&self, 0)
403417
}
404418
#[inline]
419+
fn zero_index_with_ndim(ndim: usize) -> Self {
420+
assert_eq!(ndim, 1);
421+
Self::default()
422+
}
423+
#[inline]
405424
fn next_for(&self, mut index: Self) -> Option<Self> {
406425
getm!(index, 0) += 1;
407426
if get!(&index, 0) < get!(self, 0) {
@@ -491,6 +510,11 @@ impl Dimension for Dim<[Ix; 2]> {
491510
#[inline]
492511
fn slice_mut(&mut self) -> &mut [Ix] { self.ixm() }
493512
#[inline]
513+
fn zero_index_with_ndim(ndim: usize) -> Self {
514+
assert_eq!(ndim, 2);
515+
Self::default()
516+
}
517+
#[inline]
494518
fn next_for(&self, index: Self) -> Option<Self> {
495519
let mut i = get!(&index, 0);
496520
let mut j = get!(&index, 1);
@@ -630,6 +654,12 @@ impl Dimension for Dim<[Ix; 3]> {
630654
m as usize * n as usize * o as usize
631655
}
632656

657+
#[inline]
658+
fn zero_index_with_ndim(ndim: usize) -> Self {
659+
assert_eq!(ndim, 3);
660+
Self::default()
661+
}
662+
633663
#[inline]
634664
fn next_for(&self, index: Self) -> Option<Self> {
635665
let mut i = get!(&index, 0);
@@ -734,6 +764,11 @@ macro_rules! large_dim {
734764
#[inline]
735765
fn slice_mut(&mut self) -> &mut [Ix] { self.ixm() }
736766
#[inline]
767+
fn zero_index_with_ndim(ndim: usize) -> Self {
768+
assert_eq!(ndim, $n);
769+
Self::default()
770+
}
771+
#[inline]
737772
$($insert_axis)*
738773
#[inline]
739774
fn try_remove_axis(&self, axis: Axis) -> Self::Smaller {
@@ -786,6 +821,11 @@ impl Dimension for IxDyn
786821
IxDyn::zeros(self.ndim())
787822
}
788823

824+
#[inline]
825+
fn zero_index_with_ndim(ndim: usize) -> Self {
826+
IxDyn::zeros(ndim)
827+
}
828+
789829
#[inline]
790830
fn insert_axis(&self, axis: Axis) -> Self::Larger {
791831
debug_assert!(axis.index() <= self.ndim());

0 commit comments

Comments
 (0)