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