##// END OF EJS Templates
Updates merge() method (1)...
Alexandre Leroux -
r622: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
@@ -6,6 +6,7
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>
@@ -87,6 +88,8 private:
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; }
@@ -118,6 +121,8 public:
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
@@ -126,49 +131,7 public:
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())
General Comments 0
You need to be logged in to leave comments. Login now