-
Notifications
You must be signed in to change notification settings - Fork 378
refactor: add a trait to describe shapes #1599
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
akern40
wants to merge
6
commits into
rust-ndarray:refactor
Choose a base branch
from
akern40:shape-trait
base: refactor
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+589
−0
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0f24c94
Working towards a shape trait
akern40 b7d4a73
Move to usize, not &usize
akern40 bf4a7a4
Minimal shape proposal
akern40 2e07be2
Import rearranging
akern40 bcfef7a
Add dynamic shape
akern40 1918542
Add documentation to shape trait
akern40 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| use alloc::boxed::Box; | ||
| use alloc::vec::Vec; | ||
| use core::iter::Cloned; | ||
| use core::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign}; | ||
| use core::ops::{Deref, DerefMut, Index, IndexMut}; | ||
| use core::slice::Iter; | ||
|
|
||
| use crate::layout::rank::DynRank; | ||
| use crate::layout::ranked::Ranked; | ||
| use crate::layout::shape::Shape; | ||
| use crate::Axis; | ||
|
|
||
| const CAP: usize = 4; | ||
|
|
||
| #[derive(Debug, Clone, PartialEq, Eq)] | ||
| pub enum DynAxesRepr<T> | ||
| { | ||
| Inline(usize, [T; CAP]), | ||
| Alloc(Box<[T]>), | ||
| } | ||
|
|
||
| /// An array shape with a dynamic rank. | ||
| pub type DShape = DynAxesRepr<usize>; | ||
|
|
||
| impl<T> Deref for DynAxesRepr<T> | ||
| { | ||
| type Target = [T]; | ||
|
|
||
| fn deref(&self) -> &Self::Target | ||
| { | ||
| match self { | ||
| DynAxesRepr::Inline(len, arr) => { | ||
| debug_assert!(*len <= arr.len()); | ||
| unsafe { arr.get_unchecked(..*len) } | ||
| } | ||
| DynAxesRepr::Alloc(items) => items, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<T> DerefMut for DynAxesRepr<T> | ||
| { | ||
| fn deref_mut(&mut self) -> &mut Self::Target | ||
| { | ||
| match self { | ||
| DynAxesRepr::Inline(len, arr) => { | ||
| debug_assert!(*len <= arr.len()); | ||
| unsafe { arr.get_unchecked_mut(..*len) } | ||
| } | ||
| DynAxesRepr::Alloc(items) => items, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<T> Index<usize> for DynAxesRepr<T> | ||
| { | ||
| type Output = T; | ||
|
|
||
| fn index(&self, index: usize) -> &Self::Output | ||
| { | ||
| &(**self)[index] | ||
| } | ||
| } | ||
|
|
||
| impl<T> IndexMut<usize> for DynAxesRepr<T> | ||
| { | ||
| fn index_mut(&mut self, index: usize) -> &mut Self::Output | ||
| { | ||
| &mut (**self)[index] | ||
| } | ||
| } | ||
|
|
||
| impl<T> Index<Axis> for DynAxesRepr<T> | ||
| { | ||
| type Output = T; | ||
|
|
||
| fn index(&self, index: Axis) -> &Self::Output | ||
| { | ||
| self.index(index.0) | ||
| } | ||
| } | ||
|
|
||
| impl<T> IndexMut<Axis> for DynAxesRepr<T> | ||
| { | ||
| fn index_mut(&mut self, index: Axis) -> &mut Self::Output | ||
| { | ||
| self.index_mut(index.0) | ||
| } | ||
| } | ||
|
|
||
| impl<Rhs, T> From<Rhs> for DynAxesRepr<T> | ||
| where | ||
| Rhs: AsRef<[T]>, | ||
| T: Default + Copy, | ||
| { | ||
| fn from(value: Rhs) -> Self | ||
| { | ||
| let value = value.as_ref(); | ||
| let n = value.len(); | ||
| if n <= CAP { | ||
| let mut inline = [T::default(); CAP]; | ||
| inline.split_at_mut(n).0.copy_from_slice(value); | ||
| Self::Inline(n, inline) | ||
| } else { | ||
| Self::Alloc(value.into()) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<T> Ranked for DynAxesRepr<T> | ||
| { | ||
| type NDim = DynRank; | ||
|
|
||
| fn ndim(&self) -> usize | ||
| { | ||
| match self { | ||
| DynAxesRepr::Inline(d, _) => d.clone(), | ||
| DynAxesRepr::Alloc(items) => items.len(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| macro_rules! impl_op { | ||
| ($op_trait:ty, $op_fn:ident, $op_assign_trait:ty, $op_assign_fn:ident) => { | ||
| /// *Panics* if the two dimensionalities are different | ||
| impl<Rhs> $op_trait for DShape | ||
| where | ||
| Rhs: Into<Self>, | ||
| { | ||
| type Output = DShape; | ||
|
|
||
| fn $op_fn(self, rhs: Rhs) -> <Self as $op_trait>::Output { | ||
| let mut output = self.clone(); | ||
| output.$op_assign_fn(rhs); | ||
| output | ||
| } | ||
| } | ||
|
|
||
| /// *Panics* if the two dimensionalities are different | ||
| impl<Rhs> $op_assign_trait for DShape | ||
| where | ||
| Rhs: Into<Self>, | ||
| { | ||
| fn $op_assign_fn(&mut self, rhs: Rhs) { | ||
| let other = rhs.into(); | ||
| for i in 0..self.ndim().max(other.ndim()) { | ||
| self[i].$op_assign_fn(other[i]); | ||
| } | ||
| } | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| impl_op!(Add<Rhs>, add, AddAssign<Rhs>, add_assign); | ||
| impl_op!(Sub<Rhs>, sub, SubAssign<Rhs>, sub_assign); | ||
| impl_op!(Mul<Rhs>, mul, MulAssign<Rhs>, mul_assign); | ||
|
|
||
| impl<T> IntoIterator for DynAxesRepr<T> | ||
| where T: Clone | ||
| { | ||
| type Item = T; | ||
| type IntoIter = alloc::vec::IntoIter<T>; | ||
|
|
||
| fn into_iter(self) -> Self::IntoIter | ||
| { | ||
| match self { | ||
| DynAxesRepr::Inline(len, arr) => Vec::from(arr[..len].to_vec()).into_iter(), | ||
| DynAxesRepr::Alloc(b) => b.into_vec().into_iter(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Shape for DynAxesRepr<usize> | ||
| { | ||
| type Iter<'a> | ||
| = Cloned<Iter<'a, usize>> | ||
| where Self: 'a; | ||
|
|
||
| fn axis_len(&self, axis: usize) -> usize | ||
| { | ||
| self[axis] | ||
| } | ||
|
|
||
| fn iter(&self) -> Self::Iter<'_> | ||
| { | ||
| (**self).iter().cloned() | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand the limit of 4 here.