From f61482465ce56ac20ec401973c82f31970c7436c Mon Sep 17 00:00:00 2001 From: Timo Betcke Date: Tue, 31 Oct 2023 12:57:10 +0000 Subject: [PATCH] Views are now resizeable --- dense/src/array/views.rs | 356 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 332 insertions(+), 24 deletions(-) diff --git a/dense/src/array/views.rs b/dense/src/array/views.rs index a66b9d18..3983dbd8 100644 --- a/dense/src/array/views.rs +++ b/dense/src/array/views.rs @@ -10,13 +10,305 @@ pub struct ArrayView< Item: Scalar, ArrayImpl: UnsafeRandomAccessByValue + Shape, const NDIM: usize, +> { + arr: &'a Array, +} + +pub struct ArrayViewMut< + 'a, + Item: Scalar, + ArrayImpl: UnsafeRandomAccessByValue + + Shape + + UnsafeRandomAccessMut, + const NDIM: usize, +> { + arr: &'a mut Array, +} + +impl< + 'a, + Item: Scalar, + ArrayImpl: UnsafeRandomAccessByValue + Shape, + const NDIM: usize, + > ArrayView<'a, Item, ArrayImpl, NDIM> +{ + pub fn new(arr: &'a Array) -> Self { + Self { arr } + } +} + +impl< + 'a, + Item: Scalar, + ArrayImpl: UnsafeRandomAccessByValue + + Shape + + UnsafeRandomAccessMut, + const NDIM: usize, + > ArrayViewMut<'a, Item, ArrayImpl, NDIM> +{ + pub fn new(arr: &'a mut Array) -> Self { + Self { arr } + } +} + +///////////////// +/// Basic traits for ArrayView + +impl< + 'a, + Item: Scalar, + ArrayImpl: UnsafeRandomAccessByValue + Shape, + const NDIM: usize, + > Shape for ArrayView<'a, Item, ArrayImpl, NDIM> +{ + fn shape(&self) -> [usize; NDIM] { + self.arr.shape() + } +} + +impl< + 'a, + Item: Scalar, + ArrayImpl: UnsafeRandomAccessByValue + Shape + Stride, + const NDIM: usize, + > Stride for ArrayView<'a, Item, ArrayImpl, NDIM> +{ + fn stride(&self) -> [usize; NDIM] { + self.arr.stride() + } +} + +impl< + 'a, + Item: Scalar, + ArrayImpl: UnsafeRandomAccessByValue + + Shape + + RawAccess + + Stride, + const NDIM: usize, + > RawAccess for ArrayView<'a, Item, ArrayImpl, NDIM> +{ + type Item = Item; + + fn data(&self) -> &[Self::Item] { + self.arr.data() + } +} + +impl< + 'a, + Item: Scalar, + ArrayImpl: UnsafeRandomAccessByValue + Shape, + const NDIM: usize, + > UnsafeRandomAccessByValue for ArrayView<'a, Item, ArrayImpl, NDIM> +{ + type Item = Item; + #[inline] + unsafe fn get_value_unchecked(&self, multi_index: [usize; NDIM]) -> Self::Item { + self.arr.get_value_unchecked(multi_index) + } +} + +impl< + 'a, + Item: Scalar, + ArrayImpl: UnsafeRandomAccessByValue + + Shape + + UnsafeRandomAccessByRef, + const NDIM: usize, + > UnsafeRandomAccessByRef for ArrayView<'a, Item, ArrayImpl, NDIM> +{ + type Item = Item; + #[inline] + unsafe fn get_unchecked(&self, multi_index: [usize; NDIM]) -> &Self::Item { + self.arr.get_unchecked(multi_index) + } +} + +impl< + 'a, + Item: Scalar, + ArrayImpl: UnsafeRandomAccessByValue + Shape + ChunkedAccess, + const NDIM: usize, + const N: usize, + > ChunkedAccess for ArrayView<'a, Item, ArrayImpl, NDIM> +{ + type Item = Item; + fn get_chunk( + &self, + chunk_index: usize, + ) -> Option> { + self.arr.get_chunk(chunk_index) + } +} + +//////////////// +/// Basic traits for ArrayViewMut + +impl< + 'a, + Item: Scalar, + ArrayImpl: UnsafeRandomAccessByValue + + Shape + + UnsafeRandomAccessMut, + const NDIM: usize, + > Shape for ArrayViewMut<'a, Item, ArrayImpl, NDIM> +{ + fn shape(&self) -> [usize; NDIM] { + self.arr.shape() + } +} + +impl< + 'a, + Item: Scalar, + ArrayImpl: UnsafeRandomAccessByValue + + Shape + + Stride + + UnsafeRandomAccessMut, + const NDIM: usize, + > Stride for ArrayViewMut<'a, Item, ArrayImpl, NDIM> +{ + fn stride(&self) -> [usize; NDIM] { + self.arr.stride() + } +} + +impl< + 'a, + Item: Scalar, + ArrayImpl: UnsafeRandomAccessByValue + + Shape + + RawAccess + + Stride + + UnsafeRandomAccessMut, + const NDIM: usize, + > RawAccess for ArrayViewMut<'a, Item, ArrayImpl, NDIM> +{ + type Item = Item; + + fn data(&self) -> &[Self::Item] { + self.arr.data() + } +} + +impl< + 'a, + Item: Scalar, + ArrayImpl: UnsafeRandomAccessByValue + + Shape + + RawAccess + + Stride + + UnsafeRandomAccessMut + + RawAccessMut, + const NDIM: usize, + > RawAccessMut for ArrayViewMut<'a, Item, ArrayImpl, NDIM> +{ + fn data_mut(&mut self) -> &mut [Self::Item] { + self.arr.data_mut() + } +} + +impl< + 'a, + Item: Scalar, + ArrayImpl: UnsafeRandomAccessByValue + + Shape + + UnsafeRandomAccessMut, + const NDIM: usize, + > UnsafeRandomAccessByValue for ArrayViewMut<'a, Item, ArrayImpl, NDIM> +{ + type Item = Item; + #[inline] + unsafe fn get_value_unchecked(&self, multi_index: [usize; NDIM]) -> Self::Item { + self.arr.get_value_unchecked(multi_index) + } +} + +impl< + 'a, + Item: Scalar, + ArrayImpl: UnsafeRandomAccessByValue + + Shape + + UnsafeRandomAccessByRef + + UnsafeRandomAccessMut, + const NDIM: usize, + > UnsafeRandomAccessByRef for ArrayViewMut<'a, Item, ArrayImpl, NDIM> +{ + type Item = Item; + #[inline] + unsafe fn get_unchecked(&self, multi_index: [usize; NDIM]) -> &Self::Item { + self.arr.get_unchecked(multi_index) + } +} + +impl< + 'a, + Item: Scalar, + ArrayImpl: UnsafeRandomAccessByValue + + Shape + + ChunkedAccess + + UnsafeRandomAccessMut, + const NDIM: usize, + const N: usize, + > ChunkedAccess for ArrayViewMut<'a, Item, ArrayImpl, NDIM> +{ + type Item = Item; + fn get_chunk( + &self, + chunk_index: usize, + ) -> Option> { + self.arr.get_chunk(chunk_index) + } +} + +impl< + 'a, + Item: Scalar, + ArrayImpl: UnsafeRandomAccessByValue + + Shape + + UnsafeRandomAccessByRef + + UnsafeRandomAccessMut, + const NDIM: usize, + > UnsafeRandomAccessMut for ArrayViewMut<'a, Item, ArrayImpl, NDIM> +{ + type Item = Item; + #[inline] + unsafe fn get_unchecked_mut(&mut self, multi_index: [usize; NDIM]) -> &mut Self::Item { + self.arr.get_unchecked_mut(multi_index) + } +} + +impl< + 'a, + Item: Scalar, + ArrayImpl: UnsafeRandomAccessByValue + + Shape + + UnsafeRandomAccessByRef + + UnsafeRandomAccessMut + + ResizeInPlace, + const NDIM: usize, + > ResizeInPlace for ArrayViewMut<'a, Item, ArrayImpl, NDIM> +{ + #[inline] + fn resize_in_place(&mut self, shape: [usize; NDIM]) { + self.arr.resize_in_place(shape) + } +} + +//////////////////////////////////////////// +pub struct ArraySubView< + 'a, + Item: Scalar, + ArrayImpl: UnsafeRandomAccessByValue + Shape, + const NDIM: usize, > { arr: &'a Array, offset: [usize; NDIM], shape: [usize; NDIM], } -pub struct ArrayViewMut< +pub struct ArraySubViewMut< 'a, Item: Scalar, ArrayImpl: UnsafeRandomAccessByValue @@ -34,7 +326,7 @@ impl< Item: Scalar, ArrayImpl: UnsafeRandomAccessByValue + Shape, const NDIM: usize, - > ArrayView<'a, Item, ArrayImpl, NDIM> + > ArraySubView<'a, Item, ArrayImpl, NDIM> { pub fn new( arr: &'a Array, @@ -62,7 +354,7 @@ impl< + Shape + UnsafeRandomAccessMut, const NDIM: usize, - > ArrayViewMut<'a, Item, ArrayImpl, NDIM> + > ArraySubViewMut<'a, Item, ArrayImpl, NDIM> { pub fn new( arr: &'a mut Array, @@ -83,14 +375,14 @@ impl< } } -// Basic traits for ArrayView +// Basic traits for ArraySubView impl< 'a, Item: Scalar, ArrayImpl: UnsafeRandomAccessByValue + Shape, const NDIM: usize, - > Shape for ArrayView<'a, Item, ArrayImpl, NDIM> + > Shape for ArraySubView<'a, Item, ArrayImpl, NDIM> { fn shape(&self) -> [usize; NDIM] { self.shape @@ -102,7 +394,7 @@ impl< Item: Scalar, ArrayImpl: UnsafeRandomAccessByValue + Shape + Stride, const NDIM: usize, - > Stride for ArrayView<'a, Item, ArrayImpl, NDIM> + > Stride for ArraySubView<'a, Item, ArrayImpl, NDIM> { fn stride(&self) -> [usize; NDIM] { self.arr.stride() @@ -117,7 +409,7 @@ impl< + RawAccess + Stride, const NDIM: usize, - > RawAccess for ArrayView<'a, Item, ArrayImpl, NDIM> + > RawAccess for ArraySubView<'a, Item, ArrayImpl, NDIM> { type Item = Item; @@ -134,7 +426,7 @@ impl< Item: Scalar, ArrayImpl: UnsafeRandomAccessByValue + Shape, const NDIM: usize, - > UnsafeRandomAccessByValue for ArrayView<'a, Item, ArrayImpl, NDIM> + > UnsafeRandomAccessByValue for ArraySubView<'a, Item, ArrayImpl, NDIM> { type Item = Item; #[inline] @@ -152,7 +444,7 @@ impl< + Shape + UnsafeRandomAccessByRef, const NDIM: usize, - > UnsafeRandomAccessByRef for ArrayView<'a, Item, ArrayImpl, NDIM> + > UnsafeRandomAccessByRef for ArraySubView<'a, Item, ArrayImpl, NDIM> { type Item = Item; #[inline] @@ -169,7 +461,7 @@ impl< ArrayImpl: UnsafeRandomAccessByValue + Shape + ChunkedAccess, const NDIM: usize, const N: usize, - > ChunkedAccess for ArrayView<'a, Item, ArrayImpl, NDIM> + > ChunkedAccess for ArraySubView<'a, Item, ArrayImpl, NDIM> { type Item = Item; #[inline] @@ -209,7 +501,7 @@ impl< + Shape + UnsafeRandomAccessMut, const NDIM: usize, - > Shape for ArrayViewMut<'a, Item, ArrayImpl, NDIM> + > Shape for ArraySubViewMut<'a, Item, ArrayImpl, NDIM> { fn shape(&self) -> [usize; NDIM] { self.shape @@ -224,7 +516,7 @@ impl< + UnsafeRandomAccessMut + Stride, const NDIM: usize, - > Stride for ArrayViewMut<'a, Item, ArrayImpl, NDIM> + > Stride for ArraySubViewMut<'a, Item, ArrayImpl, NDIM> { fn stride(&self) -> [usize; NDIM] { self.arr.stride() @@ -240,7 +532,7 @@ impl< + RawAccess + Stride, const NDIM: usize, - > RawAccess for ArrayViewMut<'a, Item, ArrayImpl, NDIM> + > RawAccess for ArraySubViewMut<'a, Item, ArrayImpl, NDIM> { type Item = Item; @@ -260,7 +552,7 @@ impl< + RawAccessMut + Stride, const NDIM: usize, - > RawAccessMut for ArrayViewMut<'a, Item, ArrayImpl, NDIM> + > RawAccessMut for ArraySubViewMut<'a, Item, ArrayImpl, NDIM> { fn data_mut(&mut self) -> &mut [Self::Item] { assert!(!self.is_empty()); @@ -277,7 +569,7 @@ impl< + Shape + UnsafeRandomAccessMut, const NDIM: usize, - > UnsafeRandomAccessByValue for ArrayViewMut<'a, Item, ArrayImpl, NDIM> + > UnsafeRandomAccessByValue for ArraySubViewMut<'a, Item, ArrayImpl, NDIM> { type Item = Item; #[inline] @@ -296,7 +588,7 @@ impl< + UnsafeRandomAccessByRef + UnsafeRandomAccessMut, const NDIM: usize, - > UnsafeRandomAccessByRef for ArrayViewMut<'a, Item, ArrayImpl, NDIM> + > UnsafeRandomAccessByRef for ArraySubViewMut<'a, Item, ArrayImpl, NDIM> { type Item = Item; #[inline] @@ -317,7 +609,7 @@ impl< + ChunkedAccess, const NDIM: usize, const N: usize, - > ChunkedAccess for ArrayViewMut<'a, Item, ArrayImpl, NDIM> + > ChunkedAccess for ArraySubViewMut<'a, Item, ArrayImpl, NDIM> { type Item = Item; #[inline] @@ -355,7 +647,7 @@ impl< + Shape + UnsafeRandomAccessMut, const NDIM: usize, - > UnsafeRandomAccessMut for ArrayViewMut<'a, Item, ArrayImpl, NDIM> + > UnsafeRandomAccessMut for ArraySubViewMut<'a, Item, ArrayImpl, NDIM> { type Item = Item; #[inline] @@ -376,12 +668,19 @@ impl< &self, offset: [usize; NDIM], shape: [usize; NDIM], - ) -> Array, NDIM> { - Array::new(ArrayView::new(self, offset, shape)) + ) -> Array, NDIM> { + Array::new(ArraySubView::new(self, offset, shape)) } +} +impl< + Item: Scalar, + ArrayImpl: UnsafeRandomAccessByValue + Shape, + const NDIM: usize, + > Array +{ pub fn view(&self) -> Array, NDIM> { - self.subview([0; NDIM], self.shape()) + Array::new(ArrayView::new(self)) } } @@ -397,12 +696,21 @@ impl< &mut self, offset: [usize; NDIM], shape: [usize; NDIM], - ) -> Array, NDIM> { - Array::new(ArrayViewMut::new(self, offset, shape)) + ) -> Array, NDIM> { + Array::new(ArraySubViewMut::new(self, offset, shape)) } +} +impl< + Item: Scalar, + ArrayImpl: UnsafeRandomAccessByValue + + Shape + + UnsafeRandomAccessMut, + const NDIM: usize, + > Array +{ pub fn view_mut(&mut self) -> Array, NDIM> { - self.subview_mut([0; NDIM], self.shape()) + Array::new(ArrayViewMut::new(self)) } }