@@ -1,298 +1,271 | |||||
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 | private: |
|
90 | private: | |
91 | std::vector<double> values() const |
|
91 | std::vector<double> values() const | |
92 | { |
|
92 | { | |
93 | auto result = std::vector<double>{}; |
|
93 | auto result = std::vector<double>{}; | |
94 | for (auto i = 0; i < m_NbComponents; ++i) { |
|
94 | for (auto i = 0; i < m_NbComponents; ++i) { | |
95 | result.push_back(*(m_It + i)); |
|
95 | result.push_back(*(m_It + i)); | |
96 | } |
|
96 | } | |
97 |
|
97 | |||
98 | return result; |
|
98 | return result; | |
99 | } |
|
99 | } | |
100 |
|
100 | |||
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 | // ///////////// // |
|
|||
239 | // 1-dim methods // |
|
|||
240 | // ///////////// // |
|
|||
241 |
|
238 | |||
242 | /** |
|
239 | /** | |
243 | * @return the data at a specified index |
|
240 | * @return the data at a specified index | |
244 | * @remarks index must be a valid position |
|
241 | * @remarks index must be a valid position | |
245 | * @remarks this method is only available for a unidimensional ArrayData |
|
|||
246 | */ |
|
242 | */ | |
247 | template <int D = Dim, typename = std::enable_if_t<D == 1> > |
|
|||
248 | double at(int index) const noexcept |
|
243 | double at(int index) const noexcept | |
249 | { |
|
244 | { | |
250 | QReadLocker locker{&m_Lock}; |
|
245 | QReadLocker locker{&m_Lock}; | |
251 | return m_Data.at(index); |
|
246 | return m_Data.at(index); | |
252 | } |
|
247 | } | |
253 |
|
248 | |||
|
249 | // ///////////// // | |||
|
250 | // 1-dim methods // | |||
|
251 | // ///////////// // | |||
|
252 | ||||
254 | /** |
|
253 | /** | |
255 | * @return the data as a vector, as a const reference |
|
254 | * @return the data as a vector, as a const reference | |
256 | * @remarks this method is only available for a unidimensional ArrayData |
|
255 | * @remarks this method is only available for a unidimensional ArrayData | |
257 | */ |
|
256 | */ | |
258 | template <int D = Dim, typename = std::enable_if_t<D == 1> > |
|
257 | template <int D = Dim, typename = std::enable_if_t<D == 1> > | |
259 | const QVector<double> &cdata() const noexcept |
|
258 | const QVector<double> &cdata() const noexcept | |
260 | { |
|
259 | { | |
261 | QReadLocker locker{&m_Lock}; |
|
260 | QReadLocker locker{&m_Lock}; | |
262 | return m_Data.at(0); |
|
|||
263 | } |
|
|||
264 |
|
||||
265 | /** |
|
|||
266 | * @return the data as a vector |
|
|||
267 | * @remarks this method is only available for a unidimensional ArrayData |
|
|||
268 | */ |
|
|||
269 | template <int D = Dim, typename = std::enable_if_t<D == 1> > |
|
|||
270 | QVector<double> data() const noexcept |
|
|||
271 | { |
|
|||
272 | QReadLocker locker{&m_Lock}; |
|
|||
273 | return m_Data[0]; |
|
|||
274 | } |
|
|||
275 |
|
||||
276 | // ///////////// // |
|
|||
277 | // 2-dim methods // |
|
|||
278 | // ///////////// // |
|
|||
279 |
|
||||
280 | /** |
|
|||
281 | * @return the data |
|
|||
282 | * @remarks this method is only available for a two-dimensional ArrayData |
|
|||
283 | */ |
|
|||
284 | template <int D = Dim, typename = std::enable_if_t<D == 2> > |
|
|||
285 | DataContainer data() const noexcept |
|
|||
286 | { |
|
|||
287 | QReadLocker locker{&m_Lock}; |
|
|||
288 | return m_Data; |
|
261 | return m_Data; | |
289 | } |
|
262 | } | |
290 |
|
263 | |||
291 | private: |
|
264 | private: | |
292 | DataContainer m_Data; |
|
265 | DataContainer m_Data; | |
293 | /// Number of components (lines). Is always 1 in a 1-dim ArrayData |
|
266 | /// Number of components (lines). Is always 1 in a 1-dim ArrayData | |
294 | int m_NbComponents; |
|
267 | int m_NbComponents; | |
295 | mutable QReadWriteLock m_Lock; |
|
268 | mutable QReadWriteLock m_Lock; | |
296 | }; |
|
269 | }; | |
297 |
|
270 | |||
298 | #endif // SCIQLOP_ARRAYDATA_H |
|
271 | #endif // SCIQLOP_ARRAYDATA_H |
General Comments 0
You need to be logged in to leave comments.
Login now