##// END OF EJS Templates
Adapts add() method to 2-dim array data
Alexandre Leroux -
r506:0599461b95ff
parent child
Show More
@@ -128,6 +128,19 public:
128 }
128 }
129
129
130 /**
130 /**
131 * @return the data of a component
132 * @param componentIndex the index of the component to retrieve the data
133 * @return the component's data, empty vector if the index is invalid
134 */
135 QVector<double> data(int componentIndex) const noexcept
136 {
137 QReadLocker locker{&m_Lock};
138
139 return (componentIndex >= 0 && componentIndex < m_Data.size()) ? m_Data.at(componentIndex)
140 : QVector<double>{};
141 }
142
143 /**
131 * @return the data as a vector, as a const reference
144 * @return the data as a vector, as a const reference
132 * @remarks this method is only available for a unidimensional ArrayData
145 * @remarks this method is only available for a unidimensional ArrayData
133 */
146 */
@@ -139,24 +152,28 public:
139 }
152 }
140
153
141 /**
154 /**
142 * Merges into the array data an other array data
155 * Merges into the array data an other array data. The two array datas must have the same number
156 * of components so the merge can be done
143 * @param other the array data to merge with
157 * @param other the array data to merge with
144 * @param prepend if true, the other array data is inserted at the beginning, otherwise it is
158 * @param prepend if true, the other array data is inserted at the beginning, otherwise it is
145 * inserted at the end
159 * inserted at the end
146 * @remarks this method is only available for a unidimensional ArrayData
147 */
160 */
148 template <int D = Dim, typename = std::enable_if_t<D == 1> >
161 void add(const ArrayData<Dim> &other, bool prepend = false)
149 void add(const ArrayData<1> &other, bool prepend = false)
150 {
162 {
151 QWriteLocker locker{&m_Lock};
163 QWriteLocker locker{&m_Lock};
152 if (!m_Data.empty()) {
164 QReadLocker otherLocker{&other.m_Lock};
153 QReadLocker otherLocker{&other.m_Lock};
165
166 auto nbComponents = m_Data.size();
167 if (nbComponents != other.m_Data.size()) {
168 return;
169 }
154
170
171 for (auto componentIndex = 0; componentIndex < nbComponents; ++componentIndex) {
155 if (prepend) {
172 if (prepend) {
156 const auto &otherData = other.data();
173 const auto &otherData = other.data(componentIndex);
157 const auto otherDataSize = otherData.size();
174 const auto otherDataSize = otherData.size();
158
175
159 auto &data = m_Data[0];
176 auto &data = m_Data[componentIndex];
160 data.insert(data.begin(), otherDataSize, 0.);
177 data.insert(data.begin(), otherDataSize, 0.);
161
178
162 for (auto i = 0; i < otherDataSize; ++i) {
179 for (auto i = 0; i < otherDataSize; ++i) {
@@ -164,7 +181,7 public:
164 }
181 }
165 }
182 }
166 else {
183 else {
167 m_Data[0] += other.data();
184 m_Data[componentIndex] += other.data(componentIndex);
168 }
185 }
169 }
186 }
170 }
187 }
General Comments 0
You need to be logged in to leave comments. Login now