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