##// END OF EJS Templates
Updates sort() method to be compatible with a single vector
Alexandre Leroux -
r643:5ccc65b48599
parent child
Show More
@@ -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 57 * @param container the container sorted
42 58 * @param sortPermutation the indices used to sort the container
43 59 * @return the container sorted
@@ -45,18 +61,24 struct SortUtils {
45 61 * indices and its range is [0 ; vector.size()[ )
46 62 */
47 63 template <typename Container>
48 static Container sort(const Container &container, const std::vector<int> &sortPermutation)
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 70 return Container{};
52 71 }
53 72
54 73 // Inits result
55 74 auto sortedData = Container{};
56 sortedData.resize(container.size());
75 sortedData.reserve(containerSize);
57 76
58 std::transform(sortPermutation.cbegin(), sortPermutation.cend(), sortedData.begin(),
59 [&container](int i) { return container.at(i); });
77 for (auto i = 0, componentIndex = 0, permutationIndex = 0; i < containerSize;
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 83 return sortedData;
62 84 }
@@ -20,27 +20,22 namespace arraydata_detail {
20 20 /// Struct used to sort ArrayData
21 21 template <int Dim>
22 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 24 const std::vector<int> &sortPermutation)
25 25 {
26 auto nbComponents = data.size();
27 auto sortedData = DataContainer(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));
26 return std::make_shared<ArrayData<Dim> >(
27 SortUtils::sort(data, nbComponents, sortPermutation), nbComponents);
34 28 }
35 29 };
36 30
37 31 /// Specialization for uni-dimensional ArrayData
38 32 template <>
39 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 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