##// END OF EJS Templates
Uses new iterator in DataSeries.cpp...
Alexandre Leroux -
r558:96f73c42ec59
parent child
Show More
@@ -1,293 +1,287
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>
25 class DataSeries;
26
27 namespace dataseries_detail {
28
29 template <int Dim>
30 class IteratorValue : public DataSeriesIteratorValue::Impl {
31 public:
32 explicit IteratorValue(const DataSeries<Dim> &dataSeries, bool begin)
33 : m_XIt(begin ? dataSeries.xAxisData()->cbegin() : dataSeries.xAxisData()->cend()),
34 m_ValuesIt(begin ? dataSeries.valuesData()->cbegin()
35 : dataSeries.valuesData()->cend())
36 {
37 }
38 IteratorValue(const IteratorValue &other) = default;
39
40 std::unique_ptr<DataSeriesIteratorValue::Impl> clone() const override
41 {
42 return std::make_unique<IteratorValue<Dim> >(*this);
43 }
44
45 bool equals(const DataSeriesIteratorValue::Impl &other) const override try {
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);
48 }
49 catch (const std::bad_cast &) {
50 return false;
51 }
52
53 void next() override
54 {
55 ++m_XIt;
56 ++m_ValuesIt;
57 }
58
59 void prev() override
60 {
61 --m_XIt;
62 --m_ValuesIt;
63 }
64
65 double x() const override { return m_XIt->at(0); }
66 double value() const override { return m_ValuesIt->at(0); }
67 double value(int componentIndex) const override { return m_ValuesIt->at(componentIndex); }
68
69 private:
70 ArrayData<1>::Iterator m_XIt;
71 typename ArrayData<Dim>::Iterator m_ValuesIt;
72 };
73 } // namespace dataseries_detail
74
24 /**
75 /**
25 * @brief The DataSeries class is the base (abstract) implementation of IDataSeries.
76 * @brief The DataSeries class is the base (abstract) implementation of IDataSeries.
26 *
77 *
27 * It proposes to set a dimension for the values ​​data.
78 * It proposes to set a dimension for the values ​​data.
28 *
79 *
29 * A DataSeries is always sorted on its x-axis data.
80 * A DataSeries is always sorted on its x-axis data.
30 *
81 *
31 * @tparam Dim The dimension of the values data
82 * @tparam Dim The dimension of the values data
32 *
83 *
33 */
84 */
34 template <int Dim>
85 template <int Dim>
35 class SCIQLOP_CORE_EXPORT DataSeries : public IDataSeries {
86 class SCIQLOP_CORE_EXPORT DataSeries : public IDataSeries {
36 public:
87 public:
37 class IteratorValue {
38 public:
39 explicit IteratorValue(const DataSeries &dataSeries, bool begin)
40 : m_XIt(begin ? dataSeries.xAxisData()->cbegin() : dataSeries.xAxisData()->cend()),
41 m_ValuesIt(begin ? dataSeries.valuesData()->cbegin()
42 : dataSeries.valuesData()->cend())
43 {
44 }
45
46 double x() const { return m_XIt->at(0); }
47 double value() const { return m_ValuesIt->at(0); }
48 double value(int componentIndex) const { return m_ValuesIt->at(componentIndex); }
49
50 void next()
51 {
52 ++m_XIt;
53 ++m_ValuesIt;
54 }
55
56 bool operator==(const IteratorValue &other) const
57 {
58 return std::tie(m_XIt, m_ValuesIt) == std::tie(other.m_XIt, other.m_ValuesIt);
59 }
60
61 private:
62 ArrayData<1>::Iterator m_XIt;
63 typename ArrayData<Dim>::Iterator m_ValuesIt;
64 };
65
66 class Iterator {
67 public:
68 using iterator_category = std::forward_iterator_tag;
69 using value_type = const IteratorValue;
70 using difference_type = std::ptrdiff_t;
71 using pointer = value_type *;
72 using reference = value_type &;
73
74 Iterator(const DataSeries &dataSeries, bool begin) : m_CurrentValue{dataSeries, begin} {}
75 virtual ~Iterator() noexcept = default;
76 Iterator(const Iterator &) = default;
77 Iterator(Iterator &&) = default;
78 Iterator &operator=(const Iterator &) = default;
79 Iterator &operator=(Iterator &&) = default;
80
81 Iterator &operator++()
82 {
83 m_CurrentValue.next();
84 return *this;
85 }
86
87 pointer operator->() const { return &m_CurrentValue; }
88
89 reference operator*() const { return m_CurrentValue; }
90
91 bool operator==(const Iterator &other) const
92 {
93 return m_CurrentValue == other.m_CurrentValue;
94 }
95
96 bool operator!=(const Iterator &other) const { return !(*this == other); }
97
98 private:
99 IteratorValue m_CurrentValue;
100 };
101
102 /// @sa IDataSeries::xAxisData()
88 /// @sa IDataSeries::xAxisData()
103 std::shared_ptr<ArrayData<1> > xAxisData() override { return m_XAxisData; }
89 std::shared_ptr<ArrayData<1> > xAxisData() override { return m_XAxisData; }
104 const std::shared_ptr<ArrayData<1> > xAxisData() const { return m_XAxisData; }
90 const std::shared_ptr<ArrayData<1> > xAxisData() const { return m_XAxisData; }
105
91
106 /// @sa IDataSeries::xAxisUnit()
92 /// @sa IDataSeries::xAxisUnit()
107 Unit xAxisUnit() const override { return m_XAxisUnit; }
93 Unit xAxisUnit() const override { return m_XAxisUnit; }
108
94
109 /// @return the values dataset
95 /// @return the values dataset
110 std::shared_ptr<ArrayData<Dim> > valuesData() { return m_ValuesData; }
96 std::shared_ptr<ArrayData<Dim> > valuesData() { return m_ValuesData; }
111 const std::shared_ptr<ArrayData<Dim> > valuesData() const { return m_ValuesData; }
97 const std::shared_ptr<ArrayData<Dim> > valuesData() const { return m_ValuesData; }
112
98
113 /// @sa IDataSeries::valuesUnit()
99 /// @sa IDataSeries::valuesUnit()
114 Unit valuesUnit() const override { return m_ValuesUnit; }
100 Unit valuesUnit() const override { return m_ValuesUnit; }
115
101
116
102
117 SqpRange range() const override
103 SqpRange range() const override
118 {
104 {
119 if (!m_XAxisData->cdata().isEmpty()) {
105 if (!m_XAxisData->cdata().isEmpty()) {
120 return SqpRange{m_XAxisData->cdata().first(), m_XAxisData->cdata().last()};
106 return SqpRange{m_XAxisData->cdata().first(), m_XAxisData->cdata().last()};
121 }
107 }
122
108
123 return SqpRange{};
109 return SqpRange{};
124 }
110 }
125
111
126 void clear()
112 void clear()
127 {
113 {
128 m_XAxisData->clear();
114 m_XAxisData->clear();
129 m_ValuesData->clear();
115 m_ValuesData->clear();
130 }
116 }
131
117
132 /// Merges into the data series an other data series
118 /// Merges into the data series an other data series
133 /// @remarks the data series to merge with is cleared after the operation
119 /// @remarks the data series to merge with is cleared after the operation
134 void merge(IDataSeries *dataSeries) override
120 void merge(IDataSeries *dataSeries) override
135 {
121 {
136 dataSeries->lockWrite();
122 dataSeries->lockWrite();
137 lockWrite();
123 lockWrite();
138
124
139 if (auto other = dynamic_cast<DataSeries<Dim> *>(dataSeries)) {
125 if (auto other = dynamic_cast<DataSeries<Dim> *>(dataSeries)) {
140 const auto &otherXAxisData = other->xAxisData()->cdata();
126 const auto &otherXAxisData = other->xAxisData()->cdata();
141 const auto &xAxisData = m_XAxisData->cdata();
127 const auto &xAxisData = m_XAxisData->cdata();
142
128
143 // As data series are sorted, we can improve performances of merge, by call the sort
129 // As data series are sorted, we can improve performances of merge, by call the sort
144 // method only if the two data series overlap.
130 // method only if the two data series overlap.
145 if (!otherXAxisData.empty()) {
131 if (!otherXAxisData.empty()) {
146 auto firstValue = otherXAxisData.front();
132 auto firstValue = otherXAxisData.front();
147 auto lastValue = otherXAxisData.back();
133 auto lastValue = otherXAxisData.back();
148
134
149 auto xAxisDataBegin = xAxisData.cbegin();
135 auto xAxisDataBegin = xAxisData.cbegin();
150 auto xAxisDataEnd = xAxisData.cend();
136 auto xAxisDataEnd = xAxisData.cend();
151
137
152 bool prepend;
138 bool prepend;
153 bool sortNeeded;
139 bool sortNeeded;
154
140
155 if (std::lower_bound(xAxisDataBegin, xAxisDataEnd, firstValue) == xAxisDataEnd) {
141 if (std::lower_bound(xAxisDataBegin, xAxisDataEnd, firstValue) == xAxisDataEnd) {
156 // Other data series if after data series
142 // Other data series if after data series
157 prepend = false;
143 prepend = false;
158 sortNeeded = false;
144 sortNeeded = false;
159 }
145 }
160 else if (std::upper_bound(xAxisDataBegin, xAxisDataEnd, lastValue)
146 else if (std::upper_bound(xAxisDataBegin, xAxisDataEnd, lastValue)
161 == xAxisDataBegin) {
147 == xAxisDataBegin) {
162 // Other data series if before data series
148 // Other data series if before data series
163 prepend = true;
149 prepend = true;
164 sortNeeded = false;
150 sortNeeded = false;
165 }
151 }
166 else {
152 else {
167 // The two data series overlap
153 // The two data series overlap
168 prepend = false;
154 prepend = false;
169 sortNeeded = true;
155 sortNeeded = true;
170 }
156 }
171
157
172 // Makes the merge
158 // Makes the merge
173 m_XAxisData->add(*other->xAxisData(), prepend);
159 m_XAxisData->add(*other->xAxisData(), prepend);
174 m_ValuesData->add(*other->valuesData(), prepend);
160 m_ValuesData->add(*other->valuesData(), prepend);
175
161
176 if (sortNeeded) {
162 if (sortNeeded) {
177 sort();
163 sort();
178 }
164 }
179 }
165 }
180
166
181 // Clears the other data series
167 // Clears the other data series
182 other->clear();
168 other->clear();
183 }
169 }
184 else {
170 else {
185 qCWarning(LOG_DataSeries())
171 qCWarning(LOG_DataSeries())
186 << QObject::tr("Detection of a type of IDataSeries we cannot merge with !");
172 << QObject::tr("Detection of a type of IDataSeries we cannot merge with !");
187 }
173 }
188 unlock();
174 unlock();
189 dataSeries->unlock();
175 dataSeries->unlock();
190 }
176 }
191
177
192 // ///////// //
178 // ///////// //
193 // Iterators //
179 // Iterators //
194 // ///////// //
180 // ///////// //
195
181
196 Iterator cbegin() const { return Iterator{*this, true}; }
182 DataSeriesIterator cbegin() const override
183 {
184 return DataSeriesIterator{DataSeriesIteratorValue{
185 std::make_unique<dataseries_detail::IteratorValue<Dim> >(*this, true)}};
186 }
197
187
198 Iterator cend() const { return Iterator{*this, false}; }
188 DataSeriesIterator cend() const override
189 {
190 return DataSeriesIterator{DataSeriesIteratorValue{
191 std::make_unique<dataseries_detail::IteratorValue<Dim> >(*this, false)}};
192 }
199
193
200 std::pair<Iterator, Iterator> subData(double min, double max) const
194 std::pair<Iterator, Iterator> subData(double min, double max) const
201 {
195 {
202 if (min > max) {
196 if (min > max) {
203 std::swap(min, max);
197 std::swap(min, max);
204 }
198 }
205
199
206 auto begin = cbegin();
200 auto begin = cbegin();
207 auto end = cend();
201 auto end = cend();
208
202
209 auto lowerIt
203 auto lowerIt
210 = std::lower_bound(begin, end, min, [](const auto &itValue, const auto &value) {
204 = std::lower_bound(begin, end, min, [](const auto &itValue, const auto &value) {
211 return itValue.x() < value;
205 return itValue.x() < value;
212 });
206 });
213 auto upperIt
207 auto upperIt
214 = std::upper_bound(begin, end, max, [](const auto &value, const auto &itValue) {
208 = std::upper_bound(begin, end, max, [](const auto &value, const auto &itValue) {
215 return value < itValue.x();
209 return value < itValue.x();
216 });
210 });
217
211
218 return std::make_pair(lowerIt, upperIt);
212 return std::make_pair(lowerIt, upperIt);
219 }
213 }
220
214
221 // /////// //
215 // /////// //
222 // Mutexes //
216 // Mutexes //
223 // /////// //
217 // /////// //
224
218
225 virtual void lockRead() { m_Lock.lockForRead(); }
219 virtual void lockRead() { m_Lock.lockForRead(); }
226 virtual void lockWrite() { m_Lock.lockForWrite(); }
220 virtual void lockWrite() { m_Lock.lockForWrite(); }
227 virtual void unlock() { m_Lock.unlock(); }
221 virtual void unlock() { m_Lock.unlock(); }
228
222
229 protected:
223 protected:
230 /// Protected ctor (DataSeries is abstract). The vectors must have the same size, otherwise a
224 /// Protected ctor (DataSeries is abstract). The vectors must have the same size, otherwise a
231 /// DataSeries with no values will be created.
225 /// DataSeries with no values will be created.
232 /// @remarks data series is automatically sorted on its x-axis data
226 /// @remarks data series is automatically sorted on its x-axis data
233 explicit DataSeries(std::shared_ptr<ArrayData<1> > xAxisData, const Unit &xAxisUnit,
227 explicit DataSeries(std::shared_ptr<ArrayData<1> > xAxisData, const Unit &xAxisUnit,
234 std::shared_ptr<ArrayData<Dim> > valuesData, const Unit &valuesUnit)
228 std::shared_ptr<ArrayData<Dim> > valuesData, const Unit &valuesUnit)
235 : m_XAxisData{xAxisData},
229 : m_XAxisData{xAxisData},
236 m_XAxisUnit{xAxisUnit},
230 m_XAxisUnit{xAxisUnit},
237 m_ValuesData{valuesData},
231 m_ValuesData{valuesData},
238 m_ValuesUnit{valuesUnit}
232 m_ValuesUnit{valuesUnit}
239 {
233 {
240 if (m_XAxisData->size() != m_ValuesData->size()) {
234 if (m_XAxisData->size() != m_ValuesData->size()) {
241 clear();
235 clear();
242 }
236 }
243
237
244 // Sorts data if it's not the case
238 // Sorts data if it's not the case
245 const auto &xAxisCData = m_XAxisData->cdata();
239 const auto &xAxisCData = m_XAxisData->cdata();
246 if (!std::is_sorted(xAxisCData.cbegin(), xAxisCData.cend())) {
240 if (!std::is_sorted(xAxisCData.cbegin(), xAxisCData.cend())) {
247 sort();
241 sort();
248 }
242 }
249 }
243 }
250
244
251 /// Copy ctor
245 /// Copy ctor
252 explicit DataSeries(const DataSeries<Dim> &other)
246 explicit DataSeries(const DataSeries<Dim> &other)
253 : m_XAxisData{std::make_shared<ArrayData<1> >(*other.m_XAxisData)},
247 : m_XAxisData{std::make_shared<ArrayData<1> >(*other.m_XAxisData)},
254 m_XAxisUnit{other.m_XAxisUnit},
248 m_XAxisUnit{other.m_XAxisUnit},
255 m_ValuesData{std::make_shared<ArrayData<Dim> >(*other.m_ValuesData)},
249 m_ValuesData{std::make_shared<ArrayData<Dim> >(*other.m_ValuesData)},
256 m_ValuesUnit{other.m_ValuesUnit}
250 m_ValuesUnit{other.m_ValuesUnit}
257 {
251 {
258 // Since a series is ordered from its construction and is always ordered, it is not
252 // Since a series is ordered from its construction and is always ordered, it is not
259 // necessary to call the sort method here ('other' is sorted)
253 // necessary to call the sort method here ('other' is sorted)
260 }
254 }
261
255
262 /// Assignment operator
256 /// Assignment operator
263 template <int D>
257 template <int D>
264 DataSeries &operator=(DataSeries<D> other)
258 DataSeries &operator=(DataSeries<D> other)
265 {
259 {
266 std::swap(m_XAxisData, other.m_XAxisData);
260 std::swap(m_XAxisData, other.m_XAxisData);
267 std::swap(m_XAxisUnit, other.m_XAxisUnit);
261 std::swap(m_XAxisUnit, other.m_XAxisUnit);
268 std::swap(m_ValuesData, other.m_ValuesData);
262 std::swap(m_ValuesData, other.m_ValuesData);
269 std::swap(m_ValuesUnit, other.m_ValuesUnit);
263 std::swap(m_ValuesUnit, other.m_ValuesUnit);
270
264
271 return *this;
265 return *this;
272 }
266 }
273
267
274 private:
268 private:
275 /**
269 /**
276 * Sorts data series on its x-axis data
270 * Sorts data series on its x-axis data
277 */
271 */
278 void sort() noexcept
272 void sort() noexcept
279 {
273 {
280 auto permutation = SortUtils::sortPermutation(*m_XAxisData, std::less<double>());
274 auto permutation = SortUtils::sortPermutation(*m_XAxisData, std::less<double>());
281 m_XAxisData = m_XAxisData->sort(permutation);
275 m_XAxisData = m_XAxisData->sort(permutation);
282 m_ValuesData = m_ValuesData->sort(permutation);
276 m_ValuesData = m_ValuesData->sort(permutation);
283 }
277 }
284
278
285 std::shared_ptr<ArrayData<1> > m_XAxisData;
279 std::shared_ptr<ArrayData<1> > m_XAxisData;
286 Unit m_XAxisUnit;
280 Unit m_XAxisUnit;
287 std::shared_ptr<ArrayData<Dim> > m_ValuesData;
281 std::shared_ptr<ArrayData<Dim> > m_ValuesData;
288 Unit m_ValuesUnit;
282 Unit m_ValuesUnit;
289
283
290 QReadWriteLock m_Lock;
284 QReadWriteLock m_Lock;
291 };
285 };
292
286
293 #endif // SCIQLOP_DATASERIES_H
287 #endif // SCIQLOP_DATASERIES_H
General Comments 0
You need to be logged in to leave comments. Login now