##// END OF EJS Templates
Handles merge of two data series
Alexandre Leroux -
r451:240544c83cc8
parent child
Show More
@@ -98,14 +98,34 public:
98 return m_Data.at(0);
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 template <int D = Dim, typename = std::enable_if_t<D == 1> >
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 QWriteLocker locker{&m_Lock};
111 QWriteLocker locker{&m_Lock};
106 if (!m_Data.empty()) {
112 if (!m_Data.empty()) {
107 QReadLocker otherLocker{&arrayData.m_Lock};
113 QReadLocker otherLocker{&other.m_Lock};
108 m_Data[0] += arrayData.data();
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
@@ -49,18 +49,64 public:
49 m_ValuesData->clear();
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 void merge(IDataSeries *dataSeries) override
54 void merge(IDataSeries *dataSeries) override
54 {
55 {
55 if (auto dimDataSeries = dynamic_cast<DataSeries<Dim> *>(dataSeries)) {
56 dataSeries->lockWrite();
56 m_XAxisData->merge(*dimDataSeries->xAxisData());
57 lockWrite();
57 m_ValuesData->merge(*dimDataSeries->valuesData());
58
58 dimDataSeries->clear();
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 else {
104 else {
61 qCWarning(LOG_DataSeries())
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 virtual void lockRead() { m_Lock.lockForRead(); }
112 virtual void lockRead() { m_Lock.lockForRead(); }
@@ -55,11 +55,7 void Variable::setDataSeries(std::shared_ptr<IDataSeries> dataSeries) noexcept
55 impl->m_DataSeries = dataSeries->clone();
55 impl->m_DataSeries = dataSeries->clone();
56 }
56 }
57 else {
57 else {
58 dataSeries->lockWrite();
59 impl->m_DataSeries->lockWrite();
60 impl->m_DataSeries->merge(dataSeries.get());
58 impl->m_DataSeries->merge(dataSeries.get());
61 impl->m_DataSeries->unlock();
62 dataSeries->unlock();
63 // emit updated();
59 // emit updated();
64 }
60 }
65 }
61 }
General Comments 0
You need to be logged in to leave comments. Login now