Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 29 additions & 45 deletions src/dimension/dimension_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use super::conversion::Convert;
use super::ops::DimAdd;
use super::{stride_offset, stride_offset_checked};
use crate::itertools::{enumerate, zip};
#[cfg(feature = "unstable")]
use crate::layout::dimensionality::*;
use crate::layout::ranked::Ranked;
use crate::IntoDimension;
use crate::RemoveAxis;
use crate::{ArrayView1, ArrayViewMut1};
Expand Down Expand Up @@ -61,6 +61,7 @@ pub trait Dimension:
+ DimAdd<Ix0, Output = Self>
+ DimAdd<Ix1, Output = <Self as Dimension>::Larger>
+ DimAdd<IxDyn, Output = IxDyn>
+ Ranked
{
/// For fixed-size dimension representations (e.g. `Ix2`), this should be
/// `Some(ndim)`, and for variable-size dimension representations (e.g.
Expand All @@ -78,12 +79,11 @@ pub trait Dimension:
/// Next larger dimension
type Larger: Dimension + RemoveAxis;

/// The dimensionality of the type, under the new, unstable API.
#[cfg(feature = "unstable")]
type Rank: Dimensionality;

/// Returns the number of dimensions (number of axes).
fn ndim(&self) -> usize;
fn ndim(&self) -> usize
{
self.rank()
}

/// Convert the dimension into a pattern matching friendly value.
fn into_pattern(self) -> Self::Pattern;
Expand Down Expand Up @@ -420,21 +420,26 @@ macro_rules! impl_insert_axis_array(
);
);

impl<const N: usize> Ranked for Dim<[Ix; N]>
where NDim<N>: Dimensionality // Limit us to < 12, since Rank must impl Dimensionality
{
type Rank = NDim<N>;

#[inline]
fn rank(&self) -> usize
{
N
}
}

impl Dimension for Dim<[Ix; 0]>
{
const NDIM: Option<usize> = Some(0);
type Pattern = ();
type Smaller = Self;
type Larger = Ix1;
#[cfg(feature = "unstable")]
type Rank = D0;
// empty product is 1 -> size is 1
#[inline]
fn ndim(&self) -> usize
{
0
}
#[inline]
fn slice(&self) -> &[Ix]
{
&[]
Expand Down Expand Up @@ -478,13 +483,6 @@ impl Dimension for Dim<[Ix; 1]>
type Pattern = Ix;
type Smaller = Ix0;
type Larger = Ix2;
#[cfg(feature = "unstable")]
type Rank = D1;
#[inline]
fn ndim(&self) -> usize
{
1
}
#[inline]
fn slice(&self) -> &[Ix]
{
Expand Down Expand Up @@ -613,13 +611,6 @@ impl Dimension for Dim<[Ix; 2]>
type Pattern = (Ix, Ix);
type Smaller = Ix1;
type Larger = Ix3;
#[cfg(feature = "unstable")]
type Rank = D2;
#[inline]
fn ndim(&self) -> usize
{
2
}
#[inline]
fn into_pattern(self) -> Self::Pattern
{
Expand Down Expand Up @@ -790,13 +781,6 @@ impl Dimension for Dim<[Ix; 3]>
type Pattern = (Ix, Ix, Ix);
type Smaller = Ix2;
type Larger = Ix4;
#[cfg(feature = "unstable")]
type Rank = D3;
#[inline]
fn ndim(&self) -> usize
{
3
}
#[inline]
fn into_pattern(self) -> Self::Pattern
{
Expand Down Expand Up @@ -924,10 +908,6 @@ macro_rules! large_dim {
type Pattern = $pattern;
type Smaller = Dim<[Ix; $n - 1]>;
type Larger = $larger;
#[cfg(feature = "unstable")]
type Rank = NDim<$n>;
#[inline]
fn ndim(&self) -> usize { $n }
#[inline]
fn into_pattern(self) -> Self::Pattern {
self.ix().convert()
Expand Down Expand Up @@ -968,6 +948,17 @@ large_dim!(6, Ix6, (Ix, Ix, Ix, Ix, Ix, Ix), IxDyn, {
}
});

impl Ranked for IxDyn
{
type Rank = DDyn;

#[inline]
fn rank(&self) -> usize
{
self.ix().len()
}
}

/// IxDyn is a "dynamic" index, pretty hard to use when indexing,
/// and memory wasteful, but it allows an arbitrary and dynamic number of axes.
impl Dimension for IxDyn
Expand All @@ -976,13 +967,6 @@ impl Dimension for IxDyn
type Pattern = Self;
type Smaller = Self;
type Larger = Self;
#[cfg(feature = "unstable")]
type Rank = DDyn;
#[inline]
fn ndim(&self) -> usize
{
self.ix().len()
}
#[inline]
fn slice(&self) -> &[Ix]
{
Expand Down
2 changes: 1 addition & 1 deletion src/layout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
//! flexible and expressive layout representations.

mod bitset;
#[cfg(feature = "unstable")]
pub mod dimensionality;
pub mod ranked;

#[allow(deprecated)]
pub use bitset::{Layout, LayoutBitset};
196 changes: 196 additions & 0 deletions src/layout/ranked.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
//! Unified trait for type- and runtime-level array rank.
//!
//! This module defines the [`Ranked`] trait, which bridges compile-time and runtime representations
//! of array dimensionality. It enables generic code to query the number of dimensions (rank) of an
//! array, whether known statically (via [`Dimensionality`]) or only at runtime. Blanket
//! implementations are provided for common pointer and container types.

use alloc::boxed::Box;
use alloc::vec::Vec;

use crate::{
layout::dimensionality::{Dimensionality, D1},
ArrayBase,
ArrayParts,
ArrayRef,
LayoutRef,
RawData,
RawRef,
};

/// A trait to unify type- and runtime-level number of dimensions.
///
/// The [`Dimensionality`] trait captures array rank at the type level; however it
/// is limited at runtime. If the `Dimensionality` is dynamic (i.e., [`DDyn`][DDyn])
/// then the dimensionality cannot be known at compile time. This trait unifies type-
/// and runtime-level dimensionality by providing:
/// 1. An associated type, [`Rank`][Rank], with type-level dimensionality
/// 2. A function, [`ndim`][ndim], that can give the dimensionality at runtime.
///
/// [DDyn]: crate::layout::dimensionality::DDyn
/// [Rank]: Ranked::Rank
/// [ndim]: Ranked::rank
/// [N]: Dimensionality::N
pub trait Ranked
{
/// The compile-time rank of the type; can be [`DDyn`][DDyn] if unknown.
///
/// [DDyn]: crate::layout::dimensionality::DDyn
type Rank: Dimensionality;

/// The runtime number of dimensions of the type.
fn rank(&self) -> usize;
}

mod blanket_impls
{
use super::*;
use alloc::rc::Rc;

#[cfg(target_has_atomic = "ptr")]
use alloc::sync::Arc;
#[cfg(not(target_has_atomic = "ptr"))]
use portable_atomic_util::Arc;

impl<T> Ranked for &T
where T: Ranked
{
type Rank = T::Rank;

fn rank(&self) -> usize
{
(*self).rank()
}
}

impl<T> Ranked for &mut T
where T: Ranked
{
type Rank = T::Rank;

fn rank(&self) -> usize
{
(**self).rank()
}
}

impl<T> Ranked for Arc<T>
where T: Ranked
{
type Rank = T::Rank;

fn rank(&self) -> usize
{
(**self).rank()
}
}

impl<T> Ranked for Rc<T>
where T: Ranked
{
type Rank = T::Rank;

fn rank(&self) -> usize
{
(**self).rank()
}
}

impl<T> Ranked for Box<T>
where T: Ranked
{
type Rank = T::Rank;

fn rank(&self) -> usize
{
(**self).rank()
}
}
}

impl<T> Ranked for [T]
{
type Rank = D1;

fn rank(&self) -> usize
{
1
}
}

impl<T> Ranked for Vec<T>
{
type Rank = D1;

fn rank(&self) -> usize
{
1
}
}

impl<T, const N: usize> Ranked for [T; N]
{
type Rank = D1;

fn rank(&self) -> usize
{
1
}
}

impl<A, D, T: ?Sized> Ranked for ArrayParts<A, D, T>
where D: Ranked
{
type Rank = D::Rank;

fn rank(&self) -> usize
{
self.dim.rank()
}
}

impl<S, D> Ranked for ArrayBase<S, D>
where
S: RawData,
D: Ranked,
{
type Rank = D::Rank;

fn rank(&self) -> usize
{
self.parts.rank()
}
}

impl<A, D> Ranked for LayoutRef<A, D>
where D: Ranked
{
type Rank = D::Rank;

fn rank(&self) -> usize
{
self.0.rank()
}
}

impl<A, D> Ranked for ArrayRef<A, D>
where D: Ranked
{
type Rank = D::Rank;

fn rank(&self) -> usize
{
self.0.rank()
}
}

impl<A, D> Ranked for RawRef<A, D>
where D: Ranked
{
type Rank = D::Rank;

fn rank(&self) -> usize
{
self.0.rank()
}
}