comparison armadillo-2.4.4/include/armadillo_bits/injector_meat.hpp @ 0:8b6102e2a9b0

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