##// END OF EJS Templates
Handles merge of two data series
Alexandre Leroux -
r451:240544c83cc8
parent child
Show More
@@ -1,147 +1,167
1 1 #ifndef SCIQLOP_ARRAYDATA_H
2 2 #define SCIQLOP_ARRAYDATA_H
3 3
4 4 #include <QReadLocker>
5 5 #include <QReadWriteLock>
6 6 #include <QVector>
7 7
8 8 #include <memory>
9 9
10 10 /**
11 11 * @brief The ArrayData class represents a dataset for a data series.
12 12 *
13 13 * A dataset can be unidimensional or two-dimensional. This property is determined by the Dim
14 14 * template-parameter.
15 15 *
16 16 * @tparam Dim the dimension of the ArrayData (one or two)
17 17 * @sa IDataSeries
18 18 */
19 19 template <int Dim>
20 20 class ArrayData {
21 21 public:
22 22 /**
23 23 * Ctor for a unidimensional ArrayData
24 24 * @param nbColumns the number of values the ArrayData will hold
25 25 */
26 26 template <int D = Dim, typename = std::enable_if_t<D == 1> >
27 27 explicit ArrayData(int nbColumns) : m_Data{1, QVector<double>{}}
28 28 {
29 29 QWriteLocker locker{&m_Lock};
30 30 m_Data[0].resize(nbColumns);
31 31 }
32 32
33 33 /**
34 34 * Ctor for a unidimensional ArrayData
35 35 * @param data the data the ArrayData will hold
36 36 */
37 37 template <int D = Dim, typename = std::enable_if_t<D == 1> >
38 38 explicit ArrayData(QVector<double> data) : m_Data{1, QVector<double>{}}
39 39 {
40 40 QWriteLocker locker{&m_Lock};
41 41 m_Data[0] = std::move(data);
42 42 }
43 43
44 44 /// Copy ctor
45 45 explicit ArrayData(const ArrayData &other)
46 46 {
47 47 QReadLocker otherLocker{&other.m_Lock};
48 48 QWriteLocker locker{&m_Lock};
49 49 m_Data = other.m_Data;
50 50 }
51 51
52 52 /**
53 53 * @return the data at a specified index
54 54 * @remarks index must be a valid position
55 55 * @remarks this method is only available for a unidimensional ArrayData
56 56 */
57 57 template <int D = Dim, typename = std::enable_if_t<D == 1> >
58 58 double at(int index) const noexcept
59 59 {
60 60 QReadLocker locker{&m_Lock};
61 61 return m_Data[0].at(index);
62 62 }
63 63
64 64 /**
65 65 * Sets a data at a specified index. The index has to be valid to be effective
66 66 * @param index the index to which the data will be set
67 67 * @param data the data to set
68 68 * @remarks this method is only available for a unidimensional ArrayData
69 69 */
70 70 template <int D = Dim, typename = std::enable_if_t<D == 1> >
71 71 void setData(int index, double data) noexcept
72 72 {
73 73 QWriteLocker locker{&m_Lock};
74 74 if (index >= 0 && index < m_Data.at(0).size()) {
75 75 m_Data[0].replace(index, data);
76 76 }
77 77 }
78 78
79 79 /**
80 80 * @return the data as a vector
81 81 * @remarks this method is only available for a unidimensional ArrayData
82 82 */
83 83 template <int D = Dim, typename = std::enable_if_t<D == 1> >
84 84 QVector<double> data() const noexcept
85 85 {
86 86 QReadLocker locker{&m_Lock};
87 87 return m_Data[0];
88 88 }
89 89
90 90 /**
91 91 * @return the data as a vector, as a const reference
92 92 * @remarks this method is only available for a unidimensional ArrayData
93 93 */
94 94 template <int D = Dim, typename = std::enable_if_t<D == 1> >
95 95 const QVector<double> &cdata() const noexcept
96 96 {
97 97 QReadLocker locker{&m_Lock};
98 98 return m_Data.at(0);
99 99 }
100 100
101 // TODO Comment
101 /**
102 * Merges into the array data an other array data
103 * @param other the array data to merge with
104 * @param prepend if true, the other array data is inserted at the beginning, otherwise it is
105 * inserted at the end
106 * @remarks this method is only available for a unidimensional ArrayData
107 */
102 108 template <int D = Dim, typename = std::enable_if_t<D == 1> >
103 void merge(const ArrayData<1> &arrayData)
109 void add(const ArrayData<1> &other, bool prepend = false)
104 110 {
105 111 QWriteLocker locker{&m_Lock};
106 112 if (!m_Data.empty()) {
107 QReadLocker otherLocker{&arrayData.m_Lock};
108 m_Data[0] += arrayData.data();
113 QReadLocker otherLocker{&other.m_Lock};
114
115 if (prepend) {
116 const auto &otherData = other.data();
117 const auto otherDataSize = otherData.size();
118
119 auto &data = m_Data[0];
120 data.insert(data.begin(), otherDataSize, 0.);
121
122 for (auto i = 0; i < otherDataSize; ++i) {
123 data.replace(i, otherData.at(i));
124 }
125 }
126 else {
127 m_Data[0] += other.data();
128 }
109 129 }
110 130 }
111 131
112 132 template <int D = Dim, typename = std::enable_if_t<D == 1> >
113 133 int size() const
114 134 {
115 135 QReadLocker locker{&m_Lock};
116 136 return m_Data[0].size();
117 137 }
118 138
119 139 template <int D = Dim, typename = std::enable_if_t<D == 1> >
120 140 std::shared_ptr<ArrayData<Dim> > sort(const std::vector<int> sortPermutation)
121 141 {
122 142 QReadLocker locker{&m_Lock};
123 143
124 144 const auto &data = m_Data.at(0);
125 145
126 146 // Inits result
127 147 auto sortedData = QVector<double>{};
128 148 sortedData.resize(data.size());
129 149
130 150 std::transform(sortPermutation.cbegin(), sortPermutation.cend(), sortedData.begin(),
131 151 [&data](int i) { return data[i]; });
132 152
133 153 return std::make_shared<ArrayData<Dim> >(std::move(sortedData));
134 154 }
135 155 void clear()
136 156 {
137 157 QWriteLocker locker{&m_Lock};
138 158 m_Data.clear();
139 159 }
140 160
141 161
142 162 private:
143 163 QVector<QVector<double> > m_Data;
144 164 mutable QReadWriteLock m_Lock;
145 165 };
146 166
147 167 #endif // SCIQLOP_ARRAYDATA_H
@@ -1,134 +1,180
1 1 #ifndef SCIQLOP_DATASERIES_H
2 2 #define SCIQLOP_DATASERIES_H
3 3
4 4 #include <Common/SortUtils.h>
5 5
6 6 #include <Data/ArrayData.h>
7 7 #include <Data/IDataSeries.h>
8 8
9 9 #include <QLoggingCategory>
10 10
11 11 #include <QReadLocker>
12 12 #include <QReadWriteLock>
13 13 #include <memory>
14 14
15 15 Q_DECLARE_LOGGING_CATEGORY(LOG_DataSeries)
16 16 Q_LOGGING_CATEGORY(LOG_DataSeries, "DataSeries")
17 17
18 18
19 19 /**
20 20 * @brief The DataSeries class is the base (abstract) implementation of IDataSeries.
21 21 *
22 22 * It proposes to set a dimension for the values ​​data.
23 23 *
24 24 * A DataSeries is always sorted on its x-axis data.
25 25 *
26 26 * @tparam Dim The dimension of the values data
27 27 *
28 28 */
29 29 template <int Dim>
30 30 class DataSeries : public IDataSeries {
31 31 public:
32 32 /// @sa IDataSeries::xAxisData()
33 33 std::shared_ptr<ArrayData<1> > xAxisData() override { return m_XAxisData; }
34 34 const std::shared_ptr<ArrayData<1> > xAxisData() const { return m_XAxisData; }
35 35
36 36 /// @sa IDataSeries::xAxisUnit()
37 37 Unit xAxisUnit() const override { return m_XAxisUnit; }
38 38
39 39 /// @return the values dataset
40 40 std::shared_ptr<ArrayData<Dim> > valuesData() { return m_ValuesData; }
41 41 const std::shared_ptr<ArrayData<Dim> > valuesData() const { return m_ValuesData; }
42 42
43 43 /// @sa IDataSeries::valuesUnit()
44 44 Unit valuesUnit() const override { return m_ValuesUnit; }
45 45
46 46 void clear()
47 47 {
48 48 m_XAxisData->clear();
49 49 m_ValuesData->clear();
50 50 }
51 51
52 /// @sa IDataSeries::merge()
52 /// Merges into the data series an other data series
53 /// @remarks the data series to merge with is cleared after the operation
53 54 void merge(IDataSeries *dataSeries) override
54 55 {
55 if (auto dimDataSeries = dynamic_cast<DataSeries<Dim> *>(dataSeries)) {
56 m_XAxisData->merge(*dimDataSeries->xAxisData());
57 m_ValuesData->merge(*dimDataSeries->valuesData());
58 dimDataSeries->clear();
56 dataSeries->lockWrite();
57 lockWrite();
58
59 if (auto other = dynamic_cast<DataSeries<Dim> *>(dataSeries)) {
60 const auto &otherXAxisData = other->xAxisData()->cdata();
61 const auto &xAxisData = m_XAxisData->cdata();
62
63 // As data series are sorted, we can improve performances of merge, by call the sort
64 // method only if the two data series overlap.
65 if (!otherXAxisData.empty()) {
66 auto firstValue = otherXAxisData.front();
67 auto lastValue = otherXAxisData.back();
68
69 auto xAxisDataBegin = xAxisData.cbegin();
70 auto xAxisDataEnd = xAxisData.cend();
71
72 bool prepend;
73 bool sortNeeded;
74
75 if (std::lower_bound(xAxisDataBegin, xAxisDataEnd, firstValue) == xAxisDataEnd) {
76 // Other data series if after data series
77 prepend = false;
78 sortNeeded = false;
79 }
80 else if (std::upper_bound(xAxisDataBegin, xAxisDataEnd, lastValue)
81 == xAxisDataBegin) {
82 // Other data series if before data series
83 prepend = true;
84 sortNeeded = false;
85 }
86 else {
87 // The two data series overlap
88 prepend = false;
89 sortNeeded = true;
90 }
91
92 // Makes the merge
93 m_XAxisData->add(*other->xAxisData(), prepend);
94 m_ValuesData->add(*other->valuesData(), prepend);
95
96 if (sortNeeded) {
97 sort();
98 }
99 }
100
101 // Clears the other data series
102 other->clear();
59 103 }
60 104 else {
61 105 qCWarning(LOG_DataSeries())
62 << QObject::tr("Dection of a type of IDataSeries we cannot merge with !");
106 << QObject::tr("Detection of a type of IDataSeries we cannot merge with !");
63 107 }
108 unlock();
109 dataSeries->unlock();
64 110 }
65 111
66 112 virtual void lockRead() { m_Lock.lockForRead(); }
67 113 virtual void lockWrite() { m_Lock.lockForWrite(); }
68 114 virtual void unlock() { m_Lock.unlock(); }
69 115
70 116 protected:
71 117 /// Protected ctor (DataSeries is abstract). The vectors must have the same size, otherwise a
72 118 /// DataSeries with no values will be created.
73 119 /// @remarks data series is automatically sorted on its x-axis data
74 120 explicit DataSeries(std::shared_ptr<ArrayData<1> > xAxisData, const Unit &xAxisUnit,
75 121 std::shared_ptr<ArrayData<Dim> > valuesData, const Unit &valuesUnit)
76 122 : m_XAxisData{xAxisData},
77 123 m_XAxisUnit{xAxisUnit},
78 124 m_ValuesData{valuesData},
79 125 m_ValuesUnit{valuesUnit}
80 126 {
81 127 if (m_XAxisData->size() != m_ValuesData->size()) {
82 128 clear();
83 129 }
84 130
85 131 // Sorts data if it's not the case
86 132 const auto &xAxisCData = m_XAxisData->cdata();
87 133 if (!std::is_sorted(xAxisCData.cbegin(), xAxisCData.cend())) {
88 134 sort();
89 135 }
90 136 }
91 137
92 138 /// Copy ctor
93 139 explicit DataSeries(const DataSeries<Dim> &other)
94 140 : m_XAxisData{std::make_shared<ArrayData<1> >(*other.m_XAxisData)},
95 141 m_XAxisUnit{other.m_XAxisUnit},
96 142 m_ValuesData{std::make_shared<ArrayData<Dim> >(*other.m_ValuesData)},
97 143 m_ValuesUnit{other.m_ValuesUnit}
98 144 {
99 145 // Since a series is ordered from its construction and is always ordered, it is not
100 146 // necessary to call the sort method here ('other' is sorted)
101 147 }
102 148
103 149 /// Assignment operator
104 150 template <int D>
105 151 DataSeries &operator=(DataSeries<D> other)
106 152 {
107 153 std::swap(m_XAxisData, other.m_XAxisData);
108 154 std::swap(m_XAxisUnit, other.m_XAxisUnit);
109 155 std::swap(m_ValuesData, other.m_ValuesData);
110 156 std::swap(m_ValuesUnit, other.m_ValuesUnit);
111 157
112 158 return *this;
113 159 }
114 160
115 161 private:
116 162 /**
117 163 * Sorts data series on its x-axis data
118 164 */
119 165 void sort() noexcept
120 166 {
121 167 auto permutation = SortUtils::sortPermutation(*m_XAxisData, std::less<double>());
122 168 m_XAxisData = m_XAxisData->sort(permutation);
123 169 m_ValuesData = m_ValuesData->sort(permutation);
124 170 }
125 171
126 172 std::shared_ptr<ArrayData<1> > m_XAxisData;
127 173 Unit m_XAxisUnit;
128 174 std::shared_ptr<ArrayData<Dim> > m_ValuesData;
129 175 Unit m_ValuesUnit;
130 176
131 177 QReadWriteLock m_Lock;
132 178 };
133 179
134 180 #endif // SCIQLOP_DATASERIES_H
@@ -1,90 +1,86
1 1 #include "Variable/Variable.h"
2 2
3 3 #include <Data/IDataSeries.h>
4 4 #include <Data/SqpDateTime.h>
5 5
6 6 #include <QReadWriteLock>
7 7 #include <QThread>
8 8
9 9 Q_LOGGING_CATEGORY(LOG_Variable, "Variable")
10 10
11 11 struct Variable::VariablePrivate {
12 12 explicit VariablePrivate(const QString &name, const SqpDateTime &dateTime,
13 13 const QVariantHash &metadata)
14 14 : m_Name{name}, m_DateTime{dateTime}, m_Metadata{metadata}, m_DataSeries{nullptr}
15 15 {
16 16 }
17 17
18 18 QString m_Name;
19 19
20 20 SqpDateTime m_DateTime; // The dateTime available in the view and loaded. not the cache.
21 21 QVariantHash m_Metadata;
22 22 std::unique_ptr<IDataSeries> m_DataSeries;
23 23 };
24 24
25 25 Variable::Variable(const QString &name, const SqpDateTime &dateTime, const QVariantHash &metadata)
26 26 : impl{spimpl::make_unique_impl<VariablePrivate>(name, dateTime, metadata)}
27 27 {
28 28 }
29 29
30 30 QString Variable::name() const noexcept
31 31 {
32 32 return impl->m_Name;
33 33 }
34 34
35 35 SqpDateTime Variable::dateTime() const noexcept
36 36 {
37 37 return impl->m_DateTime;
38 38 }
39 39
40 40 void Variable::setDateTime(const SqpDateTime &dateTime) noexcept
41 41 {
42 42 impl->m_DateTime = dateTime;
43 43 }
44 44
45 45 void Variable::setDataSeries(std::shared_ptr<IDataSeries> dataSeries) noexcept
46 46 {
47 47 qCDebug(LOG_Variable()) << "Variable::setDataSeries" << QThread::currentThread()->objectName();
48 48 if (!dataSeries) {
49 49 /// @todo ALX : log
50 50 return;
51 51 }
52 52
53 53 // Inits the data series of the variable
54 54 if (!impl->m_DataSeries) {
55 55 impl->m_DataSeries = dataSeries->clone();
56 56 }
57 57 else {
58 dataSeries->lockWrite();
59 impl->m_DataSeries->lockWrite();
60 58 impl->m_DataSeries->merge(dataSeries.get());
61 impl->m_DataSeries->unlock();
62 dataSeries->unlock();
63 59 // emit updated();
64 60 }
65 61 }
66 62
67 63 IDataSeries *Variable::dataSeries() const noexcept
68 64 {
69 65 return impl->m_DataSeries.get();
70 66 }
71 67
72 68 QVariantHash Variable::metadata() const noexcept
73 69 {
74 70 return impl->m_Metadata;
75 71 }
76 72
77 73 bool Variable::contains(const SqpDateTime &dateTime) const noexcept
78 74 {
79 75 return impl->m_DateTime.contains(dateTime);
80 76 }
81 77
82 78 bool Variable::intersect(const SqpDateTime &dateTime) const noexcept
83 79 {
84 80 return impl->m_DateTime.intersect(dateTime);
85 81 }
86 82
87 83 bool Variable::isInside(const SqpDateTime &dateTime) const noexcept
88 84 {
89 85 return dateTime.contains(SqpDateTime{impl->m_DateTime.m_TStart, impl->m_DateTime.m_TEnd});
90 86 }
General Comments 0
You need to be logged in to leave comments. Login now