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