##// END OF EJS Templates
Adapts sort() method to 2-dim array data (1)...
Alexandre Leroux -
r504:64f65829e732
parent child
Show More
@@ -34,6 +34,31 struct SortUtils {
34 [&](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)); });
35 return permutation;
35 return permutation;
36 }
36 }
37
38 /**
39 * Sorts a container according to indices passed in parameter
40 * @param container the container sorted
41 * @param sortPermutation the indices used to sort the container
42 * @return the container sorted
43 * @warning no verification is made on validity of sortPermutation (i.e. the vector has unique
44 * indices and its range is [0 ; vector.size()[ )
45 */
46 template <typename Container>
47 static Container sort(const Container &container, const std::vector<int> &sortPermutation)
48 {
49 if (container.size() != sortPermutation.size()) {
50 return Container{};
51 }
52
53 // Inits result
54 auto sortedData = Container{};
55 sortedData.resize(container.size());
56
57 std::transform(sortPermutation.cbegin(), sortPermutation.cend(), sortedData.begin(),
58 [&container](int i) { return container.at(i); });
59
60 return sortedData;
61 }
37 };
62 };
38
63
39 #endif // SCIQLOP_SORTUTILS_H
64 #endif // SCIQLOP_SORTUTILS_H
@@ -1,6 +1,8
1 #ifndef SCIQLOP_ARRAYDATA_H
1 #ifndef SCIQLOP_ARRAYDATA_H
2 #define SCIQLOP_ARRAYDATA_H
2 #define SCIQLOP_ARRAYDATA_H
3
3
4 #include <Common/SortUtils.h>
5
4 #include <QReadLocker>
6 #include <QReadLocker>
5 #include <QReadWriteLock>
7 #include <QReadWriteLock>
6 #include <QVector>
8 #include <QVector>
@@ -140,20 +142,10 public:
140 }
142 }
141
143
142 template <int D = Dim, typename = std::enable_if_t<D == 1> >
144 template <int D = Dim, typename = std::enable_if_t<D == 1> >
143 std::shared_ptr<ArrayData<Dim> > sort(const std::vector<int> sortPermutation)
145 std::shared_ptr<ArrayData<Dim> > sort(const std::vector<int> &sortPermutation)
144 {
146 {
145 QReadLocker locker{&m_Lock};
147 QReadLocker locker{&m_Lock};
146
148 return std::make_shared<ArrayData<Dim> >(SortUtils::sort(m_Data.at(0), sortPermutation));
147 const auto &data = m_Data.at(0);
148
149 // Inits result
150 auto sortedData = QVector<double>{};
151 sortedData.resize(data.size());
152
153 std::transform(sortPermutation.cbegin(), sortPermutation.cend(), sortedData.begin(),
154 [&data](int i) { return data[i]; });
155
156 return std::make_shared<ArrayData<Dim> >(std::move(sortedData));
157 }
149 }
158
150
159 void clear()
151 void clear()
General Comments 0
You need to be logged in to leave comments. Login now