Skip to content

Use .as_slice_memory_order in .to_owned() and .map() #148

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 2 commits into from
Mar 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ script:
cargo test --verbose &&
([ -z "$FEATURES" ] || cargo build --verbose --features "$FEATURES") &&
([ -z "$FEATURES" ] || cargo test --verbose --features "$FEATURES") &&
([ "$BENCH" != 1 ] || cargo bench --verbose --features "$FEATURES")
([ "$BENCH" != 1 ] || cargo bench --no-run --verbose --features "$FEATURES")
22 changes: 15 additions & 7 deletions src/impl_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
pub fn to_owned(&self) -> OwnedArray<A, D>
where A: Clone
{
let data = if let Some(slc) = self.as_slice() {
slc.to_vec()
let (data, strides) = if let Some(slc) = self.as_slice_memory_order() {
(slc.to_vec(), self.strides.clone())
} else {
self.iter().cloned().collect()
(self.iter().cloned().collect(), self.dim.default_strides())
};
unsafe {
ArrayBase::from_vec_dim_unchecked(self.dim.clone(), data)
ArrayBase::from_vec_dim_stride_unchecked(self.dim.clone(), strides, data)
}
}

Expand Down Expand Up @@ -1074,9 +1074,17 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
where F: FnMut(&'a A) -> B,
A: 'a,
{
let v = ::iterators::to_vec(self.iter().map(f));
unsafe {
ArrayBase::from_vec_dim_unchecked(self.dim.clone(), v)
if let Some(slc) = self.as_slice_memory_order() {
let v = ::iterators::to_vec(slc.iter().map(f));
unsafe {
ArrayBase::from_vec_dim_stride_unchecked(
self.dim.clone(), self.strides.clone(), v)
}
} else {
let v = ::iterators::to_vec(self.iter().map(f));
unsafe {
ArrayBase::from_vec_dim_unchecked(self.dim.clone(), v)
}
}
}
}
2 changes: 2 additions & 0 deletions src/iterators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -888,13 +888,15 @@ send_sync_read_write!(AxisChunksIterMut);
/// to deliver exactly as many items as it said it would.
pub unsafe trait TrustedIterator { }

use std::slice;
use std::iter;
use linspace::Linspace;

unsafe impl<F> TrustedIterator for Linspace<F> { }
unsafe impl<'a, A, D> TrustedIterator for Elements<'a, A, D> { }
unsafe impl<I, F> TrustedIterator for iter::Map<I, F>
where I: TrustedIterator { }
unsafe impl<'a, A> TrustedIterator for slice::Iter<'a, A> { }


/// Like Iterator::collect, but only for trusted length iterators
Expand Down
26 changes: 26 additions & 0 deletions tests/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,32 @@ fn test_f_order() {
assert_eq!(dupc, dupf);
}

#[test]
fn to_owned_memory_order() {
// check that .to_owned() makes f-contiguous arrays out of f-contiguous
// input.
let c = arr2(&[[1, 2, 3],
[4, 5, 6]]);
let mut f = c.view();
f.swap_axes(0, 1);
let fo = f.to_owned();
assert_eq!(f, fo);
assert_eq!(f.strides(), fo.strides());
}

#[test]
fn map_memory_order() {
let a = arr3(&[[[1, 2, 3],
[4, 5, 6]],
[[7, 8, 9],
[0, -1, -2]]]);
let mut v = a.view();
v.swap_axes(0, 1);
let amap = v.map(|x| *x >= 3);
assert_eq!(amap.dim(), v.dim());
assert_eq!(amap.strides(), v.strides());
}

#[test]
fn test_contiguous() {
let c = arr3(&[[[1, 2, 3],
Expand Down