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
CowArray feature #632
Merged
Merged
CowArray feature #632
Changes from 3 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
b39a0bb
Merge pull request #1 from rust-ndarray/master
7778299
COW implementation + tests
20cc81e
Finished the tests
efd686d
Merge pull request #5 from rust-ndarray/master
279a177
Merge branch 'master' of github.com:andrei-papou/ndarray into cow-fea…
885527a
Fixed some tests
d34a675
Fixed rustfmt errors
502144e
Apply ArrayCow tests to non-standard layouts
jturner314 aefaacf
Improve documentation for ArrayCow
jturner314 fde676d
Use match
jturner314 a1d07d6
Rename ArrayCow to CowArray
jturner314 46b70ef
Merge pull request #7 from jturner314/improve-cow-feature
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,55 @@ | ||
// Copyright 2019 ndarray developers. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
use crate::imp_prelude::*; | ||
|
||
/// Methods specific to `ArrayCow`. | ||
/// | ||
/// ***See also all methods for [`ArrayBase`]*** | ||
/// | ||
/// [`ArrayBase`]: struct.ArrayBase.html | ||
impl<'a, A, D> ArrayCow<'a, A, D> | ||
where | ||
D: Dimension, | ||
{ | ||
pub fn is_view(&self) -> bool { | ||
self.data.is_view() | ||
} | ||
|
||
pub fn is_owned(&self) -> bool { | ||
self.data.is_owned() | ||
} | ||
} | ||
|
||
impl<'a, A, D> From<ArrayView<'a, A, D>> for ArrayCow<'a, A, D> | ||
where | ||
D: Dimension, | ||
{ | ||
fn from(view: ArrayView<'a, A, D>) -> ArrayCow<'a, A, D> { | ||
ArrayBase { | ||
data: CowRepr::View(view.data), | ||
ptr: view.ptr, | ||
dim: view.dim, | ||
strides: view.strides, | ||
} | ||
} | ||
} | ||
|
||
impl<'a, A, D> From<Array<A, D>> for ArrayCow<'a, A, D> | ||
where | ||
D: Dimension, | ||
{ | ||
fn from(array: Array<A, D>) -> ArrayCow<'a, A, D> { | ||
ArrayBase { | ||
data: CowRepr::Owned(array.data), | ||
ptr: array.ptr, | ||
dim: array.dim, | ||
strides: array.strides, | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -25,6 +25,7 @@ pub use crate::{ | |
Array, | ||
ArcArray, | ||
RcArray, | ||
ArrayCow, | ||
ArrayView, | ||
ArrayViewMut, | ||
RawArrayView, | ||
|
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 |
---|---|---|
|
@@ -1891,3 +1891,119 @@ fn array_macros() { | |
let empty2: Array<f32, Ix2> = array![[]]; | ||
assert_eq!(empty2, array![[]]); | ||
} | ||
|
||
#[cfg(test)] | ||
mod array_cow_tests { | ||
use super::*; | ||
use ndarray::Data; | ||
|
||
fn is_content_identical<A, S1, S2, D>(arr1: &ArrayBase<S1, D>, arr2: &ArrayBase<S2, D>) -> bool | ||
where A: Clone + PartialEq, | ||
S1: Data<Elem=A>, | ||
S2: Data<Elem=A>, | ||
D: Dimension | ||
{ | ||
arr1.iter().zip(arr2.iter()).all(|(x1, x2)| x1 == x2) | ||
} | ||
|
||
#[test] | ||
fn test_is_view() { | ||
let arr: Array<i32, Ix2> = array![[1, 2], [3, 4]]; | ||
let arr_cow = ArrayCow::<i32, Ix2>::from(arr.view()); | ||
assert!(arr_cow.is_view()); | ||
let arr_cow = ArrayCow::<i32, Ix2>::from(arr.clone()); | ||
assert!(!arr_cow.is_view()); | ||
} | ||
|
||
#[test] | ||
fn test_is_owned() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's only a suggestion, but I would merge |
||
let arr: Array<i32, Ix2> = array![[1, 2], [3, 4]]; | ||
let arr_cow = ArrayCow::<i32, Ix2>::from(arr.clone()); | ||
assert!(arr_cow.is_owned()); | ||
let arr_cow = ArrayCow::<i32, Ix2>::from(arr.view()); | ||
assert!(!arr_cow.is_owned()); | ||
} | ||
|
||
#[test] | ||
fn test_element_mutation() { | ||
let arr: Array2<i32> = array![[1, 2], [3, 4]]; | ||
let mut arr_cow = ArrayCow::<i32, Ix2>::from(arr.view()); | ||
arr_cow[(1, 1)] = 2; | ||
let expected_arr: Array2<i32> = array![[1, 2], [3, 2]]; | ||
assert!(arr_cow.is_owned()); | ||
assert!(is_content_identical(&arr_cow, &expected_arr)); | ||
|
||
let mut arr_cow = ArrayCow::<i32, Ix2>::from(arr.clone()); | ||
let prev_ptr = arr_cow.as_ptr(); | ||
arr_cow[(1, 1)] = 2; | ||
assert_eq!(arr_cow.as_ptr(), prev_ptr); | ||
assert!(is_content_identical(&arr_cow, &expected_arr)); | ||
} | ||
|
||
#[test] | ||
fn test_clone() { | ||
let arr: Array2<i32> = array![[1, 2], [3, 4]]; | ||
let arr_cow = ArrayCow::<i32, Ix2>::from(arr.view()); | ||
let arr_cow_clone = arr_cow.clone(); | ||
assert!(arr_cow_clone.is_view()); | ||
assert!(is_content_identical(&arr_cow, &arr_cow_clone)); | ||
assert_eq!(arr_cow.dim(), arr_cow_clone.dim()); | ||
assert_eq!(arr_cow.strides(), arr_cow_clone.strides()); | ||
|
||
let arr_cow = ArrayCow::<i32, Ix2>::from(arr.clone()); | ||
let arr_cow_clone = arr_cow.clone(); | ||
assert!(arr_cow_clone.is_owned()); | ||
assert!(is_content_identical(&arr_cow, &arr_cow_clone)); | ||
assert_eq!(arr_cow.dim(), arr_cow_clone.dim()); | ||
assert_eq!(arr_cow.strides(), arr_cow_clone.strides()); | ||
} | ||
|
||
#[test] | ||
fn test_clone_from() { | ||
let arr: Array2<i32> = array![[1, 2], [3, 4]]; | ||
let other_arr: Array2<i32> = array![[11, 12], [13, 14]]; | ||
|
||
fn perform_checks(arr1: &ArrayCow<i32, Ix2>, arr2: &ArrayCow<i32, Ix2>) { | ||
assert!(is_content_identical(arr1, arr2)); | ||
assert_eq!(arr1.dim(), arr2.dim()); | ||
assert_eq!(arr1.strides(), arr2.strides()); | ||
} | ||
|
||
let arr_cow_src = ArrayCow::<i32, Ix2>::from(arr.view()); | ||
let mut arr_cow_dst = ArrayCow::<i32, Ix2>::from(other_arr.clone()); | ||
arr_cow_dst.clone_from(&arr_cow_src); | ||
assert!(arr_cow_dst.is_view()); | ||
perform_checks(&arr_cow_src, &arr_cow_dst); | ||
|
||
let arr_cow_src = ArrayCow::<i32, Ix2>::from(arr.view()); | ||
let mut arr_cow_dst = ArrayCow::<i32, Ix2>::from(other_arr.view()); | ||
arr_cow_dst.clone_from(&arr_cow_src); | ||
assert!(arr_cow_dst.is_view()); | ||
perform_checks(&arr_cow_src, &arr_cow_dst); | ||
|
||
let arr_cow_src = ArrayCow::<i32, Ix2>::from(arr.clone()); | ||
let mut arr_cow_dst = ArrayCow::<i32, Ix2>::from(other_arr.view()); | ||
arr_cow_dst.clone_from(&arr_cow_src); | ||
assert!(arr_cow_dst.is_owned()); | ||
perform_checks(&arr_cow_src, &arr_cow_dst); | ||
|
||
let arr_cow_src = ArrayCow::<i32, Ix2>::from(arr.clone()); | ||
let mut arr_cow_dst = ArrayCow::<i32, Ix2>::from(other_arr.clone()); | ||
arr_cow_dst.clone_from(&arr_cow_src); | ||
assert!(arr_cow_dst.is_owned()); | ||
perform_checks(&arr_cow_src, &arr_cow_dst); | ||
} | ||
|
||
#[test] | ||
fn test_into_owned() { | ||
let arr: Array2<i32> = array![[1, 2], [3, 4]]; | ||
let cont_arr = ArrayCow::<i32, Ix2>::from(arr.view()).into_owned(); | ||
assert!(is_content_identical(&arr, &cont_arr)); | ||
|
||
let cont_arr = ArrayCow::<i32, Ix2>::from(arr.clone()); | ||
let prev_ptr = cont_arr.as_ptr(); | ||
let cont_arr = cont_arr.into_owned(); | ||
assert_eq!(cont_arr.as_ptr(), prev_ptr); | ||
assert!(is_content_identical(&arr, &cont_arr)); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason why you can't use the good old
assert_eq!
? Your first usage looks likeand I don't understand why
assert_eq!
can't do the job.