##// END OF EJS Templates
Merge branch 'feature/MergeFix' into develop
Alexandre Leroux -
r625: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
@@ -1,271 +1,282
1 1 #ifndef SCIQLOP_ARRAYDATA_H
2 2 #define SCIQLOP_ARRAYDATA_H
3 3
4 4 #include "Data/ArrayDataIterator.h"
5 5 #include <Common/SortUtils.h>
6 6
7 7 #include <QReadLocker>
8 8 #include <QReadWriteLock>
9 9 #include <QVector>
10 10
11 11 #include <memory>
12 12
13 13 template <int Dim>
14 14 class ArrayData;
15 15
16 16 using DataContainer = QVector<double>;
17 17
18 18 namespace arraydata_detail {
19 19
20 20 /// Struct used to sort ArrayData
21 21 template <int Dim>
22 22 struct Sort {
23 23 static std::shared_ptr<ArrayData<Dim> > sort(const DataContainer &data, int nbComponents,
24 24 const std::vector<int> &sortPermutation)
25 25 {
26 26 return std::make_shared<ArrayData<Dim> >(
27 27 SortUtils::sort(data, nbComponents, sortPermutation), nbComponents);
28 28 }
29 29 };
30 30
31 31 /// Specialization for uni-dimensional ArrayData
32 32 template <>
33 33 struct Sort<1> {
34 34 static std::shared_ptr<ArrayData<1> > sort(const DataContainer &data, int nbComponents,
35 35 const std::vector<int> &sortPermutation)
36 36 {
37 37 Q_UNUSED(nbComponents)
38 38 return std::make_shared<ArrayData<1> >(SortUtils::sort(data, 1, sortPermutation));
39 39 }
40 40 };
41 41
42 42 template <int Dim>
43 43 class IteratorValue : public ArrayDataIteratorValue::Impl {
44 44 public:
45 45 explicit IteratorValue(const DataContainer &container, int nbComponents, bool begin)
46 46 : m_It{begin ? container.cbegin() : container.cend()}, m_NbComponents{nbComponents}
47 47 {
48 48 }
49 49
50 50 IteratorValue(const IteratorValue &other) = default;
51 51
52 52 std::unique_ptr<ArrayDataIteratorValue::Impl> clone() const override
53 53 {
54 54 return std::make_unique<IteratorValue<Dim> >(*this);
55 55 }
56 56
57 57 bool equals(const ArrayDataIteratorValue::Impl &other) const override try {
58 58 const auto &otherImpl = dynamic_cast<const IteratorValue &>(other);
59 59 return std::tie(m_It, m_NbComponents) == std::tie(otherImpl.m_It, otherImpl.m_NbComponents);
60 60 }
61 61 catch (const std::bad_cast &) {
62 62 return false;
63 63 }
64 64
65 65 void next() override { std::advance(m_It, m_NbComponents); }
66 66 void prev() override { std::advance(m_It, -m_NbComponents); }
67 67
68 68 double at(int componentIndex) const override { return *(m_It + componentIndex); }
69 69 double first() const override { return *m_It; }
70 70 double min() const override
71 71 {
72 72 auto values = this->values();
73 73 auto end = values.cend();
74 74 auto it = std::min_element(values.cbegin(), end, [](const auto &v1, const auto &v2) {
75 75 return SortUtils::minCompareWithNaN(v1, v2);
76 76 });
77 77
78 78 return it != end ? *it : std::numeric_limits<double>::quiet_NaN();
79 79 }
80 80 double max() const override
81 81 {
82 82 auto values = this->values();
83 83 auto end = values.cend();
84 84 auto it = std::max_element(values.cbegin(), end, [](const auto &v1, const auto &v2) {
85 85 return SortUtils::maxCompareWithNaN(v1, v2);
86 86 });
87 87 return it != end ? *it : std::numeric_limits<double>::quiet_NaN();
88 88 }
89 89
90 private:
91 std::vector<double> values() const
90 QVector<double> values() const override
92 91 {
93 auto result = std::vector<double>{};
92 auto result = QVector<double>{};
94 93 for (auto i = 0; i < m_NbComponents; ++i) {
95 94 result.push_back(*(m_It + i));
96 95 }
97 96
98 97 return result;
99 98 }
100 99
100 private:
101 101 DataContainer::const_iterator m_It;
102 102 int m_NbComponents;
103 103 };
104 104
105 105 } // namespace arraydata_detail
106 106
107 107 /**
108 108 * @brief The ArrayData class represents a dataset for a data series.
109 109 *
110 110 * A dataset can be unidimensional or two-dimensional. This property is determined by the Dim
111 111 * template-parameter. In a case of a two-dimensional dataset, each dataset component has the same
112 112 * number of values
113 113 *
114 114 * @tparam Dim the dimension of the ArrayData (one or two)
115 115 * @sa IDataSeries
116 116 */
117 117 template <int Dim>
118 118 class ArrayData {
119 119 public:
120 120 // ///// //
121 121 // Ctors //
122 122 // ///// //
123 123
124 124 /**
125 125 * Ctor for a unidimensional ArrayData
126 126 * @param data the data the ArrayData will hold
127 127 */
128 128 template <int D = Dim, typename = std::enable_if_t<D == 1> >
129 129 explicit ArrayData(DataContainer data) : m_Data{std::move(data)}, m_NbComponents{1}
130 130 {
131 131 }
132 132
133 133 /**
134 134 * Ctor for a two-dimensional ArrayData. The number of components (number of lines) must be
135 135 * greater than 2 and must be a divisor of the total number of data in the vector
136 136 * @param data the data the ArrayData will hold
137 137 * @param nbComponents the number of components
138 138 * @throws std::invalid_argument if the number of components is less than 2 or is not a divisor
139 139 * of the size of the data
140 140 */
141 141 template <int D = Dim, typename = std::enable_if_t<D == 2> >
142 142 explicit ArrayData(DataContainer data, int nbComponents)
143 143 : m_Data{std::move(data)}, m_NbComponents{nbComponents}
144 144 {
145 145 if (nbComponents < 2) {
146 146 throw std::invalid_argument{
147 147 QString{"A multidimensional ArrayData must have at least 2 components (found: %1)"}
148 148 .arg(nbComponents)
149 149 .toStdString()};
150 150 }
151 151
152 152 if (m_Data.size() % m_NbComponents != 0) {
153 153 throw std::invalid_argument{QString{
154 154 "The number of components (%1) is inconsistent with the total number of data (%2)"}
155 155 .arg(m_Data.size(), nbComponents)
156 156 .toStdString()};
157 157 }
158 158 }
159 159
160 160 /// Copy ctor
161 161 explicit ArrayData(const ArrayData &other)
162 162 {
163 163 QReadLocker otherLocker{&other.m_Lock};
164 164 m_Data = other.m_Data;
165 165 m_NbComponents = other.m_NbComponents;
166 166 }
167 167
168 168 // /////////////// //
169 169 // General methods //
170 170 // /////////////// //
171 171
172 172 /**
173 173 * Merges into the array data an other array data. The two array datas must have the same number
174 174 * of components so the merge can be done
175 175 * @param other the array data to merge with
176 176 * @param prepend if true, the other array data is inserted at the beginning, otherwise it is
177 177 * inserted at the end
178 178 */
179 179 void add(const ArrayData<Dim> &other, bool prepend = false)
180 180 {
181 181 QWriteLocker locker{&m_Lock};
182 182 QReadLocker otherLocker{&other.m_Lock};
183 183
184 184 if (m_NbComponents != other.componentCount()) {
185 185 return;
186 186 }
187 187
188 188 if (prepend) {
189 189 auto otherDataSize = other.m_Data.size();
190 190 m_Data.insert(m_Data.begin(), otherDataSize, 0.);
191 191 for (auto i = 0; i < otherDataSize; ++i) {
192 192 m_Data.replace(i, other.m_Data.at(i));
193 193 }
194 194 }
195 195 else {
196 196 m_Data.append(other.m_Data);
197 197 }
198 198 }
199 199
200 200 void clear()
201 201 {
202 202 QWriteLocker locker{&m_Lock};
203 203 m_Data.clear();
204 204 }
205 205
206 206 int componentCount() const noexcept { return m_NbComponents; }
207 207
208 208 /// @return the size (i.e. number of values) of a single component
209 209 /// @remarks in a case of a two-dimensional ArrayData, each component has the same size
210 210 int size() const
211 211 {
212 212 QReadLocker locker{&m_Lock};
213 213 return m_Data.size() / m_NbComponents;
214 214 }
215 215
216 216 std::shared_ptr<ArrayData<Dim> > sort(const std::vector<int> &sortPermutation)
217 217 {
218 218 QReadLocker locker{&m_Lock};
219 219 return arraydata_detail::Sort<Dim>::sort(m_Data, m_NbComponents, sortPermutation);
220 220 }
221 221
222 222 // ///////// //
223 223 // Iterators //
224 224 // ///////// //
225 225
226 226 ArrayDataIterator cbegin() const
227 227 {
228 228 return ArrayDataIterator{ArrayDataIteratorValue{
229 229 std::make_unique<arraydata_detail::IteratorValue<Dim> >(m_Data, m_NbComponents, true)}};
230 230 }
231 231 ArrayDataIterator cend() const
232 232 {
233 233 return ArrayDataIterator{
234 234 ArrayDataIteratorValue{std::make_unique<arraydata_detail::IteratorValue<Dim> >(
235 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 251 * @return the data at a specified index
241 252 * @remarks index must be a valid position
242 253 */
243 254 double at(int index) const noexcept
244 255 {
245 256 QReadLocker locker{&m_Lock};
246 257 return m_Data.at(index);
247 258 }
248 259
249 260 // ///////////// //
250 261 // 1-dim methods //
251 262 // ///////////// //
252 263
253 264 /**
254 265 * @return the data as a vector, as a const reference
255 266 * @remarks this method is only available for a unidimensional ArrayData
256 267 */
257 268 template <int D = Dim, typename = std::enable_if_t<D == 1> >
258 269 const QVector<double> &cdata() const noexcept
259 270 {
260 271 QReadLocker locker{&m_Lock};
261 272 return m_Data;
262 273 }
263 274
264 275 private:
265 276 DataContainer m_Data;
266 277 /// Number of components (lines). Is always 1 in a 1-dim ArrayData
267 278 int m_NbComponents;
268 279 mutable QReadWriteLock m_Lock;
269 280 };
270 281
271 282 #endif // SCIQLOP_ARRAYDATA_H
@@ -1,56 +1,60
1 1 #ifndef SCIQLOP_ARRAYDATAITERATOR_H
2 2 #define SCIQLOP_ARRAYDATAITERATOR_H
3 3
4 4 #include "CoreGlobal.h"
5 5 #include "Data/SqpIterator.h"
6 6
7 #include <QVector>
7 8 #include <memory>
8 9
9 10 /**
10 11 * @brief The ArrayDataIteratorValue class represents the current value of an array data iterator.
11 12 * It offers standard access methods for the data in the series (at(), first()), but it is up to
12 13 * each array data to define its own implementation of how to retrieve this data (one-dim or two-dim
13 14 * array), by implementing the ArrayDataIteratorValue::Impl interface
14 15 * @sa ArrayDataIterator
15 16 */
16 17 class SCIQLOP_CORE_EXPORT ArrayDataIteratorValue {
17 18 public:
18 19 struct Impl {
19 20 virtual ~Impl() noexcept = default;
20 21 virtual std::unique_ptr<Impl> clone() const = 0;
21 22 virtual bool equals(const Impl &other) const = 0;
22 23 virtual void next() = 0;
23 24 virtual void prev() = 0;
24 25 virtual double at(int componentIndex) const = 0;
25 26 virtual double first() const = 0;
26 27 virtual double min() const = 0;
27 28 virtual double max() const = 0;
29 virtual QVector<double> values() const = 0;
28 30 };
29 31
30 32 explicit ArrayDataIteratorValue(std::unique_ptr<Impl> impl);
31 33 ArrayDataIteratorValue(const ArrayDataIteratorValue &other);
32 34 ArrayDataIteratorValue(ArrayDataIteratorValue &&other) = default;
33 35 ArrayDataIteratorValue &operator=(ArrayDataIteratorValue other);
34 36
35 37 bool equals(const ArrayDataIteratorValue &other) const;
36 38
37 39 /// Advances to the next value
38 40 void next();
39 41 /// Moves back to the previous value
40 42 void prev();
41 43 /// Gets value of a specified component
42 44 double at(int componentIndex) const;
43 45 /// Gets value of first component
44 46 double first() const;
45 47 /// Gets min value among all components
46 48 double min() const;
47 49 /// Gets max value among all components
48 50 double max() const;
51 /// Gets all values
52 QVector<double> values() const;
49 53
50 54 private:
51 55 std::unique_ptr<Impl> m_Impl;
52 56 };
53 57
54 58 using ArrayDataIterator = SqpIterator<ArrayDataIteratorValue>;
55 59
56 60 #endif // SCIQLOP_ARRAYDATAITERATOR_H
@@ -1,333 +1,317
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(); }
71 QVector<double> values() const override { return m_ValuesIt->values(); }
70 72
71 73 private:
72 74 ArrayDataIterator m_XIt;
73 75 ArrayDataIterator m_ValuesIt;
74 76 };
75 77 } // namespace dataseries_detail
76 78
77 79 /**
78 80 * @brief The DataSeries class is the base (abstract) implementation of IDataSeries.
79 81 *
80 82 * It proposes to set a dimension for the values ​​data.
81 83 *
82 84 * A DataSeries is always sorted on its x-axis data.
83 85 *
84 86 * @tparam Dim The dimension of the values data
85 87 *
86 88 */
87 89 template <int Dim>
88 90 class SCIQLOP_CORE_EXPORT DataSeries : public IDataSeries {
91 friend class DataSeriesMergeHelper;
92
89 93 public:
94 /// Tag needed to define the push_back() method
95 /// @sa push_back()
96 using value_type = DataSeriesIteratorValue;
97
90 98 /// @sa IDataSeries::xAxisData()
91 99 std::shared_ptr<ArrayData<1> > xAxisData() override { return m_XAxisData; }
92 100 const std::shared_ptr<ArrayData<1> > xAxisData() const { return m_XAxisData; }
93 101
94 102 /// @sa IDataSeries::xAxisUnit()
95 103 Unit xAxisUnit() const override { return m_XAxisUnit; }
96 104
97 105 /// @return the values dataset
98 106 std::shared_ptr<ArrayData<Dim> > valuesData() { return m_ValuesData; }
99 107 const std::shared_ptr<ArrayData<Dim> > valuesData() const { return m_ValuesData; }
100 108
101 109 /// @sa IDataSeries::valuesUnit()
102 110 Unit valuesUnit() const override { return m_ValuesUnit; }
103 111
104 112
105 113 SqpRange range() const override
106 114 {
107 115 if (!m_XAxisData->cdata().isEmpty()) {
108 116 return SqpRange{m_XAxisData->cdata().first(), m_XAxisData->cdata().last()};
109 117 }
110 118
111 119 return SqpRange{};
112 120 }
113 121
114 122 void clear()
115 123 {
116 124 m_XAxisData->clear();
117 125 m_ValuesData->clear();
118 126 }
119 127
128 bool isEmpty() const noexcept { return m_XAxisData->size() == 0; }
129
120 130 /// Merges into the data series an other data series
121 131 /// @remarks the data series to merge with is cleared after the operation
122 132 void merge(IDataSeries *dataSeries) override
123 133 {
124 134 dataSeries->lockWrite();
125 135 lockWrite();
126 136
127 137 if (auto other = dynamic_cast<DataSeries<Dim> *>(dataSeries)) {
128 const auto &otherXAxisData = other->xAxisData()->cdata();
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();
138 DataSeriesMergeHelper::merge(*other, *this);
171 139 }
172 140 else {
173 141 qCWarning(LOG_DataSeries())
174 142 << QObject::tr("Detection of a type of IDataSeries we cannot merge with !");
175 143 }
176 144 unlock();
177 145 dataSeries->unlock();
178 146 }
179 147
180 148 // ///////// //
181 149 // Iterators //
182 150 // ///////// //
183 151
184 152 DataSeriesIterator cbegin() const override
185 153 {
186 154 return DataSeriesIterator{DataSeriesIteratorValue{
187 155 std::make_unique<dataseries_detail::IteratorValue<Dim> >(*this, true)}};
188 156 }
189 157
190 158 DataSeriesIterator cend() const override
191 159 {
192 160 return DataSeriesIterator{DataSeriesIteratorValue{
193 161 std::make_unique<dataseries_detail::IteratorValue<Dim> >(*this, false)}};
194 162 }
195 163
196 164 /// @sa IDataSeries::minXAxisData()
197 165 DataSeriesIterator minXAxisData(double minXAxisData) const override
198 166 {
199 167 return std::lower_bound(
200 168 cbegin(), cend(), minXAxisData,
201 169 [](const auto &itValue, const auto &value) { return itValue.x() < value; });
202 170 }
203 171
204 172 /// @sa IDataSeries::maxXAxisData()
205 173 DataSeriesIterator maxXAxisData(double maxXAxisData) const override
206 174 {
207 175 // Gets the first element that greater than max value
208 176 auto it = std::upper_bound(
209 177 cbegin(), cend(), maxXAxisData,
210 178 [](const auto &value, const auto &itValue) { return value < itValue.x(); });
211 179
212 180 return it == cbegin() ? cend() : --it;
213 181 }
214 182
215 183 std::pair<DataSeriesIterator, DataSeriesIterator> xAxisRange(double minXAxisData,
216 184 double maxXAxisData) const override
217 185 {
218 186 if (minXAxisData > maxXAxisData) {
219 187 std::swap(minXAxisData, maxXAxisData);
220 188 }
221 189
222 190 auto begin = cbegin();
223 191 auto end = cend();
224 192
225 193 auto lowerIt = std::lower_bound(
226 194 begin, end, minXAxisData,
227 195 [](const auto &itValue, const auto &value) { return itValue.x() < value; });
228 196 auto upperIt = std::upper_bound(
229 197 begin, end, maxXAxisData,
230 198 [](const auto &value, const auto &itValue) { return value < itValue.x(); });
231 199
232 200 return std::make_pair(lowerIt, upperIt);
233 201 }
234 202
235 203 std::pair<DataSeriesIterator, DataSeriesIterator>
236 204 valuesBounds(double minXAxisData, double maxXAxisData) const override
237 205 {
238 206 // Places iterators to the correct x-axis range
239 207 auto xAxisRangeIts = xAxisRange(minXAxisData, maxXAxisData);
240 208
241 209 // Returns end iterators if the range is empty
242 210 if (xAxisRangeIts.first == xAxisRangeIts.second) {
243 211 return std::make_pair(cend(), cend());
244 212 }
245 213
246 214 // Gets the iterator on the min of all values data
247 215 auto minIt = std::min_element(
248 216 xAxisRangeIts.first, xAxisRangeIts.second, [](const auto &it1, const auto &it2) {
249 217 return SortUtils::minCompareWithNaN(it1.minValue(), it2.minValue());
250 218 });
251 219
252 220 // Gets the iterator on the max of all values data
253 221 auto maxIt = std::max_element(
254 222 xAxisRangeIts.first, xAxisRangeIts.second, [](const auto &it1, const auto &it2) {
255 223 return SortUtils::maxCompareWithNaN(it1.maxValue(), it2.maxValue());
256 224 });
257 225
258 226 return std::make_pair(minIt, maxIt);
259 227 }
260 228
261 229 // /////// //
262 230 // Mutexes //
263 231 // /////// //
264 232
265 233 virtual void lockRead() { m_Lock.lockForRead(); }
266 234 virtual void lockWrite() { m_Lock.lockForWrite(); }
267 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 253 protected:
270 254 /// Protected ctor (DataSeries is abstract). The vectors must have the same size, otherwise a
271 255 /// DataSeries with no values will be created.
272 256 /// @remarks data series is automatically sorted on its x-axis data
273 257 explicit DataSeries(std::shared_ptr<ArrayData<1> > xAxisData, const Unit &xAxisUnit,
274 258 std::shared_ptr<ArrayData<Dim> > valuesData, const Unit &valuesUnit)
275 259 : m_XAxisData{xAxisData},
276 260 m_XAxisUnit{xAxisUnit},
277 261 m_ValuesData{valuesData},
278 262 m_ValuesUnit{valuesUnit}
279 263 {
280 264 if (m_XAxisData->size() != m_ValuesData->size()) {
281 265 clear();
282 266 }
283 267
284 268 // Sorts data if it's not the case
285 269 const auto &xAxisCData = m_XAxisData->cdata();
286 270 if (!std::is_sorted(xAxisCData.cbegin(), xAxisCData.cend())) {
287 271 sort();
288 272 }
289 273 }
290 274
291 275 /// Copy ctor
292 276 explicit DataSeries(const DataSeries<Dim> &other)
293 277 : m_XAxisData{std::make_shared<ArrayData<1> >(*other.m_XAxisData)},
294 278 m_XAxisUnit{other.m_XAxisUnit},
295 279 m_ValuesData{std::make_shared<ArrayData<Dim> >(*other.m_ValuesData)},
296 280 m_ValuesUnit{other.m_ValuesUnit}
297 281 {
298 282 // Since a series is ordered from its construction and is always ordered, it is not
299 283 // necessary to call the sort method here ('other' is sorted)
300 284 }
301 285
302 286 /// Assignment operator
303 287 template <int D>
304 288 DataSeries &operator=(DataSeries<D> other)
305 289 {
306 290 std::swap(m_XAxisData, other.m_XAxisData);
307 291 std::swap(m_XAxisUnit, other.m_XAxisUnit);
308 292 std::swap(m_ValuesData, other.m_ValuesData);
309 293 std::swap(m_ValuesUnit, other.m_ValuesUnit);
310 294
311 295 return *this;
312 296 }
313 297
314 298 private:
315 299 /**
316 300 * Sorts data series on its x-axis data
317 301 */
318 302 void sort() noexcept
319 303 {
320 304 auto permutation = SortUtils::sortPermutation(*m_XAxisData, std::less<double>());
321 305 m_XAxisData = m_XAxisData->sort(permutation);
322 306 m_ValuesData = m_ValuesData->sort(permutation);
323 307 }
324 308
325 309 std::shared_ptr<ArrayData<1> > m_XAxisData;
326 310 Unit m_XAxisUnit;
327 311 std::shared_ptr<ArrayData<Dim> > m_ValuesData;
328 312 Unit m_ValuesUnit;
329 313
330 314 QReadWriteLock m_Lock;
331 315 };
332 316
333 317 #endif // SCIQLOP_DATASERIES_H
@@ -1,60 +1,64
1 1 #ifndef SCIQLOP_DATASERIESITERATOR_H
2 2 #define SCIQLOP_DATASERIESITERATOR_H
3 3
4 4 #include "CoreGlobal.h"
5 5 #include "Data/SqpIterator.h"
6 6
7 #include <QVector>
7 8 #include <memory>
8 9
9 10 /**
10 11 * @brief The DataSeriesIteratorValue class represents the current value of a data series iterator.
11 12 * It offers standard access methods for the data in the series (x-axis, values), but it is up to
12 13 * each series to define its own implementation of how to retrieve this data, by implementing the
13 14 * DataSeriesIteratorValue::Impl interface
14 15 *
15 16 * @sa DataSeriesIterator
16 17 */
17 18 class SCIQLOP_CORE_EXPORT DataSeriesIteratorValue {
18 19 public:
19 20 struct Impl {
20 21 virtual ~Impl() noexcept = default;
21 22 virtual std::unique_ptr<Impl> clone() const = 0;
22 23 virtual bool equals(const Impl &other) const = 0;
23 24 virtual void next() = 0;
24 25 virtual void prev() = 0;
25 26 virtual double x() const = 0;
26 27 virtual double value() const = 0;
27 28 virtual double value(int componentIndex) const = 0;
28 29 virtual double minValue() const = 0;
29 30 virtual double maxValue() const = 0;
31 virtual QVector<double> values() const = 0;
30 32 };
31 33
32 34 explicit DataSeriesIteratorValue(std::unique_ptr<Impl> impl);
33 35 DataSeriesIteratorValue(const DataSeriesIteratorValue &other);
34 36 DataSeriesIteratorValue(DataSeriesIteratorValue &&other) = default;
35 37 DataSeriesIteratorValue &operator=(DataSeriesIteratorValue other);
36 38
37 39 bool equals(const DataSeriesIteratorValue &other) const;
38 40
39 41 /// Advances to the next value
40 42 void next();
41 43 /// Moves back to the previous value
42 44 void prev();
43 45 /// Gets x-axis data
44 46 double x() const;
45 47 /// Gets value data
46 48 double value() const;
47 49 /// Gets value data depending on an index
48 50 double value(int componentIndex) const;
49 51 /// Gets min of all values data
50 52 double minValue() const;
51 53 /// Gets max of all values data
52 54 double maxValue() const;
55 /// Gets all values data
56 QVector<double> values() const;
53 57
54 58 private:
55 59 std::unique_ptr<Impl> m_Impl;
56 60 };
57 61
58 62 using DataSeriesIterator = SqpIterator<DataSeriesIteratorValue>;
59 63
60 64 #endif // SCIQLOP_DATASERIESITERATOR_H
@@ -1,52 +1,57
1 1 #include "Data/ArrayDataIterator.h"
2 2
3 3 ArrayDataIteratorValue::ArrayDataIteratorValue(std::unique_ptr<ArrayDataIteratorValue::Impl> impl)
4 4 : m_Impl{std::move(impl)}
5 5 {
6 6 }
7 7
8 8 ArrayDataIteratorValue::ArrayDataIteratorValue(const ArrayDataIteratorValue &other)
9 9 : m_Impl{other.m_Impl->clone()}
10 10 {
11 11 }
12 12
13 13 ArrayDataIteratorValue &ArrayDataIteratorValue::operator=(ArrayDataIteratorValue other)
14 14 {
15 15 std::swap(m_Impl, other.m_Impl);
16 16 return *this;
17 17 }
18 18
19 19 bool ArrayDataIteratorValue::equals(const ArrayDataIteratorValue &other) const
20 20 {
21 21 return m_Impl->equals(*other.m_Impl);
22 22 }
23 23
24 24 void ArrayDataIteratorValue::next()
25 25 {
26 26 m_Impl->next();
27 27 }
28 28
29 29 void ArrayDataIteratorValue::prev()
30 30 {
31 31 m_Impl->prev();
32 32 }
33 33
34 34 double ArrayDataIteratorValue::at(int componentIndex) const
35 35 {
36 36 return m_Impl->at(componentIndex);
37 37 }
38 38
39 39 double ArrayDataIteratorValue::first() const
40 40 {
41 41 return m_Impl->first();
42 42 }
43 43
44 44 double ArrayDataIteratorValue::min() const
45 45 {
46 46 return m_Impl->min();
47 47 }
48 48
49 49 double ArrayDataIteratorValue::max() const
50 50 {
51 51 return m_Impl->max();
52 52 }
53
54 QVector<double> ArrayDataIteratorValue::values() const
55 {
56 return m_Impl->values();
57 }
@@ -1,58 +1,63
1 1 #include "Data/DataSeriesIterator.h"
2 2
3 3 DataSeriesIteratorValue::DataSeriesIteratorValue(
4 4 std::unique_ptr<DataSeriesIteratorValue::Impl> impl)
5 5 : m_Impl{std::move(impl)}
6 6 {
7 7 }
8 8
9 9 DataSeriesIteratorValue::DataSeriesIteratorValue(const DataSeriesIteratorValue &other)
10 10 : m_Impl{other.m_Impl->clone()}
11 11 {
12 12 }
13 13
14 14 DataSeriesIteratorValue &DataSeriesIteratorValue::operator=(DataSeriesIteratorValue other)
15 15 {
16 16 std::swap(m_Impl, other.m_Impl);
17 17 return *this;
18 18 }
19 19
20 20 bool DataSeriesIteratorValue::equals(const DataSeriesIteratorValue &other) const
21 21 {
22 22 return m_Impl->equals(*other.m_Impl);
23 23 }
24 24
25 25 void DataSeriesIteratorValue::next()
26 26 {
27 27 m_Impl->next();
28 28 }
29 29
30 30 void DataSeriesIteratorValue::prev()
31 31 {
32 32 m_Impl->prev();
33 33 }
34 34
35 35 double DataSeriesIteratorValue::x() const
36 36 {
37 37 return m_Impl->x();
38 38 }
39 39
40 40 double DataSeriesIteratorValue::value() const
41 41 {
42 42 return m_Impl->value();
43 43 }
44 44
45 45 double DataSeriesIteratorValue::value(int componentIndex) const
46 46 {
47 47 return m_Impl->value(componentIndex);
48 48 }
49 49
50 50 double DataSeriesIteratorValue::minValue() const
51 51 {
52 52 return m_Impl->minValue();
53 53 }
54 54
55 55 double DataSeriesIteratorValue::maxValue() const
56 56 {
57 57 return m_Impl->maxValue();
58 58 }
59
60 QVector<double> DataSeriesIteratorValue::values() const
61 {
62 return m_Impl->values();
63 }
@@ -1,39 +1,45
1 1 # On ignore toutes les règles vera++ pour le fichier spimpl
2 2 Common/spimpl\.h:\d+:.*
3 3
4 4 # Ignore false positive relative to two class definitions in a same file
5 5 ArrayData\.h:\d+:.*IPSIS_S01.*
6 6 ArrayDataIterator\.h:\d+:.*IPSIS_S01.*
7 7 DataSourceItem\.h:\d+:.*IPSIS_S01.*
8 8 DataSeries\.h:\d+:.*IPSIS_S01.*
9 9 DataSeriesIterator\.h:\d+:.*IPSIS_S01.*
10 DataSeriesMergeHelper\.h:\d+:.*IPSIS_S01.*
10 11
11 12 # Ignore false positive relative to a template class
13 ArrayData\.h:\d+:.*IPSIS_S04_METHOD.*found: push_back
12 14 ArrayData\.h:\d+:.*IPSIS_S04_VARIABLE.*found: (D)
13 15 ArrayData\.h:\d+:.*IPSIS_S04_NAMESPACE.*found: (arraydata_detail)
14 16 ArrayData\.h:\d+:.*IPSIS_S06.*found: (D)
15 17 ArrayData\.h:\d+:.*IPSIS_S06.*found: (Dim)
16 18 DataSeries\.h:\d+:.*IPSIS_S04_METHOD.*found: LOG_DataSeries
19 DataSeries\.h:\d+:.*IPSIS_S04_METHOD.*found: push_back
17 20 DataSeries\.h:\d+:.*IPSIS_S04_VARIABLE.*
18 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 26 # Ignore false positive relative to iterators
21 27 SqpIterator\.h:\d+:.*IPSIS_S04_VARIABLE.*found: (forward_iterator_tag)
22 28 SqpIterator\.h:\d+:.*IPSIS_S04_VARIABLE.*found: (T)
23 29 SqpIterator\.h:\d+:.*IPSIS_S04_VARIABLE.*found: (ptrdiff_t)
24 30 SqpIterator\.h:\d+:.*IPSIS_S04_VARIABLE.*found: (value_type)
25 31 SqpIterator\.h:\d+:.*IPSIS_S06.*found: (iterator_category)
26 32 SqpIterator\.h:\d+:.*IPSIS_S06.*found: (forward_iterator_tag)
27 33 SqpIterator\.h:\d+:.*IPSIS_S06.*found: (value_type)
28 34 SqpIterator\.h:\d+:.*IPSIS_S06.*found: (T)
29 35 SqpIterator\.h:\d+:.*IPSIS_S06.*found: (difference_type)
30 36 SqpIterator\.h:\d+:.*IPSIS_S06.*found: (ptrdiff_t)
31 37 SqpIterator\.h:\d+:.*IPSIS_S06.*found: (pointer)
32 38 SqpIterator\.h:\d+:.*IPSIS_S06.*found: (reference)
33 39 SqpIterator\.h:\d+:.*IPSIS_S06.*found: (value_type)
34 40
35 41 # Ignore false positive relative to an alias
36 42 DataSourceItemAction\.h:\d+:.*IPSIS_S06.*found: (ExecuteFunction)
37 43
38 44 # Ignore false positive relative to unnamed namespace
39 45 VariableController\.cpp:\d+:.*IPSIS_F13.*
General Comments 0
You need to be logged in to leave comments. Login now