Skip to content

Add .to_vec() for one dimensional arrays #259

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 1 commit into from
Dec 27, 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
27 changes: 27 additions & 0 deletions src/impl_1d.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2016 bluss and 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.


//! Methods for one-dimensional arrays.
use imp_prelude::*;

impl<A, S> ArrayBase<S, Ix1>
where S: Data<Elem=A>,
{
/// Return an vector with the elements of the one-dimensional array.
pub fn to_vec(&self) -> Vec<A>
where A: Clone,
{
if let Some(slc) = self.as_slice() {
slc.to_vec()
} else {
::iterators::to_vec(self.iter().map(|x| x.clone()))
}
}
}

1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,7 @@ impl<A, S, D> ArrayBase<S, D>
}


mod impl_1d;
mod impl_2d;

mod numeric;
Expand Down
14 changes: 14 additions & 0 deletions tests/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,20 @@ fn test_map_axis() {
assert_eq!(c, answer2);
}

#[test]
fn test_to_vec() {
let mut a = arr2(&[[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10,11,12]]);

a.islice(s![..;-1, ..]);
assert_eq!(a.row(3).to_vec(), vec![1, 2, 3]);
assert_eq!(a.column(2).to_vec(), vec![12, 9, 6, 3]);
a.islice(s![.., ..;-1]);
assert_eq!(a.row(3).to_vec(), vec![3, 2, 1]);
}

#[test]
fn test_array_clone_unalias() {
let a = Array::<i32, _>::zeros((3, 3));
Expand Down