Permute values without unnecessary allocation
This commit is contained in:
parent
f90bb8d64a
commit
752d1f300d
|
@ -5,13 +5,12 @@
|
||||||
use crate::cs::{CsLane, CsLaneIter, CsLaneIterMut, CsLaneMut, CsMatrix};
|
use crate::cs::{CsLane, CsLaneIter, CsLaneIterMut, CsLaneMut, CsMatrix};
|
||||||
use crate::csc::CscMatrix;
|
use crate::csc::CscMatrix;
|
||||||
use crate::pattern::{SparsityPattern, SparsityPatternFormatError, SparsityPatternIter};
|
use crate::pattern::{SparsityPattern, SparsityPatternFormatError, SparsityPatternIter};
|
||||||
use crate::utils::apply_permutation;
|
|
||||||
use crate::{SparseEntry, SparseEntryMut, SparseFormatError, SparseFormatErrorKind};
|
use crate::{SparseEntry, SparseEntryMut, SparseFormatError, SparseFormatErrorKind};
|
||||||
|
|
||||||
use nalgebra::Scalar;
|
use nalgebra::Scalar;
|
||||||
use num_traits::One;
|
use num_traits::One;
|
||||||
use num_traits::Zero;
|
|
||||||
|
|
||||||
|
use std::iter::FromIterator;
|
||||||
use std::slice::{Iter, IterMut};
|
use std::slice::{Iter, IterMut};
|
||||||
|
|
||||||
/// A CSR representation of a sparse matrix.
|
/// A CSR representation of a sparse matrix.
|
||||||
|
@ -189,7 +188,7 @@ impl<T> CsrMatrix<T> {
|
||||||
values: Vec<T>,
|
values: Vec<T>,
|
||||||
) -> Result<Self, SparseFormatError>
|
) -> Result<Self, SparseFormatError>
|
||||||
where
|
where
|
||||||
T: Scalar + Zero,
|
T: Scalar,
|
||||||
{
|
{
|
||||||
use SparsityPatternFormatError::*;
|
use SparsityPatternFormatError::*;
|
||||||
let count = col_indices.len();
|
let count = col_indices.len();
|
||||||
|
@ -228,11 +227,10 @@ impl<T> CsrMatrix<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
// permute indices
|
// 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
|
// permute values
|
||||||
let mut sorted_values: Vec<T> = vec![T::zero(); count];
|
let sorted_values: Vec<T> = Vec::from_iter((p.iter().map(|i| &values[*i])).cloned());
|
||||||
apply_permutation(&mut sorted_values, &values, &p);
|
|
||||||
|
|
||||||
return Self::try_from_csr_data(
|
return Self::try_from_csr_data(
|
||||||
num_rows,
|
num_rows,
|
||||||
|
|
|
@ -292,7 +292,7 @@ fn csr_matrix_try_from_invalid_csr_data() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[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();
|
let invalid_data: InvalidCsrDataExamples = InvalidCsrDataExamples::new();
|
||||||
{
|
{
|
||||||
// Empty offset array (invalid length)
|
// Empty offset array (invalid length)
|
||||||
|
|
Loading…
Reference in New Issue