Skip to content

Commit

Permalink
Added new references function
Browse files Browse the repository at this point in the history
  • Loading branch information
tbetcke committed Dec 11, 2024
1 parent de71c60 commit 340cb0b
Show file tree
Hide file tree
Showing 3 changed files with 336 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/dense/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub mod operations;
pub mod operators;
pub mod random;
pub mod rank1_array;
pub mod reference;
pub mod slice;
pub mod views;

Expand Down
333 changes: 333 additions & 0 deletions src/dense/array/reference.rs
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))
}
}
2 changes: 2 additions & 0 deletions src/dense/array/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ impl<
> Array<Item, ArrayImpl, NDIM>
{
/// Return a view onto the array.
#[deprecated(note = "Please use arr.r() instead.")]
pub fn view(&self) -> Array<Item, ArrayView<'_, Item, ArrayImpl, NDIM>, NDIM> {
Array::new(ArrayView::new(self))
}
Expand All @@ -60,6 +61,7 @@ impl<
> Array<Item, ArrayImpl, NDIM>
{
/// Return a mutable view onto the array.
#[deprecated(note = "Please use arr.r_mut() instead.")]
pub fn view_mut(&mut self) -> Array<Item, ArrayViewMut<'_, Item, ArrayImpl, NDIM>, NDIM> {
Array::new(ArrayViewMut::new(self))
}
Expand Down

0 comments on commit 340cb0b

Please sign in to comment.