diff options
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/apply_permutation.h | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/src/common/apply_permutation.h b/src/common/apply_permutation.h index 4de224690..4fd952686 100644 --- a/src/common/apply_permutation.h +++ b/src/common/apply_permutation.h @@ -32,12 +32,18 @@ #include <vector> #include <functional> +#include "misc_log_ex.h" namespace tools { -void apply_permutation(std::vector<size_t> permutation, const std::function<void(size_t, size_t)> &swap) +template<typename F> +void apply_permutation(std::vector<size_t> permutation, const F &swap) { + //sanity check + for (size_t n = 0; n < permutation.size(); ++n) + CHECK_AND_ASSERT_THROW_MES(std::find(permutation.begin(), permutation.end(), n) != permutation.end(), "Bad permutation"); + for (size_t i = 0; i < permutation.size(); ++i) { size_t current = i; @@ -55,6 +61,7 @@ void apply_permutation(std::vector<size_t> permutation, const std::function<void template<typename T> void apply_permutation(const std::vector<size_t> &permutation, std::vector<T> &v) { + CHECK_AND_ASSERT_THROW_MES(permutation.size() == v.size(), "Mismatched vector sizes"); apply_permutation(permutation, [&v](size_t i0, size_t i1){ std::swap(v[i0], v[i1]); }); } |