forked from M-Labs/nalgebra
757b99e843
* CSC: Create constructor for unsorted but otherwise valid data * Test creating csc matrix from unsorted but valid data * Add function for validation and sorting * Move validation function to cs.rs * Restore pattern unit test * Add unit test for 'major offset out of bounds' case * Avoid permutation allocations on 'happy path' * Reuse allocated permutation * Fix comments for test-data examples * Remove unnecessary iter variable * Set up buffers for sorting up front * Use common apply_permutation function * Use common compute_sort_permutation function * Move unsafe down to unchecked call * Add panic cases to documentation * Remove unnecessary Zero bound * Move buffer set up away from loop * Lift T::Zero from cs.rs * Improve checking if values are provided * Simplify copying from slices & add test for wrong values length * Check duplicates after sorting * Fix formatting * Check values length at the beginning * Check length of values if values != None
27 lines
1.1 KiB
Rust
27 lines
1.1 KiB
Rust
//! Helper functions for sparse matrix computations
|
|
|
|
/// permutes entries of in_slice according to permutation slice and puts them to out_slice
|
|
#[inline]
|
|
pub fn apply_permutation<T: Clone>(out_slice: &mut [T], in_slice: &[T], permutation: &[usize]) {
|
|
assert_eq!(out_slice.len(), in_slice.len());
|
|
assert_eq!(out_slice.len(), permutation.len());
|
|
for (out_element, old_pos) in out_slice.iter_mut().zip(permutation) {
|
|
*out_element = in_slice[*old_pos].clone();
|
|
}
|
|
}
|
|
|
|
/// computes permutation by using provided indices as keys
|
|
#[inline]
|
|
pub fn compute_sort_permutation(permutation: &mut [usize], indices: &[usize]) {
|
|
assert_eq!(permutation.len(), indices.len());
|
|
// Set permutation to identity
|
|
for (i, p) in permutation.iter_mut().enumerate() {
|
|
*p = i;
|
|
}
|
|
|
|
// Compute permutation needed to bring minor indices into sorted order
|
|
// Note: Using sort_unstable here avoids internal allocations, which is crucial since
|
|
// each lane might have a small number of elements
|
|
permutation.sort_unstable_by_key(|idx| indices[*idx]);
|
|
}
|