Skip to content

Commit bcfef7a

Browse files
committed
Add dynamic shape
Also add a Zero impl for NShape
1 parent 2e07be2 commit bcfef7a

3 files changed

Lines changed: 36 additions & 47 deletions

File tree

src/layout/dyn_repr.rs

Lines changed: 20 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use alloc::boxed::Box;
22
use alloc::vec::Vec;
3+
use core::iter::Cloned;
34
use core::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign};
45
use core::ops::{Deref, DerefMut, Index, IndexMut};
56
use core::slice::Iter;
6-
use num_traits::Zero;
77

88
use crate::layout::rank::DynRank;
99
use crate::layout::ranked::Ranked;
10-
use crate::layout::shape::{IntoShape, Shape};
10+
use crate::layout::shape::Shape;
1111
use crate::Axis;
1212

1313
const CAP: usize = 4;
@@ -19,19 +19,9 @@ pub enum DynAxesRepr<T>
1919
Alloc(Box<[T]>),
2020
}
2121

22+
/// An array shape with a dynamic rank.
2223
pub type DShape = DynAxesRepr<usize>;
2324

24-
impl<T> DynAxesRepr<T>
25-
{
26-
fn ndim(&self) -> usize
27-
{
28-
match self {
29-
DynAxesRepr::Inline(len, _) => *len,
30-
DynAxesRepr::Alloc(items) => items.len(),
31-
}
32-
}
33-
}
34-
3525
impl<T> Deref for DynAxesRepr<T>
3626
{
3727
type Target = [T];
@@ -52,7 +42,13 @@ impl<T> DerefMut for DynAxesRepr<T>
5242
{
5343
fn deref_mut(&mut self) -> &mut Self::Target
5444
{
55-
todo!()
45+
match self {
46+
DynAxesRepr::Inline(len, arr) => {
47+
debug_assert!(*len <= arr.len());
48+
unsafe { arr.get_unchecked_mut(..*len) }
49+
}
50+
DynAxesRepr::Alloc(items) => items,
51+
}
5652
}
5753
}
5854

@@ -92,33 +88,6 @@ impl<T> IndexMut<Axis> for DynAxesRepr<T>
9288
}
9389
}
9490

95-
impl<T> Default for DynAxesRepr<T>
96-
where T: Zero + Copy
97-
{
98-
fn default() -> Self
99-
{
100-
Self::Inline(1, [T::zero(); 4])
101-
}
102-
}
103-
104-
impl Zero for DShape
105-
{
106-
fn zero() -> Self
107-
{
108-
Self::Inline(1, [0usize; CAP])
109-
}
110-
111-
fn is_zero(&self) -> bool
112-
{
113-
self.iter().all(|e| e.is_zero())
114-
}
115-
116-
fn set_zero(&mut self)
117-
{
118-
self.iter_mut().for_each(|e| e.set_zero());
119-
}
120-
}
121-
12291
impl<Rhs, T> From<Rhs> for DynAxesRepr<T>
12392
where
12493
Rhs: AsRef<[T]>,
@@ -156,7 +125,7 @@ macro_rules! impl_op {
156125
/// *Panics* if the two dimensionalities are different
157126
impl<Rhs> $op_trait for DShape
158127
where
159-
Rhs: IntoShape<NDim = DynRank>,
128+
Rhs: Into<Self>,
160129
{
161130
type Output = DShape;
162131

@@ -170,10 +139,10 @@ macro_rules! impl_op {
170139
/// *Panics* if the two dimensionalities are different
171140
impl<Rhs> $op_assign_trait for DShape
172141
where
173-
Rhs: IntoShape<NDim = DynRank>,
142+
Rhs: Into<Self>,
174143
{
175144
fn $op_assign_fn(&mut self, rhs: Rhs) {
176-
let other = rhs.into_shape();
145+
let other = rhs.into();
177146
for i in 0..self.ndim().max(other.ndim()) {
178147
self[i].$op_assign_fn(other[i]);
179148
}
@@ -204,11 +173,16 @@ where T: Clone
204173
impl Shape for DynAxesRepr<usize>
205174
{
206175
type Iter<'a>
207-
= Iter<'a, usize>
176+
= Cloned<Iter<'a, usize>>
208177
where Self: 'a;
209178

179+
fn axis_len(&self, axis: usize) -> usize
180+
{
181+
self[axis]
182+
}
183+
210184
fn iter(&self) -> Self::Iter<'_>
211185
{
212-
(**self).iter()
186+
(**self).iter().cloned()
213187
}
214188
}

src/layout/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub mod rank;
1313
pub mod ranked;
1414
mod shape;
1515
mod n_repr;
16+
mod dyn_repr;
1617

1718
use core::any::type_name;
1819
use core::error::Error;
@@ -23,6 +24,7 @@ use crate::layout::ranked::Ranked;
2324

2425
#[allow(deprecated)]
2526
pub use bitset::{Layout, LayoutBitset};
27+
pub use dyn_repr::DShape;
2628
pub use n_repr::NShape;
2729
pub use shape::Shape;
2830

src/layout/n_repr.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,19 @@ where ConstRank<N>: Rank
171171
}
172172
}
173173

174+
impl<const N: usize> Zero for NShape<N>
175+
{
176+
fn zero() -> Self
177+
{
178+
Self([usize::zero(); N])
179+
}
180+
181+
fn is_zero(&self) -> bool
182+
{
183+
self.0.iter().all(|e| *e == 0usize)
184+
}
185+
}
186+
174187
macro_rules! impl_op {
175188
($op_trait:ty, $op_fn:ident, $op_assign_trait:ty, $op_assign_fn:ident) => {
176189
impl<Rhs, const N: usize> $op_trait for NShape<N>
@@ -270,6 +283,6 @@ impl<const N: usize> Default for NShape<N>
270283
{
271284
fn default() -> Self
272285
{
273-
Self([0; N])
286+
Self::zero()
274287
}
275288
}

0 commit comments

Comments
 (0)