Skip to content

Adds an array reference type #1440

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

Merged
merged 34 commits into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
e1ba6ca
Adds an array reference type.
akern40 Oct 4, 2024
cdf5012
Adds some documentation
akern40 Oct 4, 2024
5a5a39c
Fixes some CI/CD issues
akern40 Oct 5, 2024
1acf0e1
More CI/CD fixes
akern40 Oct 5, 2024
09800eb
Last CI/CD fix?
akern40 Oct 5, 2024
0d0a96a
Switches to the "same-repr" approach to allow array views to still be…
akern40 Oct 6, 2024
0f3f8d9
Changes back to Copy-capable ArrayBase and unchanged ArrayBase internals
akern40 Oct 6, 2024
fbafbb2
Removes unused import
akern40 Oct 6, 2024
52b276e
Introduces LayoutRef and starts to move functionality from ArrayBase …
akern40 Oct 11, 2024
a504217
Working version of the conversion to reference types
akern40 Oct 13, 2024
aa58113
Uses a new design with two reference types
akern40 Oct 13, 2024
9a2edb1
Satisfying CI/CD
akern40 Oct 13, 2024
e739d04
Adds Index, equality, IntoIterator, and Hash traits, plus approximates
akern40 Oct 13, 2024
fb7d87c
Finishes conversions of all possible ArrayBase impls to ArrayRef
akern40 Oct 14, 2024
fb24680
Satisfying CI/CD
akern40 Oct 14, 2024
bdc24bd
Adds some aliases to avoid breaking changes, and adds an example of h…
akern40 Oct 14, 2024
c72bf8f
Adds Borrow and ToOwned
akern40 Oct 14, 2024
2cce543
Tests that the *Assign operators work for slices via deref
akern40 Oct 14, 2024
7f70e9e
Somehow missed a `use` for `ToOwned`
akern40 Oct 14, 2024
c835680
Implicitly use deref
akern40 Oct 14, 2024
03fda7f
Adds documentation and aliases for `LayoutRef`
akern40 Oct 20, 2024
407b6c4
Fixes doc links
akern40 Oct 20, 2024
a9b5c37
Adds formatting for ArrayRef
akern40 Oct 20, 2024
8a26c34
Change some examples over to ArrayRef
akern40 Oct 20, 2024
8bb8f39
Adds missed #[repr(transparent)] for RawRef
akern40 Oct 20, 2024
775f06b
Simplifies deref logic and avoids null check
akern40 Oct 20, 2024
6eed17d
Adds documentation to ArrayRef
akern40 Oct 20, 2024
9c9d8c2
Adds missing aliases to ArrayBase from RawRef and LayoutRef
akern40 Oct 21, 2024
4e0bcc1
Adds a short snippet of documentation for `RawRef` and some top-level…
akern40 Oct 21, 2024
1bb06e7
Makes as_ref more explicit through methods on ArrayBase
akern40 Oct 26, 2024
435a9e7
Restore remove_index to DataOwned
akern40 Oct 26, 2024
6fd61c1
Fixes unused imports
akern40 Oct 26, 2024
0dedc3e
Re-fixes the covariant arrayview with the new reference structure
akern40 Mar 18, 2025
a71b509
Fixes the clippy lint and docs broken link
akern40 Mar 18, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Adds documentation and aliases for LayoutRef
  • Loading branch information
akern40 committed Mar 18, 2025
commit 03fda7fc0f9354b6f020a1d57ba0db9065fc7138
19 changes: 18 additions & 1 deletion src/aliases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//!

use crate::dimension::Dim;
use crate::{ArcArray, Array, ArrayRef, ArrayView, ArrayViewMut, Ix, IxDynImpl};
use crate::{ArcArray, Array, ArrayRef, ArrayView, ArrayViewMut, Ix, IxDynImpl, LayoutRef};

/// Create a zero-dimensional index
#[allow(non_snake_case)]
Expand Down Expand Up @@ -140,6 +140,23 @@ pub type ArrayRef6<A> = ArrayRef<A, Ix6>;
/// dynamic-dimensional array reference
pub type ArrayRefD<A> = ArrayRef<A, IxDyn>;

/// zero-dimensional layout reference
pub type LayoutRef0<A> = LayoutRef<A, Ix0>;
/// one-dimensional layout reference
pub type LayoutRef1<A> = LayoutRef<A, Ix1>;
/// two-dimensional layout reference
pub type LayoutRef2<A> = LayoutRef<A, Ix2>;
/// three-dimensional layout reference
pub type LayoutRef3<A> = LayoutRef<A, Ix3>;
/// four-dimensional layout reference
pub type LayoutRef4<A> = LayoutRef<A, Ix4>;
/// five-dimensional layout reference
pub type LayoutRef5<A> = LayoutRef<A, Ix5>;
/// six-dimensional layout reference
pub type LayoutRef6<A> = LayoutRef<A, Ix6>;
/// dynamic-dimensional layout reference
pub type LayoutRefD<A> = LayoutRef<A, IxDyn>;

/// zero-dimensional array view
pub type ArrayView0<'a, A> = ArrayView<'a, A, Ix0>;
/// one-dimensional array view
Expand Down
86 changes: 86 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1297,6 +1297,92 @@ where S: RawData<Elem = A>
}

/// A reference to the layout of an *n*-dimensional array.
///
/// This type can be used to read and write to the layout of an array;
/// that is to say, its shape and strides. It does not provide any read
/// or write access to the array's underlying data. It is generic on two
/// types: `D`, its dimensionality, and `A`, the element type of its data.
///
/// ## Example
/// Say we wanted to write a function that provides the aspect ratio
/// of any 2D array: the ratio of its width (number of columns) to its
/// height (number of rows). We would write that as follows:
/// ```rust
/// use ndarray::{LayoutRef2, array};
///
/// fn aspect_ratio<T, A>(layout: &T) -> (usize, usize)
/// where T: AsRef<LayoutRef2<A>>
/// {
/// let layout = layout.as_ref();
/// (layout.ncols(), layout.nrows())
/// }
///
/// let arr = array![[1, 2], [3, 4]];
/// assert_eq!(aspect_ratio(&arr), (2, 2));
/// ```
/// Similarly, new traits that provide functions that only depend on
/// or alter the layout of an array should do so via a blanket
/// implementation. Lets write a trait that both provides the aspect ratio
/// and lets users cut down arrays to a desired aspect ratio.
/// For simplicity, we'll panic if the user provides an aspect ratio
/// where either element is larger than the array's size.
/// ```rust
/// use ndarray::{LayoutRef2, array, s};
///
/// trait Ratioable<A> {
/// fn aspect_ratio(&self) -> (usize, usize)
/// where Self: AsRef<LayoutRef2<A>>;
///
/// fn cut_to_ratio(&mut self, ratio: (usize, usize))
/// where Self: AsMut<LayoutRef2<A>>;
/// }
///
/// impl<T, A> Ratioable<A> for T
/// where T: AsRef<LayoutRef2<A>> + AsMut<LayoutRef2<A>>
/// {
/// fn aspect_ratio(&self) -> (usize, usize)
/// {
/// let layout = self.as_ref();
/// (layout.ncols(), layout.nrows())
/// }
///
/// fn cut_to_ratio(&mut self, ratio: (usize, usize))
/// {
/// let layout = self.as_mut();
/// layout.slice_collapse(s![..ratio.1, ..ratio.0]);
/// }
/// }
///
/// let mut arr = array![[1, 2, 3], [4, 5, 6]];
/// assert_eq!(arr.aspect_ratio(), (3, 2));
/// arr.cut_to_ratio((2, 2));
/// assert_eq!(arr, array![[1, 2], [4, 5]]);
/// ```
/// Continue reading for why we use `AsRef` instead of taking `&LayoutRef` directly.
///
/// ## Writing Functions
/// Writing functions that accept `LayoutRef` is not as simple as taking
/// a `&LayoutRef` argument, as the above examples show. This is because
/// `LayoutRef` can be obtained either cheaply or expensively, depending
/// on the method used. `LayoutRef` can be obtained from all kinds of arrays
/// -- [owned](Array), [shared](ArcArray), [viewed](ArrayView), [referenced](ArrayRef),
/// and [raw referenced](RawRef) -- via `.as_ref()`. Critically, this way of
/// obtaining a `LayoutRef` is cheap, as it does not guarantee that the
/// underlying data is uniquely held.
///
/// However, `LayoutRef`s can be obtained a second way: they sit at the bottom
/// of a "deref chain" going from shared arrays, through `ArrayRef`, through
/// `RawRef`, and finally to `LayoutRef`. As a result, `LayoutRef`s can also
/// be obtained via auto-dereferencing. When requesting a mutable reference --
/// `&mut LayoutRef` -- the `deref_mut` to `ArrayRef` triggers a (possibly
/// expensive) guarantee that the data is uniquely held (see [`ArrayRef`]
/// for more information).
///
/// To help users avoid this error cost, functions that operate on `LayoutRef`s
/// should take their parameters as a generic type `T: AsRef<LayoutRef<A, D>>`,
/// as the above examples show. This aids the caller in two ways: they can pass
/// their arrays by reference (`&arr`) instead of explicitly calling `as_ref`,
/// and they will avoid paying a performance penalty for mutating the shape.
//
// # Safety for Implementors
//
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy