From 64f65829e732d4f2185c1f9967e750c97ca7a14e 2017-08-07 07:37:28 From: Alexandre Leroux Date: 2017-08-07 07:37:28 Subject: [PATCH] Adapts sort() method to 2-dim array data (1) Moves sort method in utils file --- diff --git a/core/include/Common/SortUtils.h b/core/include/Common/SortUtils.h index 42a5c76..b79705c 100644 --- a/core/include/Common/SortUtils.h +++ b/core/include/Common/SortUtils.h @@ -34,6 +34,31 @@ struct SortUtils { [&](int i, int j) { return compare(container.at(i), container.at(j)); }); return permutation; } + + /** + * Sorts a container according to indices passed in parameter + * @param container the container sorted + * @param sortPermutation the indices used to sort the container + * @return the container sorted + * @warning no verification is made on validity of sortPermutation (i.e. the vector has unique + * indices and its range is [0 ; vector.size()[ ) + */ + template + static Container sort(const Container &container, const std::vector &sortPermutation) + { + if (container.size() != sortPermutation.size()) { + return Container{}; + } + + // Inits result + auto sortedData = Container{}; + sortedData.resize(container.size()); + + std::transform(sortPermutation.cbegin(), sortPermutation.cend(), sortedData.begin(), + [&container](int i) { return container.at(i); }); + + return sortedData; + } }; #endif // SCIQLOP_SORTUTILS_H diff --git a/core/include/Data/ArrayData.h b/core/include/Data/ArrayData.h index 258e7f0..9cbcb50 100644 --- a/core/include/Data/ArrayData.h +++ b/core/include/Data/ArrayData.h @@ -1,6 +1,8 @@ #ifndef SCIQLOP_ARRAYDATA_H #define SCIQLOP_ARRAYDATA_H +#include + #include #include #include @@ -140,20 +142,10 @@ public: } template > - std::shared_ptr > sort(const std::vector sortPermutation) + std::shared_ptr > sort(const std::vector &sortPermutation) { QReadLocker locker{&m_Lock}; - - const auto &data = m_Data.at(0); - - // Inits result - auto sortedData = QVector{}; - sortedData.resize(data.size()); - - std::transform(sortPermutation.cbegin(), sortPermutation.cend(), sortedData.begin(), - [&data](int i) { return data[i]; }); - - return std::make_shared >(std::move(sortedData)); + return std::make_shared >(SortUtils::sort(m_Data.at(0), sortPermutation)); } void clear()