@@ -1,38 +1,39 | |||
|
1 | 1 | #ifndef SCIQLOP_SORTUTILS_H |
|
2 | 2 | #define SCIQLOP_SORTUTILS_H |
|
3 | 3 | |
|
4 | 4 | #include <algorithm> |
|
5 | #include <numeric> | |
|
5 | 6 | #include <vector> |
|
6 | 7 | |
|
7 | 8 | /** |
|
8 | 9 | * Utility class with methods for sorting data |
|
9 | 10 | */ |
|
10 | 11 | struct SortUtils { |
|
11 | 12 | /** |
|
12 | 13 | * Generates a vector representing the index of insertion of each data of a container if this |
|
13 | 14 | * one had to be sorted according to a comparison function. |
|
14 | 15 | * |
|
15 | 16 | * For example: |
|
16 | 17 | * If the container is a vector {1; 4; 2; 5; 3} and the comparison function is std::less, the |
|
17 | 18 | * result would be : {0; 3; 1; 4; 2} |
|
18 | 19 | * |
|
19 | 20 | * @tparam Container the type of the container. |
|
20 | 21 | * @tparam Compare the type of the comparison function |
|
21 | 22 | * @param container the container from which to generate the result. The container must have a |
|
22 | 23 | * at() method that returns a value associated to an index |
|
23 | 24 | * @param compare the comparison function |
|
24 | 25 | */ |
|
25 | 26 | template <typename Container, typename Compare> |
|
26 | 27 | static std::vector<int> sortPermutation(const Container &container, const Compare &compare) |
|
27 | 28 | { |
|
28 | 29 | auto permutation = std::vector<int>{}; |
|
29 | 30 | permutation.resize(container.size()); |
|
30 | 31 | |
|
31 | 32 | std::iota(permutation.begin(), permutation.end(), 0); |
|
32 | 33 | std::sort(permutation.begin(), permutation.end(), |
|
33 | 34 | [&](int i, int j) { return compare(container.at(i), container.at(j)); }); |
|
34 | 35 | return permutation; |
|
35 | 36 | } |
|
36 | 37 | }; |
|
37 | 38 | |
|
38 | 39 | #endif // SCIQLOP_SORTUTILS_H |
General Comments 0
You need to be logged in to leave comments.
Login now