##// END OF EJS Templates
Updates merge() method (1)...
Alexandre Leroux -
r668:aaf56376a038
parent child
Show More
@@ -0,0 +1,132
1 #ifndef SCIQLOP_DATASERIESMERGEHELPER_H
2 #define SCIQLOP_DATASERIESMERGEHELPER_H
3
4 template <int Dim>
5 class DataSeries;
6
7 namespace detail {
8
9 /**
10 * Scope that can be used for a merge operation
11 * @tparam FEnd the type of function that will be executed at the end of the scope
12 */
13 template <typename FEnd>
14 struct MergeScope {
15 explicit MergeScope(FEnd end) : m_End{end} {}
16 virtual ~MergeScope() noexcept { m_End(); }
17 FEnd m_End;
18 };
19
20 /**
21 * Creates a scope for merge operation
22 * @tparam end the function executed at the end of the scope
23 */
24 template <typename FEnd>
25 MergeScope<FEnd> scope(FEnd end)
26 {
27 return MergeScope<FEnd>{end};
28 }
29
30 /**
31 * Enum used to position a data series relative to another during a merge operation
32 */
33 enum class MergePosition { LOWER_THAN, GREATER_THAN, EQUAL, OVERLAP };
34
35 /**
36 * Computes the position of the first data series relative to the second data series
37 * @param lhs the first data series
38 * @param rhs the second data series
39 * @return the merge position computed
40 * @remarks the data series must not be empty
41 */
42 template <int Dim>
43 MergePosition mergePosition(DataSeries<Dim> &lhs, DataSeries<Dim> &rhs)
44 {
45 Q_ASSERT(!lhs.isEmpty() && !rhs.isEmpty());
46
47 // Case lhs < rhs
48 auto lhsLast = --lhs.cend();
49 auto rhsFirst = rhs.cbegin();
50 if (lhsLast->x() < rhsFirst->x()) {
51 return MergePosition::LOWER_THAN;
52 }
53
54 // Case lhs > rhs
55 auto lhsFirst = lhs.cbegin();
56 auto rhsLast = --rhs.cend();
57 if (lhsFirst->x() > rhsLast->x()) {
58 return MergePosition::GREATER_THAN;
59 }
60
61 // Other cases
62 auto equal = std::equal(lhs.cbegin(), lhs.cend(), rhs.cbegin(), rhs.cend(),
63 [](const auto &it1, const auto &it2) {
64 return it1.x() == it2.x() && it1.values() == it2.values();
65 });
66 return equal ? MergePosition::EQUAL : MergePosition::OVERLAP;
67 }
68
69 } // namespace detail
70
71
72 /// Helper used to merge two DataSeries
73 /// @sa DataSeries
74 struct DataSeriesMergeHelper {
75 /// Merges the source data series into the dest data series. Data of the source data series are
76 /// consumed
77 template <int Dim>
78 static void merge(DataSeries<Dim> &source, DataSeries<Dim> &dest)
79 {
80 // Creates a scope to clear source data series at the end of the merge
81 auto _ = detail::scope([&source]() { source.clear(); });
82
83 // Case : source data series is empty -> no merge is made
84 if (source.isEmpty()) {
85 return;
86 }
87
88 // Case : dest data series is empty -> we simply swap the data
89 if (dest.isEmpty()) {
90 std::swap(dest.m_XAxisData, source.m_XAxisData);
91 std::swap(dest.m_ValuesData, source.m_ValuesData);
92 return;
93 }
94
95 // Gets the position of the source in relation to the destination
96 auto sourcePosition = detail::mergePosition(source, dest);
97
98 switch (sourcePosition) {
99 case detail::MergePosition::LOWER_THAN:
100 case detail::MergePosition::GREATER_THAN: {
101 auto prepend = sourcePosition == detail::MergePosition::LOWER_THAN;
102 dest.m_XAxisData->add(*source.m_XAxisData, prepend);
103 dest.m_ValuesData->add(*source.m_ValuesData, prepend);
104 break;
105 }
106 case detail::MergePosition::EQUAL:
107 // the data series equal each other : no merge made
108 break;
109 case detail::MergePosition::OVERLAP: {
110 // the two data series overlap : merge is made
111 auto temp = dest.clone();
112 if (auto tempSeries = dynamic_cast<DataSeries<Dim> *>(temp.get())) {
113 // Makes the merge :
114 // - Data are sorted by x-axis values
115 // - If two entries are in the source range and the other range, only one entry
116 // is retained as result
117 // - The results are stored directly in the data series
118 dest.clear();
119 std::set_union(
120 tempSeries->cbegin(), tempSeries->cend(), source.cbegin(), source.cend(),
121 std::back_inserter(dest),
122 [](const auto &it1, const auto &it2) { return it1.x() < it2.x(); });
123 }
124 break;
125 }
126 default:
127 Q_ASSERT(false);
128 }
129 }
130 };
131
132 #endif // SCIQLOP_DATASERIESMERGEHELPER_H
@@ -1,334 +1,297
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 #include <Data/DataSeriesMergeHelper.h>
9 10 #include <Data/IDataSeries.h>
10 11
11 12 #include <QLoggingCategory>
12 13 #include <QReadLocker>
13 14 #include <QReadWriteLock>
14 15 #include <memory>
15 16
16 17 // We don't use the Qt macro since the log is used in the header file, which causes multiple log
17 18 // definitions with inheritance. Inline method is used instead
18 19 inline const QLoggingCategory &LOG_DataSeries()
19 20 {
20 21 static const QLoggingCategory category{"DataSeries"};
21 22 return category;
22 23 }
23 24
24 25 template <int Dim>
25 26 class DataSeries;
26 27
27 28 namespace dataseries_detail {
28 29
29 30 template <int Dim>
30 31 class IteratorValue : public DataSeriesIteratorValue::Impl {
31 32 public:
32 33 explicit IteratorValue(const DataSeries<Dim> &dataSeries, bool begin)
33 34 : m_XIt(begin ? dataSeries.xAxisData()->cbegin() : dataSeries.xAxisData()->cend()),
34 35 m_ValuesIt(begin ? dataSeries.valuesData()->cbegin()
35 36 : dataSeries.valuesData()->cend())
36 37 {
37 38 }
38 39 IteratorValue(const IteratorValue &other) = default;
39 40
40 41 std::unique_ptr<DataSeriesIteratorValue::Impl> clone() const override
41 42 {
42 43 return std::make_unique<IteratorValue<Dim> >(*this);
43 44 }
44 45
45 46 bool equals(const DataSeriesIteratorValue::Impl &other) const override try {
46 47 const auto &otherImpl = dynamic_cast<const IteratorValue &>(other);
47 48 return std::tie(m_XIt, m_ValuesIt) == std::tie(otherImpl.m_XIt, otherImpl.m_ValuesIt);
48 49 }
49 50 catch (const std::bad_cast &) {
50 51 return false;
51 52 }
52 53
53 54 void next() override
54 55 {
55 56 ++m_XIt;
56 57 ++m_ValuesIt;
57 58 }
58 59
59 60 void prev() override
60 61 {
61 62 --m_XIt;
62 63 --m_ValuesIt;
63 64 }
64 65
65 66 double x() const override { return m_XIt->at(0); }
66 67 double value() const override { return m_ValuesIt->at(0); }
67 68 double value(int componentIndex) const override { return m_ValuesIt->at(componentIndex); }
68 69 double minValue() const override { return m_ValuesIt->min(); }
69 70 double maxValue() const override { return m_ValuesIt->max(); }
70 71 QVector<double> values() const override { return m_ValuesIt->values(); }
71 72
72 73 private:
73 74 ArrayDataIterator m_XIt;
74 75 ArrayDataIterator m_ValuesIt;
75 76 };
76 77 } // namespace dataseries_detail
77 78
78 79 /**
79 80 * @brief The DataSeries class is the base (abstract) implementation of IDataSeries.
80 81 *
81 82 * It proposes to set a dimension for the values ​​data.
82 83 *
83 84 * A DataSeries is always sorted on its x-axis data.
84 85 *
85 86 * @tparam Dim The dimension of the values data
86 87 *
87 88 */
88 89 template <int Dim>
89 90 class SCIQLOP_CORE_EXPORT DataSeries : public IDataSeries {
91 friend class DataSeriesMergeHelper;
92
90 93 public:
91 94 /// @sa IDataSeries::xAxisData()
92 95 std::shared_ptr<ArrayData<1> > xAxisData() override { return m_XAxisData; }
93 96 const std::shared_ptr<ArrayData<1> > xAxisData() const { return m_XAxisData; }
94 97
95 98 /// @sa IDataSeries::xAxisUnit()
96 99 Unit xAxisUnit() const override { return m_XAxisUnit; }
97 100
98 101 /// @return the values dataset
99 102 std::shared_ptr<ArrayData<Dim> > valuesData() { return m_ValuesData; }
100 103 const std::shared_ptr<ArrayData<Dim> > valuesData() const { return m_ValuesData; }
101 104
102 105 /// @sa IDataSeries::valuesUnit()
103 106 Unit valuesUnit() const override { return m_ValuesUnit; }
104 107
105 108
106 109 SqpRange range() const override
107 110 {
108 111 if (!m_XAxisData->cdata().isEmpty()) {
109 112 return SqpRange{m_XAxisData->cdata().first(), m_XAxisData->cdata().last()};
110 113 }
111 114
112 115 return SqpRange{};
113 116 }
114 117
115 118 void clear()
116 119 {
117 120 m_XAxisData->clear();
118 121 m_ValuesData->clear();
119 122 }
120 123
124 bool isEmpty() const noexcept { return m_XAxisData->size() == 0; }
125
121 126 /// Merges into the data series an other data series
122 127 /// @remarks the data series to merge with is cleared after the operation
123 128 void merge(IDataSeries *dataSeries) override
124 129 {
125 130 dataSeries->lockWrite();
126 131 lockWrite();
127 132
128 133 if (auto other = dynamic_cast<DataSeries<Dim> *>(dataSeries)) {
129 const auto &otherXAxisData = other->xAxisData()->cdata();
130 const auto &xAxisData = m_XAxisData->cdata();
131
132 // As data series are sorted, we can improve performances of merge, by call the sort
133 // method only if the two data series overlap.
134 if (!otherXAxisData.empty()) {
135 auto firstValue = otherXAxisData.front();
136 auto lastValue = otherXAxisData.back();
137
138 auto xAxisDataBegin = xAxisData.cbegin();
139 auto xAxisDataEnd = xAxisData.cend();
140
141 bool prepend;
142 bool sortNeeded;
143
144 if (std::lower_bound(xAxisDataBegin, xAxisDataEnd, firstValue) == xAxisDataEnd) {
145 // Other data series if after data series
146 prepend = false;
147 sortNeeded = false;
148 }
149 else if (std::upper_bound(xAxisDataBegin, xAxisDataEnd, lastValue)
150 == xAxisDataBegin) {
151 // Other data series if before data series
152 prepend = true;
153 sortNeeded = false;
154 }
155 else {
156 // The two data series overlap
157 prepend = false;
158 sortNeeded = true;
159 }
160
161 // Makes the merge
162 m_XAxisData->add(*other->xAxisData(), prepend);
163 m_ValuesData->add(*other->valuesData(), prepend);
164
165 if (sortNeeded) {
166 sort();
167 }
168 }
169
170 // Clears the other data series
171 other->clear();
134 DataSeriesMergeHelper::merge(*other, *this);
172 135 }
173 136 else {
174 137 qCWarning(LOG_DataSeries())
175 138 << QObject::tr("Detection of a type of IDataSeries we cannot merge with !");
176 139 }
177 140 unlock();
178 141 dataSeries->unlock();
179 142 }
180 143
181 144 // ///////// //
182 145 // Iterators //
183 146 // ///////// //
184 147
185 148 DataSeriesIterator cbegin() const override
186 149 {
187 150 return DataSeriesIterator{DataSeriesIteratorValue{
188 151 std::make_unique<dataseries_detail::IteratorValue<Dim> >(*this, true)}};
189 152 }
190 153
191 154 DataSeriesIterator cend() const override
192 155 {
193 156 return DataSeriesIterator{DataSeriesIteratorValue{
194 157 std::make_unique<dataseries_detail::IteratorValue<Dim> >(*this, false)}};
195 158 }
196 159
197 160 /// @sa IDataSeries::minXAxisData()
198 161 DataSeriesIterator minXAxisData(double minXAxisData) const override
199 162 {
200 163 return std::lower_bound(
201 164 cbegin(), cend(), minXAxisData,
202 165 [](const auto &itValue, const auto &value) { return itValue.x() < value; });
203 166 }
204 167
205 168 /// @sa IDataSeries::maxXAxisData()
206 169 DataSeriesIterator maxXAxisData(double maxXAxisData) const override
207 170 {
208 171 // Gets the first element that greater than max value
209 172 auto it = std::upper_bound(
210 173 cbegin(), cend(), maxXAxisData,
211 174 [](const auto &value, const auto &itValue) { return value < itValue.x(); });
212 175
213 176 return it == cbegin() ? cend() : --it;
214 177 }
215 178
216 179 std::pair<DataSeriesIterator, DataSeriesIterator> xAxisRange(double minXAxisData,
217 180 double maxXAxisData) const override
218 181 {
219 182 if (minXAxisData > maxXAxisData) {
220 183 std::swap(minXAxisData, maxXAxisData);
221 184 }
222 185
223 186 auto begin = cbegin();
224 187 auto end = cend();
225 188
226 189 auto lowerIt = std::lower_bound(
227 190 begin, end, minXAxisData,
228 191 [](const auto &itValue, const auto &value) { return itValue.x() < value; });
229 192 auto upperIt = std::upper_bound(
230 193 begin, end, maxXAxisData,
231 194 [](const auto &value, const auto &itValue) { return value < itValue.x(); });
232 195
233 196 return std::make_pair(lowerIt, upperIt);
234 197 }
235 198
236 199 std::pair<DataSeriesIterator, DataSeriesIterator>
237 200 valuesBounds(double minXAxisData, double maxXAxisData) const override
238 201 {
239 202 // Places iterators to the correct x-axis range
240 203 auto xAxisRangeIts = xAxisRange(minXAxisData, maxXAxisData);
241 204
242 205 // Returns end iterators if the range is empty
243 206 if (xAxisRangeIts.first == xAxisRangeIts.second) {
244 207 return std::make_pair(cend(), cend());
245 208 }
246 209
247 210 // Gets the iterator on the min of all values data
248 211 auto minIt = std::min_element(
249 212 xAxisRangeIts.first, xAxisRangeIts.second, [](const auto &it1, const auto &it2) {
250 213 return SortUtils::minCompareWithNaN(it1.minValue(), it2.minValue());
251 214 });
252 215
253 216 // Gets the iterator on the max of all values data
254 217 auto maxIt = std::max_element(
255 218 xAxisRangeIts.first, xAxisRangeIts.second, [](const auto &it1, const auto &it2) {
256 219 return SortUtils::maxCompareWithNaN(it1.maxValue(), it2.maxValue());
257 220 });
258 221
259 222 return std::make_pair(minIt, maxIt);
260 223 }
261 224
262 225 // /////// //
263 226 // Mutexes //
264 227 // /////// //
265 228
266 229 virtual void lockRead() { m_Lock.lockForRead(); }
267 230 virtual void lockWrite() { m_Lock.lockForWrite(); }
268 231 virtual void unlock() { m_Lock.unlock(); }
269 232
270 233 protected:
271 234 /// Protected ctor (DataSeries is abstract). The vectors must have the same size, otherwise a
272 235 /// DataSeries with no values will be created.
273 236 /// @remarks data series is automatically sorted on its x-axis data
274 237 explicit DataSeries(std::shared_ptr<ArrayData<1> > xAxisData, const Unit &xAxisUnit,
275 238 std::shared_ptr<ArrayData<Dim> > valuesData, const Unit &valuesUnit)
276 239 : m_XAxisData{xAxisData},
277 240 m_XAxisUnit{xAxisUnit},
278 241 m_ValuesData{valuesData},
279 242 m_ValuesUnit{valuesUnit}
280 243 {
281 244 if (m_XAxisData->size() != m_ValuesData->size()) {
282 245 clear();
283 246 }
284 247
285 248 // Sorts data if it's not the case
286 249 const auto &xAxisCData = m_XAxisData->cdata();
287 250 if (!std::is_sorted(xAxisCData.cbegin(), xAxisCData.cend())) {
288 251 sort();
289 252 }
290 253 }
291 254
292 255 /// Copy ctor
293 256 explicit DataSeries(const DataSeries<Dim> &other)
294 257 : m_XAxisData{std::make_shared<ArrayData<1> >(*other.m_XAxisData)},
295 258 m_XAxisUnit{other.m_XAxisUnit},
296 259 m_ValuesData{std::make_shared<ArrayData<Dim> >(*other.m_ValuesData)},
297 260 m_ValuesUnit{other.m_ValuesUnit}
298 261 {
299 262 // Since a series is ordered from its construction and is always ordered, it is not
300 263 // necessary to call the sort method here ('other' is sorted)
301 264 }
302 265
303 266 /// Assignment operator
304 267 template <int D>
305 268 DataSeries &operator=(DataSeries<D> other)
306 269 {
307 270 std::swap(m_XAxisData, other.m_XAxisData);
308 271 std::swap(m_XAxisUnit, other.m_XAxisUnit);
309 272 std::swap(m_ValuesData, other.m_ValuesData);
310 273 std::swap(m_ValuesUnit, other.m_ValuesUnit);
311 274
312 275 return *this;
313 276 }
314 277
315 278 private:
316 279 /**
317 280 * Sorts data series on its x-axis data
318 281 */
319 282 void sort() noexcept
320 283 {
321 284 auto permutation = SortUtils::sortPermutation(*m_XAxisData, std::less<double>());
322 285 m_XAxisData = m_XAxisData->sort(permutation);
323 286 m_ValuesData = m_ValuesData->sort(permutation);
324 287 }
325 288
326 289 std::shared_ptr<ArrayData<1> > m_XAxisData;
327 290 Unit m_XAxisUnit;
328 291 std::shared_ptr<ArrayData<Dim> > m_ValuesData;
329 292 Unit m_ValuesUnit;
330 293
331 294 QReadWriteLock m_Lock;
332 295 };
333 296
334 297 #endif // SCIQLOP_DATASERIES_H
General Comments 0
You need to be logged in to leave comments. Login now