##// END OF EJS Templates
Creates method to retrieve nb points of a data series
Alexandre Leroux -
r717:3f8156a1a068
parent child
Show More
@@ -1,367 +1,374
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 = std::vector<double>;
16 using DataContainer = std::vector<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, bool IsConst>
42 template <int Dim, bool IsConst>
43 class IteratorValue;
43 class IteratorValue;
44
44
45 template <int Dim, bool IsConst>
45 template <int Dim, bool IsConst>
46 struct IteratorValueBuilder {
46 struct IteratorValueBuilder {
47 };
47 };
48
48
49 template <int Dim>
49 template <int Dim>
50 struct IteratorValueBuilder<Dim, true> {
50 struct IteratorValueBuilder<Dim, true> {
51 using DataContainerIterator = DataContainer::const_iterator;
51 using DataContainerIterator = DataContainer::const_iterator;
52
52
53 static void swap(IteratorValue<Dim, true> &o1, IteratorValue<Dim, true> &o2) {}
53 static void swap(IteratorValue<Dim, true> &o1, IteratorValue<Dim, true> &o2) {}
54 };
54 };
55
55
56 template <int Dim>
56 template <int Dim>
57 struct IteratorValueBuilder<Dim, false> {
57 struct IteratorValueBuilder<Dim, false> {
58 using DataContainerIterator = DataContainer::iterator;
58 using DataContainerIterator = DataContainer::iterator;
59
59
60 static void swap(IteratorValue<Dim, false> &o1, IteratorValue<Dim, false> &o2)
60 static void swap(IteratorValue<Dim, false> &o1, IteratorValue<Dim, false> &o2)
61 {
61 {
62 for (auto i = 0; i < o1.m_NbComponents; ++i) {
62 for (auto i = 0; i < o1.m_NbComponents; ++i) {
63 std::iter_swap(o1.m_It + i, o2.m_It + i);
63 std::iter_swap(o1.m_It + i, o2.m_It + i);
64 }
64 }
65 }
65 }
66 };
66 };
67
67
68 template <int Dim, bool IsConst>
68 template <int Dim, bool IsConst>
69 class IteratorValue : public ArrayDataIteratorValue::Impl {
69 class IteratorValue : public ArrayDataIteratorValue::Impl {
70 public:
70 public:
71 friend class ArrayData<Dim>;
71 friend class ArrayData<Dim>;
72 friend class IteratorValueBuilder<Dim, IsConst>;
72 friend class IteratorValueBuilder<Dim, IsConst>;
73
73
74 using DataContainerIterator =
74 using DataContainerIterator =
75 typename IteratorValueBuilder<Dim, IsConst>::DataContainerIterator;
75 typename IteratorValueBuilder<Dim, IsConst>::DataContainerIterator;
76
76
77 template <bool IC = IsConst, typename = std::enable_if_t<IC == true> >
77 template <bool IC = IsConst, typename = std::enable_if_t<IC == true> >
78 explicit IteratorValue(const DataContainer &container, int nbComponents, bool begin)
78 explicit IteratorValue(const DataContainer &container, int nbComponents, bool begin)
79 : m_It{begin ? container.cbegin() : container.cend()}, m_NbComponents{nbComponents}
79 : m_It{begin ? container.cbegin() : container.cend()}, m_NbComponents{nbComponents}
80 {
80 {
81 }
81 }
82
82
83 template <bool IC = IsConst, typename = std::enable_if_t<IC == false> >
83 template <bool IC = IsConst, typename = std::enable_if_t<IC == false> >
84 explicit IteratorValue(DataContainer &container, int nbComponents, bool begin)
84 explicit IteratorValue(DataContainer &container, int nbComponents, bool begin)
85 : m_It{begin ? container.begin() : container.end()}, m_NbComponents{nbComponents}
85 : m_It{begin ? container.begin() : container.end()}, m_NbComponents{nbComponents}
86 {
86 {
87 }
87 }
88
88
89 IteratorValue(const IteratorValue &other) = default;
89 IteratorValue(const IteratorValue &other) = default;
90
90
91 std::unique_ptr<ArrayDataIteratorValue::Impl> clone() const override
91 std::unique_ptr<ArrayDataIteratorValue::Impl> clone() const override
92 {
92 {
93 return std::make_unique<IteratorValue<Dim, IsConst> >(*this);
93 return std::make_unique<IteratorValue<Dim, IsConst> >(*this);
94 }
94 }
95
95
96 int distance(const ArrayDataIteratorValue::Impl &other) const override try {
96 int distance(const ArrayDataIteratorValue::Impl &other) const override try {
97 /// @todo ALX : validate
97 /// @todo ALX : validate
98 const auto &otherImpl = dynamic_cast<const IteratorValue &>(other);
98 const auto &otherImpl = dynamic_cast<const IteratorValue &>(other);
99 return std::distance(otherImpl.m_It, m_It) / m_NbComponents;
99 return std::distance(otherImpl.m_It, m_It) / m_NbComponents;
100 }
100 }
101 catch (const std::bad_cast &) {
101 catch (const std::bad_cast &) {
102 return 0;
102 return 0;
103 }
103 }
104
104
105 bool equals(const ArrayDataIteratorValue::Impl &other) const override try {
105 bool equals(const ArrayDataIteratorValue::Impl &other) const override try {
106 const auto &otherImpl = dynamic_cast<const IteratorValue &>(other);
106 const auto &otherImpl = dynamic_cast<const IteratorValue &>(other);
107 return std::tie(m_It, m_NbComponents) == std::tie(otherImpl.m_It, otherImpl.m_NbComponents);
107 return std::tie(m_It, m_NbComponents) == std::tie(otherImpl.m_It, otherImpl.m_NbComponents);
108 }
108 }
109 catch (const std::bad_cast &) {
109 catch (const std::bad_cast &) {
110 return false;
110 return false;
111 }
111 }
112
112
113 bool lowerThan(const ArrayDataIteratorValue::Impl &other) const override try {
113 bool lowerThan(const ArrayDataIteratorValue::Impl &other) const override try {
114 const auto &otherImpl = dynamic_cast<const IteratorValue &>(other);
114 const auto &otherImpl = dynamic_cast<const IteratorValue &>(other);
115 return m_It < otherImpl.m_It;
115 return m_It < otherImpl.m_It;
116 }
116 }
117 catch (const std::bad_cast &) {
117 catch (const std::bad_cast &) {
118 return false;
118 return false;
119 }
119 }
120
120
121 std::unique_ptr<ArrayDataIteratorValue::Impl> advance(int offset) const override
121 std::unique_ptr<ArrayDataIteratorValue::Impl> advance(int offset) const override
122 {
122 {
123 auto result = clone();
123 auto result = clone();
124 result->next(offset);
124 result->next(offset);
125 return result;
125 return result;
126 }
126 }
127
127
128 void next(int offset) override { std::advance(m_It, offset * m_NbComponents); }
128 void next(int offset) override { std::advance(m_It, offset * m_NbComponents); }
129 void prev() override { std::advance(m_It, -m_NbComponents); }
129 void prev() override { std::advance(m_It, -m_NbComponents); }
130
130
131 double at(int componentIndex) const override { return *(m_It + componentIndex); }
131 double at(int componentIndex) const override { return *(m_It + componentIndex); }
132 double first() const override { return *m_It; }
132 double first() const override { return *m_It; }
133 double min() const override
133 double min() const override
134 {
134 {
135 auto values = this->values();
135 auto values = this->values();
136 auto end = values.cend();
136 auto end = values.cend();
137 auto it = std::min_element(values.cbegin(), end, [](const auto &v1, const auto &v2) {
137 auto it = std::min_element(values.cbegin(), end, [](const auto &v1, const auto &v2) {
138 return SortUtils::minCompareWithNaN(v1, v2);
138 return SortUtils::minCompareWithNaN(v1, v2);
139 });
139 });
140
140
141 return it != end ? *it : std::numeric_limits<double>::quiet_NaN();
141 return it != end ? *it : std::numeric_limits<double>::quiet_NaN();
142 }
142 }
143 double max() const override
143 double max() const override
144 {
144 {
145 auto values = this->values();
145 auto values = this->values();
146 auto end = values.cend();
146 auto end = values.cend();
147 auto it = std::max_element(values.cbegin(), end, [](const auto &v1, const auto &v2) {
147 auto it = std::max_element(values.cbegin(), end, [](const auto &v1, const auto &v2) {
148 return SortUtils::maxCompareWithNaN(v1, v2);
148 return SortUtils::maxCompareWithNaN(v1, v2);
149 });
149 });
150 return it != end ? *it : std::numeric_limits<double>::quiet_NaN();
150 return it != end ? *it : std::numeric_limits<double>::quiet_NaN();
151 }
151 }
152
152
153 QVector<double> values() const override
153 QVector<double> values() const override
154 {
154 {
155 auto result = QVector<double>{};
155 auto result = QVector<double>{};
156 for (auto i = 0; i < m_NbComponents; ++i) {
156 for (auto i = 0; i < m_NbComponents; ++i) {
157 result.push_back(*(m_It + i));
157 result.push_back(*(m_It + i));
158 }
158 }
159
159
160 return result;
160 return result;
161 }
161 }
162
162
163 void swap(ArrayDataIteratorValue::Impl &other) override
163 void swap(ArrayDataIteratorValue::Impl &other) override
164 {
164 {
165 auto &otherImpl = dynamic_cast<IteratorValue &>(other);
165 auto &otherImpl = dynamic_cast<IteratorValue &>(other);
166 IteratorValueBuilder<Dim, IsConst>::swap(*this, otherImpl);
166 IteratorValueBuilder<Dim, IsConst>::swap(*this, otherImpl);
167 }
167 }
168
168
169 private:
169 private:
170 DataContainerIterator m_It;
170 DataContainerIterator m_It;
171 int m_NbComponents;
171 int m_NbComponents;
172 };
172 };
173
173
174 } // namespace arraydata_detail
174 } // namespace arraydata_detail
175
175
176 /**
176 /**
177 * @brief The ArrayData class represents a dataset for a data series.
177 * @brief The ArrayData class represents a dataset for a data series.
178 *
178 *
179 * A dataset can be unidimensional or two-dimensional. This property is determined by the Dim
179 * A dataset can be unidimensional or two-dimensional. This property is determined by the Dim
180 * template-parameter. In a case of a two-dimensional dataset, each dataset component has the same
180 * template-parameter. In a case of a two-dimensional dataset, each dataset component has the same
181 * number of values
181 * number of values
182 *
182 *
183 * @tparam Dim the dimension of the ArrayData (one or two)
183 * @tparam Dim the dimension of the ArrayData (one or two)
184 * @sa IDataSeries
184 * @sa IDataSeries
185 */
185 */
186 template <int Dim>
186 template <int Dim>
187 class ArrayData {
187 class ArrayData {
188 public:
188 public:
189 // ///// //
189 // ///// //
190 // Ctors //
190 // Ctors //
191 // ///// //
191 // ///// //
192
192
193 /**
193 /**
194 * Ctor for a unidimensional ArrayData
194 * Ctor for a unidimensional ArrayData
195 * @param data the data the ArrayData will hold
195 * @param data the data the ArrayData will hold
196 */
196 */
197 template <int D = Dim, typename = std::enable_if_t<D == 1> >
197 template <int D = Dim, typename = std::enable_if_t<D == 1> >
198 explicit ArrayData(DataContainer data) : m_Data{std::move(data)}, m_NbComponents{1}
198 explicit ArrayData(DataContainer data) : m_Data{std::move(data)}, m_NbComponents{1}
199 {
199 {
200 }
200 }
201
201
202 /**
202 /**
203 * Ctor for a two-dimensional ArrayData. The number of components (number of lines) must be
203 * Ctor for a two-dimensional ArrayData. The number of components (number of lines) must be
204 * greater than 2 and must be a divisor of the total number of data in the vector
204 * greater than 2 and must be a divisor of the total number of data in the vector
205 * @param data the data the ArrayData will hold
205 * @param data the data the ArrayData will hold
206 * @param nbComponents the number of components
206 * @param nbComponents the number of components
207 * @throws std::invalid_argument if the number of components is less than 2 or is not a divisor
207 * @throws std::invalid_argument if the number of components is less than 2 or is not a divisor
208 * of the size of the data
208 * of the size of the data
209 */
209 */
210 template <int D = Dim, typename = std::enable_if_t<D == 2> >
210 template <int D = Dim, typename = std::enable_if_t<D == 2> >
211 explicit ArrayData(DataContainer data, int nbComponents)
211 explicit ArrayData(DataContainer data, int nbComponents)
212 : m_Data{std::move(data)}, m_NbComponents{nbComponents}
212 : m_Data{std::move(data)}, m_NbComponents{nbComponents}
213 {
213 {
214 if (nbComponents < 2) {
214 if (nbComponents < 2) {
215 throw std::invalid_argument{
215 throw std::invalid_argument{
216 QString{"A multidimensional ArrayData must have at least 2 components (found: %1)"}
216 QString{"A multidimensional ArrayData must have at least 2 components (found: %1)"}
217 .arg(nbComponents)
217 .arg(nbComponents)
218 .toStdString()};
218 .toStdString()};
219 }
219 }
220
220
221 if (m_Data.size() % m_NbComponents != 0) {
221 if (m_Data.size() % m_NbComponents != 0) {
222 throw std::invalid_argument{QString{
222 throw std::invalid_argument{QString{
223 "The number of components (%1) is inconsistent with the total number of data (%2)"}
223 "The number of components (%1) is inconsistent with the total number of data (%2)"}
224 .arg(m_Data.size(), nbComponents)
224 .arg(m_Data.size(), nbComponents)
225 .toStdString()};
225 .toStdString()};
226 }
226 }
227 }
227 }
228
228
229 /// Copy ctor
229 /// Copy ctor
230 explicit ArrayData(const ArrayData &other)
230 explicit ArrayData(const ArrayData &other)
231 {
231 {
232 QReadLocker otherLocker{&other.m_Lock};
232 QReadLocker otherLocker{&other.m_Lock};
233 m_Data = other.m_Data;
233 m_Data = other.m_Data;
234 m_NbComponents = other.m_NbComponents;
234 m_NbComponents = other.m_NbComponents;
235 }
235 }
236
236
237 // /////////////// //
237 // /////////////// //
238 // General methods //
238 // General methods //
239 // /////////////// //
239 // /////////////// //
240
240
241 /**
241 /**
242 * Merges into the array data an other array data. The two array datas must have the same number
242 * Merges into the array data an other array data. The two array datas must have the same number
243 * of components so the merge can be done
243 * of components so the merge can be done
244 * @param other the array data to merge with
244 * @param other the array data to merge with
245 * @param prepend if true, the other array data is inserted at the beginning, otherwise it is
245 * @param prepend if true, the other array data is inserted at the beginning, otherwise it is
246 * inserted at the end
246 * inserted at the end
247 */
247 */
248 void add(const ArrayData<Dim> &other, bool prepend = false)
248 void add(const ArrayData<Dim> &other, bool prepend = false)
249 {
249 {
250 QWriteLocker locker{&m_Lock};
250 QWriteLocker locker{&m_Lock};
251 QReadLocker otherLocker{&other.m_Lock};
251 QReadLocker otherLocker{&other.m_Lock};
252
252
253 if (m_NbComponents != other.componentCount()) {
253 if (m_NbComponents != other.componentCount()) {
254 return;
254 return;
255 }
255 }
256
256
257 insert(other.cbegin(), other.cend(), prepend);
257 insert(other.cbegin(), other.cend(), prepend);
258 }
258 }
259
259
260 void clear()
260 void clear()
261 {
261 {
262 QWriteLocker locker{&m_Lock};
262 QWriteLocker locker{&m_Lock};
263 m_Data.clear();
263 m_Data.clear();
264 }
264 }
265
265
266 int componentCount() const noexcept { return m_NbComponents; }
266 int componentCount() const noexcept { return m_NbComponents; }
267
267
268 /// @return the size (i.e. number of values) of a single component
268 /// @return the size (i.e. number of values) of a single component
269 /// @remarks in a case of a two-dimensional ArrayData, each component has the same size
269 /// @remarks in a case of a two-dimensional ArrayData, each component has the same size
270 int size() const
270 int size() const
271 {
271 {
272 QReadLocker locker{&m_Lock};
272 QReadLocker locker{&m_Lock};
273 return m_Data.size() / m_NbComponents;
273 return m_Data.size() / m_NbComponents;
274 }
274 }
275
275
276 /// @return the total size (i.e. number of values) of the array data
277 int totalSize() const
278 {
279 QReadLocker locker{&m_Lock};
280 return m_Data.size();
281 }
282
276 std::shared_ptr<ArrayData<Dim> > sort(const std::vector<int> &sortPermutation)
283 std::shared_ptr<ArrayData<Dim> > sort(const std::vector<int> &sortPermutation)
277 {
284 {
278 QReadLocker locker{&m_Lock};
285 QReadLocker locker{&m_Lock};
279 return arraydata_detail::Sort<Dim>::sort(m_Data, m_NbComponents, sortPermutation);
286 return arraydata_detail::Sort<Dim>::sort(m_Data, m_NbComponents, sortPermutation);
280 }
287 }
281
288
282 // ///////// //
289 // ///////// //
283 // Iterators //
290 // Iterators //
284 // ///////// //
291 // ///////// //
285
292
286 ArrayDataIterator begin()
293 ArrayDataIterator begin()
287 {
294 {
288 return ArrayDataIterator{
295 return ArrayDataIterator{
289 ArrayDataIteratorValue{std::make_unique<arraydata_detail::IteratorValue<Dim, false> >(
296 ArrayDataIteratorValue{std::make_unique<arraydata_detail::IteratorValue<Dim, false> >(
290 m_Data, m_NbComponents, true)}};
297 m_Data, m_NbComponents, true)}};
291 }
298 }
292
299
293 ArrayDataIterator end()
300 ArrayDataIterator end()
294 {
301 {
295 return ArrayDataIterator{
302 return ArrayDataIterator{
296 ArrayDataIteratorValue{std::make_unique<arraydata_detail::IteratorValue<Dim, false> >(
303 ArrayDataIteratorValue{std::make_unique<arraydata_detail::IteratorValue<Dim, false> >(
297 m_Data, m_NbComponents, false)}};
304 m_Data, m_NbComponents, false)}};
298 }
305 }
299
306
300 ArrayDataIterator cbegin() const
307 ArrayDataIterator cbegin() const
301 {
308 {
302 return ArrayDataIterator{
309 return ArrayDataIterator{
303 ArrayDataIteratorValue{std::make_unique<arraydata_detail::IteratorValue<Dim, true> >(
310 ArrayDataIteratorValue{std::make_unique<arraydata_detail::IteratorValue<Dim, true> >(
304 m_Data, m_NbComponents, true)}};
311 m_Data, m_NbComponents, true)}};
305 }
312 }
306
313
307 ArrayDataIterator cend() const
314 ArrayDataIterator cend() const
308 {
315 {
309 return ArrayDataIterator{
316 return ArrayDataIterator{
310 ArrayDataIteratorValue{std::make_unique<arraydata_detail::IteratorValue<Dim, true> >(
317 ArrayDataIteratorValue{std::make_unique<arraydata_detail::IteratorValue<Dim, true> >(
311 m_Data, m_NbComponents, false)}};
318 m_Data, m_NbComponents, false)}};
312 }
319 }
313
320
314 void erase(ArrayDataIterator first, ArrayDataIterator last)
321 void erase(ArrayDataIterator first, ArrayDataIterator last)
315 {
322 {
316 auto firstImpl = dynamic_cast<arraydata_detail::IteratorValue<Dim, false> *>(first->impl());
323 auto firstImpl = dynamic_cast<arraydata_detail::IteratorValue<Dim, false> *>(first->impl());
317 auto lastImpl = dynamic_cast<arraydata_detail::IteratorValue<Dim, false> *>(last->impl());
324 auto lastImpl = dynamic_cast<arraydata_detail::IteratorValue<Dim, false> *>(last->impl());
318
325
319 if (firstImpl && lastImpl) {
326 if (firstImpl && lastImpl) {
320 m_Data.erase(firstImpl->m_It, lastImpl->m_It);
327 m_Data.erase(firstImpl->m_It, lastImpl->m_It);
321 }
328 }
322 }
329 }
323
330
324 void insert(ArrayDataIterator first, ArrayDataIterator last, bool prepend = false)
331 void insert(ArrayDataIterator first, ArrayDataIterator last, bool prepend = false)
325 {
332 {
326 auto firstImpl = dynamic_cast<arraydata_detail::IteratorValue<Dim, true> *>(first->impl());
333 auto firstImpl = dynamic_cast<arraydata_detail::IteratorValue<Dim, true> *>(first->impl());
327 auto lastImpl = dynamic_cast<arraydata_detail::IteratorValue<Dim, true> *>(last->impl());
334 auto lastImpl = dynamic_cast<arraydata_detail::IteratorValue<Dim, true> *>(last->impl());
328
335
329 if (firstImpl && lastImpl) {
336 if (firstImpl && lastImpl) {
330 auto insertIt = prepend ? m_Data.begin() : m_Data.end();
337 auto insertIt = prepend ? m_Data.begin() : m_Data.end();
331
338
332 m_Data.insert(insertIt, firstImpl->m_It, lastImpl->m_It);
339 m_Data.insert(insertIt, firstImpl->m_It, lastImpl->m_It);
333 }
340 }
334 }
341 }
335
342
336 /**
343 /**
337 * @return the data at a specified index
344 * @return the data at a specified index
338 * @remarks index must be a valid position
345 * @remarks index must be a valid position
339 */
346 */
340 double at(int index) const noexcept
347 double at(int index) const noexcept
341 {
348 {
342 QReadLocker locker{&m_Lock};
349 QReadLocker locker{&m_Lock};
343 return m_Data.at(index);
350 return m_Data.at(index);
344 }
351 }
345
352
346 // ///////////// //
353 // ///////////// //
347 // 1-dim methods //
354 // 1-dim methods //
348 // ///////////// //
355 // ///////////// //
349
356
350 /**
357 /**
351 * @return the data as a vector, as a const reference
358 * @return the data as a vector, as a const reference
352 * @remarks this method is only available for a unidimensional ArrayData
359 * @remarks this method is only available for a unidimensional ArrayData
353 */
360 */
354 template <int D = Dim, typename = std::enable_if_t<D == 1> >
361 template <int D = Dim, typename = std::enable_if_t<D == 1> >
355 DataContainer cdata() const noexcept
362 DataContainer cdata() const noexcept
356 {
363 {
357 return m_Data;
364 return m_Data;
358 }
365 }
359
366
360 private:
367 private:
361 DataContainer m_Data;
368 DataContainer m_Data;
362 /// Number of components (lines). Is always 1 in a 1-dim ArrayData
369 /// Number of components (lines). Is always 1 in a 1-dim ArrayData
363 int m_NbComponents;
370 int m_NbComponents;
364 mutable QReadWriteLock m_Lock;
371 mutable QReadWriteLock m_Lock;
365 };
372 };
366
373
367 #endif // SCIQLOP_ARRAYDATA_H
374 #endif // SCIQLOP_ARRAYDATA_H
@@ -1,399 +1,400
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/DataSeriesMergeHelper.h>
9 #include <Data/DataSeriesMergeHelper.h>
10 #include <Data/IDataSeries.h>
10 #include <Data/IDataSeries.h>
11
11
12 #include <QLoggingCategory>
12 #include <QLoggingCategory>
13 #include <QReadLocker>
13 #include <QReadLocker>
14 #include <QReadWriteLock>
14 #include <QReadWriteLock>
15 #include <memory>
15 #include <memory>
16
16
17 // We don't use the Qt macro since the log is used in the header file, which causes multiple log
17 // We don't use the Qt macro since the log is used in the header file, which causes multiple log
18 // definitions with inheritance. Inline method is used instead
18 // definitions with inheritance. Inline method is used instead
19 inline const QLoggingCategory &LOG_DataSeries()
19 inline const QLoggingCategory &LOG_DataSeries()
20 {
20 {
21 static const QLoggingCategory category{"DataSeries"};
21 static const QLoggingCategory category{"DataSeries"};
22 return category;
22 return category;
23 }
23 }
24
24
25 template <int Dim>
25 template <int Dim>
26 class DataSeries;
26 class DataSeries;
27
27
28 namespace dataseries_detail {
28 namespace dataseries_detail {
29
29
30 template <int Dim, bool IsConst>
30 template <int Dim, bool IsConst>
31 class IteratorValue : public DataSeriesIteratorValue::Impl {
31 class IteratorValue : public DataSeriesIteratorValue::Impl {
32 public:
32 public:
33 friend class DataSeries<Dim>;
33 friend class DataSeries<Dim>;
34
34
35 template <bool IC = IsConst, typename = std::enable_if_t<IC == false> >
35 template <bool IC = IsConst, typename = std::enable_if_t<IC == false> >
36 explicit IteratorValue(DataSeries<Dim> &dataSeries, bool begin)
36 explicit IteratorValue(DataSeries<Dim> &dataSeries, bool begin)
37 : m_XIt(begin ? dataSeries.xAxisData()->begin() : dataSeries.xAxisData()->end()),
37 : m_XIt(begin ? dataSeries.xAxisData()->begin() : dataSeries.xAxisData()->end()),
38 m_ValuesIt(begin ? dataSeries.valuesData()->begin() : dataSeries.valuesData()->end())
38 m_ValuesIt(begin ? dataSeries.valuesData()->begin() : dataSeries.valuesData()->end())
39 {
39 {
40 }
40 }
41
41
42 template <bool IC = IsConst, typename = std::enable_if_t<IC == true> >
42 template <bool IC = IsConst, typename = std::enable_if_t<IC == true> >
43 explicit IteratorValue(const DataSeries<Dim> &dataSeries, bool begin)
43 explicit IteratorValue(const DataSeries<Dim> &dataSeries, bool begin)
44 : m_XIt(begin ? dataSeries.xAxisData()->cbegin() : dataSeries.xAxisData()->cend()),
44 : m_XIt(begin ? dataSeries.xAxisData()->cbegin() : dataSeries.xAxisData()->cend()),
45 m_ValuesIt(begin ? dataSeries.valuesData()->cbegin()
45 m_ValuesIt(begin ? dataSeries.valuesData()->cbegin()
46 : dataSeries.valuesData()->cend())
46 : dataSeries.valuesData()->cend())
47 {
47 {
48 }
48 }
49
49
50 IteratorValue(const IteratorValue &other) = default;
50 IteratorValue(const IteratorValue &other) = default;
51
51
52 std::unique_ptr<DataSeriesIteratorValue::Impl> clone() const override
52 std::unique_ptr<DataSeriesIteratorValue::Impl> clone() const override
53 {
53 {
54 return std::make_unique<IteratorValue<Dim, IsConst> >(*this);
54 return std::make_unique<IteratorValue<Dim, IsConst> >(*this);
55 }
55 }
56
56
57 int distance(const DataSeriesIteratorValue::Impl &other) const override try {
57 int distance(const DataSeriesIteratorValue::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 m_XIt->distance(*otherImpl.m_XIt);
59 return m_XIt->distance(*otherImpl.m_XIt);
60 }
60 }
61 catch (const std::bad_cast &) {
61 catch (const std::bad_cast &) {
62 return 0;
62 return 0;
63 }
63 }
64
64
65 bool equals(const DataSeriesIteratorValue::Impl &other) const override try {
65 bool equals(const DataSeriesIteratorValue::Impl &other) const override try {
66 const auto &otherImpl = dynamic_cast<const IteratorValue &>(other);
66 const auto &otherImpl = dynamic_cast<const IteratorValue &>(other);
67 return std::tie(m_XIt, m_ValuesIt) == std::tie(otherImpl.m_XIt, otherImpl.m_ValuesIt);
67 return std::tie(m_XIt, m_ValuesIt) == std::tie(otherImpl.m_XIt, otherImpl.m_ValuesIt);
68 }
68 }
69 catch (const std::bad_cast &) {
69 catch (const std::bad_cast &) {
70 return false;
70 return false;
71 }
71 }
72
72
73 bool lowerThan(const DataSeriesIteratorValue::Impl &other) const override try {
73 bool lowerThan(const DataSeriesIteratorValue::Impl &other) const override try {
74 const auto &otherImpl = dynamic_cast<const IteratorValue &>(other);
74 const auto &otherImpl = dynamic_cast<const IteratorValue &>(other);
75 return m_XIt->lowerThan(*otherImpl.m_XIt);
75 return m_XIt->lowerThan(*otherImpl.m_XIt);
76 }
76 }
77 catch (const std::bad_cast &) {
77 catch (const std::bad_cast &) {
78 return false;
78 return false;
79 }
79 }
80
80
81 std::unique_ptr<DataSeriesIteratorValue::Impl> advance(int offset) const override
81 std::unique_ptr<DataSeriesIteratorValue::Impl> advance(int offset) const override
82 {
82 {
83 auto result = clone();
83 auto result = clone();
84 result->next(offset);
84 result->next(offset);
85 return result;
85 return result;
86 }
86 }
87
87
88 void next(int offset) override
88 void next(int offset) override
89 {
89 {
90 m_XIt->next(offset);
90 m_XIt->next(offset);
91 m_ValuesIt->next(offset);
91 m_ValuesIt->next(offset);
92 }
92 }
93
93
94 void prev() override
94 void prev() override
95 {
95 {
96 --m_XIt;
96 --m_XIt;
97 --m_ValuesIt;
97 --m_ValuesIt;
98 }
98 }
99
99
100 double x() const override { return m_XIt->at(0); }
100 double x() const override { return m_XIt->at(0); }
101 double value() const override { return m_ValuesIt->at(0); }
101 double value() const override { return m_ValuesIt->at(0); }
102 double value(int componentIndex) const override { return m_ValuesIt->at(componentIndex); }
102 double value(int componentIndex) const override { return m_ValuesIt->at(componentIndex); }
103 double minValue() const override { return m_ValuesIt->min(); }
103 double minValue() const override { return m_ValuesIt->min(); }
104 double maxValue() const override { return m_ValuesIt->max(); }
104 double maxValue() const override { return m_ValuesIt->max(); }
105 QVector<double> values() const override { return m_ValuesIt->values(); }
105 QVector<double> values() const override { return m_ValuesIt->values(); }
106
106
107 void swap(DataSeriesIteratorValue::Impl &other) override
107 void swap(DataSeriesIteratorValue::Impl &other) override
108 {
108 {
109 auto &otherImpl = dynamic_cast<IteratorValue &>(other);
109 auto &otherImpl = dynamic_cast<IteratorValue &>(other);
110 m_XIt->impl()->swap(*otherImpl.m_XIt->impl());
110 m_XIt->impl()->swap(*otherImpl.m_XIt->impl());
111 m_ValuesIt->impl()->swap(*otherImpl.m_ValuesIt->impl());
111 m_ValuesIt->impl()->swap(*otherImpl.m_ValuesIt->impl());
112 }
112 }
113
113
114 private:
114 private:
115 ArrayDataIterator m_XIt;
115 ArrayDataIterator m_XIt;
116 ArrayDataIterator m_ValuesIt;
116 ArrayDataIterator m_ValuesIt;
117 };
117 };
118 } // namespace dataseries_detail
118 } // namespace dataseries_detail
119
119
120 /**
120 /**
121 * @brief The DataSeries class is the base (abstract) implementation of IDataSeries.
121 * @brief The DataSeries class is the base (abstract) implementation of IDataSeries.
122 *
122 *
123 * It proposes to set a dimension for the values ​​data.
123 * It proposes to set a dimension for the values ​​data.
124 *
124 *
125 * A DataSeries is always sorted on its x-axis data.
125 * A DataSeries is always sorted on its x-axis data.
126 *
126 *
127 * @tparam Dim The dimension of the values data
127 * @tparam Dim The dimension of the values data
128 *
128 *
129 */
129 */
130 template <int Dim>
130 template <int Dim>
131 class SCIQLOP_CORE_EXPORT DataSeries : public IDataSeries {
131 class SCIQLOP_CORE_EXPORT DataSeries : public IDataSeries {
132 friend class DataSeriesMergeHelper;
132 friend class DataSeriesMergeHelper;
133
133
134 public:
134 public:
135 /// @sa IDataSeries::xAxisData()
135 /// @sa IDataSeries::xAxisData()
136 std::shared_ptr<ArrayData<1> > xAxisData() override { return m_XAxisData; }
136 std::shared_ptr<ArrayData<1> > xAxisData() override { return m_XAxisData; }
137 const std::shared_ptr<ArrayData<1> > xAxisData() const { return m_XAxisData; }
137 const std::shared_ptr<ArrayData<1> > xAxisData() const { return m_XAxisData; }
138
138
139 /// @sa IDataSeries::xAxisUnit()
139 /// @sa IDataSeries::xAxisUnit()
140 Unit xAxisUnit() const override { return m_XAxisUnit; }
140 Unit xAxisUnit() const override { return m_XAxisUnit; }
141
141
142 /// @return the values dataset
142 /// @return the values dataset
143 std::shared_ptr<ArrayData<Dim> > valuesData() { return m_ValuesData; }
143 std::shared_ptr<ArrayData<Dim> > valuesData() { return m_ValuesData; }
144 const std::shared_ptr<ArrayData<Dim> > valuesData() const { return m_ValuesData; }
144 const std::shared_ptr<ArrayData<Dim> > valuesData() const { return m_ValuesData; }
145
145
146 /// @sa IDataSeries::valuesUnit()
146 /// @sa IDataSeries::valuesUnit()
147 Unit valuesUnit() const override { return m_ValuesUnit; }
147 Unit valuesUnit() const override { return m_ValuesUnit; }
148
148
149 int nbPoints() const override { return m_XAxisData->totalSize() + m_ValuesData->totalSize(); }
149
150
150 SqpRange range() const override
151 SqpRange range() const override
151 {
152 {
152 if (!m_XAxisData->cdata().empty()) {
153 if (!m_XAxisData->cdata().empty()) {
153 return SqpRange{m_XAxisData->cdata().front(), m_XAxisData->cdata().back()};
154 return SqpRange{m_XAxisData->cdata().front(), m_XAxisData->cdata().back()};
154 }
155 }
155
156
156 return SqpRange{};
157 return SqpRange{};
157 }
158 }
158
159
159 void clear()
160 void clear()
160 {
161 {
161 m_XAxisData->clear();
162 m_XAxisData->clear();
162 m_ValuesData->clear();
163 m_ValuesData->clear();
163 }
164 }
164
165
165 bool isEmpty() const noexcept { return m_XAxisData->size() == 0; }
166 bool isEmpty() const noexcept { return m_XAxisData->size() == 0; }
166
167
167 /// Merges into the data series an other data series
168 /// Merges into the data series an other data series
168 /// @remarks the data series to merge with is cleared after the operation
169 /// @remarks the data series to merge with is cleared after the operation
169 void merge(IDataSeries *dataSeries) override
170 void merge(IDataSeries *dataSeries) override
170 {
171 {
171 dataSeries->lockWrite();
172 dataSeries->lockWrite();
172 lockWrite();
173 lockWrite();
173
174
174 if (auto other = dynamic_cast<DataSeries<Dim> *>(dataSeries)) {
175 if (auto other = dynamic_cast<DataSeries<Dim> *>(dataSeries)) {
175 DataSeriesMergeHelper::merge(*other, *this);
176 DataSeriesMergeHelper::merge(*other, *this);
176 }
177 }
177 else {
178 else {
178 qCWarning(LOG_DataSeries())
179 qCWarning(LOG_DataSeries())
179 << QObject::tr("Detection of a type of IDataSeries we cannot merge with !");
180 << QObject::tr("Detection of a type of IDataSeries we cannot merge with !");
180 }
181 }
181 unlock();
182 unlock();
182 dataSeries->unlock();
183 dataSeries->unlock();
183 }
184 }
184
185
185 void purge(double min, double max) override
186 void purge(double min, double max) override
186 {
187 {
187 // Nothing to purge if series is empty
188 // Nothing to purge if series is empty
188 if (isEmpty()) {
189 if (isEmpty()) {
189 return;
190 return;
190 }
191 }
191
192
192 if (min > max) {
193 if (min > max) {
193 std::swap(min, max);
194 std::swap(min, max);
194 }
195 }
195
196
196 // Nothing to purge if series min/max are inside purge range
197 // Nothing to purge if series min/max are inside purge range
197 auto xMin = cbegin()->x();
198 auto xMin = cbegin()->x();
198 auto xMax = (--cend())->x();
199 auto xMax = (--cend())->x();
199 if (xMin >= min && xMax <= max) {
200 if (xMin >= min && xMax <= max) {
200 return;
201 return;
201 }
202 }
202
203
203 auto lowerIt = std::lower_bound(
204 auto lowerIt = std::lower_bound(
204 begin(), end(), min, [](const auto &it, const auto &val) { return it.x() < val; });
205 begin(), end(), min, [](const auto &it, const auto &val) { return it.x() < val; });
205 erase(begin(), lowerIt);
206 erase(begin(), lowerIt);
206 auto upperIt = std::upper_bound(
207 auto upperIt = std::upper_bound(
207 begin(), end(), max, [](const auto &val, const auto &it) { return val < it.x(); });
208 begin(), end(), max, [](const auto &val, const auto &it) { return val < it.x(); });
208 erase(upperIt, end());
209 erase(upperIt, end());
209 }
210 }
210
211
211 // ///////// //
212 // ///////// //
212 // Iterators //
213 // Iterators //
213 // ///////// //
214 // ///////// //
214
215
215 DataSeriesIterator begin() override
216 DataSeriesIterator begin() override
216 {
217 {
217 return DataSeriesIterator{DataSeriesIteratorValue{
218 return DataSeriesIterator{DataSeriesIteratorValue{
218 std::make_unique<dataseries_detail::IteratorValue<Dim, false> >(*this, true)}};
219 std::make_unique<dataseries_detail::IteratorValue<Dim, false> >(*this, true)}};
219 }
220 }
220
221
221 DataSeriesIterator end() override
222 DataSeriesIterator end() override
222 {
223 {
223 return DataSeriesIterator{DataSeriesIteratorValue{
224 return DataSeriesIterator{DataSeriesIteratorValue{
224 std::make_unique<dataseries_detail::IteratorValue<Dim, false> >(*this, false)}};
225 std::make_unique<dataseries_detail::IteratorValue<Dim, false> >(*this, false)}};
225 }
226 }
226
227
227 DataSeriesIterator cbegin() const override
228 DataSeriesIterator cbegin() const override
228 {
229 {
229 return DataSeriesIterator{DataSeriesIteratorValue{
230 return DataSeriesIterator{DataSeriesIteratorValue{
230 std::make_unique<dataseries_detail::IteratorValue<Dim, true> >(*this, true)}};
231 std::make_unique<dataseries_detail::IteratorValue<Dim, true> >(*this, true)}};
231 }
232 }
232
233
233 DataSeriesIterator cend() const override
234 DataSeriesIterator cend() const override
234 {
235 {
235 return DataSeriesIterator{DataSeriesIteratorValue{
236 return DataSeriesIterator{DataSeriesIteratorValue{
236 std::make_unique<dataseries_detail::IteratorValue<Dim, true> >(*this, false)}};
237 std::make_unique<dataseries_detail::IteratorValue<Dim, true> >(*this, false)}};
237 }
238 }
238
239
239 void erase(DataSeriesIterator first, DataSeriesIterator last)
240 void erase(DataSeriesIterator first, DataSeriesIterator last)
240 {
241 {
241 auto firstImpl
242 auto firstImpl
242 = dynamic_cast<dataseries_detail::IteratorValue<Dim, false> *>(first->impl());
243 = dynamic_cast<dataseries_detail::IteratorValue<Dim, false> *>(first->impl());
243 auto lastImpl = dynamic_cast<dataseries_detail::IteratorValue<Dim, false> *>(last->impl());
244 auto lastImpl = dynamic_cast<dataseries_detail::IteratorValue<Dim, false> *>(last->impl());
244
245
245 if (firstImpl && lastImpl) {
246 if (firstImpl && lastImpl) {
246 m_XAxisData->erase(firstImpl->m_XIt, lastImpl->m_XIt);
247 m_XAxisData->erase(firstImpl->m_XIt, lastImpl->m_XIt);
247 m_ValuesData->erase(firstImpl->m_ValuesIt, lastImpl->m_ValuesIt);
248 m_ValuesData->erase(firstImpl->m_ValuesIt, lastImpl->m_ValuesIt);
248 }
249 }
249 }
250 }
250
251
251 void insert(DataSeriesIterator first, DataSeriesIterator last, bool prepend = false)
252 void insert(DataSeriesIterator first, DataSeriesIterator last, bool prepend = false)
252 {
253 {
253 auto firstImpl = dynamic_cast<dataseries_detail::IteratorValue<Dim, true> *>(first->impl());
254 auto firstImpl = dynamic_cast<dataseries_detail::IteratorValue<Dim, true> *>(first->impl());
254 auto lastImpl = dynamic_cast<dataseries_detail::IteratorValue<Dim, true> *>(last->impl());
255 auto lastImpl = dynamic_cast<dataseries_detail::IteratorValue<Dim, true> *>(last->impl());
255
256
256 if (firstImpl && lastImpl) {
257 if (firstImpl && lastImpl) {
257 m_XAxisData->insert(firstImpl->m_XIt, lastImpl->m_XIt, prepend);
258 m_XAxisData->insert(firstImpl->m_XIt, lastImpl->m_XIt, prepend);
258 m_ValuesData->insert(firstImpl->m_ValuesIt, lastImpl->m_ValuesIt, prepend);
259 m_ValuesData->insert(firstImpl->m_ValuesIt, lastImpl->m_ValuesIt, prepend);
259 }
260 }
260 }
261 }
261
262
262 /// @sa IDataSeries::minXAxisData()
263 /// @sa IDataSeries::minXAxisData()
263 DataSeriesIterator minXAxisData(double minXAxisData) const override
264 DataSeriesIterator minXAxisData(double minXAxisData) const override
264 {
265 {
265 return std::lower_bound(
266 return std::lower_bound(
266 cbegin(), cend(), minXAxisData,
267 cbegin(), cend(), minXAxisData,
267 [](const auto &itValue, const auto &value) { return itValue.x() < value; });
268 [](const auto &itValue, const auto &value) { return itValue.x() < value; });
268 }
269 }
269
270
270 /// @sa IDataSeries::maxXAxisData()
271 /// @sa IDataSeries::maxXAxisData()
271 DataSeriesIterator maxXAxisData(double maxXAxisData) const override
272 DataSeriesIterator maxXAxisData(double maxXAxisData) const override
272 {
273 {
273 // Gets the first element that greater than max value
274 // Gets the first element that greater than max value
274 auto it = std::upper_bound(
275 auto it = std::upper_bound(
275 cbegin(), cend(), maxXAxisData,
276 cbegin(), cend(), maxXAxisData,
276 [](const auto &value, const auto &itValue) { return value < itValue.x(); });
277 [](const auto &value, const auto &itValue) { return value < itValue.x(); });
277
278
278 return it == cbegin() ? cend() : --it;
279 return it == cbegin() ? cend() : --it;
279 }
280 }
280
281
281 std::pair<DataSeriesIterator, DataSeriesIterator> xAxisRange(double minXAxisData,
282 std::pair<DataSeriesIterator, DataSeriesIterator> xAxisRange(double minXAxisData,
282 double maxXAxisData) const override
283 double maxXAxisData) const override
283 {
284 {
284 if (minXAxisData > maxXAxisData) {
285 if (minXAxisData > maxXAxisData) {
285 std::swap(minXAxisData, maxXAxisData);
286 std::swap(minXAxisData, maxXAxisData);
286 }
287 }
287
288
288 auto begin = cbegin();
289 auto begin = cbegin();
289 auto end = cend();
290 auto end = cend();
290
291
291 auto lowerIt = std::lower_bound(
292 auto lowerIt = std::lower_bound(
292 begin, end, minXAxisData,
293 begin, end, minXAxisData,
293 [](const auto &itValue, const auto &value) { return itValue.x() < value; });
294 [](const auto &itValue, const auto &value) { return itValue.x() < value; });
294 auto upperIt = std::upper_bound(
295 auto upperIt = std::upper_bound(
295 lowerIt, end, maxXAxisData,
296 lowerIt, end, maxXAxisData,
296 [](const auto &value, const auto &itValue) { return value < itValue.x(); });
297 [](const auto &value, const auto &itValue) { return value < itValue.x(); });
297
298
298 return std::make_pair(lowerIt, upperIt);
299 return std::make_pair(lowerIt, upperIt);
299 }
300 }
300
301
301 std::pair<DataSeriesIterator, DataSeriesIterator>
302 std::pair<DataSeriesIterator, DataSeriesIterator>
302 valuesBounds(double minXAxisData, double maxXAxisData) const override
303 valuesBounds(double minXAxisData, double maxXAxisData) const override
303 {
304 {
304 // Places iterators to the correct x-axis range
305 // Places iterators to the correct x-axis range
305 auto xAxisRangeIts = xAxisRange(minXAxisData, maxXAxisData);
306 auto xAxisRangeIts = xAxisRange(minXAxisData, maxXAxisData);
306
307
307 // Returns end iterators if the range is empty
308 // Returns end iterators if the range is empty
308 if (xAxisRangeIts.first == xAxisRangeIts.second) {
309 if (xAxisRangeIts.first == xAxisRangeIts.second) {
309 return std::make_pair(cend(), cend());
310 return std::make_pair(cend(), cend());
310 }
311 }
311
312
312 // Gets the iterator on the min of all values data
313 // Gets the iterator on the min of all values data
313 auto minIt = std::min_element(
314 auto minIt = std::min_element(
314 xAxisRangeIts.first, xAxisRangeIts.second, [](const auto &it1, const auto &it2) {
315 xAxisRangeIts.first, xAxisRangeIts.second, [](const auto &it1, const auto &it2) {
315 return SortUtils::minCompareWithNaN(it1.minValue(), it2.minValue());
316 return SortUtils::minCompareWithNaN(it1.minValue(), it2.minValue());
316 });
317 });
317
318
318 // Gets the iterator on the max of all values data
319 // Gets the iterator on the max of all values data
319 auto maxIt = std::max_element(
320 auto maxIt = std::max_element(
320 xAxisRangeIts.first, xAxisRangeIts.second, [](const auto &it1, const auto &it2) {
321 xAxisRangeIts.first, xAxisRangeIts.second, [](const auto &it1, const auto &it2) {
321 return SortUtils::maxCompareWithNaN(it1.maxValue(), it2.maxValue());
322 return SortUtils::maxCompareWithNaN(it1.maxValue(), it2.maxValue());
322 });
323 });
323
324
324 return std::make_pair(minIt, maxIt);
325 return std::make_pair(minIt, maxIt);
325 }
326 }
326
327
327 // /////// //
328 // /////// //
328 // Mutexes //
329 // Mutexes //
329 // /////// //
330 // /////// //
330
331
331 virtual void lockRead() { m_Lock.lockForRead(); }
332 virtual void lockRead() { m_Lock.lockForRead(); }
332 virtual void lockWrite() { m_Lock.lockForWrite(); }
333 virtual void lockWrite() { m_Lock.lockForWrite(); }
333 virtual void unlock() { m_Lock.unlock(); }
334 virtual void unlock() { m_Lock.unlock(); }
334
335
335 protected:
336 protected:
336 /// Protected ctor (DataSeries is abstract). The vectors must have the same size, otherwise a
337 /// Protected ctor (DataSeries is abstract). The vectors must have the same size, otherwise a
337 /// DataSeries with no values will be created.
338 /// DataSeries with no values will be created.
338 /// @remarks data series is automatically sorted on its x-axis data
339 /// @remarks data series is automatically sorted on its x-axis data
339 explicit DataSeries(std::shared_ptr<ArrayData<1> > xAxisData, const Unit &xAxisUnit,
340 explicit DataSeries(std::shared_ptr<ArrayData<1> > xAxisData, const Unit &xAxisUnit,
340 std::shared_ptr<ArrayData<Dim> > valuesData, const Unit &valuesUnit)
341 std::shared_ptr<ArrayData<Dim> > valuesData, const Unit &valuesUnit)
341 : m_XAxisData{xAxisData},
342 : m_XAxisData{xAxisData},
342 m_XAxisUnit{xAxisUnit},
343 m_XAxisUnit{xAxisUnit},
343 m_ValuesData{valuesData},
344 m_ValuesData{valuesData},
344 m_ValuesUnit{valuesUnit}
345 m_ValuesUnit{valuesUnit}
345 {
346 {
346 if (m_XAxisData->size() != m_ValuesData->size()) {
347 if (m_XAxisData->size() != m_ValuesData->size()) {
347 clear();
348 clear();
348 }
349 }
349
350
350 // Sorts data if it's not the case
351 // Sorts data if it's not the case
351 const auto &xAxisCData = m_XAxisData->cdata();
352 const auto &xAxisCData = m_XAxisData->cdata();
352 if (!std::is_sorted(xAxisCData.cbegin(), xAxisCData.cend())) {
353 if (!std::is_sorted(xAxisCData.cbegin(), xAxisCData.cend())) {
353 sort();
354 sort();
354 }
355 }
355 }
356 }
356
357
357 /// Copy ctor
358 /// Copy ctor
358 explicit DataSeries(const DataSeries<Dim> &other)
359 explicit DataSeries(const DataSeries<Dim> &other)
359 : m_XAxisData{std::make_shared<ArrayData<1> >(*other.m_XAxisData)},
360 : m_XAxisData{std::make_shared<ArrayData<1> >(*other.m_XAxisData)},
360 m_XAxisUnit{other.m_XAxisUnit},
361 m_XAxisUnit{other.m_XAxisUnit},
361 m_ValuesData{std::make_shared<ArrayData<Dim> >(*other.m_ValuesData)},
362 m_ValuesData{std::make_shared<ArrayData<Dim> >(*other.m_ValuesData)},
362 m_ValuesUnit{other.m_ValuesUnit}
363 m_ValuesUnit{other.m_ValuesUnit}
363 {
364 {
364 // Since a series is ordered from its construction and is always ordered, it is not
365 // Since a series is ordered from its construction and is always ordered, it is not
365 // necessary to call the sort method here ('other' is sorted)
366 // necessary to call the sort method here ('other' is sorted)
366 }
367 }
367
368
368 /// Assignment operator
369 /// Assignment operator
369 template <int D>
370 template <int D>
370 DataSeries &operator=(DataSeries<D> other)
371 DataSeries &operator=(DataSeries<D> other)
371 {
372 {
372 std::swap(m_XAxisData, other.m_XAxisData);
373 std::swap(m_XAxisData, other.m_XAxisData);
373 std::swap(m_XAxisUnit, other.m_XAxisUnit);
374 std::swap(m_XAxisUnit, other.m_XAxisUnit);
374 std::swap(m_ValuesData, other.m_ValuesData);
375 std::swap(m_ValuesData, other.m_ValuesData);
375 std::swap(m_ValuesUnit, other.m_ValuesUnit);
376 std::swap(m_ValuesUnit, other.m_ValuesUnit);
376
377
377 return *this;
378 return *this;
378 }
379 }
379
380
380 private:
381 private:
381 /**
382 /**
382 * Sorts data series on its x-axis data
383 * Sorts data series on its x-axis data
383 */
384 */
384 void sort() noexcept
385 void sort() noexcept
385 {
386 {
386 auto permutation = SortUtils::sortPermutation(*m_XAxisData, std::less<double>());
387 auto permutation = SortUtils::sortPermutation(*m_XAxisData, std::less<double>());
387 m_XAxisData = m_XAxisData->sort(permutation);
388 m_XAxisData = m_XAxisData->sort(permutation);
388 m_ValuesData = m_ValuesData->sort(permutation);
389 m_ValuesData = m_ValuesData->sort(permutation);
389 }
390 }
390
391
391 std::shared_ptr<ArrayData<1> > m_XAxisData;
392 std::shared_ptr<ArrayData<1> > m_XAxisData;
392 Unit m_XAxisUnit;
393 Unit m_XAxisUnit;
393 std::shared_ptr<ArrayData<Dim> > m_ValuesData;
394 std::shared_ptr<ArrayData<Dim> > m_ValuesData;
394 Unit m_ValuesUnit;
395 Unit m_ValuesUnit;
395
396
396 QReadWriteLock m_Lock;
397 QReadWriteLock m_Lock;
397 };
398 };
398
399
399 #endif // SCIQLOP_DATASERIES_H
400 #endif // SCIQLOP_DATASERIES_H
@@ -1,109 +1,113
1 #ifndef SCIQLOP_IDATASERIES_H
1 #ifndef SCIQLOP_IDATASERIES_H
2 #define SCIQLOP_IDATASERIES_H
2 #define SCIQLOP_IDATASERIES_H
3
3
4 #include <Common/MetaTypes.h>
4 #include <Common/MetaTypes.h>
5 #include <Data/DataSeriesIterator.h>
5 #include <Data/DataSeriesIterator.h>
6 #include <Data/SqpRange.h>
6 #include <Data/SqpRange.h>
7
7
8 #include <memory>
8 #include <memory>
9
9
10 #include <QString>
10 #include <QString>
11
11
12 template <int Dim>
12 template <int Dim>
13 class ArrayData;
13 class ArrayData;
14
14
15 struct Unit {
15 struct Unit {
16 explicit Unit(const QString &name = {}, bool timeUnit = false)
16 explicit Unit(const QString &name = {}, bool timeUnit = false)
17 : m_Name{name}, m_TimeUnit{timeUnit}
17 : m_Name{name}, m_TimeUnit{timeUnit}
18 {
18 {
19 }
19 }
20
20
21 inline bool operator==(const Unit &other) const
21 inline bool operator==(const Unit &other) const
22 {
22 {
23 return std::tie(m_Name, m_TimeUnit) == std::tie(other.m_Name, other.m_TimeUnit);
23 return std::tie(m_Name, m_TimeUnit) == std::tie(other.m_Name, other.m_TimeUnit);
24 }
24 }
25 inline bool operator!=(const Unit &other) const { return !(*this == other); }
25 inline bool operator!=(const Unit &other) const { return !(*this == other); }
26
26
27 QString m_Name; ///< Unit name
27 QString m_Name; ///< Unit name
28 bool m_TimeUnit; ///< The unit is a unit of time (UTC)
28 bool m_TimeUnit; ///< The unit is a unit of time (UTC)
29 };
29 };
30
30
31 /**
31 /**
32 * @brief The IDataSeries aims to declare a data series.
32 * @brief The IDataSeries aims to declare a data series.
33 *
33 *
34 * A data series is an entity that contains at least :
34 * A data series is an entity that contains at least :
35 * - one dataset representing the x-axis
35 * - one dataset representing the x-axis
36 * - one dataset representing the values
36 * - one dataset representing the values
37 *
37 *
38 * Each dataset is represented by an ArrayData, and is associated with a unit.
38 * Each dataset is represented by an ArrayData, and is associated with a unit.
39 *
39 *
40 * An ArrayData can be unidimensional or two-dimensional, depending on the implementation of the
40 * An ArrayData can be unidimensional or two-dimensional, depending on the implementation of the
41 * IDataSeries. The x-axis dataset is always unidimensional.
41 * IDataSeries. The x-axis dataset is always unidimensional.
42 *
42 *
43 * @sa ArrayData
43 * @sa ArrayData
44 */
44 */
45 class IDataSeries {
45 class IDataSeries {
46 public:
46 public:
47 virtual ~IDataSeries() noexcept = default;
47 virtual ~IDataSeries() noexcept = default;
48
48
49 /// Returns the x-axis dataset
49 /// Returns the x-axis dataset
50 virtual std::shared_ptr<ArrayData<1> > xAxisData() = 0;
50 virtual std::shared_ptr<ArrayData<1> > xAxisData() = 0;
51
51
52 /// Returns the x-axis dataset (as const)
52 /// Returns the x-axis dataset (as const)
53 virtual const std::shared_ptr<ArrayData<1> > xAxisData() const = 0;
53 virtual const std::shared_ptr<ArrayData<1> > xAxisData() const = 0;
54
54
55 virtual Unit xAxisUnit() const = 0;
55 virtual Unit xAxisUnit() const = 0;
56
56
57 virtual Unit valuesUnit() const = 0;
57 virtual Unit valuesUnit() const = 0;
58
58
59 virtual void merge(IDataSeries *dataSeries) = 0;
59 virtual void merge(IDataSeries *dataSeries) = 0;
60 /// Removes from data series all entries whose value on the x-axis is not between min and max
60 /// Removes from data series all entries whose value on the x-axis is not between min and max
61 virtual void purge(double min, double max) = 0;
61 virtual void purge(double min, double max) = 0;
62
62
63 /// @todo Review the name and signature of this method
63 /// @todo Review the name and signature of this method
64 virtual std::shared_ptr<IDataSeries> subDataSeries(const SqpRange &range) = 0;
64 virtual std::shared_ptr<IDataSeries> subDataSeries(const SqpRange &range) = 0;
65
65
66 virtual std::unique_ptr<IDataSeries> clone() const = 0;
66 virtual std::unique_ptr<IDataSeries> clone() const = 0;
67
68 /// @return the total number of points contained in the data series
69 virtual int nbPoints() const = 0;
70
67 virtual SqpRange range() const = 0;
71 virtual SqpRange range() const = 0;
68
72
69 // ///////// //
73 // ///////// //
70 // Iterators //
74 // Iterators //
71 // ///////// //
75 // ///////// //
72
76
73 virtual DataSeriesIterator cbegin() const = 0;
77 virtual DataSeriesIterator cbegin() const = 0;
74 virtual DataSeriesIterator cend() const = 0;
78 virtual DataSeriesIterator cend() const = 0;
75 virtual DataSeriesIterator begin() = 0;
79 virtual DataSeriesIterator begin() = 0;
76 virtual DataSeriesIterator end() = 0;
80 virtual DataSeriesIterator end() = 0;
77
81
78 /// @return the iterator to the first entry of the data series whose x-axis data is greater than
82 /// @return the iterator to the first entry of the data series whose x-axis data is greater than
79 /// or equal to the value passed in parameter, or the end iterator if there is no matching value
83 /// or equal to the value passed in parameter, or the end iterator if there is no matching value
80 virtual DataSeriesIterator minXAxisData(double minXAxisData) const = 0;
84 virtual DataSeriesIterator minXAxisData(double minXAxisData) const = 0;
81
85
82 /// @return the iterator to the last entry of the data series whose x-axis data is less than or
86 /// @return the iterator to the last entry of the data series whose x-axis data is less than or
83 /// equal to the value passed in parameter, or the end iterator if there is no matching value
87 /// equal to the value passed in parameter, or the end iterator if there is no matching value
84 virtual DataSeriesIterator maxXAxisData(double maxXAxisData) const = 0;
88 virtual DataSeriesIterator maxXAxisData(double maxXAxisData) const = 0;
85
89
86 /// @return the iterators pointing to the range of data whose x-axis values are between min and
90 /// @return the iterators pointing to the range of data whose x-axis values are between min and
87 /// max passed in parameters
91 /// max passed in parameters
88 virtual std::pair<DataSeriesIterator, DataSeriesIterator>
92 virtual std::pair<DataSeriesIterator, DataSeriesIterator>
89 xAxisRange(double minXAxisData, double maxXAxisData) const = 0;
93 xAxisRange(double minXAxisData, double maxXAxisData) const = 0;
90
94
91 /// @return two iterators pointing to the data that have respectively the min and the max value
95 /// @return two iterators pointing to the data that have respectively the min and the max value
92 /// data of a data series' range. The search is performed for a given x-axis range.
96 /// data of a data series' range. The search is performed for a given x-axis range.
93 /// @sa xAxisRange()
97 /// @sa xAxisRange()
94 virtual std::pair<DataSeriesIterator, DataSeriesIterator>
98 virtual std::pair<DataSeriesIterator, DataSeriesIterator>
95 valuesBounds(double minXAxisData, double maxXAxisData) const = 0;
99 valuesBounds(double minXAxisData, double maxXAxisData) const = 0;
96
100
97 // /////// //
101 // /////// //
98 // Mutexes //
102 // Mutexes //
99 // /////// //
103 // /////// //
100
104
101 virtual void lockRead() = 0;
105 virtual void lockRead() = 0;
102 virtual void lockWrite() = 0;
106 virtual void lockWrite() = 0;
103 virtual void unlock() = 0;
107 virtual void unlock() = 0;
104 };
108 };
105
109
106 // Required for using shared_ptr in signals/slots
110 // Required for using shared_ptr in signals/slots
107 SCIQLOP_REGISTER_META_TYPE(IDATASERIES_PTR_REGISTRY, std::shared_ptr<IDataSeries>)
111 SCIQLOP_REGISTER_META_TYPE(IDATASERIES_PTR_REGISTRY, std::shared_ptr<IDataSeries>)
108
112
109 #endif // SCIQLOP_IDATASERIES_H
113 #endif // SCIQLOP_IDATASERIES_H
General Comments 0
You need to be logged in to leave comments. Login now