##// END OF EJS Templates
Merge branch 'feature/MergeFix' into develop
Alexandre Leroux -
r671:2d8c14c32b86 merge
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
@@ -87,10 +87,9 public:
87 return it != end ? *it : std::numeric_limits<double>::quiet_NaN();
87 return it != end ? *it : std::numeric_limits<double>::quiet_NaN();
88 }
88 }
89
89
90 private:
90 QVector<double> values() const override
91 std::vector<double> values() const
92 {
91 {
93 auto result = std::vector<double>{};
92 auto result = QVector<double>{};
94 for (auto i = 0; i < m_NbComponents; ++i) {
93 for (auto i = 0; i < m_NbComponents; ++i) {
95 result.push_back(*(m_It + i));
94 result.push_back(*(m_It + i));
96 }
95 }
@@ -98,6 +97,7 private:
98 return result;
97 return result;
99 }
98 }
100
99
100 private:
101 DataContainer::const_iterator m_It;
101 DataContainer::const_iterator m_It;
102 int m_NbComponents;
102 int m_NbComponents;
103 };
103 };
@@ -235,6 +235,17 public:
235 m_Data, m_NbComponents, false)}};
235 m_Data, m_NbComponents, false)}};
236 }
236 }
237
237
238 /// Inserts at the end of the array data the values passed as a parameter. This
239 /// method is intended to be used in the context of generating a back insert iterator, or only
240 /// if it's ensured that the total size of the vector is consistent with the number of
241 /// components of the array data
242 /// @param values the values to insert
243 /// @sa http://en.cppreference.com/w/cpp/iterator/back_inserter
244 void push_back(const QVector<double> &values)
245 {
246 Q_ASSERT(values.size() % m_NbComponents == 0);
247 m_Data.append(values);
248 }
238
249
239 /**
250 /**
240 * @return the data at a specified index
251 * @return the data at a specified index
@@ -4,6 +4,7
4 #include "CoreGlobal.h"
4 #include "CoreGlobal.h"
5 #include "Data/SqpIterator.h"
5 #include "Data/SqpIterator.h"
6
6
7 #include <QVector>
7 #include <memory>
8 #include <memory>
8
9
9 /**
10 /**
@@ -25,6 +26,7 public:
25 virtual double first() const = 0;
26 virtual double first() const = 0;
26 virtual double min() const = 0;
27 virtual double min() const = 0;
27 virtual double max() const = 0;
28 virtual double max() const = 0;
29 virtual QVector<double> values() const = 0;
28 };
30 };
29
31
30 explicit ArrayDataIteratorValue(std::unique_ptr<Impl> impl);
32 explicit ArrayDataIteratorValue(std::unique_ptr<Impl> impl);
@@ -46,6 +48,8 public:
46 double min() const;
48 double min() const;
47 /// Gets max value among all components
49 /// Gets max value among all components
48 double max() const;
50 double max() const;
51 /// Gets all values
52 QVector<double> values() const;
49
53
50 private:
54 private:
51 std::unique_ptr<Impl> m_Impl;
55 std::unique_ptr<Impl> m_Impl;
@@ -6,6 +6,7
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/IDataSeries.h>
10 #include <Data/IDataSeries.h>
10
11
11 #include <QLoggingCategory>
12 #include <QLoggingCategory>
@@ -67,6 +68,7 public:
67 double value(int componentIndex) const override { return m_ValuesIt->at(componentIndex); }
68 double value(int componentIndex) const override { return m_ValuesIt->at(componentIndex); }
68 double minValue() const override { return m_ValuesIt->min(); }
69 double minValue() const override { return m_ValuesIt->min(); }
69 double maxValue() const override { return m_ValuesIt->max(); }
70 double maxValue() const override { return m_ValuesIt->max(); }
71 QVector<double> values() const override { return m_ValuesIt->values(); }
70
72
71 private:
73 private:
72 ArrayDataIterator m_XIt;
74 ArrayDataIterator m_XIt;
@@ -86,7 +88,13 private:
86 */
88 */
87 template <int Dim>
89 template <int Dim>
88 class SCIQLOP_CORE_EXPORT DataSeries : public IDataSeries {
90 class SCIQLOP_CORE_EXPORT DataSeries : public IDataSeries {
91 friend class DataSeriesMergeHelper;
92
89 public:
93 public:
94 /// Tag needed to define the push_back() method
95 /// @sa push_back()
96 using value_type = DataSeriesIteratorValue;
97
90 /// @sa IDataSeries::xAxisData()
98 /// @sa IDataSeries::xAxisData()
91 std::shared_ptr<ArrayData<1> > xAxisData() override { return m_XAxisData; }
99 std::shared_ptr<ArrayData<1> > xAxisData() override { return m_XAxisData; }
92 const std::shared_ptr<ArrayData<1> > xAxisData() const { return m_XAxisData; }
100 const std::shared_ptr<ArrayData<1> > xAxisData() const { return m_XAxisData; }
@@ -117,6 +125,8 public:
117 m_ValuesData->clear();
125 m_ValuesData->clear();
118 }
126 }
119
127
128 bool isEmpty() const noexcept { return m_XAxisData->size() == 0; }
129
120 /// Merges into the data series an other data series
130 /// Merges into the data series an other data series
121 /// @remarks the data series to merge with is cleared after the operation
131 /// @remarks the data series to merge with is cleared after the operation
122 void merge(IDataSeries *dataSeries) override
132 void merge(IDataSeries *dataSeries) override
@@ -125,49 +135,7 public:
125 lockWrite();
135 lockWrite();
126
136
127 if (auto other = dynamic_cast<DataSeries<Dim> *>(dataSeries)) {
137 if (auto other = dynamic_cast<DataSeries<Dim> *>(dataSeries)) {
128 const auto &otherXAxisData = other->xAxisData()->cdata();
138 DataSeriesMergeHelper::merge(*other, *this);
129 const auto &xAxisData = m_XAxisData->cdata();
130
131 // As data series are sorted, we can improve performances of merge, by call the sort
132 // method only if the two data series overlap.
133 if (!otherXAxisData.empty()) {
134 auto firstValue = otherXAxisData.front();
135 auto lastValue = otherXAxisData.back();
136
137 auto xAxisDataBegin = xAxisData.cbegin();
138 auto xAxisDataEnd = xAxisData.cend();
139
140 bool prepend;
141 bool sortNeeded;
142
143 if (std::lower_bound(xAxisDataBegin, xAxisDataEnd, firstValue) == xAxisDataEnd) {
144 // Other data series if after data series
145 prepend = false;
146 sortNeeded = false;
147 }
148 else if (std::upper_bound(xAxisDataBegin, xAxisDataEnd, lastValue)
149 == xAxisDataBegin) {
150 // Other data series if before data series
151 prepend = true;
152 sortNeeded = false;
153 }
154 else {
155 // The two data series overlap
156 prepend = false;
157 sortNeeded = true;
158 }
159
160 // Makes the merge
161 m_XAxisData->add(*other->xAxisData(), prepend);
162 m_ValuesData->add(*other->valuesData(), prepend);
163
164 if (sortNeeded) {
165 sort();
166 }
167 }
168
169 // Clears the other data series
170 other->clear();
171 }
139 }
172 else {
140 else {
173 qCWarning(LOG_DataSeries())
141 qCWarning(LOG_DataSeries())
@@ -266,6 +234,22 public:
266 virtual void lockWrite() { m_Lock.lockForWrite(); }
234 virtual void lockWrite() { m_Lock.lockForWrite(); }
267 virtual void unlock() { m_Lock.unlock(); }
235 virtual void unlock() { m_Lock.unlock(); }
268
236
237 // ///// //
238 // Other //
239 // ///// //
240
241 /// Inserts at the end of the data series the value of the iterator passed as a parameter. This
242 /// method is intended to be used in the context of generating a back insert iterator
243 /// @param iteratorValue the iterator value containing the values to insert
244 /// @sa http://en.cppreference.com/w/cpp/iterator/back_inserter
245 /// @sa merge()
246 /// @sa value_type
247 void push_back(const value_type &iteratorValue)
248 {
249 m_XAxisData->push_back(QVector<double>{iteratorValue.x()});
250 m_ValuesData->push_back(iteratorValue.values());
251 }
252
269 protected:
253 protected:
270 /// Protected ctor (DataSeries is abstract). The vectors must have the same size, otherwise a
254 /// Protected ctor (DataSeries is abstract). The vectors must have the same size, otherwise a
271 /// DataSeries with no values will be created.
255 /// DataSeries with no values will be created.
@@ -4,6 +4,7
4 #include "CoreGlobal.h"
4 #include "CoreGlobal.h"
5 #include "Data/SqpIterator.h"
5 #include "Data/SqpIterator.h"
6
6
7 #include <QVector>
7 #include <memory>
8 #include <memory>
8
9
9 /**
10 /**
@@ -27,6 +28,7 public:
27 virtual double value(int componentIndex) const = 0;
28 virtual double value(int componentIndex) const = 0;
28 virtual double minValue() const = 0;
29 virtual double minValue() const = 0;
29 virtual double maxValue() const = 0;
30 virtual double maxValue() const = 0;
31 virtual QVector<double> values() const = 0;
30 };
32 };
31
33
32 explicit DataSeriesIteratorValue(std::unique_ptr<Impl> impl);
34 explicit DataSeriesIteratorValue(std::unique_ptr<Impl> impl);
@@ -50,6 +52,8 public:
50 double minValue() const;
52 double minValue() const;
51 /// Gets max of all values data
53 /// Gets max of all values data
52 double maxValue() const;
54 double maxValue() const;
55 /// Gets all values data
56 QVector<double> values() const;
53
57
54 private:
58 private:
55 std::unique_ptr<Impl> m_Impl;
59 std::unique_ptr<Impl> m_Impl;
@@ -50,3 +50,8 double ArrayDataIteratorValue::max() const
50 {
50 {
51 return m_Impl->max();
51 return m_Impl->max();
52 }
52 }
53
54 QVector<double> ArrayDataIteratorValue::values() const
55 {
56 return m_Impl->values();
57 }
@@ -56,3 +56,8 double DataSeriesIteratorValue::maxValue() const
56 {
56 {
57 return m_Impl->maxValue();
57 return m_Impl->maxValue();
58 }
58 }
59
60 QVector<double> DataSeriesIteratorValue::values() const
61 {
62 return m_Impl->values();
63 }
@@ -7,15 +7,21 ArrayDataIterator\.h:\d+:.*IPSIS_S01.*
7 DataSourceItem\.h:\d+:.*IPSIS_S01.*
7 DataSourceItem\.h:\d+:.*IPSIS_S01.*
8 DataSeries\.h:\d+:.*IPSIS_S01.*
8 DataSeries\.h:\d+:.*IPSIS_S01.*
9 DataSeriesIterator\.h:\d+:.*IPSIS_S01.*
9 DataSeriesIterator\.h:\d+:.*IPSIS_S01.*
10 DataSeriesMergeHelper\.h:\d+:.*IPSIS_S01.*
10
11
11 # Ignore false positive relative to a template class
12 # Ignore false positive relative to a template class
13 ArrayData\.h:\d+:.*IPSIS_S04_METHOD.*found: push_back
12 ArrayData\.h:\d+:.*IPSIS_S04_VARIABLE.*found: (D)
14 ArrayData\.h:\d+:.*IPSIS_S04_VARIABLE.*found: (D)
13 ArrayData\.h:\d+:.*IPSIS_S04_NAMESPACE.*found: (arraydata_detail)
15 ArrayData\.h:\d+:.*IPSIS_S04_NAMESPACE.*found: (arraydata_detail)
14 ArrayData\.h:\d+:.*IPSIS_S06.*found: (D)
16 ArrayData\.h:\d+:.*IPSIS_S06.*found: (D)
15 ArrayData\.h:\d+:.*IPSIS_S06.*found: (Dim)
17 ArrayData\.h:\d+:.*IPSIS_S06.*found: (Dim)
16 DataSeries\.h:\d+:.*IPSIS_S04_METHOD.*found: LOG_DataSeries
18 DataSeries\.h:\d+:.*IPSIS_S04_METHOD.*found: LOG_DataSeries
19 DataSeries\.h:\d+:.*IPSIS_S04_METHOD.*found: push_back
17 DataSeries\.h:\d+:.*IPSIS_S04_VARIABLE.*
20 DataSeries\.h:\d+:.*IPSIS_S04_VARIABLE.*
18 DataSeries\.h:\d+:.*IPSIS_S04_NAMESPACE.*found: (dataseries_detail)
21 DataSeries\.h:\d+:.*IPSIS_S04_NAMESPACE.*found: (dataseries_detail)
22 DataSeries\.h:\d+:.*IPSIS_S05.*
23 DataSeries\.h:\d+:.*IPSIS_S06.*found: (value_type)
24 DataSeries\.h:\d+:.*IPSIS_S06.*found: (DataSeriesIteratorValue)
19
25
20 # Ignore false positive relative to iterators
26 # Ignore false positive relative to iterators
21 SqpIterator\.h:\d+:.*IPSIS_S04_VARIABLE.*found: (forward_iterator_tag)
27 SqpIterator\.h:\d+:.*IPSIS_S04_VARIABLE.*found: (forward_iterator_tag)
General Comments 0
You need to be logged in to leave comments. Login now