comparison armadillo-3.900.4/include/armadillo_bits/injector_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) 2010 NICTA (www.nicta.com.au)
2 // Copyright (C) 2010 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
9 //! \addtogroup injector
10 //! @{
11
12
13
14 template<typename eT>
15 inline
16 mat_injector_row<eT>::mat_injector_row()
17 : n_cols(0)
18 {
19 arma_extra_debug_sigprint();
20
21 A.set_size( podarray_prealloc_n_elem::val );
22 }
23
24
25
26 template<typename eT>
27 inline
28 void
29 mat_injector_row<eT>::insert(const eT val) const
30 {
31 arma_extra_debug_sigprint();
32
33 if(n_cols < A.n_elem)
34 {
35 A[n_cols] = val;
36 ++n_cols;
37 }
38 else
39 {
40 B.set_size(2 * A.n_elem);
41
42 arrayops::copy(B.memptr(), A.memptr(), n_cols);
43
44 B[n_cols] = val;
45 ++n_cols;
46
47 std::swap( access::rw(A.mem), access::rw(B.mem) );
48 std::swap( access::rw(A.n_elem), access::rw(B.n_elem) );
49 }
50 }
51
52
53
54 //
55 //
56 //
57
58
59
60 template<typename T1>
61 inline
62 mat_injector<T1>::mat_injector(T1& in_X, const typename mat_injector<T1>::elem_type val)
63 : X(in_X)
64 , n_rows(1)
65 {
66 arma_extra_debug_sigprint();
67
68 typedef typename mat_injector<T1>::elem_type eT;
69
70 AA = new podarray< mat_injector_row<eT>* >;
71 BB = new podarray< mat_injector_row<eT>* >;
72
73 podarray< mat_injector_row<eT>* >& A = *AA;
74
75 A.set_size(n_rows);
76
77 for(uword row=0; row<n_rows; ++row)
78 {
79 A[row] = new mat_injector_row<eT>;
80 }
81
82 (*(A[0])).insert(val);
83 }
84
85
86
87 template<typename T1>
88 inline
89 mat_injector<T1>::mat_injector(T1& in_X, const injector_end_of_row<>& x)
90 : X(in_X)
91 , n_rows(1)
92 {
93 arma_extra_debug_sigprint();
94 arma_ignore(x);
95
96 typedef typename mat_injector<T1>::elem_type eT;
97
98 AA = new podarray< mat_injector_row<eT>* >;
99 BB = new podarray< mat_injector_row<eT>* >;
100
101 podarray< mat_injector_row<eT>* >& A = *AA;
102
103 A.set_size(n_rows);
104
105 for(uword row=0; row<n_rows; ++row)
106 {
107 A[row] = new mat_injector_row<eT>;
108 }
109
110 (*this).end_of_row();
111 }
112
113
114
115 template<typename T1>
116 inline
117 mat_injector<T1>::~mat_injector()
118 {
119 arma_extra_debug_sigprint();
120
121 typedef typename mat_injector<T1>::elem_type eT;
122
123 podarray< mat_injector_row<eT>* >& A = *AA;
124
125 if(n_rows > 0)
126 {
127 uword max_n_cols = (*(A[0])).n_cols;
128
129 for(uword row=1; row<n_rows; ++row)
130 {
131 const uword n_cols = (*(A[row])).n_cols;
132
133 if(max_n_cols < n_cols)
134 {
135 max_n_cols = n_cols;
136 }
137 }
138
139 const uword max_n_rows = ((*(A[n_rows-1])).n_cols == 0) ? n_rows-1 : n_rows;
140
141 if(is_Mat_only<T1>::value == true)
142 {
143 X.set_size(max_n_rows, max_n_cols);
144
145 for(uword row=0; row<max_n_rows; ++row)
146 {
147 const uword n_cols = (*(A[row])).n_cols;
148
149 for(uword col=0; col<n_cols; ++col)
150 {
151 X.at(row,col) = (*(A[row])).A[col];
152 }
153
154 for(uword col=n_cols; col<max_n_cols; ++col)
155 {
156 X.at(row,col) = eT(0);
157 }
158 }
159 }
160 else
161 if(is_Row<T1>::value == true)
162 {
163 arma_debug_check( (max_n_rows > 1), "matrix initialisation: incompatible dimensions" );
164
165 const uword n_cols = (*(A[0])).n_cols;
166
167 X.set_size(1, n_cols);
168
169 arrayops::copy( X.memptr(), (*(A[0])).A.memptr(), n_cols );
170 }
171 else
172 if(is_Col<T1>::value == true)
173 {
174 const bool is_vec = ( (max_n_rows == 1) || (max_n_cols == 1) );
175
176 arma_debug_check( (is_vec == false), "matrix initialisation: incompatible dimensions" );
177
178 const uword n_elem = (std::max)(max_n_rows, max_n_cols);
179
180 X.set_size(n_elem, 1);
181
182 uword i = 0;
183 for(uword row=0; row<max_n_rows; ++row)
184 {
185 const uword n_cols = (*(A[0])).n_cols;
186
187 for(uword col=0; col<n_cols; ++col)
188 {
189 X[i] = (*(A[row])).A[col];
190 ++i;
191 }
192
193 for(uword col=n_cols; col<max_n_cols; ++col)
194 {
195 X[i] = eT(0);
196 ++i;
197 }
198 }
199 }
200 }
201
202 for(uword row=0; row<n_rows; ++row)
203 {
204 delete A[row];
205 }
206
207 delete AA;
208 delete BB;
209 }
210
211
212
213 template<typename T1>
214 inline
215 void
216 mat_injector<T1>::insert(const typename mat_injector<T1>::elem_type val) const
217 {
218 arma_extra_debug_sigprint();
219
220 typedef typename mat_injector<T1>::elem_type eT;
221
222 podarray< mat_injector_row<eT>* >& A = *AA;
223
224 (*(A[n_rows-1])).insert(val);
225 }
226
227
228
229
230 template<typename T1>
231 inline
232 void
233 mat_injector<T1>::end_of_row() const
234 {
235 arma_extra_debug_sigprint();
236
237 typedef typename mat_injector<T1>::elem_type eT;
238
239 podarray< mat_injector_row<eT>* >& A = *AA;
240 podarray< mat_injector_row<eT>* >& B = *BB;
241
242 B.set_size( n_rows+1 );
243
244 arrayops::copy(B.memptr(), A.memptr(), n_rows);
245
246 for(uword row=n_rows; row<(n_rows+1); ++row)
247 {
248 B[row] = new mat_injector_row<eT>;
249 }
250
251 std::swap(AA, BB);
252
253 n_rows += 1;
254 }
255
256
257
258 template<typename T1>
259 arma_inline
260 const mat_injector<T1>&
261 operator<<(const mat_injector<T1>& ref, const typename mat_injector<T1>::elem_type val)
262 {
263 arma_extra_debug_sigprint();
264
265 ref.insert(val);
266
267 return ref;
268 }
269
270
271
272 template<typename T1>
273 arma_inline
274 const mat_injector<T1>&
275 operator<<(const mat_injector<T1>& ref, const injector_end_of_row<>& x)
276 {
277 arma_extra_debug_sigprint();
278 arma_ignore(x);
279
280 ref.end_of_row();
281
282 return ref;
283 }
284
285
286
287 //// using a mixture of operator << and , doesn't work yet
288 //// e.g. A << 1, 2, 3 << endr
289 //// in the above "3 << endr" requires special handling.
290 //// similarly, special handling is necessary for "endr << 3"
291 ////
292 // template<typename T1>
293 // arma_inline
294 // const mat_injector<T1>&
295 // operator,(const mat_injector<T1>& ref, const typename mat_injector<T1>::elem_type val)
296 // {
297 // arma_extra_debug_sigprint();
298 //
299 // ref.insert(val);
300 //
301 // return ref;
302 // }
303
304
305
306 // template<typename T1>
307 // arma_inline
308 // const mat_injector<T1>&
309 // operator,(const mat_injector<T1>& ref, const injector_end_of_row<>& x)
310 // {
311 // arma_extra_debug_sigprint();
312 // arma_ignore(x);
313 //
314 // ref.end_of_row();
315 //
316 // return ref;
317 // }
318
319
320
321
322 //
323 //
324 //
325
326
327
328 template<typename oT>
329 inline
330 field_injector_row<oT>::field_injector_row()
331 : n_cols(0)
332 {
333 arma_extra_debug_sigprint();
334
335 AA = new field<oT>;
336 BB = new field<oT>;
337
338 field<oT>& A = *AA;
339
340 A.set_size( field_prealloc_n_elem::val );
341 }
342
343
344
345 template<typename oT>
346 inline
347 field_injector_row<oT>::~field_injector_row()
348 {
349 arma_extra_debug_sigprint();
350
351 delete AA;
352 delete BB;
353 }
354
355
356
357 template<typename oT>
358 inline
359 void
360 field_injector_row<oT>::insert(const oT& val) const
361 {
362 arma_extra_debug_sigprint();
363
364 field<oT>& A = *AA;
365 field<oT>& B = *BB;
366
367 if(n_cols < A.n_elem)
368 {
369 A[n_cols] = val;
370 ++n_cols;
371 }
372 else
373 {
374 B.set_size(2 * A.n_elem);
375
376 for(uword i=0; i<n_cols; ++i)
377 {
378 B[i] = A[i];
379 }
380
381 B[n_cols] = val;
382 ++n_cols;
383
384 std::swap(AA, BB);
385 }
386 }
387
388
389
390 //
391 //
392 //
393
394
395 template<typename T1>
396 inline
397 field_injector<T1>::field_injector(T1& in_X, const typename field_injector<T1>::object_type& val)
398 : X(in_X)
399 , n_rows(1)
400 {
401 arma_extra_debug_sigprint();
402
403 typedef typename field_injector<T1>::object_type oT;
404
405 AA = new podarray< field_injector_row<oT>* >;
406 BB = new podarray< field_injector_row<oT>* >;
407
408 podarray< field_injector_row<oT>* >& A = *AA;
409
410 A.set_size(n_rows);
411
412 for(uword row=0; row<n_rows; ++row)
413 {
414 A[row] = new field_injector_row<oT>;
415 }
416
417 (*(A[0])).insert(val);
418 }
419
420
421
422 template<typename T1>
423 inline
424 field_injector<T1>::field_injector(T1& in_X, const injector_end_of_row<>& x)
425 : X(in_X)
426 , n_rows(1)
427 {
428 arma_extra_debug_sigprint();
429 arma_ignore(x);
430
431 typedef typename field_injector<T1>::object_type oT;
432
433 AA = new podarray< field_injector_row<oT>* >;
434 BB = new podarray< field_injector_row<oT>* >;
435
436 podarray< field_injector_row<oT>* >& A = *AA;
437
438 A.set_size(n_rows);
439
440 for(uword row=0; row<n_rows; ++row)
441 {
442 A[row] = new field_injector_row<oT>;
443 }
444
445 (*this).end_of_row();
446 }
447
448
449
450 template<typename T1>
451 inline
452 field_injector<T1>::~field_injector()
453 {
454 arma_extra_debug_sigprint();
455
456 typedef typename field_injector<T1>::object_type oT;
457
458 podarray< field_injector_row<oT>* >& A = *AA;
459
460 if(n_rows > 0)
461 {
462 uword max_n_cols = (*(A[0])).n_cols;
463
464 for(uword row=1; row<n_rows; ++row)
465 {
466 const uword n_cols = (*(A[row])).n_cols;
467
468 if(max_n_cols < n_cols)
469 {
470 max_n_cols = n_cols;
471 }
472 }
473
474 const uword max_n_rows = ((*(A[n_rows-1])).n_cols == 0) ? n_rows-1 : n_rows;
475
476 X.set_size(max_n_rows, max_n_cols);
477
478 for(uword row=0; row<max_n_rows; ++row)
479 {
480 const uword n_cols = (*(A[row])).n_cols;
481
482 for(uword col=0; col<n_cols; ++col)
483 {
484 const field<oT>& tmp = *((*(A[row])).AA);
485 X.at(row,col) = tmp[col];
486 }
487
488 for(uword col=n_cols; col<max_n_cols; ++col)
489 {
490 X.at(row,col) = oT();
491 }
492 }
493 }
494
495
496 for(uword row=0; row<n_rows; ++row)
497 {
498 delete A[row];
499 }
500
501 delete AA;
502 delete BB;
503 }
504
505
506
507 template<typename T1>
508 inline
509 void
510 field_injector<T1>::insert(const typename field_injector<T1>::object_type& val) const
511 {
512 arma_extra_debug_sigprint();
513
514 typedef typename field_injector<T1>::object_type oT;
515
516 podarray< field_injector_row<oT>* >& A = *AA;
517
518 (*(A[n_rows-1])).insert(val);
519 }
520
521
522
523
524 template<typename T1>
525 inline
526 void
527 field_injector<T1>::end_of_row() const
528 {
529 arma_extra_debug_sigprint();
530
531 typedef typename field_injector<T1>::object_type oT;
532
533 podarray< field_injector_row<oT>* >& A = *AA;
534 podarray< field_injector_row<oT>* >& B = *BB;
535
536 B.set_size( n_rows+1 );
537
538 for(uword row=0; row<n_rows; ++row)
539 {
540 B[row] = A[row];
541 }
542
543 for(uword row=n_rows; row<(n_rows+1); ++row)
544 {
545 B[row] = new field_injector_row<oT>;
546 }
547
548 std::swap(AA, BB);
549
550 n_rows += 1;
551 }
552
553
554
555 template<typename T1>
556 arma_inline
557 const field_injector<T1>&
558 operator<<(const field_injector<T1>& ref, const typename field_injector<T1>::object_type& val)
559 {
560 arma_extra_debug_sigprint();
561
562 ref.insert(val);
563
564 return ref;
565 }
566
567
568
569 template<typename T1>
570 arma_inline
571 const field_injector<T1>&
572 operator<<(const field_injector<T1>& ref, const injector_end_of_row<>& x)
573 {
574 arma_extra_debug_sigprint();
575 arma_ignore(x);
576
577 ref.end_of_row();
578
579 return ref;
580 }
581
582
583
584 //! @}