37template <
typename T,
typename Compare>
38std::vector<std::size_t>
sort_permutation(
const std::vector<T>& vec, Compare& compare) {
39 std::vector<std::size_t> p(vec.size());
40 std::iota(p.begin(), p.end(), 0);
41 std::sort(p.begin(), p.end(), [&](std::size_t i, std::size_t j) { return compare(vec[i], vec[j]); });
45template <
typename T> std::vector<T>
apply_permutation(
const std::vector<T>& vec,
const std::vector<std::size_t>& p) {
46 std::vector<T> sorted_vec(vec.size());
47 std::transform(p.begin(), p.end(), sorted_vec.begin(), [&](std::size_t i) { return vec[i]; });
52 std::vector<bool> done(vec.size());
53 for (std::size_t i = 0; i < vec.size(); ++i) {
57 for (std::size_t j = p[i]; i != j; j = p[j]) {
58 std::swap(vec[i], vec[j]);
std::vector< T > apply_permutation(const std::vector< T > &vec, const std::vector< std::size_t > &p)
void apply_permutation_in_place(std::vector< T > &vec, const std::vector< std::size_t > &p)
std::vector< std::size_t > sort_permutation(const std::vector< T > &vec, Compare &compare)
Serializable Credit Default Swap.