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