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