injector_meat.hpp
Go to the documentation of this file.
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 
16 
17 
18 
19 template<typename eT>
20 inline
22  : n_cols(0)
23  {
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  {
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
68  : X(in_X)
69  , n_rows(1)
70  {
72 
73  typedef typename mat_injector<T1>::elem_type eT;
74 
77 
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
95  : X(in_X)
96  , n_rows(1)
97  {
99  arma_ignore(x);
100 
101  typedef typename mat_injector<T1>::elem_type eT;
102 
105 
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
123  {
125 
126  typedef typename mat_injector<T1>::elem_type eT;
127 
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
222  {
224 
225  typedef typename mat_injector<T1>::elem_type eT;
226 
228 
229  (*(A[n_rows-1])).insert(val);
230  }
231 
232 
233 
234 
235 template<typename T1>
236 inline
237 void
239  {
241 
242  typedef typename mat_injector<T1>::elem_type eT;
243 
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>
265 const mat_injector<T1>&
266 operator<<(const mat_injector<T1>& ref, const typename mat_injector<T1>::elem_type val)
267  {
269 
270  ref.insert(val);
271 
272  return ref;
273  }
274 
275 
276 
277 template<typename T1>
279 const mat_injector<T1>&
280 operator<<(const mat_injector<T1>& ref, const injector_end_of_row& x)
281  {
283  arma_ignore(x);
284 
285  ref.end_of_row();
286 
287  return ref;
288  }
289 
290 
291 
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
336  : n_cols(0)
337  {
339 
340  AA = new field<oT>;
341  BB = new field<oT>;
342 
343  field<oT>& A = *AA;
344 
346  }
347 
348 
349 
350 template<typename oT>
351 inline
353  {
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  {
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
403  : X(in_X)
404  , n_rows(1)
405  {
407 
408  typedef typename field_injector<T1>::object_type oT;
409 
412 
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
430  : X(in_X)
431  , n_rows(1)
432  {
434  arma_ignore(x);
435 
436  typedef typename field_injector<T1>::object_type oT;
437 
440 
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
458  {
460 
461  typedef typename field_injector<T1>::object_type oT;
462 
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
516  {
518 
519  typedef typename field_injector<T1>::object_type oT;
520 
522 
523  (*(A[n_rows-1])).insert(val);
524  }
525 
526 
527 
528 
529 template<typename T1>
530 inline
531 void
533  {
535 
536  typedef typename field_injector<T1>::object_type oT;
537 
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>
562 const field_injector<T1>&
563 operator<<(const field_injector<T1>& ref, const typename field_injector<T1>::object_type& val)
564  {
566 
567  ref.insert(val);
568 
569  return ref;
570  }
571 
572 
573 
574 template<typename T1>
576 const field_injector<T1>&
577 operator<<(const field_injector<T1>& ref, const injector_end_of_row& x)
578  {
580  arma_ignore(x);
581 
582  ref.end_of_row();
583 
584  return ref;
585  }
586 
587 
588 
A lightweight array for POD types. If the amount of memory requested is small, the stack is used...
arma_hot static arma_inline void copy(eT *dest, const eT *src, const uword n_elem)
static const uword val
podarray< eT > A
const uword n_rows
number of rows in the field (read-only)
Definition: field_bones.hpp:37
const uword n_cols
number of columns in the field (read-only)
Definition: field_bones.hpp:38
void insert(const object_type &val) const
T1::object_type object_type
void set_size(const uword n_obj_in)
Definition: field_meat.hpp:155
arma_inline const Op< T1, op_max > max(const Base< typename T1::elem_type, T1 > &X, const uword dim=0)
Delayed &#39;maximum values&#39; operation. The dimension, along which the maxima are found, is set via &#39;dim&#39;. For dim = 0, the maximum value of each column is found (i.e. searches by traversing across rows). For dim = 1, the maximum value of each row is found (i.e. searches by traversing across columns). The default is dim = 0.
Definition: fn_max.hpp:28
podarray< mat_injector_row< elem_type > * > * AA
void end_of_row() const
podarray< eT > B
u32 uword
Definition: typedef.hpp:85
podarray< field_injector_row< object_type > * > * AA
void insert(const oT &val) const
static arma_inline T1 & rw(const T1 &x)
internal function to allow modification of data declared as read-only
Definition: access.hpp:23
#define arma_debug_check
Definition: debug.hpp:1084
subview_field< oT > col(const uword col_num)
creation of subview_field (column of a field)
Definition: field_meat.hpp:359
podarray< mat_injector_row< elem_type > * > * BB
mat_injector(T1 &in_X, const elem_type val)
void insert(const elem_type val) const
#define arma_ignore(variable)
void end_of_row() const
podarray< field_injector_row< object_type > * > * BB
#define arma_extra_debug_sigprint
Definition: debug.hpp:1116
subview_field< oT > row(const uword row_num)
creation of subview_field (row of a field)
Definition: field_meat.hpp:329
static const uword val
Definition: field_bones.hpp:22
arma_inline eT * memptr()
#define arma_inline
void insert(const eT val) const
void set_size(const uword new_n_elem)
const uword n_elem
number of elements in the field (read-only)
Definition: field_bones.hpp:39
T1::elem_type elem_type
field_injector(T1 &in_X, const object_type &val)


armadillo_matrix
Author(s):
autogenerated on Fri Apr 16 2021 02:31:57