annotate armadillo-2.4.4/include/armadillo_bits/injector_meat.hpp @ 5:79b343f3e4b8

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