From 07e656efd83c8a7d8a202ac72e0811cbbb06fb0e 2017-07-26 08:37:25 From: Alexandre Leroux Date: 2017-07-26 08:37:25 Subject: [PATCH] Makes a Data series be sorted (2) Implements sort() method --- diff --git a/core/include/Common/SortUtils.h b/core/include/Common/SortUtils.h new file mode 100644 index 0000000..111216f --- /dev/null +++ b/core/include/Common/SortUtils.h @@ -0,0 +1,38 @@ +#ifndef SCIQLOP_SORTUTILS_H +#define SCIQLOP_SORTUTILS_H + +#include +#include + +/** + * Utility class with methods for sorting data + */ +struct SortUtils { + /** + * Generates a vector representing the index of insertion of each data of a container if this + * one had to be sorted according to a comparison function. + * + * For example: + * If the container is a vector {1; 4; 2; 5; 3} and the comparison function is std::less, the + * result would be : {0; 3; 1; 4; 2} + * + * @tparam Container the type of the container. + * @tparam Compare the type of the comparison function + * @param container the container from which to generate the result. The container must have a + * at() method that returns a value associated to an index + * @param compare the comparison function + */ + template + static std::vector sortPermutation(const Container &container, const Compare &compare) + { + auto permutation = std::vector{}; + permutation.resize(container.size()); + + std::iota(permutation.begin(), permutation.end(), 0); + std::sort(permutation.begin(), permutation.end(), + [&](int i, int j) { return compare(container.at(i), container.at(j)); }); + return permutation; + } +}; + +#endif // SCIQLOP_SORTUTILS_H diff --git a/core/include/Data/ArrayData.h b/core/include/Data/ArrayData.h index 907cfb6..55fb8c5 100644 --- a/core/include/Data/ArrayData.h +++ b/core/include/Data/ArrayData.h @@ -4,6 +4,9 @@ #include #include #include + +#include + /** * @brief The ArrayData class represents a dataset for a data series. * @@ -47,6 +50,18 @@ public: } /** + * @return the data at a specified index + * @remarks index must be a valid position + * @remarks this method is only available for a unidimensional ArrayData + */ + template > + double at(int index) const noexcept + { + QReadLocker locker{&m_Lock}; + return m_Data[0].at(index); + } + + /** * Sets a data at a specified index. The index has to be valid to be effective * @param index the index to which the data will be set * @param data the data to set @@ -101,6 +116,22 @@ public: return m_Data[0].size(); } + template > + 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)); + } void clear() { QWriteLocker locker{&m_Lock}; diff --git a/core/include/Data/DataSeries.h b/core/include/Data/DataSeries.h index 97fdc93..7e22a94 100644 --- a/core/include/Data/DataSeries.h +++ b/core/include/Data/DataSeries.h @@ -1,6 +1,8 @@ #ifndef SCIQLOP_DATASERIES_H #define SCIQLOP_DATASERIES_H +#include + #include #include @@ -116,7 +118,9 @@ private: */ void sort() noexcept { - /// @todo ALX + auto permutation = SortUtils::sortPermutation(*m_XAxisData, std::less()); + m_XAxisData = m_XAxisData->sort(permutation); + m_ValuesData = m_ValuesData->sort(permutation); } std::shared_ptr > m_XAxisData;