@@ -0,0 +1,38 | |||||
|
1 | #ifndef SCIQLOP_SORTUTILS_H | |||
|
2 | #define SCIQLOP_SORTUTILS_H | |||
|
3 | ||||
|
4 | #include <algorithm> | |||
|
5 | #include <vector> | |||
|
6 | ||||
|
7 | /** | |||
|
8 | * Utility class with methods for sorting data | |||
|
9 | */ | |||
|
10 | struct SortUtils { | |||
|
11 | /** | |||
|
12 | * Generates a vector representing the index of insertion of each data of a container if this | |||
|
13 | * one had to be sorted according to a comparison function. | |||
|
14 | * | |||
|
15 | * For example: | |||
|
16 | * If the container is a vector {1; 4; 2; 5; 3} and the comparison function is std::less, the | |||
|
17 | * result would be : {0; 3; 1; 4; 2} | |||
|
18 | * | |||
|
19 | * @tparam Container the type of the container. | |||
|
20 | * @tparam Compare the type of the comparison function | |||
|
21 | * @param container the container from which to generate the result. The container must have a | |||
|
22 | * at() method that returns a value associated to an index | |||
|
23 | * @param compare the comparison function | |||
|
24 | */ | |||
|
25 | template <typename Container, typename Compare> | |||
|
26 | static std::vector<int> sortPermutation(const Container &container, const Compare &compare) | |||
|
27 | { | |||
|
28 | auto permutation = std::vector<int>{}; | |||
|
29 | permutation.resize(container.size()); | |||
|
30 | ||||
|
31 | std::iota(permutation.begin(), permutation.end(), 0); | |||
|
32 | std::sort(permutation.begin(), permutation.end(), | |||
|
33 | [&](int i, int j) { return compare(container.at(i), container.at(j)); }); | |||
|
34 | return permutation; | |||
|
35 | } | |||
|
36 | }; | |||
|
37 | ||||
|
38 | #endif // SCIQLOP_SORTUTILS_H |
@@ -4,6 +4,9 | |||||
4 | #include <QReadLocker> |
|
4 | #include <QReadLocker> | |
5 | #include <QReadWriteLock> |
|
5 | #include <QReadWriteLock> | |
6 | #include <QVector> |
|
6 | #include <QVector> | |
|
7 | ||||
|
8 | #include <memory> | |||
|
9 | ||||
7 | /** |
|
10 | /** | |
8 | * @brief The ArrayData class represents a dataset for a data series. |
|
11 | * @brief The ArrayData class represents a dataset for a data series. | |
9 | * |
|
12 | * | |
@@ -47,6 +50,18 public: | |||||
47 | } |
|
50 | } | |
48 |
|
51 | |||
49 | /** |
|
52 | /** | |
|
53 | * @return the data at a specified index | |||
|
54 | * @remarks index must be a valid position | |||
|
55 | * @remarks this method is only available for a unidimensional ArrayData | |||
|
56 | */ | |||
|
57 | template <int D = Dim, typename = std::enable_if_t<D == 1> > | |||
|
58 | double at(int index) const noexcept | |||
|
59 | { | |||
|
60 | QReadLocker locker{&m_Lock}; | |||
|
61 | return m_Data[0].at(index); | |||
|
62 | } | |||
|
63 | ||||
|
64 | /** | |||
50 | * Sets a data at a specified index. The index has to be valid to be effective |
|
65 | * Sets a data at a specified index. The index has to be valid to be effective | |
51 | * @param index the index to which the data will be set |
|
66 | * @param index the index to which the data will be set | |
52 | * @param data the data to set |
|
67 | * @param data the data to set | |
@@ -101,6 +116,22 public: | |||||
101 | return m_Data[0].size(); |
|
116 | return m_Data[0].size(); | |
102 | } |
|
117 | } | |
103 |
|
118 | |||
|
119 | template <int D = Dim, typename = std::enable_if_t<D == 1> > | |||
|
120 | std::shared_ptr<ArrayData<Dim> > sort(const std::vector<int> sortPermutation) | |||
|
121 | { | |||
|
122 | QReadLocker locker{&m_Lock}; | |||
|
123 | ||||
|
124 | const auto &data = m_Data.at(0); | |||
|
125 | ||||
|
126 | // Inits result | |||
|
127 | auto sortedData = QVector<double>{}; | |||
|
128 | sortedData.resize(data.size()); | |||
|
129 | ||||
|
130 | std::transform(sortPermutation.cbegin(), sortPermutation.cend(), sortedData.begin(), | |||
|
131 | [&data](int i) { return data[i]; }); | |||
|
132 | ||||
|
133 | return std::make_shared<ArrayData<Dim> >(std::move(sortedData)); | |||
|
134 | } | |||
104 | void clear() |
|
135 | void clear() | |
105 | { |
|
136 | { | |
106 | QWriteLocker locker{&m_Lock}; |
|
137 | QWriteLocker locker{&m_Lock}; |
@@ -1,6 +1,8 | |||||
1 | #ifndef SCIQLOP_DATASERIES_H |
|
1 | #ifndef SCIQLOP_DATASERIES_H | |
2 | #define SCIQLOP_DATASERIES_H |
|
2 | #define SCIQLOP_DATASERIES_H | |
3 |
|
3 | |||
|
4 | #include <Common/SortUtils.h> | |||
|
5 | ||||
4 | #include <Data/ArrayData.h> |
|
6 | #include <Data/ArrayData.h> | |
5 | #include <Data/IDataSeries.h> |
|
7 | #include <Data/IDataSeries.h> | |
6 |
|
8 | |||
@@ -116,7 +118,9 private: | |||||
116 | */ |
|
118 | */ | |
117 | void sort() noexcept |
|
119 | void sort() noexcept | |
118 | { |
|
120 | { | |
119 | /// @todo ALX |
|
121 | auto permutation = SortUtils::sortPermutation(*m_XAxisData, std::less<double>()); | |
|
122 | m_XAxisData = m_XAxisData->sort(permutation); | |||
|
123 | m_ValuesData = m_ValuesData->sort(permutation); | |||
120 | } |
|
124 | } | |
121 |
|
125 | |||
122 | std::shared_ptr<ArrayData<1> > m_XAxisData; |
|
126 | std::shared_ptr<ArrayData<1> > m_XAxisData; |
General Comments 0
You need to be logged in to leave comments.
Login now