mirror of
https://github.com/MadeOfJelly/MushMachine.git
synced 2024-12-04 19:23:28 +01:00
add permutation (sort) to std_utils
This commit is contained in:
parent
7912b58108
commit
ee3386e1ad
37
framework/std_utils/src/mm/permutation.hpp
Normal file
37
framework/std_utils/src/mm/permutation.hpp
Normal file
@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <numeric>
|
||||
#include <algorithm>
|
||||
|
||||
namespace MM::std_utils {
|
||||
|
||||
// TODO: move to some lib in engine
|
||||
// TODO: determain the ideal sort
|
||||
// uses std::sort rn
|
||||
template<typename F>
|
||||
std::vector<size_t> generate_sort_permutation(size_t size, F&& sort_fn) {
|
||||
std::vector<size_t> index_mapping;
|
||||
|
||||
index_mapping.resize(size);
|
||||
std::iota(index_mapping.begin(), index_mapping.end(), 0);
|
||||
|
||||
std::sort(index_mapping.begin(), index_mapping.end(), sort_fn);
|
||||
|
||||
return index_mapping;
|
||||
}
|
||||
|
||||
// TODO: reimplement, this naive solution is kind of costly
|
||||
template<typename ContainerType>
|
||||
void apply_permutation(ContainerType& vec, const std::vector<size_t>& perm) {
|
||||
ContainerType new_vec(vec.size());
|
||||
|
||||
for (size_t i = 0; i < vec.size(); i++) {
|
||||
new_vec[i] = vec[perm[i]]; // thx ec and random guy
|
||||
}
|
||||
|
||||
vec = std::move(new_vec);
|
||||
}
|
||||
|
||||
} // MM::std_utils
|
||||
|
Loading…
Reference in New Issue
Block a user