Skip to content

add sparse sort inner vectors function

Reference issue

Maybe #299 (closed) , #364 (closed) , #2558 (closed)

What does this implement/fix?

Adds utility to sort the inner vectors of a sparse matrix / vector with comparison function (default is std::less<>). Some sparse algorithms implicitly assume sorted inner vectors, and will not function properly otherwise. Other algorithms could benefit from sorted inner vectors, such as sparse transpositions, factorizations, and so on.

The sort is implemented with std::sort using a custom iterator CompressedStorageIterator that sorts the inner indices and values in parallel. Usually, a temporary vector of indices is used to apply the sorting permutation to the indices and values. This requires only one pass and no auxiliary storage.

CompressedStorageIterator can be used for many STL algorithms to operate on both the indices and values in parallel, such as std::swap_ranges, std::rotate, which may simplify further improvements to the sparse module.

Adapted from https://artificial-mind.net/blog/2020/11/28/std-sort-multiple-ranges

Additional information

Edited by Charles Schlosser

Merge request reports

Loading