-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
336 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,333 @@ | ||
//! Reference to an array. | ||
//! | ||
//! A reference is an owned struct that holds a reference to an array. It is used to | ||
//! pass arrays to functions without transferring ownership. | ||
use crate::dense::types::RlstBase; | ||
|
||
use crate::dense::array::Array; | ||
|
||
use crate::dense::traits::{ | ||
ChunkedAccess, RawAccess, RawAccessMut, ResizeInPlace, Shape, Stride, UnsafeRandomAccessByRef, | ||
UnsafeRandomAccessByValue, UnsafeRandomAccessMut, | ||
}; | ||
|
||
/// Basic structure for a `View` | ||
pub struct ArrayRef< | ||
'a, | ||
Item: RlstBase, | ||
ArrayImpl: UnsafeRandomAccessByValue<NDIM, Item = Item> + Shape<NDIM>, | ||
const NDIM: usize, | ||
> { | ||
arr: &'a Array<Item, ArrayImpl, NDIM>, | ||
} | ||
|
||
impl< | ||
'a, | ||
Item: RlstBase, | ||
ArrayImpl: UnsafeRandomAccessByValue<NDIM, Item = Item> + Shape<NDIM>, | ||
const NDIM: usize, | ||
> ArrayRef<'a, Item, ArrayImpl, NDIM> | ||
{ | ||
/// Create new view | ||
pub fn new(arr: &'a Array<Item, ArrayImpl, NDIM>) -> Self { | ||
Self { arr } | ||
} | ||
} | ||
|
||
impl< | ||
Item: RlstBase, | ||
ArrayImpl: UnsafeRandomAccessByValue<NDIM, Item = Item> + Shape<NDIM>, | ||
const NDIM: usize, | ||
> Shape<NDIM> for ArrayRef<'_, Item, ArrayImpl, NDIM> | ||
{ | ||
fn shape(&self) -> [usize; NDIM] { | ||
self.arr.shape() | ||
} | ||
} | ||
|
||
impl< | ||
Item: RlstBase, | ||
ArrayImpl: UnsafeRandomAccessByValue<NDIM, Item = Item> + Shape<NDIM> + Stride<NDIM>, | ||
const NDIM: usize, | ||
> Stride<NDIM> for ArrayRef<'_, Item, ArrayImpl, NDIM> | ||
{ | ||
fn stride(&self) -> [usize; NDIM] { | ||
self.arr.stride() | ||
} | ||
} | ||
|
||
impl< | ||
Item: RlstBase, | ||
ArrayImpl: UnsafeRandomAccessByValue<NDIM, Item = Item> | ||
+ Shape<NDIM> | ||
+ RawAccess<Item = Item> | ||
+ Stride<NDIM>, | ||
const NDIM: usize, | ||
> RawAccess for ArrayRef<'_, Item, ArrayImpl, NDIM> | ||
{ | ||
type Item = Item; | ||
|
||
fn data(&self) -> &[Self::Item] { | ||
self.arr.data() | ||
} | ||
|
||
fn buff_ptr(&self) -> *const Self::Item { | ||
self.arr.buff_ptr() | ||
} | ||
|
||
fn offset(&self) -> usize { | ||
self.arr.offset() | ||
} | ||
} | ||
|
||
impl< | ||
Item: RlstBase, | ||
ArrayImpl: UnsafeRandomAccessByValue<NDIM, Item = Item> + Shape<NDIM>, | ||
const NDIM: usize, | ||
> UnsafeRandomAccessByValue<NDIM> for ArrayRef<'_, 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< | ||
Item: RlstBase, | ||
ArrayImpl: UnsafeRandomAccessByValue<NDIM, Item = Item> | ||
+ Shape<NDIM> | ||
+ UnsafeRandomAccessByRef<NDIM, Item = Item>, | ||
const NDIM: usize, | ||
> UnsafeRandomAccessByRef<NDIM> for ArrayRef<'_, 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< | ||
Item: RlstBase, | ||
ArrayImpl: UnsafeRandomAccessByValue<NDIM, Item = Item> + Shape<NDIM> + ChunkedAccess<N, Item = Item>, | ||
const NDIM: usize, | ||
const N: usize, | ||
> ChunkedAccess<N> for ArrayRef<'_, Item, ArrayImpl, NDIM> | ||
{ | ||
type Item = Item; | ||
fn get_chunk( | ||
&self, | ||
chunk_index: usize, | ||
) -> Option<crate::dense::types::DataChunk<Self::Item, N>> { | ||
self.arr.get_chunk(chunk_index) | ||
} | ||
} | ||
|
||
/////////// ArrayRefMut | ||
|
||
/// Mutable array view | ||
pub struct ArrayRefMut< | ||
'a, | ||
Item: RlstBase, | ||
ArrayImpl: UnsafeRandomAccessByValue<NDIM, Item = Item> | ||
+ Shape<NDIM> | ||
+ UnsafeRandomAccessMut<NDIM, Item = Item>, | ||
const NDIM: usize, | ||
> { | ||
arr: &'a mut Array<Item, ArrayImpl, NDIM>, | ||
} | ||
|
||
impl< | ||
'a, | ||
Item: RlstBase, | ||
ArrayImpl: UnsafeRandomAccessByValue<NDIM, Item = Item> | ||
+ Shape<NDIM> | ||
+ UnsafeRandomAccessMut<NDIM, Item = Item>, | ||
const NDIM: usize, | ||
> ArrayRefMut<'a, Item, ArrayImpl, NDIM> | ||
{ | ||
/// Create new mutable view | ||
pub fn new(arr: &'a mut Array<Item, ArrayImpl, NDIM>) -> Self { | ||
Self { arr } | ||
} | ||
} | ||
|
||
impl< | ||
Item: RlstBase, | ||
ArrayImpl: UnsafeRandomAccessByValue<NDIM, Item = Item> | ||
+ Shape<NDIM> | ||
+ UnsafeRandomAccessMut<NDIM, Item = Item>, | ||
const NDIM: usize, | ||
> Shape<NDIM> for ArrayRefMut<'_, Item, ArrayImpl, NDIM> | ||
{ | ||
fn shape(&self) -> [usize; NDIM] { | ||
self.arr.shape() | ||
} | ||
} | ||
|
||
impl< | ||
Item: RlstBase, | ||
ArrayImpl: UnsafeRandomAccessByValue<NDIM, Item = Item> | ||
+ Shape<NDIM> | ||
+ Stride<NDIM> | ||
+ UnsafeRandomAccessMut<NDIM, Item = Item>, | ||
const NDIM: usize, | ||
> Stride<NDIM> for ArrayRefMut<'_, Item, ArrayImpl, NDIM> | ||
{ | ||
fn stride(&self) -> [usize; NDIM] { | ||
self.arr.stride() | ||
} | ||
} | ||
|
||
impl< | ||
Item: RlstBase, | ||
ArrayImpl: UnsafeRandomAccessByValue<NDIM, Item = Item> | ||
+ Shape<NDIM> | ||
+ RawAccess<Item = Item> | ||
+ Stride<NDIM> | ||
+ UnsafeRandomAccessMut<NDIM, Item = Item>, | ||
const NDIM: usize, | ||
> RawAccess for ArrayRefMut<'_, Item, ArrayImpl, NDIM> | ||
{ | ||
type Item = Item; | ||
|
||
fn data(&self) -> &[Self::Item] { | ||
self.arr.data() | ||
} | ||
|
||
fn buff_ptr(&self) -> *const Self::Item { | ||
self.arr.buff_ptr() | ||
} | ||
|
||
fn offset(&self) -> usize { | ||
self.arr.offset() | ||
} | ||
} | ||
|
||
impl< | ||
Item: RlstBase, | ||
ArrayImpl: UnsafeRandomAccessByValue<NDIM, Item = Item> | ||
+ Shape<NDIM> | ||
+ RawAccess<Item = Item> | ||
+ Stride<NDIM> | ||
+ UnsafeRandomAccessMut<NDIM, Item = Item> | ||
+ RawAccessMut<Item = Item>, | ||
const NDIM: usize, | ||
> RawAccessMut for ArrayRefMut<'_, Item, ArrayImpl, NDIM> | ||
{ | ||
fn data_mut(&mut self) -> &mut [Self::Item] { | ||
self.arr.data_mut() | ||
} | ||
|
||
fn buff_ptr_mut(&mut self) -> *mut Self::Item { | ||
self.arr.buff_ptr_mut() | ||
} | ||
} | ||
|
||
impl< | ||
Item: RlstBase, | ||
ArrayImpl: UnsafeRandomAccessByValue<NDIM, Item = Item> | ||
+ Shape<NDIM> | ||
+ UnsafeRandomAccessMut<NDIM, Item = Item>, | ||
const NDIM: usize, | ||
> UnsafeRandomAccessByValue<NDIM> for ArrayRefMut<'_, 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< | ||
Item: RlstBase, | ||
ArrayImpl: UnsafeRandomAccessByValue<NDIM, Item = Item> | ||
+ Shape<NDIM> | ||
+ UnsafeRandomAccessByRef<NDIM, Item = Item> | ||
+ UnsafeRandomAccessMut<NDIM, Item = Item>, | ||
const NDIM: usize, | ||
> UnsafeRandomAccessByRef<NDIM> for ArrayRefMut<'_, 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< | ||
Item: RlstBase, | ||
ArrayImpl: UnsafeRandomAccessByValue<NDIM, Item = Item> | ||
+ Shape<NDIM> | ||
+ ChunkedAccess<N, Item = Item> | ||
+ UnsafeRandomAccessMut<NDIM, Item = Item>, | ||
const NDIM: usize, | ||
const N: usize, | ||
> ChunkedAccess<N> for ArrayRefMut<'_, Item, ArrayImpl, NDIM> | ||
{ | ||
type Item = Item; | ||
fn get_chunk( | ||
&self, | ||
chunk_index: usize, | ||
) -> Option<crate::dense::types::DataChunk<Self::Item, N>> { | ||
self.arr.get_chunk(chunk_index) | ||
} | ||
} | ||
|
||
impl< | ||
Item: RlstBase, | ||
ArrayImpl: UnsafeRandomAccessByValue<NDIM, Item = Item> | ||
+ Shape<NDIM> | ||
+ UnsafeRandomAccessMut<NDIM, Item = Item>, | ||
const NDIM: usize, | ||
> UnsafeRandomAccessMut<NDIM> for ArrayRefMut<'_, 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< | ||
Item: RlstBase, | ||
ArrayImpl: UnsafeRandomAccessByValue<NDIM, Item = Item> | ||
+ Shape<NDIM> | ||
+ UnsafeRandomAccessByRef<NDIM, Item = Item> | ||
+ UnsafeRandomAccessMut<NDIM, Item = Item> | ||
+ ResizeInPlace<NDIM>, | ||
const NDIM: usize, | ||
> ResizeInPlace<NDIM> for ArrayRefMut<'_, Item, ArrayImpl, NDIM> | ||
{ | ||
#[inline] | ||
fn resize_in_place(&mut self, shape: [usize; NDIM]) { | ||
self.arr.resize_in_place(shape) | ||
} | ||
} | ||
|
||
impl< | ||
Item: RlstBase, | ||
ArrayImpl: UnsafeRandomAccessByValue<NDIM, Item = Item> + Shape<NDIM>, | ||
const NDIM: usize, | ||
> Array<Item, ArrayImpl, NDIM> | ||
{ | ||
/// Return a reference to an array. | ||
pub fn r(&self) -> Array<Item, ArrayRef<'_, Item, ArrayImpl, NDIM>, NDIM> { | ||
Array::new(ArrayRef::new(self)) | ||
} | ||
} | ||
impl< | ||
Item: RlstBase, | ||
ArrayImpl: UnsafeRandomAccessByValue<NDIM, Item = Item> | ||
+ Shape<NDIM> | ||
+ UnsafeRandomAccessMut<NDIM, Item = Item>, | ||
const NDIM: usize, | ||
> Array<Item, ArrayImpl, NDIM> | ||
{ | ||
/// Return a mutable view onto the array. | ||
pub fn r_mut(&mut self) -> Array<Item, ArrayRefMut<'_, Item, ArrayImpl, NDIM>, NDIM> { | ||
Array::new(ArrayRefMut::new(self)) | ||
} | ||
} |
This file contains 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