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