diff --git a/Cargo.toml b/Cargo.toml index efc035f00..12560040f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -74,6 +74,8 @@ matrixmultiply-threading = ["matrixmultiply/threading"] portable-atomic-critical-section = ["portable-atomic/critical-section"] +unstable = [] + [target.'cfg(not(target_has_atomic = "ptr"))'.dependencies] portable-atomic = { version = "1.6.0" } diff --git a/src/dimension/dimension_trait.rs b/src/dimension/dimension_trait.rs index 3544a7f3c..c1bbfa86a 100644 --- a/src/dimension/dimension_trait.rs +++ b/src/dimension/dimension_trait.rs @@ -17,6 +17,8 @@ use super::conversion::Convert; use super::ops::DimAdd; use super::{stride_offset, stride_offset_checked}; use crate::itertools::{enumerate, zip}; +use crate::layout::rank::*; +use crate::layout::ranked::Ranked; use crate::IntoDimension; use crate::RemoveAxis; use crate::{ArrayView1, ArrayViewMut1}; @@ -59,6 +61,7 @@ pub trait Dimension: + DimAdd + DimAdd::Larger> + DimAdd + + Ranked { /// For fixed-size dimension representations (e.g. `Ix2`), this should be /// `Some(ndim)`, and for variable-size dimension representations (e.g. @@ -76,9 +79,6 @@ pub trait Dimension: /// Next larger dimension type Larger: Dimension + RemoveAxis; - /// Returns the number of dimensions (number of axes). - fn ndim(&self) -> usize; - /// Convert the dimension into a pattern matching friendly value. fn into_pattern(self) -> Self::Pattern; @@ -414,6 +414,18 @@ macro_rules! impl_insert_axis_array( ); ); +impl Ranked for Dim<[Ix; N]> +where ConstRank: Rank // Limit us to < 12, since Rank must impl Dimensionality +{ + type NDim = ConstRank; + + #[inline] + fn ndim(&self) -> usize + { + N + } +} + impl Dimension for Dim<[Ix; 0]> { const NDIM: Option = Some(0); @@ -422,11 +434,6 @@ impl Dimension for Dim<[Ix; 0]> type Larger = Ix1; // empty product is 1 -> size is 1 #[inline] - fn ndim(&self) -> usize - { - 0 - } - #[inline] fn slice(&self) -> &[Ix] { &[] @@ -471,11 +478,6 @@ impl Dimension for Dim<[Ix; 1]> type Smaller = Ix0; type Larger = Ix2; #[inline] - fn ndim(&self) -> usize - { - 1 - } - #[inline] fn slice(&self) -> &[Ix] { self.ix() @@ -604,11 +606,6 @@ impl Dimension for Dim<[Ix; 2]> type Smaller = Ix1; type Larger = Ix3; #[inline] - fn ndim(&self) -> usize - { - 2 - } - #[inline] fn into_pattern(self) -> Self::Pattern { self.ix().convert() @@ -779,11 +776,6 @@ impl Dimension for Dim<[Ix; 3]> type Smaller = Ix2; type Larger = Ix4; #[inline] - fn ndim(&self) -> usize - { - 3 - } - #[inline] fn into_pattern(self) -> Self::Pattern { self.ix().convert() @@ -911,8 +903,6 @@ macro_rules! large_dim { type Smaller = Dim<[Ix; $n - 1]>; type Larger = $larger; #[inline] - fn ndim(&self) -> usize { $n } - #[inline] fn into_pattern(self) -> Self::Pattern { self.ix().convert() } @@ -952,6 +942,17 @@ large_dim!(6, Ix6, (Ix, Ix, Ix, Ix, Ix, Ix), IxDyn, { } }); +impl Ranked for IxDyn +{ + type NDim = DynRank; + + #[inline] + fn ndim(&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 @@ -961,11 +962,6 @@ impl Dimension for IxDyn type Smaller = Self; type Larger = Self; #[inline] - fn ndim(&self) -> usize - { - self.ix().len() - } - #[inline] fn slice(&self) -> &[Ix] { self.ix() diff --git a/src/dimension/dynindeximpl.rs b/src/dimension/dynindeximpl.rs index 60aeacd80..6f8ad1d49 100644 --- a/src/dimension/dynindeximpl.rs +++ b/src/dimension/dynindeximpl.rs @@ -1,4 +1,5 @@ use crate::imp_prelude::*; +use crate::layout::ranked::Ranked; #[cfg(not(feature = "std"))] use alloc::boxed::Box; use alloc::vec; diff --git a/src/dimension/mod.rs b/src/dimension/mod.rs index 4ada0b127..68f625115 100644 --- a/src/dimension/mod.rs +++ b/src/dimension/mod.rs @@ -850,17 +850,17 @@ mod test #[test] fn max_abs_offset_check_overflow_examples() { - let dim = (1, ::std::isize::MAX as usize, 1).into_dimension(); + let dim = (1, isize::MAX as usize, 1).into_dimension(); let strides = (1, 1, 1).into_dimension(); max_abs_offset_check_overflow::(&dim, &strides).unwrap(); - let dim = (1, ::std::isize::MAX as usize, 2).into_dimension(); + let dim = (1, isize::MAX as usize, 2).into_dimension(); let strides = (1, 1, 1).into_dimension(); max_abs_offset_check_overflow::(&dim, &strides).unwrap_err(); let dim = (0, 2, 2).into_dimension(); - let strides = (1, ::std::isize::MAX as usize, 1).into_dimension(); + let strides = (1, isize::MAX as usize, 1).into_dimension(); max_abs_offset_check_overflow::(&dim, &strides).unwrap_err(); let dim = (0, 2, 2).into_dimension(); - let strides = (1, ::std::isize::MAX as usize / 4, 1).into_dimension(); + let strides = (1, isize::MAX as usize / 4, 1).into_dimension(); max_abs_offset_check_overflow::(&dim, &strides).unwrap_err(); } diff --git a/src/dimension/ndindex.rs b/src/dimension/ndindex.rs index ca2a3ea69..4ed368c20 100644 --- a/src/dimension/ndindex.rs +++ b/src/dimension/ndindex.rs @@ -2,6 +2,7 @@ use std::fmt::Debug; use super::{stride_offset, stride_offset_checked}; use crate::itertools::zip; +use crate::layout::ranked::Ranked; use crate::{Dim, Dimension, IntoDimension, Ix, Ix0, Ix1, Ix2, Ix3, Ix4, Ix5, Ix6, IxDyn, IxDynImpl}; /// Tuple or fixed size arrays that can be used to index an array. diff --git a/src/dimension/remove_axis.rs b/src/dimension/remove_axis.rs index 7ba3b5330..5510c6d64 100644 --- a/src/dimension/remove_axis.rs +++ b/src/dimension/remove_axis.rs @@ -6,7 +6,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use crate::{Axis, Dim, Dimension, Ix, Ix0, Ix1}; +use crate::{layout::ranked::Ranked, Axis, Dim, Dimension, Ix, Ix0, Ix1}; /// Array shape with a next smaller dimension. /// diff --git a/src/impl_owned_array.rs b/src/impl_owned_array.rs index fb06f9656..c721b7dfa 100644 --- a/src/impl_owned_array.rs +++ b/src/impl_owned_array.rs @@ -1,5 +1,6 @@ #[cfg(not(feature = "std"))] use alloc::vec::Vec; +use core::num::NonZeroUsize; use core::ptr::NonNull; use std::mem; use std::mem::MaybeUninit; @@ -36,22 +37,21 @@ impl Array /// ``` pub fn into_scalar(self) -> A { - let size = mem::size_of::(); - if size == 0 { + let size = match NonZeroUsize::new(mem::size_of::()) { + Some(size) => size, // Any index in the `Vec` is fine since all elements are identical. - self.data.into_vec().remove(0) - } else { - // Find the index in the `Vec` corresponding to `self.ptr`. - // (This is necessary because the element in the array might not be - // the first element in the `Vec`, such as if the array was created - // by `array![1, 2, 3, 4].slice_move(s![2])`.) - let first = self.parts.ptr.as_ptr() as usize; - let base = self.data.as_ptr() as usize; - let index = (first - base) / size; - debug_assert_eq!((first - base) % size, 0); - // Remove the element at the index and return it. - self.data.into_vec().remove(index) - } + None => return self.data.into_vec().remove(0), + }; + // Find the index in the `Vec` corresponding to `self.ptr`. + // (This is necessary because the element in the array might not be + // the first element in the `Vec`, such as if the array was created + // by `array![1, 2, 3, 4].slice_move(s![2])`.) + let first = self.parts.ptr.as_ptr() as usize; + let base = self.data.as_ptr() as usize; + let index = (first - base) / size; + debug_assert_eq!((first - base) % size.get(), 0); + // Remove the element at the index and return it. + self.data.into_vec().remove(index) } } diff --git a/src/indexes.rs b/src/indexes.rs index a73c7fcb4..ca91b65ec 100644 --- a/src/indexes.rs +++ b/src/indexes.rs @@ -7,6 +7,7 @@ // except according to those terms. use super::Dimension; use crate::dimension::IntoDimension; +use crate::layout::ranked::Ranked; use crate::split_at::SplitAt; use crate::zip::Offset; use crate::Axis; @@ -57,10 +58,7 @@ where D: Dimension #[inline] fn next(&mut self) -> Option { - let index = match self.index { - None => return None, - Some(ref ix) => ix.clone(), - }; + let index = self.index.as_ref()?.clone(); self.index = self.dim.next_for(index.clone()); Some(index.into_pattern()) } diff --git a/src/iterators/mod.rs b/src/iterators/mod.rs index 85ef78c8c..1eaf579b0 100644 --- a/src/iterators/mod.rs +++ b/src/iterators/mod.rs @@ -72,10 +72,7 @@ impl Iterator for Baseiter #[inline] fn next(&mut self) -> Option { - let index = match self.index { - None => return None, - Some(ref ix) => ix.clone(), - }; + let index = self.index.as_ref()?.clone(); let offset = D::stride_offset(&index, &self.strides); self.index = self.dim.next_for(index); unsafe { Some(self.ptr.offset(offset)) } @@ -515,10 +512,7 @@ impl<'a, A, D: Dimension> Iterator for IndexedIter<'a, A, D> #[inline] fn next(&mut self) -> Option { - let index = match self.0.inner.index { - None => return None, - Some(ref ix) => ix.clone(), - }; + let index = self.0.inner.index.as_ref()?.clone(); match self.0.next() { None => None, Some(elem) => Some((index.into_pattern(), elem)), @@ -698,10 +692,7 @@ impl<'a, A, D: Dimension> Iterator for IndexedIterMut<'a, A, D> #[inline] fn next(&mut self) -> Option { - let index = match self.0.inner.index { - None => return None, - Some(ref ix) => ix.clone(), - }; + let index = self.0.inner.index.as_ref()?.clone(); match self.0.next() { None => None, Some(elem) => Some((index.into_pattern(), elem)), diff --git a/src/layout/mod.rs b/src/layout/mod.rs index 7f549ebb2..9f5d1e4e1 100644 --- a/src/layout/mod.rs +++ b/src/layout/mod.rs @@ -1,4 +1,16 @@ +//! Building blocks for describing array layout. +//! +//! This module contains types and traits used to describe how an array is structured in memory. +//! At present, it includes utilities for compactly encoding layout information +//! and abstractions for representing an array’s dimensionality. +//! +//! Over time, this module will also define traits and types for shapes, strides, and complete +//! array layouts, providing a clearer separation between these concerns and enabling more +//! flexible and expressive layout representations. + mod bitset; +pub mod rank; +pub mod ranked; #[allow(deprecated)] pub use bitset::{Layout, LayoutBitset}; diff --git a/src/layout/rank.rs b/src/layout/rank.rs new file mode 100644 index 000000000..2881b593d --- /dev/null +++ b/src/layout/rank.rs @@ -0,0 +1,331 @@ +//! Type-level representations of array dimensionality. +//! +//! This module defines the [`Rank`] trait and related types used to represent +//! the number of axes an array has, either at compile time ([`ConstRank`]) or dynamically +//! ([`DynRank`]). These types support basic type-level operations such as addition and +//! maximum, which are used to model array operations like concatenation and broadcasting. + +use core::fmt::Debug; + +/// A trait representing a dimensionality, i.e., an unsigned integer indicating how many axes an array has. +/// +/// `ndarray` encodes an array’s dimensionality in the type system when possible, which is useful for +/// debugging and for writing generic array code. However, some operations produce arrays whose +/// dimensionality cannot be known at compile time. This trait provides a common abstraction for both +/// statically known and dynamic dimensionalities. +/// +/// Compile-time dimensionalities are currently supported for values from 0 to 12, inclusive. +/// Any dimensionality above 12 must be represented with [`DynRank`], even if it is known at compile time. +/// +/// The `Smaller` and `Larger` associated types allow users to move to adjacent dimensionalities at the type level. +/// +/// ## Dynamic dimensionalities +/// A type implementing `Rank` does not expose its dimensionality as a runtime value. +/// In dynamic cases, `DynRank` means that the dimensionality is not known at compile time. +/// The actual number of axes is taken directly from the array’s shape. +pub trait Rank: + Copy + + Eq + + Debug + + Send + + Sync + + RMax + + RMax + + RMax + + RMax + + RMax + + RAdd + + RAdd + + RAdd + + RAdd + + RAdd + + RAdd +{ + /// The dimensionality as a constant `usize`, or `None` if it is dynamic. + const N: Option; + + /// The next-smaller possible dimensionality. + /// + /// For the smallest possible dimensionality (currently 0-dimensional), there + /// is of course no "smaller" dimensionality. Instead, `ConstRank::<0>::Smaller` just + /// refers back to `ConstRank<0>`; in other words, it uses a "base case" of 0-dimensionality. + type Smaller: Rank; + + /// The next-larger dimensionality. + /// + /// For the largest compile-time dimensionality (currently 12-dimensional), there + /// is no "larger" compile-time dimensionality. Instead, `ConstRank::<12>::Larger` just + /// refers to `DynRank`; in other words, it "escapes" to a dynamically-determined dimensionality. + type Larger: Rank; +} + +/// Adds two dimensionalities at compile time. +/// +/// The addition of a constant dimensionality with a dynamic dimensionality +/// will always result in a dynamic dimensionality, effectively "erasing" +/// the compile-time knowledge. +/// +/// This type is analogous to the existing [`crate::DimAdd`], but specifically +/// for dimensionality instead of `Dimension` types. +/// +/// ## Example +/// ``` +/// use ndarray::layout::rank::*; +/// use core::any::TypeId; +/// +/// type Added = >::Output; +/// assert_eq!(TypeId::of::(), TypeId::of::()); +/// +/// type AddedDyn = >::Output; +/// assert_eq!(TypeId::of::(), TypeId::of::()); +/// ``` +pub trait RAdd +{ + /// The result of the type-level addition of two dimensionalities. + type Output: Rank; +} + +/// Takes the maximum of two dimensionalities at compile time. +/// +/// The maximum of a constant dimensionality and a dynamic dimensionality +/// will always result in a dynamic dimensionality, effectively "erasing" +/// the compile-time knowledge. +/// +/// This type is analogous to the existing [`crate::DimMax`], but specifically +/// for dimensionality instead of `Dimension` types. +/// +/// ## Example +/// ``` +/// use ndarray::layout::rank::*; +/// use core::any::TypeId; +/// +/// type Added = >::Output; +/// assert_eq!(TypeId::of::(), TypeId::of::()); +/// +/// type AddedDyn = >::Output; +/// assert_eq!(TypeId::of::(), TypeId::of::()); +/// ``` +pub trait RMax +{ + /// The result of the type-level maximum of two dimensionalities. + type Output: Rank; +} + +/// The N-dimensional static dimensionality. +/// +/// This type captures dimensionalities that are known at compile-time. +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +pub struct ConstRank; + +/// The 0-dimensionality, for "dimensionless" arrays with a single value. +/// +/// See [`Rank`] and [`ConstRank`] for more information. +pub type R0 = ConstRank<0>; + +macro_rules! def_d_aliases { + ($(($alias:ident, $N:literal)),*) => { + $( + /// A dimensionality for arrays that are + #[doc = stringify!($N)] + /// D. + /// + /// See [`Rank`] and [`ConstRank`] for more information. + pub type $alias = ConstRank<$N>; + )+ + }; +} + +def_d_aliases!( + (R1, 1), + (R2, 2), + (R3, 3), + (R4, 4), + (R5, 5), + (R6, 6), + (R7, 7), + (R8, 8), + (R9, 9), + (R10, 10), + (R11, 11), + (R12, 12) +); + +/// Implement addition for a given dimensionality. +macro_rules! impl_add { + ($left:literal, ($($right:literal),*), ddyn: ($($rightd:literal),*)) => { + // $left + $right still gets you a compile-time dimension + $( + impl RAdd> for ConstRank<$left> + { + type Output = ConstRank<{$left + $right}>; + } + )* + + // $left + $rightd gets you a dynamic dimensionality + $( + impl RAdd> for ConstRank<$left> + { + type Output = DynRank; + } + )* + }; +} + +// There's got to be a macro way to do this in one line to help with +// any future additions of extra dimenions, although it might +// also slow down compile times. +impl_add!(0, (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12), ddyn: ()); +impl_add!(1, (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11), ddyn: (12)); +impl_add!(2, (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10), ddyn: (11, 12)); +impl_add!(3, (0, 1, 2, 3, 4, 5, 6, 7, 8, 9), ddyn: (10, 11, 12)); +impl_add!(4, (0, 1, 2, 3, 4, 5, 6, 7, 8), ddyn: (9, 10, 11, 12)); +impl_add!(5, (0, 1, 2, 3, 4, 5, 6, 7), ddyn: (8, 9, 10, 11, 12)); +impl_add!(6, (0, 1, 2, 3, 4, 5, 6), ddyn: (7, 8, 9, 10, 11, 12)); +impl_add!(7, (0, 1, 2, 3, 4, 5), ddyn: (6, 7, 8, 9, 10, 11, 12)); +impl_add!(8, (0, 1, 2, 3, 4), ddyn: (5, 6, 7, 8, 9, 10, 11, 12)); +impl_add!(9, (0, 1, 2, 3), ddyn: (4, 5, 6, 7, 8, 9, 10, 11, 12)); +impl_add!(10, (0, 1, 2), ddyn: (3, 4, 5, 6, 7, 8, 9, 10, 11, 12)); +impl_add!(11, (0, 1), ddyn: (2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)); +impl_add!(12, (0), ddyn: (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)); + +macro_rules! impl_max { + // Base case, just a target with some lowers + ($($lower:literal),+, target: $target:literal) => { + $( + impl RMax> for ConstRank<$target> + { + type Output = ConstRank<$target>; + } + )+ + }; + // General case: at least one lower, at least one upper + ($($lower:literal),+$(,)? target: $target:literal, $first_upper:literal$(, $($upper:literal),+)?) => { + $( + impl RMax> for ConstRank<$target> + { + type Output = ConstRank<$target>; + } + )+ + impl RMax> for ConstRank<$target> + { + type Output = ConstRank<$first_upper>; + } + $( + $( + impl RMax> for ConstRank<$target> + { + type Output = ConstRank<$upper>; + } + )+ + )? + impl_max!($($lower),+, $target, target: $first_upper$(, $($upper),+)?); + }; + // Helper syntax: zero lowers, target, at least one upper + (target: $target:literal, $first_upper:literal, $($upper:literal),+) => { + impl RMax> for ConstRank<$target> + { + type Output = ConstRank<$first_upper>; + } + $( + impl RMax> for ConstRank<$target> + { + type Output = ConstRank<$upper>; + } + )+ + impl_max!($target, target: $first_upper, $($upper),+); + }; +} + +impl_max!(target: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12); + +impl RMax> for ConstRank +where ConstRank: Rank +{ + type Output = Self; +} + +macro_rules! impl_dimensionality { + ($($d:literal),+) => { + $( + impl Rank for ConstRank<$d> + { + const N: Option = Some($d); + + type Smaller = ConstRank<{$d - 1}>; + + type Larger = ConstRank<{$d + 1}>; + } + )+ + }; +} + +impl Rank for R0 +{ + const N: Option = Some(0); + + type Smaller = Self; + + type Larger = R1; +} + +impl_dimensionality!(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11); + +impl Rank for ConstRank<12> +{ + const N: Option = Some(12); + + type Smaller = R11; + + type Larger = DynRank; +} + +/// The dynamic dimensionality. +/// +/// This type captures dimensionalities that are unknown at compile-time. +/// See [`Rank`] for more information. +/// +/// This type does not carry any information about runtime dimensionality, +/// it just indicate that dimensionality is not known at compile-time. +/// This is done to avoid multiple sources of truth for runtime array +/// dimensionality. +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +pub struct DynRank; + +impl Rank for DynRank +{ + const N: Option = None; + + type Smaller = Self; + + type Larger = Self; +} + +impl RAdd for DynRank +{ + type Output = DynRank; +} + +impl RAdd> for DynRank +{ + type Output = DynRank; +} + +impl RAdd for ConstRank +{ + type Output = DynRank; +} + +impl RMax for DynRank +{ + type Output = DynRank; +} + +impl RMax> for DynRank +{ + type Output = DynRank; +} + +impl RMax for ConstRank +{ + type Output = DynRank; +} diff --git a/src/layout/ranked.rs b/src/layout/ranked.rs new file mode 100644 index 000000000..f71cce097 --- /dev/null +++ b/src/layout/ranked.rs @@ -0,0 +1,195 @@ +//! 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 [`Rank`]) 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::rank::{Rank, R1}, + ArrayBase, + ArrayParts, + ArrayRef, + LayoutRef, + RawData, + RawRef, +}; + +/// A trait to unify type- and runtime-level number of dimensions. +/// +/// The [`Rank`] trait captures array rank at the type level; however it +/// is limited at runtime. If the `Rank` is dynamic (i.e., [`DynRank`][DynRank]) +/// then the dimensionality cannot be known at compile time. This trait unifies type- +/// and runtime-level dimensionality by providing: +/// 1. An associated type, [`NDim`][NDim], with type-level dimensionality +/// 2. A function, [`ndim`][ndim], that can give the dimensionality at runtime. +/// +/// [DynRank]: crate::layout::rank::DynRank +/// [NDim]: Ranked::NDim +/// [ndim]: Ranked::ndim +pub trait Ranked +{ + /// The compile-time rank of the type; can be [`DynRank`][DynRank] if unknown. + /// + /// [DynRank]: crate::layout::rank::DynRank + type NDim: Rank; + + /// The runtime number of dimensions of the type. + fn ndim(&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 Ranked for &T + where T: Ranked + { + type NDim = T::NDim; + + fn ndim(&self) -> usize + { + (*self).ndim() + } + } + + impl Ranked for &mut T + where T: Ranked + { + type NDim = T::NDim; + + fn ndim(&self) -> usize + { + (**self).ndim() + } + } + + impl Ranked for Arc + where T: Ranked + { + type NDim = T::NDim; + + fn ndim(&self) -> usize + { + (**self).ndim() + } + } + + impl Ranked for Rc + where T: Ranked + { + type NDim = T::NDim; + + fn ndim(&self) -> usize + { + (**self).ndim() + } + } + + impl Ranked for Box + where T: Ranked + { + type NDim = T::NDim; + + fn ndim(&self) -> usize + { + (**self).ndim() + } + } +} + +impl Ranked for [T] +{ + type NDim = R1; + + fn ndim(&self) -> usize + { + 1 + } +} + +impl Ranked for Vec +{ + type NDim = R1; + + fn ndim(&self) -> usize + { + 1 + } +} + +impl Ranked for [T; N] +{ + type NDim = R1; + + fn ndim(&self) -> usize + { + 1 + } +} + +impl Ranked for ArrayParts +where D: Ranked +{ + type NDim = D::NDim; + + fn ndim(&self) -> usize + { + self.dim.ndim() + } +} + +impl Ranked for ArrayBase +where + S: RawData, + D: Ranked, +{ + type NDim = D::NDim; + + fn ndim(&self) -> usize + { + self.parts.ndim() + } +} + +impl Ranked for LayoutRef +where D: Ranked +{ + type NDim = D::NDim; + + fn ndim(&self) -> usize + { + self.0.ndim() + } +} + +impl Ranked for ArrayRef +where D: Ranked +{ + type NDim = D::NDim; + + fn ndim(&self) -> usize + { + self.0.ndim() + } +} + +impl Ranked for RawRef +where D: Ranked +{ + type NDim = D::NDim; + + fn ndim(&self) -> usize + { + self.0.ndim() + } +} diff --git a/src/lib.rs b/src/lib.rs index 2b9b656e3..4cfb6ddb7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,7 +13,6 @@ clippy::deref_addrof, clippy::manual_map, // is not an error clippy::while_let_on_iterator, // is not an error - clippy::from_iter_instead_of_collect, // using from_iter is good style clippy::incompatible_msrv, // false positive PointerExt::offset )] #![doc(test(attr(deny(warnings))))] @@ -197,7 +196,7 @@ mod extension; mod geomspace; mod indexes; mod iterators; -mod layout; +pub mod layout; mod linalg_traits; mod linspace; #[cfg(feature = "std")] diff --git a/src/prelude.rs b/src/prelude.rs index 072eb4825..a4b099bd1 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -35,6 +35,9 @@ pub use crate::{ #[doc(no_inline)] pub use crate::{Axis, Dim, Dimension}; +#[doc(no_inline)] +pub use crate::layout::ranked::Ranked; + #[doc(no_inline)] pub use crate::{ArrayRef0, ArrayRef1, ArrayRef2, ArrayRef3, ArrayRef4, ArrayRef5, ArrayRef6, ArrayRefD}; diff --git a/src/tri.rs b/src/tri.rs index 9f15db71a..1e10c3b6a 100644 --- a/src/tri.rs +++ b/src/tri.rs @@ -159,8 +159,6 @@ where #[cfg(test)] mod tests { - use core::isize; - use crate::{array, dimension, Array0, Array1, Array2, Array3, ShapeBuilder}; use alloc::vec; diff --git a/tests/numeric.rs b/tests/numeric.rs index 7e6964812..ea1cdf2b5 100644 --- a/tests/numeric.rs +++ b/tests/numeric.rs @@ -5,7 +5,6 @@ use approx::assert_abs_diff_eq; use ndarray::{arr0, arr1, arr2, array, aview1, Array, Array1, Array2, Array3, Axis}; -use std::f64; #[test] fn test_mean_with_nan_values() @@ -176,7 +175,7 @@ fn var_too_large_ddof() fn var_nan_ddof() { let a = Array2::::zeros((2, 3)); - let v = a.var(::std::f64::NAN); + let v = a.var(f64::NAN); assert!(v.is_nan()); } @@ -219,7 +218,7 @@ fn std_too_large_ddof() fn std_nan_ddof() { let a = Array2::::zeros((2, 3)); - let v = a.std(::std::f64::NAN); + let v = a.std(f64::NAN); assert!(v.is_nan()); } @@ -375,7 +374,7 @@ fn var_axis_too_large_ddof() fn var_axis_nan_ddof() { let a = Array2::::zeros((2, 3)); - let v = a.var_axis(Axis(1), ::std::f64::NAN); + let v = a.var_axis(Axis(1), f64::NAN); assert_eq!(v.shape(), &[2]); v.mapv(|x| assert!(x.is_nan())); }