comparison armadillo-3.900.4/include/armadillo_bits/SpValProxy_meat.hpp @ 49:1ec0e2823891

Switch to using subrepo copies of qm-dsp, nnls-chroma, vamp-plugin-sdk; update Armadillo version; assume build without external BLAS/LAPACK
author Chris Cannam
date Thu, 13 Jun 2013 10:25:24 +0100
parents
children
comparison
equal deleted inserted replaced
48:69251e11a913 49:1ec0e2823891
1 // Copyright (C) 2011-2012 Ryan Curtin
2 // Copyright (C) 2012 Conrad Sanderson
3 //
4 // This Source Code Form is subject to the terms of the Mozilla Public
5 // License, v. 2.0. If a copy of the MPL was not distributed with this
6 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
7
8 //! \addtogroup SpValProxy
9 //! @{
10
11 //! SpValProxy implementation.
12 template<typename T1>
13 arma_inline
14 SpValProxy<T1>::SpValProxy(uword in_row, uword in_col, T1& in_parent, eT* in_val_ptr)
15 : row(in_row)
16 , col(in_col)
17 , val_ptr(in_val_ptr)
18 , parent(in_parent)
19 {
20 // Nothing to do.
21 }
22
23
24
25 template<typename T1>
26 arma_inline
27 SpValProxy<T1>&
28 SpValProxy<T1>::operator=(const SpValProxy<T1>& rhs)
29 {
30 return (*this).operator=(eT(rhs));
31 }
32
33
34
35 template<typename T1>
36 template<typename T2>
37 arma_inline
38 SpValProxy<T1>&
39 SpValProxy<T1>::operator=(const SpValProxy<T2>& rhs)
40 {
41 return (*this).operator=(eT(rhs));
42 }
43
44
45
46 template<typename T1>
47 arma_inline
48 SpValProxy<T1>&
49 SpValProxy<T1>::operator=(const eT rhs)
50 {
51 if (rhs != eT(0)) // A nonzero element is being assigned.
52 {
53
54 if (val_ptr)
55 {
56 // The value exists and merely needs to be updated.
57 *val_ptr = rhs;
58 }
59
60 else
61 {
62 // The value is nonzero and must be added.
63 val_ptr = &parent.add_element(row, col, rhs);
64 }
65
66 }
67 else // A zero is being assigned.~
68 {
69
70 if (val_ptr)
71 {
72 // The element exists, but we need to remove it, because it is being set to 0.
73 parent.delete_element(row, col);
74 val_ptr = NULL;
75 }
76
77 // If the element does not exist, we do not need to do anything at all.
78
79 }
80
81 return *this;
82 }
83
84
85
86 template<typename T1>
87 arma_inline
88 SpValProxy<T1>&
89 SpValProxy<T1>::operator+=(const eT rhs)
90 {
91 if (val_ptr)
92 {
93 // The value already exists and merely needs to be updated.
94 *val_ptr += rhs;
95 check_zero();
96 }
97 else
98 {
99 if (rhs != eT(0))
100 {
101 // The value does not exist and must be added.
102 val_ptr = &parent.add_element(row, col, rhs);
103 }
104 }
105
106 return *this;
107 }
108
109
110
111 template<typename T1>
112 arma_inline
113 SpValProxy<T1>&
114 SpValProxy<T1>::operator-=(const eT rhs)
115 {
116 if (val_ptr)
117 {
118 // The value already exists and merely needs to be updated.
119 *val_ptr -= rhs;
120 check_zero();
121 }
122 else
123 {
124 if (rhs != eT(0))
125 {
126 // The value does not exist and must be added.
127 val_ptr = &parent.add_element(row, col, -rhs);
128 }
129 }
130
131 return *this;
132 }
133
134
135
136 template<typename T1>
137 arma_inline
138 SpValProxy<T1>&
139 SpValProxy<T1>::operator*=(const eT rhs)
140 {
141 if (rhs != eT(0))
142 {
143
144 if (val_ptr)
145 {
146 // The value already exists and merely needs to be updated.
147 *val_ptr *= rhs;
148 check_zero();
149 }
150
151 }
152 else
153 {
154
155 if (val_ptr)
156 {
157 // Since we are multiplying by zero, the value can be deleted.
158 parent.delete_element(row, col);
159 val_ptr = NULL;
160 }
161
162 }
163
164 return *this;
165 }
166
167
168
169 template<typename T1>
170 arma_inline
171 SpValProxy<T1>&
172 SpValProxy<T1>::operator/=(const eT rhs)
173 {
174 if (rhs != eT(0)) // I hope this is true!
175 {
176
177 if (val_ptr)
178 {
179 *val_ptr /= rhs;
180 check_zero();
181 }
182
183 }
184 else
185 {
186
187 if (val_ptr)
188 {
189 *val_ptr /= rhs; // That is where it gets ugly.
190 // Now check if it's 0.
191 if (*val_ptr == eT(0))
192 {
193 parent.delete_element(row, col);
194 val_ptr = NULL;
195 }
196 }
197
198 else
199 {
200 eT val = eT(0) / rhs; // This may vary depending on type and implementation.
201
202 if (val != eT(0))
203 {
204 // Ok, now we have to add it.
205 val_ptr = &parent.add_element(row, col, val);
206 }
207
208 }
209 }
210
211 return *this;
212 }
213
214
215
216 template<typename T1>
217 arma_inline
218 SpValProxy<T1>&
219 SpValProxy<T1>::operator++()
220 {
221 if (val_ptr)
222 {
223 (*val_ptr) += eT(1);
224 check_zero();
225 }
226
227 else
228 {
229 val_ptr = &parent.add_element(row, col, eT(1));
230 }
231
232 return *this;
233 }
234
235
236
237 template<typename T1>
238 arma_inline
239 SpValProxy<T1>&
240 SpValProxy<T1>::operator--()
241 {
242 if (val_ptr)
243 {
244 (*val_ptr) -= eT(1);
245 check_zero();
246 }
247
248 else
249 {
250 val_ptr = &parent.add_element(row, col, eT(-1));
251 }
252
253 return *this;
254 }
255
256
257
258 template<typename T1>
259 arma_inline
260 typename T1::elem_type
261 SpValProxy<T1>::operator++(const int)
262 {
263 if (val_ptr)
264 {
265 (*val_ptr) += eT(1);
266 check_zero();
267 }
268
269 else
270 {
271 val_ptr = &parent.add_element(row, col, eT(1));
272 }
273
274 if (val_ptr) // It may have changed to now be 0.
275 {
276 return *(val_ptr) - eT(1);
277 }
278 else
279 {
280 return eT(0);
281 }
282 }
283
284
285
286 template<typename T1>
287 arma_inline
288 typename T1::elem_type
289 SpValProxy<T1>::operator--(const int)
290 {
291 if (val_ptr)
292 {
293 (*val_ptr) -= eT(1);
294 check_zero();
295 }
296
297 else
298 {
299 val_ptr = &parent.add_element(row, col, eT(-1));
300 }
301
302 if (val_ptr) // It may have changed to now be 0.
303 {
304 return *(val_ptr) + eT(1);
305 }
306 else
307 {
308 return eT(0);
309 }
310 }
311
312
313
314 template<typename T1>
315 arma_inline
316 SpValProxy<T1>::operator eT() const
317 {
318 if (val_ptr)
319 {
320 return *val_ptr;
321 }
322 else
323 {
324 return eT(0);
325 }
326 }
327
328
329
330 template<typename T1>
331 arma_inline
332 arma_hot
333 void
334 SpValProxy<T1>::check_zero()
335 {
336 if (*val_ptr == eT(0))
337 {
338 parent.delete_element(row, col);
339 val_ptr = NULL;
340 }
341 }
342
343
344
345 //! @}