From c9661ef06ed77433b5ac7de35e7c330c6a839074 Mon Sep 17 00:00:00 2001 From: bluss Date: Tue, 27 Dec 2016 16:11:00 +0100 Subject: [PATCH] Add .to_vec() for one dimensional arrays --- src/impl_1d.rs | 27 +++++++++++++++++++++++++++ src/lib.rs | 1 + tests/array.rs | 14 ++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 src/impl_1d.rs diff --git a/src/impl_1d.rs b/src/impl_1d.rs new file mode 100644 index 000000000..4d013e7d5 --- /dev/null +++ b/src/impl_1d.rs @@ -0,0 +1,27 @@ +// Copyright 2016 bluss and ndarray developers. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , 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 ArrayBase + where S: Data, +{ + /// Return an vector with the elements of the one-dimensional array. + pub fn to_vec(&self) -> Vec + where A: Clone, + { + if let Some(slc) = self.as_slice() { + slc.to_vec() + } else { + ::iterators::to_vec(self.iter().map(|x| x.clone())) + } + } +} + diff --git a/src/lib.rs b/src/lib.rs index 5e049b13e..59e332308 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -601,6 +601,7 @@ impl ArrayBase } +mod impl_1d; mod impl_2d; mod numeric; diff --git a/tests/array.rs b/tests/array.rs index 7740f9cf4..07d21aebb 100644 --- a/tests/array.rs +++ b/tests/array.rs @@ -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::::zeros((3, 3));