@@ -37,7 +37,23 struct SortUtils { | |||||
37 | } |
|
37 | } | |
38 |
|
38 | |||
39 | /** |
|
39 | /** | |
40 | * Sorts a container according to indices passed in parameter |
|
40 | * Sorts a container according to indices passed in parameter. The number of data in the | |
|
41 | * container must be a multiple of the number of indices used to sort the container. | |||
|
42 | * | |||
|
43 | * Example 1: | |||
|
44 | * container: {1, 2, 3, 4, 5, 6} | |||
|
45 | * sortPermutation: {1, 0} | |||
|
46 | * | |||
|
47 | * Values will be sorted three by three, and the result will be: | |||
|
48 | * {4, 5, 6, 1, 2, 3} | |||
|
49 | * | |||
|
50 | * Example 2: | |||
|
51 | * container: {1, 2, 3, 4, 5, 6} | |||
|
52 | * sortPermutation: {2, 0, 1} | |||
|
53 | * | |||
|
54 | * Values will be sorted two by two, and the result will be: | |||
|
55 | * {5, 6, 1, 2, 3, 4} | |||
|
56 | * | |||
41 | * @param container the container sorted |
|
57 | * @param container the container sorted | |
42 | * @param sortPermutation the indices used to sort the container |
|
58 | * @param sortPermutation the indices used to sort the container | |
43 | * @return the container sorted |
|
59 | * @return the container sorted | |
@@ -45,18 +61,24 struct SortUtils { | |||||
45 | * indices and its range is [0 ; vector.size()[ ) |
|
61 | * indices and its range is [0 ; vector.size()[ ) | |
46 | */ |
|
62 | */ | |
47 | template <typename Container> |
|
63 | template <typename Container> | |
48 |
static Container sort(const Container &container, |
|
64 | static Container sort(const Container &container, int nbValues, | |
|
65 | const std::vector<int> &sortPermutation) | |||
49 | { |
|
66 | { | |
50 | if (container.size() != sortPermutation.size()) { |
|
67 | auto containerSize = container.size(); | |
|
68 | if (containerSize % nbValues != 0 | |||
|
69 | || ((containerSize / nbValues) != sortPermutation.size())) { | |||
51 | return Container{}; |
|
70 | return Container{}; | |
52 | } |
|
71 | } | |
53 |
|
72 | |||
54 | // Inits result |
|
73 | // Inits result | |
55 | auto sortedData = Container{}; |
|
74 | auto sortedData = Container{}; | |
56 |
sortedData.res |
|
75 | sortedData.reserve(containerSize); | |
57 |
|
76 | |||
58 | std::transform(sortPermutation.cbegin(), sortPermutation.cend(), sortedData.begin(), |
|
77 | for (auto i = 0, componentIndex = 0, permutationIndex = 0; i < containerSize; | |
59 | [&container](int i) { return container.at(i); }); |
|
78 | ++i, componentIndex = i % nbValues, permutationIndex = i / nbValues) { | |
|
79 | auto insertIndex = sortPermutation.at(permutationIndex) * nbValues + componentIndex; | |||
|
80 | sortedData.append(container.at(insertIndex)); | |||
|
81 | } | |||
60 |
|
82 | |||
61 | return sortedData; |
|
83 | return sortedData; | |
62 | } |
|
84 | } |
@@ -20,27 +20,22 namespace arraydata_detail { | |||||
20 | /// Struct used to sort ArrayData |
|
20 | /// Struct used to sort ArrayData | |
21 | template <int Dim> |
|
21 | template <int Dim> | |
22 | struct Sort { |
|
22 | struct Sort { | |
23 | static std::shared_ptr<ArrayData<Dim> > sort(const DataContainer &data, |
|
23 | static std::shared_ptr<ArrayData<Dim> > sort(const DataContainer &data, int nbComponents, | |
24 | const std::vector<int> &sortPermutation) |
|
24 | const std::vector<int> &sortPermutation) | |
25 | { |
|
25 | { | |
26 | auto nbComponents = data.size(); |
|
26 | return std::make_shared<ArrayData<Dim> >( | |
27 | auto sortedData = DataContainer(nbComponents); |
|
27 | SortUtils::sort(data, nbComponents, sortPermutation), nbComponents); | |
28 |
|
||||
29 | for (auto i = 0; i < nbComponents; ++i) { |
|
|||
30 | sortedData[i] = SortUtils::sort(data.at(i), sortPermutation); |
|
|||
31 | } |
|
|||
32 |
|
||||
33 | return std::make_shared<ArrayData<Dim> >(std::move(sortedData)); |
|
|||
34 | } |
|
28 | } | |
35 | }; |
|
29 | }; | |
36 |
|
30 | |||
37 | /// Specialization for uni-dimensional ArrayData |
|
31 | /// Specialization for uni-dimensional ArrayData | |
38 | template <> |
|
32 | template <> | |
39 | struct Sort<1> { |
|
33 | struct Sort<1> { | |
40 | static std::shared_ptr<ArrayData<1> > sort(const DataContainer &data, |
|
34 | static std::shared_ptr<ArrayData<1> > sort(const DataContainer &data, int nbComponents, | |
41 | const std::vector<int> &sortPermutation) |
|
35 | const std::vector<int> &sortPermutation) | |
42 | { |
|
36 | { | |
43 | return std::make_shared<ArrayData<1> >(SortUtils::sort(data.at(0), sortPermutation)); |
|
37 | Q_UNUSED(nbComponents) | |
|
38 | return std::make_shared<ArrayData<1> >(SortUtils::sort(data, 1, sortPermutation)); | |||
44 | } |
|
39 | } | |
45 | }; |
|
40 | }; | |
46 |
|
41 |
General Comments 0
You need to be logged in to leave comments.
Login now