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