@@ -1,271 +1,271 | |||||
1 | #ifndef SCIQLOP_ARRAYDATA_H |
|
1 | #ifndef SCIQLOP_ARRAYDATA_H | |
2 | #define SCIQLOP_ARRAYDATA_H |
|
2 | #define SCIQLOP_ARRAYDATA_H | |
3 |
|
3 | |||
4 | #include "Data/ArrayDataIterator.h" |
|
4 | #include "Data/ArrayDataIterator.h" | |
5 | #include <Common/SortUtils.h> |
|
5 | #include <Common/SortUtils.h> | |
6 |
|
6 | |||
7 | #include <QReadLocker> |
|
7 | #include <QReadLocker> | |
8 | #include <QReadWriteLock> |
|
8 | #include <QReadWriteLock> | |
9 | #include <QVector> |
|
9 | #include <QVector> | |
10 |
|
10 | |||
11 | #include <memory> |
|
11 | #include <memory> | |
12 |
|
12 | |||
13 | template <int Dim> |
|
13 | template <int Dim> | |
14 | class ArrayData; |
|
14 | class ArrayData; | |
15 |
|
15 | |||
16 | using DataContainer = QVector<double>; |
|
16 | using DataContainer = QVector<double>; | |
17 |
|
17 | |||
18 | namespace arraydata_detail { |
|
18 | namespace arraydata_detail { | |
19 |
|
19 | |||
20 | /// Struct used to sort ArrayData |
|
20 | /// Struct used to sort ArrayData | |
21 | template <int Dim> |
|
21 | template <int Dim> | |
22 | struct Sort { |
|
22 | struct Sort { | |
23 | static std::shared_ptr<ArrayData<Dim> > sort(const DataContainer &data, int nbComponents, |
|
23 | static std::shared_ptr<ArrayData<Dim> > sort(const DataContainer &data, int nbComponents, | |
24 | const std::vector<int> &sortPermutation) |
|
24 | const std::vector<int> &sortPermutation) | |
25 | { |
|
25 | { | |
26 | return std::make_shared<ArrayData<Dim> >( |
|
26 | return std::make_shared<ArrayData<Dim> >( | |
27 | SortUtils::sort(data, nbComponents, sortPermutation), nbComponents); |
|
27 | SortUtils::sort(data, nbComponents, sortPermutation), nbComponents); | |
28 | } |
|
28 | } | |
29 | }; |
|
29 | }; | |
30 |
|
30 | |||
31 | /// Specialization for uni-dimensional ArrayData |
|
31 | /// Specialization for uni-dimensional ArrayData | |
32 | template <> |
|
32 | template <> | |
33 | struct Sort<1> { |
|
33 | struct Sort<1> { | |
34 | static std::shared_ptr<ArrayData<1> > sort(const DataContainer &data, int nbComponents, |
|
34 | static std::shared_ptr<ArrayData<1> > sort(const DataContainer &data, int nbComponents, | |
35 | const std::vector<int> &sortPermutation) |
|
35 | const std::vector<int> &sortPermutation) | |
36 | { |
|
36 | { | |
37 | Q_UNUSED(nbComponents) |
|
37 | Q_UNUSED(nbComponents) | |
38 | return std::make_shared<ArrayData<1> >(SortUtils::sort(data, 1, sortPermutation)); |
|
38 | return std::make_shared<ArrayData<1> >(SortUtils::sort(data, 1, sortPermutation)); | |
39 | } |
|
39 | } | |
40 | }; |
|
40 | }; | |
41 |
|
41 | |||
42 | template <int Dim> |
|
42 | template <int Dim> | |
43 | class IteratorValue : public ArrayDataIteratorValue::Impl { |
|
43 | class IteratorValue : public ArrayDataIteratorValue::Impl { | |
44 | public: |
|
44 | public: | |
45 | explicit IteratorValue(const DataContainer &container, int nbComponents, bool begin) |
|
45 | explicit IteratorValue(const DataContainer &container, int nbComponents, bool begin) | |
46 | : m_It{begin ? container.cbegin() : container.cend()}, m_NbComponents{nbComponents} |
|
46 | : m_It{begin ? container.cbegin() : container.cend()}, m_NbComponents{nbComponents} | |
47 | { |
|
47 | { | |
48 | } |
|
48 | } | |
49 |
|
49 | |||
50 | IteratorValue(const IteratorValue &other) = default; |
|
50 | IteratorValue(const IteratorValue &other) = default; | |
51 |
|
51 | |||
52 | std::unique_ptr<ArrayDataIteratorValue::Impl> clone() const override |
|
52 | std::unique_ptr<ArrayDataIteratorValue::Impl> clone() const override | |
53 | { |
|
53 | { | |
54 | return std::make_unique<IteratorValue<Dim> >(*this); |
|
54 | return std::make_unique<IteratorValue<Dim> >(*this); | |
55 | } |
|
55 | } | |
56 |
|
56 | |||
57 | bool equals(const ArrayDataIteratorValue::Impl &other) const override try { |
|
57 | bool equals(const ArrayDataIteratorValue::Impl &other) const override try { | |
58 | const auto &otherImpl = dynamic_cast<const IteratorValue &>(other); |
|
58 | const auto &otherImpl = dynamic_cast<const IteratorValue &>(other); | |
59 | return std::tie(m_It, m_NbComponents) == std::tie(otherImpl.m_It, otherImpl.m_NbComponents); |
|
59 | return std::tie(m_It, m_NbComponents) == std::tie(otherImpl.m_It, otherImpl.m_NbComponents); | |
60 | } |
|
60 | } | |
61 | catch (const std::bad_cast &) { |
|
61 | catch (const std::bad_cast &) { | |
62 | return false; |
|
62 | return false; | |
63 | } |
|
63 | } | |
64 |
|
64 | |||
65 | void next() override { std::advance(m_It, m_NbComponents); } |
|
65 | void next() override { std::advance(m_It, m_NbComponents); } | |
66 | void prev() override { std::advance(m_It, -m_NbComponents); } |
|
66 | void prev() override { std::advance(m_It, -m_NbComponents); } | |
67 |
|
67 | |||
68 | double at(int componentIndex) const override { return *(m_It + componentIndex); } |
|
68 | double at(int componentIndex) const override { return *(m_It + componentIndex); } | |
69 | double first() const override { return *m_It; } |
|
69 | double first() const override { return *m_It; } | |
70 | double min() const override |
|
70 | double min() const override | |
71 | { |
|
71 | { | |
72 | auto values = this->values(); |
|
72 | auto values = this->values(); | |
73 | auto end = values.cend(); |
|
73 | auto end = values.cend(); | |
74 | auto it = std::min_element(values.cbegin(), end, [](const auto &v1, const auto &v2) { |
|
74 | auto it = std::min_element(values.cbegin(), end, [](const auto &v1, const auto &v2) { | |
75 | return SortUtils::minCompareWithNaN(v1, v2); |
|
75 | return SortUtils::minCompareWithNaN(v1, v2); | |
76 | }); |
|
76 | }); | |
77 |
|
77 | |||
78 | return it != end ? *it : std::numeric_limits<double>::quiet_NaN(); |
|
78 | return it != end ? *it : std::numeric_limits<double>::quiet_NaN(); | |
79 | } |
|
79 | } | |
80 | double max() const override |
|
80 | double max() const override | |
81 | { |
|
81 | { | |
82 | auto values = this->values(); |
|
82 | auto values = this->values(); | |
83 | auto end = values.cend(); |
|
83 | auto end = values.cend(); | |
84 | auto it = std::max_element(values.cbegin(), end, [](const auto &v1, const auto &v2) { |
|
84 | auto it = std::max_element(values.cbegin(), end, [](const auto &v1, const auto &v2) { | |
85 | return SortUtils::maxCompareWithNaN(v1, v2); |
|
85 | return SortUtils::maxCompareWithNaN(v1, v2); | |
86 | }); |
|
86 | }); | |
87 | return it != end ? *it : std::numeric_limits<double>::quiet_NaN(); |
|
87 | return it != end ? *it : std::numeric_limits<double>::quiet_NaN(); | |
88 | } |
|
88 | } | |
89 |
|
89 | |||
90 | private: |
|
90 | QVector<double> values() const override | |
91 | std::vector<double> values() const |
|
|||
92 | { |
|
91 | { | |
93 |
auto result = |
|
92 | auto result = QVector<double>{}; | |
94 | for (auto i = 0; i < m_NbComponents; ++i) { |
|
93 | for (auto i = 0; i < m_NbComponents; ++i) { | |
95 | result.push_back(*(m_It + i)); |
|
94 | result.push_back(*(m_It + i)); | |
96 | } |
|
95 | } | |
97 |
|
96 | |||
98 | return result; |
|
97 | return result; | |
99 | } |
|
98 | } | |
100 |
|
99 | |||
|
100 | private: | |||
101 | DataContainer::const_iterator m_It; |
|
101 | DataContainer::const_iterator m_It; | |
102 | int m_NbComponents; |
|
102 | int m_NbComponents; | |
103 | }; |
|
103 | }; | |
104 |
|
104 | |||
105 | } // namespace arraydata_detail |
|
105 | } // namespace arraydata_detail | |
106 |
|
106 | |||
107 | /** |
|
107 | /** | |
108 | * @brief The ArrayData class represents a dataset for a data series. |
|
108 | * @brief The ArrayData class represents a dataset for a data series. | |
109 | * |
|
109 | * | |
110 | * A dataset can be unidimensional or two-dimensional. This property is determined by the Dim |
|
110 | * A dataset can be unidimensional or two-dimensional. This property is determined by the Dim | |
111 | * template-parameter. In a case of a two-dimensional dataset, each dataset component has the same |
|
111 | * template-parameter. In a case of a two-dimensional dataset, each dataset component has the same | |
112 | * number of values |
|
112 | * number of values | |
113 | * |
|
113 | * | |
114 | * @tparam Dim the dimension of the ArrayData (one or two) |
|
114 | * @tparam Dim the dimension of the ArrayData (one or two) | |
115 | * @sa IDataSeries |
|
115 | * @sa IDataSeries | |
116 | */ |
|
116 | */ | |
117 | template <int Dim> |
|
117 | template <int Dim> | |
118 | class ArrayData { |
|
118 | class ArrayData { | |
119 | public: |
|
119 | public: | |
120 | // ///// // |
|
120 | // ///// // | |
121 | // Ctors // |
|
121 | // Ctors // | |
122 | // ///// // |
|
122 | // ///// // | |
123 |
|
123 | |||
124 | /** |
|
124 | /** | |
125 | * Ctor for a unidimensional ArrayData |
|
125 | * Ctor for a unidimensional ArrayData | |
126 | * @param data the data the ArrayData will hold |
|
126 | * @param data the data the ArrayData will hold | |
127 | */ |
|
127 | */ | |
128 | template <int D = Dim, typename = std::enable_if_t<D == 1> > |
|
128 | template <int D = Dim, typename = std::enable_if_t<D == 1> > | |
129 | explicit ArrayData(DataContainer data) : m_Data{std::move(data)}, m_NbComponents{1} |
|
129 | explicit ArrayData(DataContainer data) : m_Data{std::move(data)}, m_NbComponents{1} | |
130 | { |
|
130 | { | |
131 | } |
|
131 | } | |
132 |
|
132 | |||
133 | /** |
|
133 | /** | |
134 | * Ctor for a two-dimensional ArrayData. The number of components (number of lines) must be |
|
134 | * Ctor for a two-dimensional ArrayData. The number of components (number of lines) must be | |
135 | * greater than 2 and must be a divisor of the total number of data in the vector |
|
135 | * greater than 2 and must be a divisor of the total number of data in the vector | |
136 | * @param data the data the ArrayData will hold |
|
136 | * @param data the data the ArrayData will hold | |
137 | * @param nbComponents the number of components |
|
137 | * @param nbComponents the number of components | |
138 | * @throws std::invalid_argument if the number of components is less than 2 or is not a divisor |
|
138 | * @throws std::invalid_argument if the number of components is less than 2 or is not a divisor | |
139 | * of the size of the data |
|
139 | * of the size of the data | |
140 | */ |
|
140 | */ | |
141 | template <int D = Dim, typename = std::enable_if_t<D == 2> > |
|
141 | template <int D = Dim, typename = std::enable_if_t<D == 2> > | |
142 | explicit ArrayData(DataContainer data, int nbComponents) |
|
142 | explicit ArrayData(DataContainer data, int nbComponents) | |
143 | : m_Data{std::move(data)}, m_NbComponents{nbComponents} |
|
143 | : m_Data{std::move(data)}, m_NbComponents{nbComponents} | |
144 | { |
|
144 | { | |
145 | if (nbComponents < 2) { |
|
145 | if (nbComponents < 2) { | |
146 | throw std::invalid_argument{ |
|
146 | throw std::invalid_argument{ | |
147 | QString{"A multidimensional ArrayData must have at least 2 components (found: %1)"} |
|
147 | QString{"A multidimensional ArrayData must have at least 2 components (found: %1)"} | |
148 | .arg(nbComponents) |
|
148 | .arg(nbComponents) | |
149 | .toStdString()}; |
|
149 | .toStdString()}; | |
150 | } |
|
150 | } | |
151 |
|
151 | |||
152 | if (m_Data.size() % m_NbComponents != 0) { |
|
152 | if (m_Data.size() % m_NbComponents != 0) { | |
153 | throw std::invalid_argument{QString{ |
|
153 | throw std::invalid_argument{QString{ | |
154 | "The number of components (%1) is inconsistent with the total number of data (%2)"} |
|
154 | "The number of components (%1) is inconsistent with the total number of data (%2)"} | |
155 | .arg(m_Data.size(), nbComponents) |
|
155 | .arg(m_Data.size(), nbComponents) | |
156 | .toStdString()}; |
|
156 | .toStdString()}; | |
157 | } |
|
157 | } | |
158 | } |
|
158 | } | |
159 |
|
159 | |||
160 | /// Copy ctor |
|
160 | /// Copy ctor | |
161 | explicit ArrayData(const ArrayData &other) |
|
161 | explicit ArrayData(const ArrayData &other) | |
162 | { |
|
162 | { | |
163 | QReadLocker otherLocker{&other.m_Lock}; |
|
163 | QReadLocker otherLocker{&other.m_Lock}; | |
164 | m_Data = other.m_Data; |
|
164 | m_Data = other.m_Data; | |
165 | m_NbComponents = other.m_NbComponents; |
|
165 | m_NbComponents = other.m_NbComponents; | |
166 | } |
|
166 | } | |
167 |
|
167 | |||
168 | // /////////////// // |
|
168 | // /////////////// // | |
169 | // General methods // |
|
169 | // General methods // | |
170 | // /////////////// // |
|
170 | // /////////////// // | |
171 |
|
171 | |||
172 | /** |
|
172 | /** | |
173 | * Merges into the array data an other array data. The two array datas must have the same number |
|
173 | * Merges into the array data an other array data. The two array datas must have the same number | |
174 | * of components so the merge can be done |
|
174 | * of components so the merge can be done | |
175 | * @param other the array data to merge with |
|
175 | * @param other the array data to merge with | |
176 | * @param prepend if true, the other array data is inserted at the beginning, otherwise it is |
|
176 | * @param prepend if true, the other array data is inserted at the beginning, otherwise it is | |
177 | * inserted at the end |
|
177 | * inserted at the end | |
178 | */ |
|
178 | */ | |
179 | void add(const ArrayData<Dim> &other, bool prepend = false) |
|
179 | void add(const ArrayData<Dim> &other, bool prepend = false) | |
180 | { |
|
180 | { | |
181 | QWriteLocker locker{&m_Lock}; |
|
181 | QWriteLocker locker{&m_Lock}; | |
182 | QReadLocker otherLocker{&other.m_Lock}; |
|
182 | QReadLocker otherLocker{&other.m_Lock}; | |
183 |
|
183 | |||
184 | if (m_NbComponents != other.componentCount()) { |
|
184 | if (m_NbComponents != other.componentCount()) { | |
185 | return; |
|
185 | return; | |
186 | } |
|
186 | } | |
187 |
|
187 | |||
188 | if (prepend) { |
|
188 | if (prepend) { | |
189 | auto otherDataSize = other.m_Data.size(); |
|
189 | auto otherDataSize = other.m_Data.size(); | |
190 | m_Data.insert(m_Data.begin(), otherDataSize, 0.); |
|
190 | m_Data.insert(m_Data.begin(), otherDataSize, 0.); | |
191 | for (auto i = 0; i < otherDataSize; ++i) { |
|
191 | for (auto i = 0; i < otherDataSize; ++i) { | |
192 | m_Data.replace(i, other.m_Data.at(i)); |
|
192 | m_Data.replace(i, other.m_Data.at(i)); | |
193 | } |
|
193 | } | |
194 | } |
|
194 | } | |
195 | else { |
|
195 | else { | |
196 | m_Data.append(other.m_Data); |
|
196 | m_Data.append(other.m_Data); | |
197 | } |
|
197 | } | |
198 | } |
|
198 | } | |
199 |
|
199 | |||
200 | void clear() |
|
200 | void clear() | |
201 | { |
|
201 | { | |
202 | QWriteLocker locker{&m_Lock}; |
|
202 | QWriteLocker locker{&m_Lock}; | |
203 | m_Data.clear(); |
|
203 | m_Data.clear(); | |
204 | } |
|
204 | } | |
205 |
|
205 | |||
206 | int componentCount() const noexcept { return m_NbComponents; } |
|
206 | int componentCount() const noexcept { return m_NbComponents; } | |
207 |
|
207 | |||
208 | /// @return the size (i.e. number of values) of a single component |
|
208 | /// @return the size (i.e. number of values) of a single component | |
209 | /// @remarks in a case of a two-dimensional ArrayData, each component has the same size |
|
209 | /// @remarks in a case of a two-dimensional ArrayData, each component has the same size | |
210 | int size() const |
|
210 | int size() const | |
211 | { |
|
211 | { | |
212 | QReadLocker locker{&m_Lock}; |
|
212 | QReadLocker locker{&m_Lock}; | |
213 | return m_Data.size() / m_NbComponents; |
|
213 | return m_Data.size() / m_NbComponents; | |
214 | } |
|
214 | } | |
215 |
|
215 | |||
216 | std::shared_ptr<ArrayData<Dim> > sort(const std::vector<int> &sortPermutation) |
|
216 | std::shared_ptr<ArrayData<Dim> > sort(const std::vector<int> &sortPermutation) | |
217 | { |
|
217 | { | |
218 | QReadLocker locker{&m_Lock}; |
|
218 | QReadLocker locker{&m_Lock}; | |
219 | return arraydata_detail::Sort<Dim>::sort(m_Data, m_NbComponents, sortPermutation); |
|
219 | return arraydata_detail::Sort<Dim>::sort(m_Data, m_NbComponents, sortPermutation); | |
220 | } |
|
220 | } | |
221 |
|
221 | |||
222 | // ///////// // |
|
222 | // ///////// // | |
223 | // Iterators // |
|
223 | // Iterators // | |
224 | // ///////// // |
|
224 | // ///////// // | |
225 |
|
225 | |||
226 | ArrayDataIterator cbegin() const |
|
226 | ArrayDataIterator cbegin() const | |
227 | { |
|
227 | { | |
228 | return ArrayDataIterator{ArrayDataIteratorValue{ |
|
228 | return ArrayDataIterator{ArrayDataIteratorValue{ | |
229 | std::make_unique<arraydata_detail::IteratorValue<Dim> >(m_Data, m_NbComponents, true)}}; |
|
229 | std::make_unique<arraydata_detail::IteratorValue<Dim> >(m_Data, m_NbComponents, true)}}; | |
230 | } |
|
230 | } | |
231 | ArrayDataIterator cend() const |
|
231 | ArrayDataIterator cend() const | |
232 | { |
|
232 | { | |
233 | return ArrayDataIterator{ |
|
233 | return ArrayDataIterator{ | |
234 | ArrayDataIteratorValue{std::make_unique<arraydata_detail::IteratorValue<Dim> >( |
|
234 | ArrayDataIteratorValue{std::make_unique<arraydata_detail::IteratorValue<Dim> >( | |
235 | m_Data, m_NbComponents, false)}}; |
|
235 | m_Data, m_NbComponents, false)}}; | |
236 | } |
|
236 | } | |
237 |
|
237 | |||
238 |
|
238 | |||
239 | /** |
|
239 | /** | |
240 | * @return the data at a specified index |
|
240 | * @return the data at a specified index | |
241 | * @remarks index must be a valid position |
|
241 | * @remarks index must be a valid position | |
242 | */ |
|
242 | */ | |
243 | double at(int index) const noexcept |
|
243 | double at(int index) const noexcept | |
244 | { |
|
244 | { | |
245 | QReadLocker locker{&m_Lock}; |
|
245 | QReadLocker locker{&m_Lock}; | |
246 | return m_Data.at(index); |
|
246 | return m_Data.at(index); | |
247 | } |
|
247 | } | |
248 |
|
248 | |||
249 | // ///////////// // |
|
249 | // ///////////// // | |
250 | // 1-dim methods // |
|
250 | // 1-dim methods // | |
251 | // ///////////// // |
|
251 | // ///////////// // | |
252 |
|
252 | |||
253 | /** |
|
253 | /** | |
254 | * @return the data as a vector, as a const reference |
|
254 | * @return the data as a vector, as a const reference | |
255 | * @remarks this method is only available for a unidimensional ArrayData |
|
255 | * @remarks this method is only available for a unidimensional ArrayData | |
256 | */ |
|
256 | */ | |
257 | template <int D = Dim, typename = std::enable_if_t<D == 1> > |
|
257 | template <int D = Dim, typename = std::enable_if_t<D == 1> > | |
258 | const QVector<double> &cdata() const noexcept |
|
258 | const QVector<double> &cdata() const noexcept | |
259 | { |
|
259 | { | |
260 | QReadLocker locker{&m_Lock}; |
|
260 | QReadLocker locker{&m_Lock}; | |
261 | return m_Data; |
|
261 | return m_Data; | |
262 | } |
|
262 | } | |
263 |
|
263 | |||
264 | private: |
|
264 | private: | |
265 | DataContainer m_Data; |
|
265 | DataContainer m_Data; | |
266 | /// Number of components (lines). Is always 1 in a 1-dim ArrayData |
|
266 | /// Number of components (lines). Is always 1 in a 1-dim ArrayData | |
267 | int m_NbComponents; |
|
267 | int m_NbComponents; | |
268 | mutable QReadWriteLock m_Lock; |
|
268 | mutable QReadWriteLock m_Lock; | |
269 | }; |
|
269 | }; | |
270 |
|
270 | |||
271 | #endif // SCIQLOP_ARRAYDATA_H |
|
271 | #endif // SCIQLOP_ARRAYDATA_H |
@@ -1,56 +1,60 | |||||
1 | #ifndef SCIQLOP_ARRAYDATAITERATOR_H |
|
1 | #ifndef SCIQLOP_ARRAYDATAITERATOR_H | |
2 | #define SCIQLOP_ARRAYDATAITERATOR_H |
|
2 | #define SCIQLOP_ARRAYDATAITERATOR_H | |
3 |
|
3 | |||
4 | #include "CoreGlobal.h" |
|
4 | #include "CoreGlobal.h" | |
5 | #include "Data/SqpIterator.h" |
|
5 | #include "Data/SqpIterator.h" | |
6 |
|
6 | |||
|
7 | #include <QVector> | |||
7 | #include <memory> |
|
8 | #include <memory> | |
8 |
|
9 | |||
9 | /** |
|
10 | /** | |
10 | * @brief The ArrayDataIteratorValue class represents the current value of an array data iterator. |
|
11 | * @brief The ArrayDataIteratorValue class represents the current value of an array data iterator. | |
11 | * It offers standard access methods for the data in the series (at(), first()), but it is up to |
|
12 | * It offers standard access methods for the data in the series (at(), first()), but it is up to | |
12 | * each array data to define its own implementation of how to retrieve this data (one-dim or two-dim |
|
13 | * each array data to define its own implementation of how to retrieve this data (one-dim or two-dim | |
13 | * array), by implementing the ArrayDataIteratorValue::Impl interface |
|
14 | * array), by implementing the ArrayDataIteratorValue::Impl interface | |
14 | * @sa ArrayDataIterator |
|
15 | * @sa ArrayDataIterator | |
15 | */ |
|
16 | */ | |
16 | class SCIQLOP_CORE_EXPORT ArrayDataIteratorValue { |
|
17 | class SCIQLOP_CORE_EXPORT ArrayDataIteratorValue { | |
17 | public: |
|
18 | public: | |
18 | struct Impl { |
|
19 | struct Impl { | |
19 | virtual ~Impl() noexcept = default; |
|
20 | virtual ~Impl() noexcept = default; | |
20 | virtual std::unique_ptr<Impl> clone() const = 0; |
|
21 | virtual std::unique_ptr<Impl> clone() const = 0; | |
21 | virtual bool equals(const Impl &other) const = 0; |
|
22 | virtual bool equals(const Impl &other) const = 0; | |
22 | virtual void next() = 0; |
|
23 | virtual void next() = 0; | |
23 | virtual void prev() = 0; |
|
24 | virtual void prev() = 0; | |
24 | virtual double at(int componentIndex) const = 0; |
|
25 | virtual double at(int componentIndex) const = 0; | |
25 | virtual double first() const = 0; |
|
26 | virtual double first() const = 0; | |
26 | virtual double min() const = 0; |
|
27 | virtual double min() const = 0; | |
27 | virtual double max() const = 0; |
|
28 | virtual double max() const = 0; | |
|
29 | virtual QVector<double> values() const = 0; | |||
28 | }; |
|
30 | }; | |
29 |
|
31 | |||
30 | explicit ArrayDataIteratorValue(std::unique_ptr<Impl> impl); |
|
32 | explicit ArrayDataIteratorValue(std::unique_ptr<Impl> impl); | |
31 | ArrayDataIteratorValue(const ArrayDataIteratorValue &other); |
|
33 | ArrayDataIteratorValue(const ArrayDataIteratorValue &other); | |
32 | ArrayDataIteratorValue(ArrayDataIteratorValue &&other) = default; |
|
34 | ArrayDataIteratorValue(ArrayDataIteratorValue &&other) = default; | |
33 | ArrayDataIteratorValue &operator=(ArrayDataIteratorValue other); |
|
35 | ArrayDataIteratorValue &operator=(ArrayDataIteratorValue other); | |
34 |
|
36 | |||
35 | bool equals(const ArrayDataIteratorValue &other) const; |
|
37 | bool equals(const ArrayDataIteratorValue &other) const; | |
36 |
|
38 | |||
37 | /// Advances to the next value |
|
39 | /// Advances to the next value | |
38 | void next(); |
|
40 | void next(); | |
39 | /// Moves back to the previous value |
|
41 | /// Moves back to the previous value | |
40 | void prev(); |
|
42 | void prev(); | |
41 | /// Gets value of a specified component |
|
43 | /// Gets value of a specified component | |
42 | double at(int componentIndex) const; |
|
44 | double at(int componentIndex) const; | |
43 | /// Gets value of first component |
|
45 | /// Gets value of first component | |
44 | double first() const; |
|
46 | double first() const; | |
45 | /// Gets min value among all components |
|
47 | /// Gets min value among all components | |
46 | double min() const; |
|
48 | double min() const; | |
47 | /// Gets max value among all components |
|
49 | /// Gets max value among all components | |
48 | double max() const; |
|
50 | double max() const; | |
|
51 | /// Gets all values | |||
|
52 | QVector<double> values() const; | |||
49 |
|
53 | |||
50 | private: |
|
54 | private: | |
51 | std::unique_ptr<Impl> m_Impl; |
|
55 | std::unique_ptr<Impl> m_Impl; | |
52 | }; |
|
56 | }; | |
53 |
|
57 | |||
54 | using ArrayDataIterator = SqpIterator<ArrayDataIteratorValue>; |
|
58 | using ArrayDataIterator = SqpIterator<ArrayDataIteratorValue>; | |
55 |
|
59 | |||
56 | #endif // SCIQLOP_ARRAYDATAITERATOR_H |
|
60 | #endif // SCIQLOP_ARRAYDATAITERATOR_H |
@@ -1,333 +1,334 | |||||
1 | #ifndef SCIQLOP_DATASERIES_H |
|
1 | #ifndef SCIQLOP_DATASERIES_H | |
2 | #define SCIQLOP_DATASERIES_H |
|
2 | #define SCIQLOP_DATASERIES_H | |
3 |
|
3 | |||
4 | #include "CoreGlobal.h" |
|
4 | #include "CoreGlobal.h" | |
5 |
|
5 | |||
6 | #include <Common/SortUtils.h> |
|
6 | #include <Common/SortUtils.h> | |
7 |
|
7 | |||
8 | #include <Data/ArrayData.h> |
|
8 | #include <Data/ArrayData.h> | |
9 | #include <Data/IDataSeries.h> |
|
9 | #include <Data/IDataSeries.h> | |
10 |
|
10 | |||
11 | #include <QLoggingCategory> |
|
11 | #include <QLoggingCategory> | |
12 | #include <QReadLocker> |
|
12 | #include <QReadLocker> | |
13 | #include <QReadWriteLock> |
|
13 | #include <QReadWriteLock> | |
14 | #include <memory> |
|
14 | #include <memory> | |
15 |
|
15 | |||
16 | // We don't use the Qt macro since the log is used in the header file, which causes multiple log |
|
16 | // We don't use the Qt macro since the log is used in the header file, which causes multiple log | |
17 | // definitions with inheritance. Inline method is used instead |
|
17 | // definitions with inheritance. Inline method is used instead | |
18 | inline const QLoggingCategory &LOG_DataSeries() |
|
18 | inline const QLoggingCategory &LOG_DataSeries() | |
19 | { |
|
19 | { | |
20 | static const QLoggingCategory category{"DataSeries"}; |
|
20 | static const QLoggingCategory category{"DataSeries"}; | |
21 | return category; |
|
21 | return category; | |
22 | } |
|
22 | } | |
23 |
|
23 | |||
24 | template <int Dim> |
|
24 | template <int Dim> | |
25 | class DataSeries; |
|
25 | class DataSeries; | |
26 |
|
26 | |||
27 | namespace dataseries_detail { |
|
27 | namespace dataseries_detail { | |
28 |
|
28 | |||
29 | template <int Dim> |
|
29 | template <int Dim> | |
30 | class IteratorValue : public DataSeriesIteratorValue::Impl { |
|
30 | class IteratorValue : public DataSeriesIteratorValue::Impl { | |
31 | public: |
|
31 | public: | |
32 | explicit IteratorValue(const DataSeries<Dim> &dataSeries, bool begin) |
|
32 | explicit IteratorValue(const DataSeries<Dim> &dataSeries, bool begin) | |
33 | : m_XIt(begin ? dataSeries.xAxisData()->cbegin() : dataSeries.xAxisData()->cend()), |
|
33 | : m_XIt(begin ? dataSeries.xAxisData()->cbegin() : dataSeries.xAxisData()->cend()), | |
34 | m_ValuesIt(begin ? dataSeries.valuesData()->cbegin() |
|
34 | m_ValuesIt(begin ? dataSeries.valuesData()->cbegin() | |
35 | : dataSeries.valuesData()->cend()) |
|
35 | : dataSeries.valuesData()->cend()) | |
36 | { |
|
36 | { | |
37 | } |
|
37 | } | |
38 | IteratorValue(const IteratorValue &other) = default; |
|
38 | IteratorValue(const IteratorValue &other) = default; | |
39 |
|
39 | |||
40 | std::unique_ptr<DataSeriesIteratorValue::Impl> clone() const override |
|
40 | std::unique_ptr<DataSeriesIteratorValue::Impl> clone() const override | |
41 | { |
|
41 | { | |
42 | return std::make_unique<IteratorValue<Dim> >(*this); |
|
42 | return std::make_unique<IteratorValue<Dim> >(*this); | |
43 | } |
|
43 | } | |
44 |
|
44 | |||
45 | bool equals(const DataSeriesIteratorValue::Impl &other) const override try { |
|
45 | bool equals(const DataSeriesIteratorValue::Impl &other) const override try { | |
46 | const auto &otherImpl = dynamic_cast<const IteratorValue &>(other); |
|
46 | const auto &otherImpl = dynamic_cast<const IteratorValue &>(other); | |
47 | return std::tie(m_XIt, m_ValuesIt) == std::tie(otherImpl.m_XIt, otherImpl.m_ValuesIt); |
|
47 | return std::tie(m_XIt, m_ValuesIt) == std::tie(otherImpl.m_XIt, otherImpl.m_ValuesIt); | |
48 | } |
|
48 | } | |
49 | catch (const std::bad_cast &) { |
|
49 | catch (const std::bad_cast &) { | |
50 | return false; |
|
50 | return false; | |
51 | } |
|
51 | } | |
52 |
|
52 | |||
53 | void next() override |
|
53 | void next() override | |
54 | { |
|
54 | { | |
55 | ++m_XIt; |
|
55 | ++m_XIt; | |
56 | ++m_ValuesIt; |
|
56 | ++m_ValuesIt; | |
57 | } |
|
57 | } | |
58 |
|
58 | |||
59 | void prev() override |
|
59 | void prev() override | |
60 | { |
|
60 | { | |
61 | --m_XIt; |
|
61 | --m_XIt; | |
62 | --m_ValuesIt; |
|
62 | --m_ValuesIt; | |
63 | } |
|
63 | } | |
64 |
|
64 | |||
65 | double x() const override { return m_XIt->at(0); } |
|
65 | double x() const override { return m_XIt->at(0); } | |
66 | double value() const override { return m_ValuesIt->at(0); } |
|
66 | double value() const override { return m_ValuesIt->at(0); } | |
67 | double value(int componentIndex) const override { return m_ValuesIt->at(componentIndex); } |
|
67 | double value(int componentIndex) const override { return m_ValuesIt->at(componentIndex); } | |
68 | double minValue() const override { return m_ValuesIt->min(); } |
|
68 | double minValue() const override { return m_ValuesIt->min(); } | |
69 | double maxValue() const override { return m_ValuesIt->max(); } |
|
69 | double maxValue() const override { return m_ValuesIt->max(); } | |
|
70 | QVector<double> values() const override { return m_ValuesIt->values(); } | |||
70 |
|
71 | |||
71 | private: |
|
72 | private: | |
72 | ArrayDataIterator m_XIt; |
|
73 | ArrayDataIterator m_XIt; | |
73 | ArrayDataIterator m_ValuesIt; |
|
74 | ArrayDataIterator m_ValuesIt; | |
74 | }; |
|
75 | }; | |
75 | } // namespace dataseries_detail |
|
76 | } // namespace dataseries_detail | |
76 |
|
77 | |||
77 | /** |
|
78 | /** | |
78 | * @brief The DataSeries class is the base (abstract) implementation of IDataSeries. |
|
79 | * @brief The DataSeries class is the base (abstract) implementation of IDataSeries. | |
79 | * |
|
80 | * | |
80 | * It proposes to set a dimension for the values ββdata. |
|
81 | * It proposes to set a dimension for the values ββdata. | |
81 | * |
|
82 | * | |
82 | * A DataSeries is always sorted on its x-axis data. |
|
83 | * A DataSeries is always sorted on its x-axis data. | |
83 | * |
|
84 | * | |
84 | * @tparam Dim The dimension of the values data |
|
85 | * @tparam Dim The dimension of the values data | |
85 | * |
|
86 | * | |
86 | */ |
|
87 | */ | |
87 | template <int Dim> |
|
88 | template <int Dim> | |
88 | class SCIQLOP_CORE_EXPORT DataSeries : public IDataSeries { |
|
89 | class SCIQLOP_CORE_EXPORT DataSeries : public IDataSeries { | |
89 | public: |
|
90 | public: | |
90 | /// @sa IDataSeries::xAxisData() |
|
91 | /// @sa IDataSeries::xAxisData() | |
91 | std::shared_ptr<ArrayData<1> > xAxisData() override { return m_XAxisData; } |
|
92 | std::shared_ptr<ArrayData<1> > xAxisData() override { return m_XAxisData; } | |
92 | const std::shared_ptr<ArrayData<1> > xAxisData() const { return m_XAxisData; } |
|
93 | const std::shared_ptr<ArrayData<1> > xAxisData() const { return m_XAxisData; } | |
93 |
|
94 | |||
94 | /// @sa IDataSeries::xAxisUnit() |
|
95 | /// @sa IDataSeries::xAxisUnit() | |
95 | Unit xAxisUnit() const override { return m_XAxisUnit; } |
|
96 | Unit xAxisUnit() const override { return m_XAxisUnit; } | |
96 |
|
97 | |||
97 | /// @return the values dataset |
|
98 | /// @return the values dataset | |
98 | std::shared_ptr<ArrayData<Dim> > valuesData() { return m_ValuesData; } |
|
99 | std::shared_ptr<ArrayData<Dim> > valuesData() { return m_ValuesData; } | |
99 | const std::shared_ptr<ArrayData<Dim> > valuesData() const { return m_ValuesData; } |
|
100 | const std::shared_ptr<ArrayData<Dim> > valuesData() const { return m_ValuesData; } | |
100 |
|
101 | |||
101 | /// @sa IDataSeries::valuesUnit() |
|
102 | /// @sa IDataSeries::valuesUnit() | |
102 | Unit valuesUnit() const override { return m_ValuesUnit; } |
|
103 | Unit valuesUnit() const override { return m_ValuesUnit; } | |
103 |
|
104 | |||
104 |
|
105 | |||
105 | SqpRange range() const override |
|
106 | SqpRange range() const override | |
106 | { |
|
107 | { | |
107 | if (!m_XAxisData->cdata().isEmpty()) { |
|
108 | if (!m_XAxisData->cdata().isEmpty()) { | |
108 | return SqpRange{m_XAxisData->cdata().first(), m_XAxisData->cdata().last()}; |
|
109 | return SqpRange{m_XAxisData->cdata().first(), m_XAxisData->cdata().last()}; | |
109 | } |
|
110 | } | |
110 |
|
111 | |||
111 | return SqpRange{}; |
|
112 | return SqpRange{}; | |
112 | } |
|
113 | } | |
113 |
|
114 | |||
114 | void clear() |
|
115 | void clear() | |
115 | { |
|
116 | { | |
116 | m_XAxisData->clear(); |
|
117 | m_XAxisData->clear(); | |
117 | m_ValuesData->clear(); |
|
118 | m_ValuesData->clear(); | |
118 | } |
|
119 | } | |
119 |
|
120 | |||
120 | /// Merges into the data series an other data series |
|
121 | /// Merges into the data series an other data series | |
121 | /// @remarks the data series to merge with is cleared after the operation |
|
122 | /// @remarks the data series to merge with is cleared after the operation | |
122 | void merge(IDataSeries *dataSeries) override |
|
123 | void merge(IDataSeries *dataSeries) override | |
123 | { |
|
124 | { | |
124 | dataSeries->lockWrite(); |
|
125 | dataSeries->lockWrite(); | |
125 | lockWrite(); |
|
126 | lockWrite(); | |
126 |
|
127 | |||
127 | if (auto other = dynamic_cast<DataSeries<Dim> *>(dataSeries)) { |
|
128 | if (auto other = dynamic_cast<DataSeries<Dim> *>(dataSeries)) { | |
128 | const auto &otherXAxisData = other->xAxisData()->cdata(); |
|
129 | const auto &otherXAxisData = other->xAxisData()->cdata(); | |
129 | const auto &xAxisData = m_XAxisData->cdata(); |
|
130 | const auto &xAxisData = m_XAxisData->cdata(); | |
130 |
|
131 | |||
131 | // As data series are sorted, we can improve performances of merge, by call the sort |
|
132 | // As data series are sorted, we can improve performances of merge, by call the sort | |
132 | // method only if the two data series overlap. |
|
133 | // method only if the two data series overlap. | |
133 | if (!otherXAxisData.empty()) { |
|
134 | if (!otherXAxisData.empty()) { | |
134 | auto firstValue = otherXAxisData.front(); |
|
135 | auto firstValue = otherXAxisData.front(); | |
135 | auto lastValue = otherXAxisData.back(); |
|
136 | auto lastValue = otherXAxisData.back(); | |
136 |
|
137 | |||
137 | auto xAxisDataBegin = xAxisData.cbegin(); |
|
138 | auto xAxisDataBegin = xAxisData.cbegin(); | |
138 | auto xAxisDataEnd = xAxisData.cend(); |
|
139 | auto xAxisDataEnd = xAxisData.cend(); | |
139 |
|
140 | |||
140 | bool prepend; |
|
141 | bool prepend; | |
141 | bool sortNeeded; |
|
142 | bool sortNeeded; | |
142 |
|
143 | |||
143 | if (std::lower_bound(xAxisDataBegin, xAxisDataEnd, firstValue) == xAxisDataEnd) { |
|
144 | if (std::lower_bound(xAxisDataBegin, xAxisDataEnd, firstValue) == xAxisDataEnd) { | |
144 | // Other data series if after data series |
|
145 | // Other data series if after data series | |
145 | prepend = false; |
|
146 | prepend = false; | |
146 | sortNeeded = false; |
|
147 | sortNeeded = false; | |
147 | } |
|
148 | } | |
148 | else if (std::upper_bound(xAxisDataBegin, xAxisDataEnd, lastValue) |
|
149 | else if (std::upper_bound(xAxisDataBegin, xAxisDataEnd, lastValue) | |
149 | == xAxisDataBegin) { |
|
150 | == xAxisDataBegin) { | |
150 | // Other data series if before data series |
|
151 | // Other data series if before data series | |
151 | prepend = true; |
|
152 | prepend = true; | |
152 | sortNeeded = false; |
|
153 | sortNeeded = false; | |
153 | } |
|
154 | } | |
154 | else { |
|
155 | else { | |
155 | // The two data series overlap |
|
156 | // The two data series overlap | |
156 | prepend = false; |
|
157 | prepend = false; | |
157 | sortNeeded = true; |
|
158 | sortNeeded = true; | |
158 | } |
|
159 | } | |
159 |
|
160 | |||
160 | // Makes the merge |
|
161 | // Makes the merge | |
161 | m_XAxisData->add(*other->xAxisData(), prepend); |
|
162 | m_XAxisData->add(*other->xAxisData(), prepend); | |
162 | m_ValuesData->add(*other->valuesData(), prepend); |
|
163 | m_ValuesData->add(*other->valuesData(), prepend); | |
163 |
|
164 | |||
164 | if (sortNeeded) { |
|
165 | if (sortNeeded) { | |
165 | sort(); |
|
166 | sort(); | |
166 | } |
|
167 | } | |
167 | } |
|
168 | } | |
168 |
|
169 | |||
169 | // Clears the other data series |
|
170 | // Clears the other data series | |
170 | other->clear(); |
|
171 | other->clear(); | |
171 | } |
|
172 | } | |
172 | else { |
|
173 | else { | |
173 | qCWarning(LOG_DataSeries()) |
|
174 | qCWarning(LOG_DataSeries()) | |
174 | << QObject::tr("Detection of a type of IDataSeries we cannot merge with !"); |
|
175 | << QObject::tr("Detection of a type of IDataSeries we cannot merge with !"); | |
175 | } |
|
176 | } | |
176 | unlock(); |
|
177 | unlock(); | |
177 | dataSeries->unlock(); |
|
178 | dataSeries->unlock(); | |
178 | } |
|
179 | } | |
179 |
|
180 | |||
180 | // ///////// // |
|
181 | // ///////// // | |
181 | // Iterators // |
|
182 | // Iterators // | |
182 | // ///////// // |
|
183 | // ///////// // | |
183 |
|
184 | |||
184 | DataSeriesIterator cbegin() const override |
|
185 | DataSeriesIterator cbegin() const override | |
185 | { |
|
186 | { | |
186 | return DataSeriesIterator{DataSeriesIteratorValue{ |
|
187 | return DataSeriesIterator{DataSeriesIteratorValue{ | |
187 | std::make_unique<dataseries_detail::IteratorValue<Dim> >(*this, true)}}; |
|
188 | std::make_unique<dataseries_detail::IteratorValue<Dim> >(*this, true)}}; | |
188 | } |
|
189 | } | |
189 |
|
190 | |||
190 | DataSeriesIterator cend() const override |
|
191 | DataSeriesIterator cend() const override | |
191 | { |
|
192 | { | |
192 | return DataSeriesIterator{DataSeriesIteratorValue{ |
|
193 | return DataSeriesIterator{DataSeriesIteratorValue{ | |
193 | std::make_unique<dataseries_detail::IteratorValue<Dim> >(*this, false)}}; |
|
194 | std::make_unique<dataseries_detail::IteratorValue<Dim> >(*this, false)}}; | |
194 | } |
|
195 | } | |
195 |
|
196 | |||
196 | /// @sa IDataSeries::minXAxisData() |
|
197 | /// @sa IDataSeries::minXAxisData() | |
197 | DataSeriesIterator minXAxisData(double minXAxisData) const override |
|
198 | DataSeriesIterator minXAxisData(double minXAxisData) const override | |
198 | { |
|
199 | { | |
199 | return std::lower_bound( |
|
200 | return std::lower_bound( | |
200 | cbegin(), cend(), minXAxisData, |
|
201 | cbegin(), cend(), minXAxisData, | |
201 | [](const auto &itValue, const auto &value) { return itValue.x() < value; }); |
|
202 | [](const auto &itValue, const auto &value) { return itValue.x() < value; }); | |
202 | } |
|
203 | } | |
203 |
|
204 | |||
204 | /// @sa IDataSeries::maxXAxisData() |
|
205 | /// @sa IDataSeries::maxXAxisData() | |
205 | DataSeriesIterator maxXAxisData(double maxXAxisData) const override |
|
206 | DataSeriesIterator maxXAxisData(double maxXAxisData) const override | |
206 | { |
|
207 | { | |
207 | // Gets the first element that greater than max value |
|
208 | // Gets the first element that greater than max value | |
208 | auto it = std::upper_bound( |
|
209 | auto it = std::upper_bound( | |
209 | cbegin(), cend(), maxXAxisData, |
|
210 | cbegin(), cend(), maxXAxisData, | |
210 | [](const auto &value, const auto &itValue) { return value < itValue.x(); }); |
|
211 | [](const auto &value, const auto &itValue) { return value < itValue.x(); }); | |
211 |
|
212 | |||
212 | return it == cbegin() ? cend() : --it; |
|
213 | return it == cbegin() ? cend() : --it; | |
213 | } |
|
214 | } | |
214 |
|
215 | |||
215 | std::pair<DataSeriesIterator, DataSeriesIterator> xAxisRange(double minXAxisData, |
|
216 | std::pair<DataSeriesIterator, DataSeriesIterator> xAxisRange(double minXAxisData, | |
216 | double maxXAxisData) const override |
|
217 | double maxXAxisData) const override | |
217 | { |
|
218 | { | |
218 | if (minXAxisData > maxXAxisData) { |
|
219 | if (minXAxisData > maxXAxisData) { | |
219 | std::swap(minXAxisData, maxXAxisData); |
|
220 | std::swap(minXAxisData, maxXAxisData); | |
220 | } |
|
221 | } | |
221 |
|
222 | |||
222 | auto begin = cbegin(); |
|
223 | auto begin = cbegin(); | |
223 | auto end = cend(); |
|
224 | auto end = cend(); | |
224 |
|
225 | |||
225 | auto lowerIt = std::lower_bound( |
|
226 | auto lowerIt = std::lower_bound( | |
226 | begin, end, minXAxisData, |
|
227 | begin, end, minXAxisData, | |
227 | [](const auto &itValue, const auto &value) { return itValue.x() < value; }); |
|
228 | [](const auto &itValue, const auto &value) { return itValue.x() < value; }); | |
228 | auto upperIt = std::upper_bound( |
|
229 | auto upperIt = std::upper_bound( | |
229 | begin, end, maxXAxisData, |
|
230 | begin, end, maxXAxisData, | |
230 | [](const auto &value, const auto &itValue) { return value < itValue.x(); }); |
|
231 | [](const auto &value, const auto &itValue) { return value < itValue.x(); }); | |
231 |
|
232 | |||
232 | return std::make_pair(lowerIt, upperIt); |
|
233 | return std::make_pair(lowerIt, upperIt); | |
233 | } |
|
234 | } | |
234 |
|
235 | |||
235 | std::pair<DataSeriesIterator, DataSeriesIterator> |
|
236 | std::pair<DataSeriesIterator, DataSeriesIterator> | |
236 | valuesBounds(double minXAxisData, double maxXAxisData) const override |
|
237 | valuesBounds(double minXAxisData, double maxXAxisData) const override | |
237 | { |
|
238 | { | |
238 | // Places iterators to the correct x-axis range |
|
239 | // Places iterators to the correct x-axis range | |
239 | auto xAxisRangeIts = xAxisRange(minXAxisData, maxXAxisData); |
|
240 | auto xAxisRangeIts = xAxisRange(minXAxisData, maxXAxisData); | |
240 |
|
241 | |||
241 | // Returns end iterators if the range is empty |
|
242 | // Returns end iterators if the range is empty | |
242 | if (xAxisRangeIts.first == xAxisRangeIts.second) { |
|
243 | if (xAxisRangeIts.first == xAxisRangeIts.second) { | |
243 | return std::make_pair(cend(), cend()); |
|
244 | return std::make_pair(cend(), cend()); | |
244 | } |
|
245 | } | |
245 |
|
246 | |||
246 | // Gets the iterator on the min of all values data |
|
247 | // Gets the iterator on the min of all values data | |
247 | auto minIt = std::min_element( |
|
248 | auto minIt = std::min_element( | |
248 | xAxisRangeIts.first, xAxisRangeIts.second, [](const auto &it1, const auto &it2) { |
|
249 | xAxisRangeIts.first, xAxisRangeIts.second, [](const auto &it1, const auto &it2) { | |
249 | return SortUtils::minCompareWithNaN(it1.minValue(), it2.minValue()); |
|
250 | return SortUtils::minCompareWithNaN(it1.minValue(), it2.minValue()); | |
250 | }); |
|
251 | }); | |
251 |
|
252 | |||
252 | // Gets the iterator on the max of all values data |
|
253 | // Gets the iterator on the max of all values data | |
253 | auto maxIt = std::max_element( |
|
254 | auto maxIt = std::max_element( | |
254 | xAxisRangeIts.first, xAxisRangeIts.second, [](const auto &it1, const auto &it2) { |
|
255 | xAxisRangeIts.first, xAxisRangeIts.second, [](const auto &it1, const auto &it2) { | |
255 | return SortUtils::maxCompareWithNaN(it1.maxValue(), it2.maxValue()); |
|
256 | return SortUtils::maxCompareWithNaN(it1.maxValue(), it2.maxValue()); | |
256 | }); |
|
257 | }); | |
257 |
|
258 | |||
258 | return std::make_pair(minIt, maxIt); |
|
259 | return std::make_pair(minIt, maxIt); | |
259 | } |
|
260 | } | |
260 |
|
261 | |||
261 | // /////// // |
|
262 | // /////// // | |
262 | // Mutexes // |
|
263 | // Mutexes // | |
263 | // /////// // |
|
264 | // /////// // | |
264 |
|
265 | |||
265 | virtual void lockRead() { m_Lock.lockForRead(); } |
|
266 | virtual void lockRead() { m_Lock.lockForRead(); } | |
266 | virtual void lockWrite() { m_Lock.lockForWrite(); } |
|
267 | virtual void lockWrite() { m_Lock.lockForWrite(); } | |
267 | virtual void unlock() { m_Lock.unlock(); } |
|
268 | virtual void unlock() { m_Lock.unlock(); } | |
268 |
|
269 | |||
269 | protected: |
|
270 | protected: | |
270 | /// Protected ctor (DataSeries is abstract). The vectors must have the same size, otherwise a |
|
271 | /// Protected ctor (DataSeries is abstract). The vectors must have the same size, otherwise a | |
271 | /// DataSeries with no values will be created. |
|
272 | /// DataSeries with no values will be created. | |
272 | /// @remarks data series is automatically sorted on its x-axis data |
|
273 | /// @remarks data series is automatically sorted on its x-axis data | |
273 | explicit DataSeries(std::shared_ptr<ArrayData<1> > xAxisData, const Unit &xAxisUnit, |
|
274 | explicit DataSeries(std::shared_ptr<ArrayData<1> > xAxisData, const Unit &xAxisUnit, | |
274 | std::shared_ptr<ArrayData<Dim> > valuesData, const Unit &valuesUnit) |
|
275 | std::shared_ptr<ArrayData<Dim> > valuesData, const Unit &valuesUnit) | |
275 | : m_XAxisData{xAxisData}, |
|
276 | : m_XAxisData{xAxisData}, | |
276 | m_XAxisUnit{xAxisUnit}, |
|
277 | m_XAxisUnit{xAxisUnit}, | |
277 | m_ValuesData{valuesData}, |
|
278 | m_ValuesData{valuesData}, | |
278 | m_ValuesUnit{valuesUnit} |
|
279 | m_ValuesUnit{valuesUnit} | |
279 | { |
|
280 | { | |
280 | if (m_XAxisData->size() != m_ValuesData->size()) { |
|
281 | if (m_XAxisData->size() != m_ValuesData->size()) { | |
281 | clear(); |
|
282 | clear(); | |
282 | } |
|
283 | } | |
283 |
|
284 | |||
284 | // Sorts data if it's not the case |
|
285 | // Sorts data if it's not the case | |
285 | const auto &xAxisCData = m_XAxisData->cdata(); |
|
286 | const auto &xAxisCData = m_XAxisData->cdata(); | |
286 | if (!std::is_sorted(xAxisCData.cbegin(), xAxisCData.cend())) { |
|
287 | if (!std::is_sorted(xAxisCData.cbegin(), xAxisCData.cend())) { | |
287 | sort(); |
|
288 | sort(); | |
288 | } |
|
289 | } | |
289 | } |
|
290 | } | |
290 |
|
291 | |||
291 | /// Copy ctor |
|
292 | /// Copy ctor | |
292 | explicit DataSeries(const DataSeries<Dim> &other) |
|
293 | explicit DataSeries(const DataSeries<Dim> &other) | |
293 | : m_XAxisData{std::make_shared<ArrayData<1> >(*other.m_XAxisData)}, |
|
294 | : m_XAxisData{std::make_shared<ArrayData<1> >(*other.m_XAxisData)}, | |
294 | m_XAxisUnit{other.m_XAxisUnit}, |
|
295 | m_XAxisUnit{other.m_XAxisUnit}, | |
295 | m_ValuesData{std::make_shared<ArrayData<Dim> >(*other.m_ValuesData)}, |
|
296 | m_ValuesData{std::make_shared<ArrayData<Dim> >(*other.m_ValuesData)}, | |
296 | m_ValuesUnit{other.m_ValuesUnit} |
|
297 | m_ValuesUnit{other.m_ValuesUnit} | |
297 | { |
|
298 | { | |
298 | // Since a series is ordered from its construction and is always ordered, it is not |
|
299 | // Since a series is ordered from its construction and is always ordered, it is not | |
299 | // necessary to call the sort method here ('other' is sorted) |
|
300 | // necessary to call the sort method here ('other' is sorted) | |
300 | } |
|
301 | } | |
301 |
|
302 | |||
302 | /// Assignment operator |
|
303 | /// Assignment operator | |
303 | template <int D> |
|
304 | template <int D> | |
304 | DataSeries &operator=(DataSeries<D> other) |
|
305 | DataSeries &operator=(DataSeries<D> other) | |
305 | { |
|
306 | { | |
306 | std::swap(m_XAxisData, other.m_XAxisData); |
|
307 | std::swap(m_XAxisData, other.m_XAxisData); | |
307 | std::swap(m_XAxisUnit, other.m_XAxisUnit); |
|
308 | std::swap(m_XAxisUnit, other.m_XAxisUnit); | |
308 | std::swap(m_ValuesData, other.m_ValuesData); |
|
309 | std::swap(m_ValuesData, other.m_ValuesData); | |
309 | std::swap(m_ValuesUnit, other.m_ValuesUnit); |
|
310 | std::swap(m_ValuesUnit, other.m_ValuesUnit); | |
310 |
|
311 | |||
311 | return *this; |
|
312 | return *this; | |
312 | } |
|
313 | } | |
313 |
|
314 | |||
314 | private: |
|
315 | private: | |
315 | /** |
|
316 | /** | |
316 | * Sorts data series on its x-axis data |
|
317 | * Sorts data series on its x-axis data | |
317 | */ |
|
318 | */ | |
318 | void sort() noexcept |
|
319 | void sort() noexcept | |
319 | { |
|
320 | { | |
320 | auto permutation = SortUtils::sortPermutation(*m_XAxisData, std::less<double>()); |
|
321 | auto permutation = SortUtils::sortPermutation(*m_XAxisData, std::less<double>()); | |
321 | m_XAxisData = m_XAxisData->sort(permutation); |
|
322 | m_XAxisData = m_XAxisData->sort(permutation); | |
322 | m_ValuesData = m_ValuesData->sort(permutation); |
|
323 | m_ValuesData = m_ValuesData->sort(permutation); | |
323 | } |
|
324 | } | |
324 |
|
325 | |||
325 | std::shared_ptr<ArrayData<1> > m_XAxisData; |
|
326 | std::shared_ptr<ArrayData<1> > m_XAxisData; | |
326 | Unit m_XAxisUnit; |
|
327 | Unit m_XAxisUnit; | |
327 | std::shared_ptr<ArrayData<Dim> > m_ValuesData; |
|
328 | std::shared_ptr<ArrayData<Dim> > m_ValuesData; | |
328 | Unit m_ValuesUnit; |
|
329 | Unit m_ValuesUnit; | |
329 |
|
330 | |||
330 | QReadWriteLock m_Lock; |
|
331 | QReadWriteLock m_Lock; | |
331 | }; |
|
332 | }; | |
332 |
|
333 | |||
333 | #endif // SCIQLOP_DATASERIES_H |
|
334 | #endif // SCIQLOP_DATASERIES_H |
@@ -1,60 +1,64 | |||||
1 | #ifndef SCIQLOP_DATASERIESITERATOR_H |
|
1 | #ifndef SCIQLOP_DATASERIESITERATOR_H | |
2 | #define SCIQLOP_DATASERIESITERATOR_H |
|
2 | #define SCIQLOP_DATASERIESITERATOR_H | |
3 |
|
3 | |||
4 | #include "CoreGlobal.h" |
|
4 | #include "CoreGlobal.h" | |
5 | #include "Data/SqpIterator.h" |
|
5 | #include "Data/SqpIterator.h" | |
6 |
|
6 | |||
|
7 | #include <QVector> | |||
7 | #include <memory> |
|
8 | #include <memory> | |
8 |
|
9 | |||
9 | /** |
|
10 | /** | |
10 | * @brief The DataSeriesIteratorValue class represents the current value of a data series iterator. |
|
11 | * @brief The DataSeriesIteratorValue class represents the current value of a data series iterator. | |
11 | * It offers standard access methods for the data in the series (x-axis, values), but it is up to |
|
12 | * It offers standard access methods for the data in the series (x-axis, values), but it is up to | |
12 | * each series to define its own implementation of how to retrieve this data, by implementing the |
|
13 | * each series to define its own implementation of how to retrieve this data, by implementing the | |
13 | * DataSeriesIteratorValue::Impl interface |
|
14 | * DataSeriesIteratorValue::Impl interface | |
14 | * |
|
15 | * | |
15 | * @sa DataSeriesIterator |
|
16 | * @sa DataSeriesIterator | |
16 | */ |
|
17 | */ | |
17 | class SCIQLOP_CORE_EXPORT DataSeriesIteratorValue { |
|
18 | class SCIQLOP_CORE_EXPORT DataSeriesIteratorValue { | |
18 | public: |
|
19 | public: | |
19 | struct Impl { |
|
20 | struct Impl { | |
20 | virtual ~Impl() noexcept = default; |
|
21 | virtual ~Impl() noexcept = default; | |
21 | virtual std::unique_ptr<Impl> clone() const = 0; |
|
22 | virtual std::unique_ptr<Impl> clone() const = 0; | |
22 | virtual bool equals(const Impl &other) const = 0; |
|
23 | virtual bool equals(const Impl &other) const = 0; | |
23 | virtual void next() = 0; |
|
24 | virtual void next() = 0; | |
24 | virtual void prev() = 0; |
|
25 | virtual void prev() = 0; | |
25 | virtual double x() const = 0; |
|
26 | virtual double x() const = 0; | |
26 | virtual double value() const = 0; |
|
27 | virtual double value() const = 0; | |
27 | virtual double value(int componentIndex) const = 0; |
|
28 | virtual double value(int componentIndex) const = 0; | |
28 | virtual double minValue() const = 0; |
|
29 | virtual double minValue() const = 0; | |
29 | virtual double maxValue() const = 0; |
|
30 | virtual double maxValue() const = 0; | |
|
31 | virtual QVector<double> values() const = 0; | |||
30 | }; |
|
32 | }; | |
31 |
|
33 | |||
32 | explicit DataSeriesIteratorValue(std::unique_ptr<Impl> impl); |
|
34 | explicit DataSeriesIteratorValue(std::unique_ptr<Impl> impl); | |
33 | DataSeriesIteratorValue(const DataSeriesIteratorValue &other); |
|
35 | DataSeriesIteratorValue(const DataSeriesIteratorValue &other); | |
34 | DataSeriesIteratorValue(DataSeriesIteratorValue &&other) = default; |
|
36 | DataSeriesIteratorValue(DataSeriesIteratorValue &&other) = default; | |
35 | DataSeriesIteratorValue &operator=(DataSeriesIteratorValue other); |
|
37 | DataSeriesIteratorValue &operator=(DataSeriesIteratorValue other); | |
36 |
|
38 | |||
37 | bool equals(const DataSeriesIteratorValue &other) const; |
|
39 | bool equals(const DataSeriesIteratorValue &other) const; | |
38 |
|
40 | |||
39 | /// Advances to the next value |
|
41 | /// Advances to the next value | |
40 | void next(); |
|
42 | void next(); | |
41 | /// Moves back to the previous value |
|
43 | /// Moves back to the previous value | |
42 | void prev(); |
|
44 | void prev(); | |
43 | /// Gets x-axis data |
|
45 | /// Gets x-axis data | |
44 | double x() const; |
|
46 | double x() const; | |
45 | /// Gets value data |
|
47 | /// Gets value data | |
46 | double value() const; |
|
48 | double value() const; | |
47 | /// Gets value data depending on an index |
|
49 | /// Gets value data depending on an index | |
48 | double value(int componentIndex) const; |
|
50 | double value(int componentIndex) const; | |
49 | /// Gets min of all values data |
|
51 | /// Gets min of all values data | |
50 | double minValue() const; |
|
52 | double minValue() const; | |
51 | /// Gets max of all values data |
|
53 | /// Gets max of all values data | |
52 | double maxValue() const; |
|
54 | double maxValue() const; | |
|
55 | /// Gets all values data | |||
|
56 | QVector<double> values() const; | |||
53 |
|
57 | |||
54 | private: |
|
58 | private: | |
55 | std::unique_ptr<Impl> m_Impl; |
|
59 | std::unique_ptr<Impl> m_Impl; | |
56 | }; |
|
60 | }; | |
57 |
|
61 | |||
58 | using DataSeriesIterator = SqpIterator<DataSeriesIteratorValue>; |
|
62 | using DataSeriesIterator = SqpIterator<DataSeriesIteratorValue>; | |
59 |
|
63 | |||
60 | #endif // SCIQLOP_DATASERIESITERATOR_H |
|
64 | #endif // SCIQLOP_DATASERIESITERATOR_H |
@@ -1,52 +1,57 | |||||
1 | #include "Data/ArrayDataIterator.h" |
|
1 | #include "Data/ArrayDataIterator.h" | |
2 |
|
2 | |||
3 | ArrayDataIteratorValue::ArrayDataIteratorValue(std::unique_ptr<ArrayDataIteratorValue::Impl> impl) |
|
3 | ArrayDataIteratorValue::ArrayDataIteratorValue(std::unique_ptr<ArrayDataIteratorValue::Impl> impl) | |
4 | : m_Impl{std::move(impl)} |
|
4 | : m_Impl{std::move(impl)} | |
5 | { |
|
5 | { | |
6 | } |
|
6 | } | |
7 |
|
7 | |||
8 | ArrayDataIteratorValue::ArrayDataIteratorValue(const ArrayDataIteratorValue &other) |
|
8 | ArrayDataIteratorValue::ArrayDataIteratorValue(const ArrayDataIteratorValue &other) | |
9 | : m_Impl{other.m_Impl->clone()} |
|
9 | : m_Impl{other.m_Impl->clone()} | |
10 | { |
|
10 | { | |
11 | } |
|
11 | } | |
12 |
|
12 | |||
13 | ArrayDataIteratorValue &ArrayDataIteratorValue::operator=(ArrayDataIteratorValue other) |
|
13 | ArrayDataIteratorValue &ArrayDataIteratorValue::operator=(ArrayDataIteratorValue other) | |
14 | { |
|
14 | { | |
15 | std::swap(m_Impl, other.m_Impl); |
|
15 | std::swap(m_Impl, other.m_Impl); | |
16 | return *this; |
|
16 | return *this; | |
17 | } |
|
17 | } | |
18 |
|
18 | |||
19 | bool ArrayDataIteratorValue::equals(const ArrayDataIteratorValue &other) const |
|
19 | bool ArrayDataIteratorValue::equals(const ArrayDataIteratorValue &other) const | |
20 | { |
|
20 | { | |
21 | return m_Impl->equals(*other.m_Impl); |
|
21 | return m_Impl->equals(*other.m_Impl); | |
22 | } |
|
22 | } | |
23 |
|
23 | |||
24 | void ArrayDataIteratorValue::next() |
|
24 | void ArrayDataIteratorValue::next() | |
25 | { |
|
25 | { | |
26 | m_Impl->next(); |
|
26 | m_Impl->next(); | |
27 | } |
|
27 | } | |
28 |
|
28 | |||
29 | void ArrayDataIteratorValue::prev() |
|
29 | void ArrayDataIteratorValue::prev() | |
30 | { |
|
30 | { | |
31 | m_Impl->prev(); |
|
31 | m_Impl->prev(); | |
32 | } |
|
32 | } | |
33 |
|
33 | |||
34 | double ArrayDataIteratorValue::at(int componentIndex) const |
|
34 | double ArrayDataIteratorValue::at(int componentIndex) const | |
35 | { |
|
35 | { | |
36 | return m_Impl->at(componentIndex); |
|
36 | return m_Impl->at(componentIndex); | |
37 | } |
|
37 | } | |
38 |
|
38 | |||
39 | double ArrayDataIteratorValue::first() const |
|
39 | double ArrayDataIteratorValue::first() const | |
40 | { |
|
40 | { | |
41 | return m_Impl->first(); |
|
41 | return m_Impl->first(); | |
42 | } |
|
42 | } | |
43 |
|
43 | |||
44 | double ArrayDataIteratorValue::min() const |
|
44 | double ArrayDataIteratorValue::min() const | |
45 | { |
|
45 | { | |
46 | return m_Impl->min(); |
|
46 | return m_Impl->min(); | |
47 | } |
|
47 | } | |
48 |
|
48 | |||
49 | double ArrayDataIteratorValue::max() const |
|
49 | double ArrayDataIteratorValue::max() const | |
50 | { |
|
50 | { | |
51 | return m_Impl->max(); |
|
51 | return m_Impl->max(); | |
52 | } |
|
52 | } | |
|
53 | ||||
|
54 | QVector<double> ArrayDataIteratorValue::values() const | |||
|
55 | { | |||
|
56 | return m_Impl->values(); | |||
|
57 | } |
@@ -1,58 +1,63 | |||||
1 | #include "Data/DataSeriesIterator.h" |
|
1 | #include "Data/DataSeriesIterator.h" | |
2 |
|
2 | |||
3 | DataSeriesIteratorValue::DataSeriesIteratorValue( |
|
3 | DataSeriesIteratorValue::DataSeriesIteratorValue( | |
4 | std::unique_ptr<DataSeriesIteratorValue::Impl> impl) |
|
4 | std::unique_ptr<DataSeriesIteratorValue::Impl> impl) | |
5 | : m_Impl{std::move(impl)} |
|
5 | : m_Impl{std::move(impl)} | |
6 | { |
|
6 | { | |
7 | } |
|
7 | } | |
8 |
|
8 | |||
9 | DataSeriesIteratorValue::DataSeriesIteratorValue(const DataSeriesIteratorValue &other) |
|
9 | DataSeriesIteratorValue::DataSeriesIteratorValue(const DataSeriesIteratorValue &other) | |
10 | : m_Impl{other.m_Impl->clone()} |
|
10 | : m_Impl{other.m_Impl->clone()} | |
11 | { |
|
11 | { | |
12 | } |
|
12 | } | |
13 |
|
13 | |||
14 | DataSeriesIteratorValue &DataSeriesIteratorValue::operator=(DataSeriesIteratorValue other) |
|
14 | DataSeriesIteratorValue &DataSeriesIteratorValue::operator=(DataSeriesIteratorValue other) | |
15 | { |
|
15 | { | |
16 | std::swap(m_Impl, other.m_Impl); |
|
16 | std::swap(m_Impl, other.m_Impl); | |
17 | return *this; |
|
17 | return *this; | |
18 | } |
|
18 | } | |
19 |
|
19 | |||
20 | bool DataSeriesIteratorValue::equals(const DataSeriesIteratorValue &other) const |
|
20 | bool DataSeriesIteratorValue::equals(const DataSeriesIteratorValue &other) const | |
21 | { |
|
21 | { | |
22 | return m_Impl->equals(*other.m_Impl); |
|
22 | return m_Impl->equals(*other.m_Impl); | |
23 | } |
|
23 | } | |
24 |
|
24 | |||
25 | void DataSeriesIteratorValue::next() |
|
25 | void DataSeriesIteratorValue::next() | |
26 | { |
|
26 | { | |
27 | m_Impl->next(); |
|
27 | m_Impl->next(); | |
28 | } |
|
28 | } | |
29 |
|
29 | |||
30 | void DataSeriesIteratorValue::prev() |
|
30 | void DataSeriesIteratorValue::prev() | |
31 | { |
|
31 | { | |
32 | m_Impl->prev(); |
|
32 | m_Impl->prev(); | |
33 | } |
|
33 | } | |
34 |
|
34 | |||
35 | double DataSeriesIteratorValue::x() const |
|
35 | double DataSeriesIteratorValue::x() const | |
36 | { |
|
36 | { | |
37 | return m_Impl->x(); |
|
37 | return m_Impl->x(); | |
38 | } |
|
38 | } | |
39 |
|
39 | |||
40 | double DataSeriesIteratorValue::value() const |
|
40 | double DataSeriesIteratorValue::value() const | |
41 | { |
|
41 | { | |
42 | return m_Impl->value(); |
|
42 | return m_Impl->value(); | |
43 | } |
|
43 | } | |
44 |
|
44 | |||
45 | double DataSeriesIteratorValue::value(int componentIndex) const |
|
45 | double DataSeriesIteratorValue::value(int componentIndex) const | |
46 | { |
|
46 | { | |
47 | return m_Impl->value(componentIndex); |
|
47 | return m_Impl->value(componentIndex); | |
48 | } |
|
48 | } | |
49 |
|
49 | |||
50 | double DataSeriesIteratorValue::minValue() const |
|
50 | double DataSeriesIteratorValue::minValue() const | |
51 | { |
|
51 | { | |
52 | return m_Impl->minValue(); |
|
52 | return m_Impl->minValue(); | |
53 | } |
|
53 | } | |
54 |
|
54 | |||
55 | double DataSeriesIteratorValue::maxValue() const |
|
55 | double DataSeriesIteratorValue::maxValue() const | |
56 | { |
|
56 | { | |
57 | return m_Impl->maxValue(); |
|
57 | return m_Impl->maxValue(); | |
58 | } |
|
58 | } | |
|
59 | ||||
|
60 | QVector<double> DataSeriesIteratorValue::values() const | |||
|
61 | { | |||
|
62 | return m_Impl->values(); | |||
|
63 | } |
General Comments 0
You need to be logged in to leave comments.
Login now