Permute values without unnecessary allocation

This commit is contained in:
Anton 2021-10-20 01:50:42 +02:00
parent f90bb8d64a
commit 752d1f300d
2 changed files with 5 additions and 7 deletions

View File

@ -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<T> CsrMatrix<T> {
values: Vec<T>,
) -> Result<Self, SparseFormatError>
where
T: Scalar + Zero,
T: Scalar,
{
use SparsityPatternFormatError::*;
let count = col_indices.len();
@ -228,11 +227,10 @@ impl<T> CsrMatrix<T> {
}
// permute indices
let sorted_col_indices: Vec<usize> = p.iter().map(|i| col_indices[*i]).collect();
let sorted_col_indices: Vec<usize> = Vec::from_iter((p.iter().map(|i| &col_indices[*i])).cloned());
// permute values
let mut sorted_values: Vec<T> = vec![T::zero(); count];
apply_permutation(&mut sorted_values, &values, &p);
let sorted_values: Vec<T> = Vec::from_iter((p.iter().map(|i| &values[*i])).cloned());
return Self::try_from_csr_data(
num_rows,

View File

@ -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)