Skip to content

Commit 288d2db

Browse files
authored
Merge pull request #259 from bluss/to-vec
Add .to_vec() for one dimensional arrays
2 parents 76694c3 + c9661ef commit 288d2db

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

src/impl_1d.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2016 bluss and ndarray developers.
2+
//
3+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6+
// option. This file may not be copied, modified, or distributed
7+
// except according to those terms.
8+
9+
10+
//! Methods for one-dimensional arrays.
11+
use imp_prelude::*;
12+
13+
impl<A, S> ArrayBase<S, Ix1>
14+
where S: Data<Elem=A>,
15+
{
16+
/// Return an vector with the elements of the one-dimensional array.
17+
pub fn to_vec(&self) -> Vec<A>
18+
where A: Clone,
19+
{
20+
if let Some(slc) = self.as_slice() {
21+
slc.to_vec()
22+
} else {
23+
::iterators::to_vec(self.iter().map(|x| x.clone()))
24+
}
25+
}
26+
}
27+

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,7 @@ impl<A, S, D> ArrayBase<S, D>
601601
}
602602

603603

604+
mod impl_1d;
604605
mod impl_2d;
605606

606607
mod numeric;

tests/array.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,6 +1096,20 @@ fn test_map_axis() {
10961096
assert_eq!(c, answer2);
10971097
}
10981098

1099+
#[test]
1100+
fn test_to_vec() {
1101+
let mut a = arr2(&[[1, 2, 3],
1102+
[4, 5, 6],
1103+
[7, 8, 9],
1104+
[10,11,12]]);
1105+
1106+
a.islice(s![..;-1, ..]);
1107+
assert_eq!(a.row(3).to_vec(), vec![1, 2, 3]);
1108+
assert_eq!(a.column(2).to_vec(), vec![12, 9, 6, 3]);
1109+
a.islice(s![.., ..;-1]);
1110+
assert_eq!(a.row(3).to_vec(), vec![3, 2, 1]);
1111+
}
1112+
10991113
#[test]
11001114
fn test_array_clone_unalias() {
11011115
let a = Array::<i32, _>::zeros((3, 3));

0 commit comments

Comments
 (0)