From 752d1f300d9087d2493dc752957ad0237329a75f Mon Sep 17 00:00:00 2001 From: Anton Date: Wed, 20 Oct 2021 01:50:42 +0200 Subject: [PATCH] Permute values without unnecessary allocation --- nalgebra-sparse/src/csr.rs | 10 ++++------ nalgebra-sparse/tests/unit_tests/csr.rs | 2 +- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/nalgebra-sparse/src/csr.rs b/nalgebra-sparse/src/csr.rs index ecb2ce65..bbeec1da 100644 --- a/nalgebra-sparse/src/csr.rs +++ b/nalgebra-sparse/src/csr.rs @@ -5,13 +5,12 @@ use crate::cs::{CsLane, CsLaneIter, CsLaneIterMut, CsLaneMut, CsMatrix}; use crate::csc::CscMatrix; use crate::pattern::{SparsityPattern, SparsityPatternFormatError, SparsityPatternIter}; -use crate::utils::apply_permutation; use crate::{SparseEntry, SparseEntryMut, SparseFormatError, SparseFormatErrorKind}; use nalgebra::Scalar; use num_traits::One; -use num_traits::Zero; +use std::iter::FromIterator; use std::slice::{Iter, IterMut}; /// A CSR representation of a sparse matrix. @@ -189,7 +188,7 @@ impl CsrMatrix { values: Vec, ) -> Result where - T: Scalar + Zero, + T: Scalar, { use SparsityPatternFormatError::*; let count = col_indices.len(); @@ -228,11 +227,10 @@ impl CsrMatrix { } // permute indices - let sorted_col_indices: Vec = p.iter().map(|i| col_indices[*i]).collect(); + let sorted_col_indices: Vec = Vec::from_iter((p.iter().map(|i| &col_indices[*i])).cloned()); // permute values - let mut sorted_values: Vec = vec![T::zero(); count]; - apply_permutation(&mut sorted_values, &values, &p); + let sorted_values: Vec = Vec::from_iter((p.iter().map(|i| &values[*i])).cloned()); return Self::try_from_csr_data( num_rows, diff --git a/nalgebra-sparse/tests/unit_tests/csr.rs b/nalgebra-sparse/tests/unit_tests/csr.rs index 6d884b75..3ca2f0dc 100644 --- a/nalgebra-sparse/tests/unit_tests/csr.rs +++ b/nalgebra-sparse/tests/unit_tests/csr.rs @@ -292,7 +292,7 @@ fn csr_matrix_try_from_invalid_csr_data() { } #[test] -fn csr_matrix_try_from_invalid_csr_data_with_new_constructor() { +fn csr_matrix_try_from_unsorted_invalid_csr_data() { let invalid_data: InvalidCsrDataExamples = InvalidCsrDataExamples::new(); { // Empty offset array (invalid length)