Skip to content
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

As standard layout method #616

Merged
merged 30 commits into from
Jun 19, 2019
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
b39a0bb
Merge pull request #1 from rust-ndarray/master
Apr 3, 2019
c0890db
Implemented `as_contiguous` method
Apr 12, 2019
4ea2c7d
Added content check for the new array to the tests
Apr 12, 2019
5cdae91
Return CowArray from as_contiguous instead of Array
Apr 26, 2019
c82359a
Removed unused import
Apr 26, 2019
5ce8d31
Fixed wording + minor improvements
Apr 28, 2019
2a67f25
Added more tests + 2 methods for ArrayCow
Apr 30, 2019
2d453c7
Split single test into multiple ones
May 3, 2019
0d54de5
Fix try_ensure_unique for CowRepr
jturner314 May 5, 2019
1f4499f
Remove into_view/owned_array from ArrayCow
jturner314 May 5, 2019
2ede464
Relax to A: Clone for RawDataClone impl of CowRepr
jturner314 May 5, 2019
17e074b
Implement DataMut for CowRepr
jturner314 May 5, 2019
5506113
Impl clone_from_with_ptr for differing CowRepr variants
jturner314 May 5, 2019
556bd59
Add Cow types to preludes
jturner314 May 5, 2019
3101ca3
Move ArrayCow method impls into separate module
jturner314 May 5, 2019
131aea7
Replace from_view/owned_array with From impls
jturner314 May 5, 2019
0d68ea6
Add docs for ArrayCow
jturner314 May 5, 2019
b84b3ec
Remove A: Clone bound from CowRepr
jturner314 May 5, 2019
ebd6cc4
Use .raw_dim() instead of .dim()
jturner314 May 6, 2019
b14b3de
Replace .map(|x| x.clone()) with .cloned()
jturner314 May 6, 2019
9ec732a
Merge pull request #4 from jturner314/as-contiguous-method
May 6, 2019
efd686d
Merge pull request #5 from rust-ndarray/master
May 15, 2019
1417bcb
Removed ArrayCow changes
May 31, 2019
8cd5d5a
Merge pull request #8 from rust-ndarray/master
May 31, 2019
d85d4a8
Merge branch 'master' of github.com:andrei-papou/ndarray into as-cont…
May 31, 2019
4c2d2dd
Updated the tests
May 31, 2019
ac51cfd
Fixed rustfmt errors
May 31, 2019
c2ae416
Changes:
Jun 3, 2019
02d181d
Refine as_standard_layout docs
jturner314 Jun 19, 2019
f669f0f
Add as_standard_layout example
jturner314 Jun 19, 2019
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
19 changes: 18 additions & 1 deletion src/impl_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ use itertools::{izip, zip};

use crate::imp_prelude::*;

use crate::arraytraits;
use crate::dimension;
use crate::dimension::IntoDimension;
use crate::dimension::{
abs_index, axes_of, do_slice, merge_axes, size_of_shape_checked, stride_offset, Axes,
};
use crate::error::{self, ErrorKind, ShapeError};
use crate::zip::Zip;
use crate::{arraytraits, CowArray};
jturner314 marked this conversation as resolved.
Show resolved Hide resolved

use crate::iter::{
AxisChunksIter, AxisChunksIterMut, AxisIter, AxisIterMut, ExactChunks, ExactChunksMut,
Expand Down Expand Up @@ -1218,6 +1218,23 @@ where
D::is_contiguous(&self.dim, &self.strides)
}

pub fn as_standard_layout(&self) -> CowArray<'_, A, D>
jturner314 marked this conversation as resolved.
Show resolved Hide resolved
where
S: Data<Elem = A>,
A: Clone,
{
if self.is_standard_layout() {
CowArray::from(self.view())
} else {
let v = self.iter().cloned().collect::<Vec<A>>();
let owned_array: Array<A, D> = unsafe {
// Safe because we use shape and content of existing array here.
ArrayBase::from_shape_vec_unchecked(self.raw_dim(), v)
jturner314 marked this conversation as resolved.
Show resolved Hide resolved
};
CowArray::from(owned_array)
}
}

/// Return a pointer to the first element in the array.
///
/// Raw access to array elements needs to follow the strided indexing
Expand Down
71 changes: 71 additions & 0 deletions tests/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1970,6 +1970,77 @@ fn array_macros() {
assert_eq!(empty2, array![[]]);
}

#[cfg(test)]
mod as_standard_layout_tests {
jturner314 marked this conversation as resolved.
Show resolved Hide resolved
use super::*;

#[test]
fn test_f_layout() {
let shape = Ix2(2, 2).strides(Ix2(1, 2));
jturner314 marked this conversation as resolved.
Show resolved Hide resolved
let arr = Array::<i32, Ix2>::from_shape_vec(shape, vec![1, 2, 3, 4]).unwrap();
assert!(!arr.is_standard_layout());
let cont_arr = arr.as_standard_layout();
assert!(cont_arr.is_standard_layout());
assert_eq!(arr, cont_arr);
assert!(cont_arr.is_owned());
}

#[test]
fn test_c_layout() {
let arr = Array::<i32, Ix2>::from_shape_vec((2, 2), vec![1, 2, 3, 4]).unwrap();
assert!(arr.is_standard_layout());
let cont_arr = arr.as_standard_layout();
assert!(cont_arr.is_standard_layout());
assert_eq!(arr, cont_arr);
assert!(cont_arr.is_view());
}

#[test]
fn test_f_layout_view() {
let shape = Ix2(2, 2).strides(Ix2(1, 2));
let arr = Array::<i32, Ix2>::from_shape_vec(shape, vec![1, 2, 3, 4]).unwrap();
let arr_view = arr.view();
assert!(!arr_view.is_standard_layout());
let cont_arr = arr.as_standard_layout();
assert!(cont_arr.is_standard_layout());
assert_eq!(arr, cont_arr);
assert!(cont_arr.is_owned());
}

#[test]
fn test_c_layout_view() {
let arr = Array::<i32, Ix2>::from_shape_vec((2, 2), vec![1, 2, 3, 4]).unwrap();
let arr_view = arr.view();
assert!(arr_view.is_standard_layout());
let cont_arr = arr_view.as_standard_layout();
assert!(cont_arr.is_standard_layout());
assert_eq!(arr, cont_arr);
assert!(cont_arr.is_view());
}

#[test]
fn test_zero_dimensional_array() {
let arr_view = ArrayView1::<i32>::from(&[]);
assert!(arr_view.is_standard_layout());
let cont_arr = arr_view.as_standard_layout();
assert!(cont_arr.is_standard_layout());
assert_eq!(arr_view, cont_arr);
assert!(cont_arr.is_view());
}

#[test]
fn test_custom_layout() {
let shape = Ix4(1, 2, 3, 2).strides(Ix4(12, 1, 2, 6));
let arr_data: Vec<i32> = (0..12).collect();
let arr = Array::<i32, Ix4>::from_shape_vec(shape, arr_data).unwrap();
assert!(!arr.is_standard_layout());
let cont_arr = arr.as_standard_layout();
assert!(cont_arr.is_standard_layout());
assert_eq!(arr, cont_arr);
assert!(cont_arr.is_owned());
}
}

#[cfg(test)]
mod array_cow_tests {
use super::*;
Expand Down