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