Skip to content

Commit

Permalink
Optimize to_vec method in Array2 implementation (collect iterator)
Browse files Browse the repository at this point in the history
  • Loading branch information
daniil-berg committed Dec 11, 2023
1 parent 032081d commit ff96aed
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/tensor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,10 @@ impl<C: TensorComponent> TensorBase for Array2<C> {
}

fn to_vec(&self) -> Vec<Vec<Self::Component>> {
let mut output = Vec::<Vec<Self::Component>>::new();
for row in self.rows() {
output.push(row.to_vec());
}
return output;
self.rows()
.into_iter()
.map(|row| row.to_vec())
.collect()
}

fn transpose(&self) -> Self {
Expand Down Expand Up @@ -301,7 +300,7 @@ mod tests {
[0., 1., 2.],
[3., 4., 5.]
];
let vector = tensor.to_vec();
let vector = TensorBase::to_vec(&tensor);
let expected = vec![
[0., 1., 2.],
[3., 4., 5.]
Expand Down

0 comments on commit ff96aed

Please sign in to comment.