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