29template<
typename T,
typename Compare>
32 std::vector<size_t> p(vec.size());
33 std::iota(p.begin(), p.end(), 0);
34 std::sort(p.begin(), p.end(),
35 [&](
size_t i,
size_t j) { return compare(vec[i], vec[j]); });
44template<
typename Derived,
typename Compare>
45std::vector<size_t>
sort_permutation(
const Eigen::MatrixBase<Derived>& vec, Compare compare)
47 std::vector<size_t> p(
static_cast<size_t>(vec.rows()));
48 std::iota(p.begin(), p.end(), 0);
49 std::sort(p.begin(), p.end(),
50 [&](
size_t i,
size_t j) { return compare(vec(static_cast<Eigen::Index>(i)), vec(static_cast<Eigen::Index>(j))); });
62 std::vector<T> sorted_vec(vec.size());
63 std::transform(p.begin(), p.end(), sorted_vec.begin(),
64 [&](
size_t i) { return vec[i]; });
73template<
typename Scalar,
int Size>
74Eigen::Vector<Scalar, Size>
apply_permutation(
const Eigen::Vector<Scalar, Size>& vec,
const std::vector<size_t>& p)
76 Eigen::Vector<Scalar, Size> sorted = vec;
77 std::transform(p.begin(), p.end(), sorted.begin(),
78 [&](
size_t i) { return vec(static_cast<Eigen::Index>(i)); });
87template<
typename Derived>
90 typename Derived::PlainObject sorted = m;
91 std::transform(p.begin(), p.end(), sorted.rowwise().begin(),
92 [&](
size_t i) { return m.row(static_cast<Eigen::Index>(i)); });
101template<
typename Derived>
104 typename Derived::PlainObject sorted = m;
105 std::transform(p.begin(), p.end(), sorted.colwise().begin(),
106 [&](
size_t i) { return m.col(static_cast<Eigen::Index>(i)); });
116 std::vector<bool> done(p.size());
117 for (
size_t i = 0; i < p.size(); ++i)
128 std::swap(vec[prev_j], vec[j]);
140template<
typename Scalar,
int Size>
143 std::vector<bool> done(p.size());
144 for (
size_t i = 0; i < p.size(); ++i)
155 std::swap(vec(
static_cast<Eigen::Index
>(prev_j)), vec(
static_cast<Eigen::Index
>(j)));
167template<
typename Scalar,
int Rows,
int Cols>
170 std::vector<bool> done(p.size());
171 for (
size_t i = 0; i < p.size(); ++i)
182 m.row(
static_cast<Eigen::Index
>(prev_j)).swap(m.row(
static_cast<Eigen::Index
>(j)));
194template<
typename Scalar,
int Rows,
int Cols>
197 std::vector<bool> done(p.size());
198 for (
size_t i = 0; i < p.size(); ++i)
209 m.col(
static_cast<Eigen::Index
>(prev_j)).swap(m.col(
static_cast<Eigen::Index
>(j)));
void apply_permutation_rowwise_in_place(Eigen::Matrix< Scalar, Rows, Cols > &m, const std::vector< size_t > &p)
Sort a matrix row-wise with a permutation.
Definition Sort.hpp:168
std::vector< T > apply_permutation(const std::vector< T > &vec, const std::vector< size_t > &p)
Sort a std::vector with a permutation.
Definition Sort.hpp:60
Derived::PlainObject apply_permutation_rowwise(const Eigen::MatrixBase< Derived > &m, const std::vector< size_t > &p)
Sort a matrix row-wise with a permutation.
Definition Sort.hpp:88
std::vector< size_t > sort_permutation(const std::vector< T > &vec, Compare compare)
Gives the permutation to sort the std::vector.
Definition Sort.hpp:30
Derived::PlainObject apply_permutation_colwise(const Eigen::MatrixBase< Derived > &m, const std::vector< size_t > &p)
Sort a matrix col-wise with a permutation.
Definition Sort.hpp:102
void apply_permutation_in_place(std::vector< T > &vec, const std::vector< size_t > &p)
Sort a std::vector with a permutation.
Definition Sort.hpp:114
void apply_permutation_colwise_in_place(Eigen::Matrix< Scalar, Rows, Cols > &m, const std::vector< size_t > &p)
Sort a matrix col-wise with a permutation.
Definition Sort.hpp:195