Add Dimension::zero_index_with_ndim()
@@ -156,6 +156,15 @@ pub trait Dimension : Clone + Eq + Debug + Send + Sync + Default +
Self::default()
}
+ #[doc(hidden)]
+ /// Return an index of same type and with the specified dimensionality.
+ ///
+ /// This method is useful for generalizing over fixed-size and
+ /// variable-size dimension representations.
+ ///
+ /// **Panics** if `Self` has a fixed size that is not `ndim`.
+ fn zero_index_with_ndim(ndim: usize) -> Self;
+
#[doc(hidden)]
#[inline]
fn first_index(&self) -> Option<Self> {
@@ -371,6 +380,11 @@ impl Dimension for Dim<[Ix; 0]> {
#[inline]
fn into_pattern(self) -> Self::Pattern { }
#[inline]
+ fn zero_index_with_ndim(ndim: usize) -> Self {
+ assert_eq!(ndim, 0);
+ Self::default()
+ }
+ #[inline]
fn next_for(&self, _index: Self) -> Option<Self> {
None
}
@@ -402,6 +416,11 @@ impl Dimension for Dim<[Ix; 1]> {
get!(&self, 0)
}
#[inline]
+ fn zero_index_with_ndim(ndim: usize) -> Self {
+ assert_eq!(ndim, 1);
+ Self::default()
+ }
+ #[inline]
fn next_for(&self, mut index: Self) -> Option<Self> {
getm!(index, 0) += 1;
if get!(&index, 0) < get!(self, 0) {
@@ -491,6 +510,11 @@ impl Dimension for Dim<[Ix; 2]> {
#[inline]
fn slice_mut(&mut self) -> &mut [Ix] { self.ixm() }
#[inline]
+ fn zero_index_with_ndim(ndim: usize) -> Self {
+ assert_eq!(ndim, 2);
+ Self::default()
+ }
+ #[inline]
fn next_for(&self, index: Self) -> Option<Self> {
let mut i = get!(&index, 0);
let mut j = get!(&index, 1);
@@ -630,6 +654,12 @@ impl Dimension for Dim<[Ix; 3]> {
m as usize * n as usize * o as usize
}
+ #[inline]
+ fn zero_index_with_ndim(ndim: usize) -> Self {
+ assert_eq!(ndim, 3);
+ Self::default()
+ }
+
#[inline]
fn next_for(&self, index: Self) -> Option<Self> {
let mut i = get!(&index, 0);
@@ -734,6 +764,11 @@ macro_rules! large_dim {
#[inline]
fn slice_mut(&mut self) -> &mut [Ix] { self.ixm() }
#[inline]
+ fn zero_index_with_ndim(ndim: usize) -> Self {
+ assert_eq!(ndim, $n);
+ Self::default()
+ }
+ #[inline]
$($insert_axis)*
#[inline]
fn try_remove_axis(&self, axis: Axis) -> Self::Smaller {
@@ -786,6 +821,11 @@ impl Dimension for IxDyn
IxDyn::zeros(self.ndim())
}
+ #[inline]
+ fn zero_index_with_ndim(ndim: usize) -> Self {
+ IxDyn::zeros(ndim)
+ }
+
#[inline]
fn insert_axis(&self, axis: Axis) -> Self::Larger {
debug_assert!(axis.index() <= self.ndim());
