00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00017
00018
00019 template<typename oT>
00020 inline
00021 field<oT>::~field()
00022 {
00023 arma_extra_debug_sigprint_this(this);
00024
00025 delete_objects();
00026
00027 if(n_elem > sizeof(mem_local)/sizeof(oT*) )
00028 {
00029 delete [] mem;
00030 }
00031
00032 if(arma_config::debug == true)
00033 {
00034
00035 access::rw(n_rows) = 0;
00036 access::rw(n_cols) = 0;
00037 access::rw(n_elem) = 0;
00038 mem = 0;
00039 }
00040 }
00041
00042
00043
00044 template<typename oT>
00045 inline
00046 field<oT>::field()
00047 : n_rows(0)
00048 , n_cols(0)
00049 , n_elem(0)
00050 , mem(0)
00051 {
00052 arma_extra_debug_sigprint_this(this);
00053 }
00054
00055
00056
00058 template<typename oT>
00059 inline
00060 field<oT>::field(const field& x)
00061 : n_rows(0)
00062 , n_cols(0)
00063 , n_elem(0)
00064 , mem(0)
00065 {
00066 arma_extra_debug_sigprint(arma_boost::format("this = %x x = %x") % this % &x);
00067
00068 init(x);
00069 }
00070
00071
00072
00074 template<typename oT>
00075 inline
00076 const field<oT>&
00077 field<oT>::operator=(const field& x)
00078 {
00079 arma_extra_debug_sigprint();
00080
00081 init(x);
00082 return *this;
00083 }
00084
00085
00086
00088 template<typename oT>
00089 inline
00090 field<oT>::field(const subview_field<oT>& X)
00091 : n_rows(0)
00092 , n_cols(0)
00093 , n_elem(0)
00094 , mem(0)
00095 {
00096 arma_extra_debug_sigprint_this(this);
00097
00098 this->operator=(X);
00099 }
00100
00101
00102
00104 template<typename oT>
00105 inline
00106 const field<oT>&
00107 field<oT>::operator=(const subview_field<oT>& X)
00108 {
00109 arma_extra_debug_sigprint();
00110
00111 subview_field<oT>::extract(*this, X);
00112 return *this;
00113 }
00114
00115
00116
00119 template<typename oT>
00120 inline
00121 field<oT>::field(const uword n_elem_in)
00122 : n_rows(0)
00123 , n_cols(0)
00124 , n_elem(0)
00125 , mem(0)
00126 {
00127 arma_extra_debug_sigprint_this(this);
00128
00129 init(n_elem_in, 1);
00130 }
00131
00132
00133
00135 template<typename oT>
00136 inline
00137 field<oT>::field(const uword n_rows_in, const uword n_cols_in)
00138 : n_rows(0)
00139 , n_cols(0)
00140 , n_elem(0)
00141 , mem(0)
00142 {
00143 arma_extra_debug_sigprint_this(this);
00144
00145 init(n_rows_in, n_cols_in);
00146 }
00147
00148
00149
00152 template<typename oT>
00153 inline
00154 void
00155 field<oT>::set_size(const uword n_elem_in)
00156 {
00157 arma_extra_debug_sigprint(arma_boost::format("n_elem_in = %d") % n_elem_in);
00158
00159 init(n_elem_in, 1);
00160 }
00161
00162
00163
00165 template<typename oT>
00166 inline
00167 void
00168 field<oT>::set_size(const uword n_rows_in, const uword n_cols_in)
00169 {
00170 arma_extra_debug_sigprint(arma_boost::format("n_rows_in = %d, n_cols_in = %d") % n_rows_in % n_cols_in);
00171
00172 init(n_rows_in, n_cols_in);
00173 }
00174
00175
00176
00178 template<typename oT>
00179 template<typename oT2>
00180 inline
00181 void
00182 field<oT>::copy_size(const field<oT2>& x)
00183 {
00184 arma_extra_debug_sigprint();
00185
00186 init(x.n_rows, x.n_cols);
00187 }
00188
00189
00190
00192 template<typename oT>
00193 arma_inline
00194 oT&
00195 field<oT>::operator[] (const uword i)
00196 {
00197 return (*mem[i]);
00198 }
00199
00200
00201
00203 template<typename oT>
00204 arma_inline
00205 const oT&
00206 field<oT>::operator[] (const uword i) const
00207 {
00208 return (*mem[i]);
00209 }
00210
00211
00212
00214 template<typename oT>
00215 arma_inline
00216 oT&
00217 field<oT>::at(const uword i)
00218 {
00219 return (*mem[i]);
00220 }
00221
00222
00223
00225 template<typename oT>
00226 arma_inline
00227 const oT&
00228 field<oT>::at(const uword i) const
00229 {
00230 return (*mem[i]);
00231 }
00232
00233
00234
00236 template<typename oT>
00237 arma_inline
00238 oT&
00239 field<oT>::operator() (const uword i)
00240 {
00241 arma_debug_check( (i >= n_elem), "field::operator(): index out of bounds");
00242 return (*mem[i]);
00243 }
00244
00245
00246
00248 template<typename oT>
00249 arma_inline
00250 const oT&
00251 field<oT>::operator() (const uword i) const
00252 {
00253 arma_debug_check( (i >= n_elem), "field::operator(): index out of bounds");
00254 return (*mem[i]);
00255 }
00256
00257
00258
00260 template<typename oT>
00261 arma_inline
00262 oT&
00263 field<oT>::operator() (const uword in_row, const uword in_col)
00264 {
00265 arma_debug_check( ((in_row >= n_rows) || (in_col >= n_cols)), "field::operator(): index out of bounds");
00266 return (*mem[in_row + in_col*n_rows]);
00267 }
00268
00269
00270
00272 template<typename oT>
00273 arma_inline
00274 const oT&
00275 field<oT>::operator() (const uword in_row, const uword in_col) const
00276 {
00277 arma_debug_check( ((in_row >= n_rows) || (in_col >= n_cols)), "field::operator(): index out of bounds");
00278 return (*mem[in_row + in_col*n_rows]);
00279 }
00280
00281
00282
00284 template<typename oT>
00285 arma_inline
00286 oT&
00287 field<oT>::at(const uword in_row, const uword in_col)
00288 {
00289 return (*mem[in_row + in_col*n_rows]);
00290 }
00291
00292
00293
00295 template<typename oT>
00296 arma_inline
00297 const oT&
00298 field<oT>::at(const uword in_row, const uword in_col) const
00299 {
00300 return (*mem[in_row + in_col*n_rows]);
00301 }
00302
00303
00304
00305 template<typename oT>
00306 inline
00307 field_injector< field<oT> >
00308 field<oT>::operator<<(const oT& val)
00309 {
00310 return field_injector< field<oT> >(*this, val);
00311 }
00312
00313
00314
00315 template<typename oT>
00316 inline
00317 field_injector< field<oT> >
00318 field<oT>::operator<<(const injector_end_of_row& x)
00319 {
00320 return field_injector< field<oT> >(*this, x);
00321 }
00322
00323
00324
00326 template<typename oT>
00327 inline
00328 subview_field<oT>
00329 field<oT>::row(const uword row_num)
00330 {
00331 arma_extra_debug_sigprint();
00332
00333 arma_debug_check( (row_num >= n_rows), "field::row(): row out of bounds" );
00334
00335 return subview_field<oT>(*this, row_num, 0, 1, n_cols);
00336 }
00337
00338
00339
00341 template<typename oT>
00342 inline
00343 const subview_field<oT>
00344 field<oT>::row(const uword row_num) const
00345 {
00346 arma_extra_debug_sigprint();
00347
00348 arma_debug_check( (row_num >= n_rows), "field::row(): row out of bounds" );
00349
00350 return subview_field<oT>(*this, row_num, 0, 1, n_cols);
00351 }
00352
00353
00354
00356 template<typename oT>
00357 inline
00358 subview_field<oT>
00359 field<oT>::col(const uword col_num)
00360 {
00361 arma_extra_debug_sigprint();
00362
00363 arma_debug_check( (col_num >= n_cols), "field::col(): out of bounds");
00364
00365 return subview_field<oT>(*this, 0, col_num, n_rows, 1);
00366 }
00367
00368
00369
00371 template<typename oT>
00372 inline
00373 const subview_field<oT>
00374 field<oT>::col(const uword col_num) const
00375 {
00376 arma_extra_debug_sigprint();
00377
00378 arma_debug_check( (col_num >= n_cols), "field::col(): out of bounds");
00379
00380 return subview_field<oT>(*this, 0, col_num, n_rows, 1);
00381 }
00382
00383
00384
00386 template<typename oT>
00387 inline
00388 subview_field<oT>
00389 field<oT>::rows(const uword in_row1, const uword in_row2)
00390 {
00391 arma_extra_debug_sigprint();
00392
00393 arma_debug_check
00394 (
00395 ( (in_row1 > in_row2) || (in_row2 >= n_rows) ),
00396 "field::rows(): indicies out of bounds or incorrectly used"
00397 );
00398
00399 const uword sub_n_rows = in_row2 - in_row1 + 1;
00400
00401 return subview_field<oT>(*this, in_row1, 0, sub_n_rows, n_cols);
00402 }
00403
00404
00405
00407 template<typename oT>
00408 inline
00409 const subview_field<oT>
00410 field<oT>::rows(const uword in_row1, const uword in_row2) const
00411 {
00412 arma_extra_debug_sigprint();
00413
00414 arma_debug_check
00415 (
00416 ( (in_row1 > in_row2) || (in_row2 >= n_rows) ),
00417 "field::rows(): indicies out of bounds or incorrectly used"
00418 );
00419
00420 const uword sub_n_rows = in_row2 - in_row1 + 1;
00421
00422 return subview_field<oT>(*this, in_row1, 0, sub_n_rows, n_cols);
00423 }
00424
00425
00426
00428 template<typename oT>
00429 inline
00430 subview_field<oT>
00431 field<oT>::cols(const uword in_col1, const uword in_col2)
00432 {
00433 arma_extra_debug_sigprint();
00434
00435 arma_debug_check
00436 (
00437 ( (in_col1 > in_col2) || (in_col2 >= n_cols) ),
00438 "field::cols(): indicies out of bounds or incorrectly used"
00439 );
00440
00441 const uword sub_n_cols = in_col2 - in_col1 + 1;
00442
00443 return subview_field<oT>(*this, 0, in_col1, n_rows, sub_n_cols);
00444 }
00445
00446
00447
00449 template<typename oT>
00450 inline
00451 const subview_field<oT>
00452 field<oT>::cols(const uword in_col1, const uword in_col2) const
00453 {
00454 arma_extra_debug_sigprint();
00455
00456 arma_debug_check
00457 (
00458 ( (in_col1 > in_col2) || (in_col2 >= n_cols) ),
00459 "field::cols(): indicies out of bounds or incorrectly used"
00460 );
00461
00462 const uword sub_n_cols = in_col2 - in_col1 + 1;
00463
00464 return subview_field<oT>(*this, 0, in_col1, n_rows, sub_n_cols);
00465 }
00466
00467
00468
00470 template<typename oT>
00471 inline
00472 subview_field<oT>
00473 field<oT>::subfield(const uword in_row1, const uword in_col1, const uword in_row2, const uword in_col2)
00474 {
00475 arma_extra_debug_sigprint();
00476
00477 arma_debug_check
00478 (
00479 (in_row1 > in_row2) || (in_col1 > in_col2) || (in_row2 >= n_rows) || (in_col2 >= n_cols),
00480 "field::subfield(): indices out of bounds or incorrectly used"
00481 );
00482
00483 const uword sub_n_rows = in_row2 - in_row1 + 1;
00484 const uword sub_n_cols = in_col2 - in_col1 + 1;
00485
00486 return subview_field<oT>(*this, in_row1, in_col1, sub_n_rows, sub_n_cols);
00487 }
00488
00489
00490
00492 template<typename oT>
00493 inline
00494 const subview_field<oT>
00495 field<oT>::subfield(const uword in_row1, const uword in_col1, const uword in_row2, const uword in_col2) const
00496 {
00497 arma_extra_debug_sigprint();
00498
00499 arma_debug_check
00500 (
00501 (in_row1 > in_row2) || (in_col1 > in_col2) || (in_row2 >= n_rows) || (in_col2 >= n_cols),
00502 "field::subfield(): indices out of bounds or incorrectly used"
00503 );
00504
00505 const uword sub_n_rows = in_row2 - in_row1 + 1;
00506 const uword sub_n_cols = in_col2 - in_col1 + 1;
00507
00508 return subview_field<oT>(*this, in_row1, in_col1, sub_n_rows, sub_n_cols);
00509 }
00510
00511
00512
00514 template<typename oT>
00515 inline
00516 subview_field<oT>
00517 field<oT>::subfield(const span& row_span, const span& col_span)
00518 {
00519 arma_extra_debug_sigprint();
00520
00521 const bool row_all = row_span.whole;
00522 const bool col_all = col_span.whole;
00523
00524 const uword local_n_rows = n_rows;
00525 const uword local_n_cols = n_cols;
00526
00527 const uword in_row1 = row_all ? 0 : row_span.a;
00528 const uword in_row2 = row_span.b;
00529 const uword sub_n_rows = row_all ? local_n_rows : in_row2 - in_row1 + 1;
00530
00531 const uword in_col1 = col_all ? 0 : col_span.a;
00532 const uword in_col2 = col_span.b;
00533 const uword sub_n_cols = col_all ? local_n_cols : in_col2 - in_col1 + 1;
00534
00535 arma_debug_check
00536 (
00537 ( row_all ? false : ((in_row1 > in_row2) || (in_row2 >= local_n_rows)) )
00538 ||
00539 ( col_all ? false : ((in_col1 > in_col2) || (in_col2 >= local_n_cols)) )
00540 ,
00541 "field::subfield(): indices out of bounds or incorrectly used"
00542 );
00543
00544 return subview_field<oT>(*this, in_row1, in_col1, sub_n_rows, sub_n_cols);
00545 }
00546
00547
00548
00550 template<typename oT>
00551 inline
00552 const subview_field<oT>
00553 field<oT>::subfield(const span& row_span, const span& col_span) const
00554 {
00555 arma_extra_debug_sigprint();
00556
00557 const bool row_all = row_span.whole;
00558 const bool col_all = col_span.whole;
00559
00560 const uword local_n_rows = n_rows;
00561 const uword local_n_cols = n_cols;
00562
00563 const uword in_row1 = row_all ? 0 : row_span.a;
00564 const uword in_row2 = row_span.b;
00565 const uword sub_n_rows = row_all ? local_n_rows : in_row2 - in_row1 + 1;
00566
00567 const uword in_col1 = col_all ? 0 : col_span.a;
00568 const uword in_col2 = col_span.b;
00569 const uword sub_n_cols = col_all ? local_n_cols : in_col2 - in_col1 + 1;
00570
00571 arma_debug_check
00572 (
00573 ( row_all ? false : ((in_row1 > in_row2) || (in_row2 >= local_n_rows)) )
00574 ||
00575 ( col_all ? false : ((in_col1 > in_col2) || (in_col2 >= local_n_cols)) )
00576 ,
00577 "field::subfield(): indices out of bounds or incorrectly used"
00578 );
00579
00580 return subview_field<oT>(*this, in_row1, in_col1, sub_n_rows, sub_n_cols);
00581 }
00582
00583
00584
00585 template<typename oT>
00586 inline
00587 subview_field<oT>
00588 field<oT>::operator()(const span& row_span, const span& col_span)
00589 {
00590 arma_extra_debug_sigprint();
00591
00592 return (*this).subfield(row_span, col_span);
00593 }
00594
00595
00596
00597 template<typename oT>
00598 inline
00599 const subview_field<oT>
00600 field<oT>::operator()(const span& row_span, const span& col_span) const
00601 {
00602 arma_extra_debug_sigprint();
00603
00604 return (*this).subfield(row_span, col_span);
00605 }
00606
00607
00608
00617
00618 template<typename oT>
00619 inline
00620 void
00621 field<oT>::print(const std::string extra_text) const
00622 {
00623 arma_extra_debug_sigprint();
00624
00625 if(extra_text.length() != 0)
00626 {
00627 const std::streamsize orig_width = cout.width();
00628
00629 cout << extra_text << '\n';
00630
00631 cout.width(orig_width);
00632 }
00633
00634 arma_ostream::print(cout, *this);
00635 }
00636
00637
00638
00647
00648 template<typename oT>
00649 inline
00650 void
00651 field<oT>::print(std::ostream& user_stream, const std::string extra_text) const
00652 {
00653 arma_extra_debug_sigprint();
00654
00655 if(extra_text.length() != 0)
00656 {
00657 const std::streamsize orig_width = user_stream.width();
00658
00659 user_stream << extra_text << '\n';
00660
00661 user_stream.width(orig_width);
00662 }
00663
00664 arma_ostream::print(user_stream, *this);
00665 }
00666
00667
00668
00670 template<typename oT>
00671 inline
00672 void
00673 field<oT>::fill(const oT& x)
00674 {
00675 arma_extra_debug_sigprint();
00676
00677 field<oT>& t = *this;
00678
00679 for(uword i=0; i<n_elem; ++i)
00680 {
00681 t[i] = x;
00682 }
00683 }
00684
00685
00686
00688 template<typename oT>
00689 inline
00690 void
00691 field<oT>::reset()
00692 {
00693 arma_extra_debug_sigprint();
00694
00695 init(0,0);
00696 }
00697
00698
00699
00701 template<typename oT>
00702 inline
00703 void
00704 field<oT>::reset_objects()
00705 {
00706 arma_extra_debug_sigprint();
00707
00708 field_aux::reset_objects(*this);
00709 }
00710
00711
00712
00714 template<typename oT>
00715 arma_inline
00716 bool
00717 field<oT>::is_empty() const
00718 {
00719 return (n_elem == 0);
00720 }
00721
00722
00723
00725 template<typename oT>
00726 arma_inline
00727 arma_warn_unused
00728 bool
00729 field<oT>::in_range(const uword i) const
00730 {
00731 return (i < n_elem);
00732 }
00733
00734
00735
00737 template<typename oT>
00738 arma_inline
00739 arma_warn_unused
00740 bool
00741 field<oT>::in_range(const span& x) const
00742 {
00743 arma_extra_debug_sigprint();
00744
00745 if(x.whole == true)
00746 {
00747 return true;
00748 }
00749 else
00750 {
00751 const uword a = x.a;
00752 const uword b = x.b;
00753
00754 return ( (a <= b) && (b < n_elem) );
00755 }
00756 }
00757
00758
00759
00761 template<typename oT>
00762 arma_inline
00763 arma_warn_unused
00764 bool
00765 field<oT>::in_range(const uword in_row, const uword in_col) const
00766 {
00767 return ( (in_row < n_rows) && (in_col < n_cols) );
00768 }
00769
00770
00771
00772 template<typename oT>
00773 arma_inline
00774 arma_warn_unused
00775 bool
00776 field<oT>::in_range(const span& row_span, const uword in_col) const
00777 {
00778 arma_extra_debug_sigprint();
00779
00780 if(row_span.whole == true)
00781 {
00782 return (in_col < n_cols);
00783 }
00784 else
00785 {
00786 const uword in_row1 = row_span.a;
00787 const uword in_row2 = row_span.b;
00788
00789 return ( (in_row1 <= in_row2) && (in_row2 < n_rows) && (in_col < n_cols) );
00790 }
00791 }
00792
00793
00794
00795 template<typename oT>
00796 arma_inline
00797 arma_warn_unused
00798 bool
00799 field<oT>::in_range(const uword in_row, const span& col_span) const
00800 {
00801 arma_extra_debug_sigprint();
00802
00803 if(col_span.whole == true)
00804 {
00805 return (in_row < n_rows);
00806 }
00807 else
00808 {
00809 const uword in_col1 = col_span.a;
00810 const uword in_col2 = col_span.b;
00811
00812 return ( (in_row < n_rows) && (in_col1 <= in_col2) && (in_col2 < n_cols) );
00813 }
00814 }
00815
00816
00817
00818 template<typename oT>
00819 arma_inline
00820 arma_warn_unused
00821 bool
00822 field<oT>::in_range(const span& row_span, const span& col_span) const
00823 {
00824 arma_extra_debug_sigprint();
00825
00826 const uword in_row1 = row_span.a;
00827 const uword in_row2 = row_span.b;
00828
00829 const uword in_col1 = col_span.a;
00830 const uword in_col2 = col_span.b;
00831
00832 const bool rows_ok = row_span.whole ? true : ( (in_row1 <= in_row2) && (in_row2 < n_rows) );
00833 const bool cols_ok = col_span.whole ? true : ( (in_col1 <= in_col2) && (in_col2 < n_cols) );
00834
00835 return ( (rows_ok == true) && (cols_ok == true) );
00836 }
00837
00838
00839
00840 template<typename oT>
00841 inline
00842 bool
00843 field<oT>::save(const std::string name, const file_type type, const bool print_status) const
00844 {
00845 arma_extra_debug_sigprint();
00846
00847 std::string err_msg;
00848 const bool save_okay = field_aux::save(*this, name, type, err_msg);
00849
00850 if( (print_status == true) && (save_okay == false) )
00851 {
00852 if(err_msg.length() > 0)
00853 {
00854 arma_warn(true, "field::save(): ", err_msg, name);
00855 }
00856 else
00857 {
00858 arma_warn(true, "field::save(): couldn't write to ", name);
00859 }
00860 }
00861
00862 return save_okay;
00863 }
00864
00865
00866
00867 template<typename oT>
00868 inline
00869 bool
00870 field<oT>::save(std::ostream& os, const file_type type, const bool print_status) const
00871 {
00872 arma_extra_debug_sigprint();
00873
00874 std::string err_msg;
00875 const bool save_okay = field_aux::save(*this, os, type, err_msg);
00876
00877 if( (print_status == true) && (save_okay == false) )
00878 {
00879 if(err_msg.length() > 0)
00880 {
00881 arma_warn(true, "field::save(): ", err_msg, "[ostream]");
00882 }
00883 else
00884 {
00885 arma_warn(true, "field::save(): couldn't write to [ostream]");
00886 }
00887 }
00888
00889 return save_okay;
00890 }
00891
00892
00893
00894 template<typename oT>
00895 inline
00896 bool
00897 field<oT>::load(const std::string name, const file_type type, const bool print_status)
00898 {
00899 arma_extra_debug_sigprint();
00900
00901 std::string err_msg;
00902 const bool load_okay = field_aux::load(*this, name, type, err_msg);
00903
00904 if( (print_status == true) && (load_okay == false) )
00905 {
00906 if(err_msg.length() > 0)
00907 {
00908 arma_warn(true, "field::load(): ", err_msg, name);
00909 }
00910 else
00911 {
00912 arma_warn(true, "field::load(): couldn't read from ", name);
00913 }
00914 }
00915
00916 if(load_okay == false)
00917 {
00918 (*this).reset();
00919 }
00920
00921 return load_okay;
00922 }
00923
00924
00925
00926 template<typename oT>
00927 inline
00928 bool
00929 field<oT>::load(std::istream& is, const file_type type, const bool print_status)
00930 {
00931 arma_extra_debug_sigprint();
00932
00933 std::string err_msg;
00934 const bool load_okay = field_aux::load(*this, is, type, err_msg);
00935
00936 if( (print_status == true) && (load_okay == false) )
00937 {
00938 if(err_msg.length() > 0)
00939 {
00940 arma_warn(true, "field::load(): ", err_msg, "[istream]");
00941 }
00942 else
00943 {
00944 arma_warn(true, "field::load(): couldn't read from [istream]");
00945 }
00946 }
00947
00948 if(load_okay == false)
00949 {
00950 (*this).reset();
00951 }
00952
00953 return load_okay;
00954 }
00955
00956
00957
00958 template<typename oT>
00959 inline
00960 bool
00961 field<oT>::quiet_save(const std::string name, const file_type type) const
00962 {
00963 arma_extra_debug_sigprint();
00964
00965 return (*this).save(name, type, false);
00966 }
00967
00968
00969
00970 template<typename oT>
00971 inline
00972 bool
00973 field<oT>::quiet_save(std::ostream& os, const file_type type) const
00974 {
00975 arma_extra_debug_sigprint();
00976
00977 return (*this).save(os, type, false);
00978 }
00979
00980
00981
00982 template<typename oT>
00983 inline
00984 bool
00985 field<oT>::quiet_load(const std::string name, const file_type type)
00986 {
00987 arma_extra_debug_sigprint();
00988
00989 return (*this).load(name, type, false);
00990 }
00991
00992
00993
00994 template<typename oT>
00995 inline
00996 bool
00997 field<oT>::quiet_load(std::istream& is, const file_type type)
00998 {
00999 arma_extra_debug_sigprint();
01000
01001 return (*this).load(is, type, false);
01002 }
01003
01004
01005
01007 template<typename oT>
01008 inline
01009 void
01010 field<oT>::init(const field<oT>& x)
01011 {
01012 arma_extra_debug_sigprint();
01013
01014 if(this != &x)
01015 {
01016 init(x.n_rows, x.n_cols);
01017
01018 field& t = *this;
01019
01020 for(uword col=0; col<x.n_cols; ++col)
01021 for(uword row=0; row<x.n_rows; ++row)
01022 {
01023 t.at(row,col) = x.at(row,col);
01024 }
01025 }
01026
01027 }
01028
01029
01030
01032 template<typename oT>
01033 inline
01034 void
01035 field<oT>::init(const uword n_rows_in, const uword n_cols_in)
01036 {
01037 arma_extra_debug_sigprint( arma_boost::format("n_rows_in = %d, n_cols_in = %d") % n_rows_in % n_cols_in );
01038
01039 const uword n_elem_new = n_rows_in * n_cols_in;
01040
01041 if(n_elem == n_elem_new)
01042 {
01043
01044
01045 access::rw(n_rows) = n_rows_in;
01046 access::rw(n_cols) = n_cols_in;
01047 }
01048 else
01049 {
01050 delete_objects();
01051
01052 if(n_elem > sizeof(mem_local)/sizeof(oT*) )
01053 {
01054 delete [] mem;
01055 }
01056
01057 if(n_elem_new <= sizeof(mem_local)/sizeof(oT*) )
01058 {
01059 mem = mem_local;
01060 }
01061 else
01062 {
01063 mem = new(std::nothrow) oT* [n_elem_new];
01064 arma_check_bad_alloc( (mem == 0), "field::init(): out of memory" );
01065 }
01066
01067 access::rw(n_elem) = n_elem_new;
01068
01069 if(n_elem_new == 0)
01070 {
01071 access::rw(n_rows) = 0;
01072 access::rw(n_cols) = 0;
01073 }
01074 else
01075 {
01076 access::rw(n_rows) = n_rows_in;
01077 access::rw(n_cols) = n_cols_in;
01078 }
01079
01080 create_objects();
01081
01082 }
01083
01084 }
01085
01086
01087
01088 template<typename oT>
01089 inline
01090 void
01091 field<oT>::delete_objects()
01092 {
01093 arma_extra_debug_sigprint( arma_boost::format("n_elem = %d") % n_elem );
01094
01095 for(uword i=0; i<n_elem; ++i)
01096 {
01097 if(mem[i] != 0)
01098 {
01099 delete mem[i];
01100 mem[i] = 0;
01101 }
01102 }
01103
01104 }
01105
01106
01107
01108 template<typename oT>
01109 inline
01110 void
01111 field<oT>::create_objects()
01112 {
01113 arma_extra_debug_sigprint( arma_boost::format("n_elem = %d") % n_elem );
01114
01115 for(uword i=0; i<n_elem; ++i)
01116 {
01117 mem[i] = new oT;
01118 }
01119
01120 }
01121
01122
01123
01124 template<typename oT>
01125 inline
01126 field<oT>::iterator::iterator(field<oT>& in_M, const bool at_end)
01127 : M(in_M)
01128 , i( (at_end == false) ? 0 : in_M.n_elem )
01129 {
01130 arma_extra_debug_sigprint();
01131 }
01132
01133
01134
01135 template<typename oT>
01136 inline
01137 oT&
01138 field<oT>::iterator::operator*()
01139 {
01140 return M[i];
01141 }
01142
01143
01144
01145 template<typename oT>
01146 inline
01147 typename field<oT>::iterator&
01148 field<oT>::iterator::operator++()
01149 {
01150 ++i;
01151
01152 return *this;
01153 }
01154
01155
01156
01157 template<typename oT>
01158 inline
01159 void
01160 field<oT>::iterator::operator++(int)
01161 {
01162 operator++();
01163 }
01164
01165
01166
01167 template<typename oT>
01168 inline
01169 typename field<oT>::iterator&
01170 field<oT>::iterator::operator--()
01171 {
01172 if(i > 0)
01173 {
01174 --i;
01175 }
01176
01177 return *this;
01178 }
01179
01180
01181
01182 template<typename oT>
01183 inline
01184 void
01185 field<oT>::iterator::operator--(int)
01186 {
01187 operator--();
01188 }
01189
01190
01191
01192 template<typename oT>
01193 inline
01194 bool
01195 field<oT>::iterator::operator!=(const typename field<oT>::iterator& X) const
01196 {
01197 return (i != X.i);
01198 }
01199
01200
01201
01202 template<typename oT>
01203 inline
01204 bool
01205 field<oT>::iterator::operator==(const typename field<oT>::iterator& X) const
01206 {
01207 return (i == X.i);
01208 }
01209
01210
01211
01212 template<typename oT>
01213 inline
01214 field<oT>::const_iterator::const_iterator(const field<oT>& in_M, const bool at_end)
01215 : M(in_M)
01216 , i( (at_end == false) ? 0 : in_M.n_elem )
01217 {
01218 arma_extra_debug_sigprint();
01219 }
01220
01221
01222
01223 template<typename oT>
01224 inline
01225 field<oT>::const_iterator::const_iterator(const typename field<oT>::iterator& X)
01226 : M(X.M)
01227 , i(X.i)
01228 {
01229 arma_extra_debug_sigprint();
01230 }
01231
01232
01233
01234 template<typename oT>
01235 inline
01236 const oT&
01237 field<oT>::const_iterator::operator*() const
01238 {
01239 return M[i];
01240 }
01241
01242
01243
01244 template<typename oT>
01245 inline
01246 typename field<oT>::const_iterator&
01247 field<oT>::const_iterator::operator++()
01248 {
01249 ++i;
01250
01251 return *this;
01252 }
01253
01254
01255
01256 template<typename oT>
01257 inline
01258 void
01259 field<oT>::const_iterator::operator++(int)
01260 {
01261 operator++();
01262 }
01263
01264
01265
01266 template<typename oT>
01267 inline
01268 typename field<oT>::const_iterator&
01269 field<oT>::const_iterator::operator--()
01270 {
01271 if(i > 0)
01272 {
01273 --i;
01274 }
01275
01276 return *this;
01277 }
01278
01279
01280
01281 template<typename oT>
01282 inline
01283 void
01284 field<oT>::const_iterator::operator--(int)
01285 {
01286 operator--();
01287 }
01288
01289
01290
01291 template<typename oT>
01292 inline
01293 bool
01294 field<oT>::const_iterator::operator!=(const typename field<oT>::const_iterator& X) const
01295 {
01296 return (i != X.i);
01297 }
01298
01299
01300
01301 template<typename oT>
01302 inline
01303 bool
01304 field<oT>::const_iterator::operator==(const typename field<oT>::const_iterator& X) const
01305 {
01306 return (i == X.i);
01307 }
01308
01309
01310
01311 template<typename oT>
01312 inline
01313 typename field<oT>::iterator
01314 field<oT>::begin()
01315 {
01316 arma_extra_debug_sigprint();
01317
01318 return field<oT>::iterator(*this);
01319 }
01320
01321
01322
01323 template<typename oT>
01324 inline
01325 typename field<oT>::const_iterator
01326 field<oT>::begin() const
01327 {
01328 arma_extra_debug_sigprint();
01329
01330 return field<oT>::const_iterator(*this);
01331 }
01332
01333
01334
01335 template<typename oT>
01336 inline
01337 typename field<oT>::iterator
01338 field<oT>::end()
01339 {
01340 arma_extra_debug_sigprint();
01341
01342 return field<oT>::iterator(*this, true);
01343 }
01344
01345
01346
01347 template<typename oT>
01348 inline
01349 typename field<oT>::const_iterator
01350 field<oT>::end() const
01351 {
01352 arma_extra_debug_sigprint();
01353
01354 return field<oT>::const_iterator(*this, true);
01355 }
01356
01357
01358
01359
01360
01361
01362
01363
01364
01365 template<typename oT>
01366 inline
01367 void
01368 field_aux::reset_objects(field<oT>& x)
01369 {
01370 arma_extra_debug_sigprint();
01371
01372 x.delete_objects();
01373 x.create_objects();
01374 }
01375
01376
01377
01378 template<typename eT>
01379 inline
01380 void
01381 field_aux::reset_objects(field< Mat<eT> >& x)
01382 {
01383 arma_extra_debug_sigprint();
01384
01385 for(uword i=0; i<x.n_elem; ++i)
01386 {
01387 (*(x.mem[i])).reset();
01388 }
01389 }
01390
01391
01392
01393 template<typename eT>
01394 inline
01395 void
01396 field_aux::reset_objects(field< Col<eT> >& x)
01397 {
01398 arma_extra_debug_sigprint();
01399
01400 for(uword i=0; i<x.n_elem; ++i)
01401 {
01402 (*(x.mem[i])).reset();
01403 }
01404 }
01405
01406
01407
01408 template<typename eT>
01409 inline
01410 void
01411 field_aux::reset_objects(field< Row<eT> >& x)
01412 {
01413 arma_extra_debug_sigprint();
01414
01415 for(uword i=0; i<x.n_elem; ++i)
01416 {
01417 (*(x.mem[i])).reset();
01418 }
01419 }
01420
01421
01422
01423 template<typename eT>
01424 inline
01425 void
01426 field_aux::reset_objects(field< Cube<eT> >& x)
01427 {
01428 arma_extra_debug_sigprint();
01429
01430 for(uword i=0; i<x.n_elem; ++i)
01431 {
01432 (*(x.mem[i])).reset();
01433 }
01434 }
01435
01436
01437
01438 inline
01439 void
01440 field_aux::reset_objects(field< std::string >& x)
01441 {
01442 arma_extra_debug_sigprint();
01443
01444 for(uword i=0; i<x.n_elem; ++i)
01445 {
01446 (*(x.mem[i])).clear();
01447 }
01448 }
01449
01450
01451
01452
01453
01454
01455
01456
01457
01458 template<typename oT>
01459 inline
01460 bool
01461 field_aux::save(const field<oT>& x, const std::string& name, const file_type type, std::string& err_msg)
01462 {
01463 arma_extra_debug_sigprint();
01464
01465 err_msg = " [sorry, saving/loading this type of field is currently not supported] filename = ";
01466
01467 return false;
01468 }
01469
01470
01471
01472 template<typename oT>
01473 inline
01474 bool
01475 field_aux::save(const field<oT>& x, std::ostream& os, const file_type type, std::string& err_msg)
01476 {
01477 arma_extra_debug_sigprint();
01478
01479 err_msg = " [sorry, saving/loading this type of field is currently not supported] filename = ";
01480
01481 return false;
01482 }
01483
01484
01485
01486 template<typename oT>
01487 inline
01488 bool
01489 field_aux::load(field<oT>& x, const std::string& name, const file_type type, std::string& err_msg)
01490 {
01491 arma_extra_debug_sigprint();
01492
01493 err_msg = " [sorry, saving/loading this type of field is currently not supported] filename = ";
01494
01495 return false;
01496 }
01497
01498
01499
01500 template<typename oT>
01501 inline
01502 bool
01503 field_aux::load(field<oT>& x, std::istream& is, const file_type type, std::string& err_msg)
01504 {
01505 arma_extra_debug_sigprint();
01506
01507 err_msg = " [sorry, saving/loading this type of field is currently not supported] filename = ";
01508
01509 return false;
01510 }
01511
01512
01513
01514 template<typename eT>
01515 inline
01516 bool
01517 field_aux::save(const field< Mat<eT> >& x, const std::string& name, const file_type type, std::string& err_msg)
01518 {
01519 arma_extra_debug_sigprint();
01520
01521 switch(type)
01522 {
01523 case arma_binary:
01524 return diskio::save_arma_binary(x, name);
01525 break;
01526
01527 case ppm_binary:
01528 return diskio::save_ppm_binary(x, name);
01529 break;
01530
01531 default:
01532 err_msg = " [unsupported type] filename = ";
01533 return false;
01534 }
01535 }
01536
01537
01538
01539 template<typename eT>
01540 inline
01541 bool
01542 field_aux::save(const field< Mat<eT> >& x, std::ostream& os, const file_type type, std::string& err_msg)
01543 {
01544 arma_extra_debug_sigprint();
01545
01546 switch(type)
01547 {
01548 case arma_binary:
01549 return diskio::save_arma_binary(x, os);
01550 break;
01551
01552 case ppm_binary:
01553 return diskio::save_ppm_binary(x, os);
01554 break;
01555
01556 default:
01557 err_msg = " [unsupported type] filename = ";
01558 return false;
01559 }
01560 }
01561
01562
01563
01564 template<typename eT>
01565 inline
01566 bool
01567 field_aux::load(field< Mat<eT> >& x, const std::string& name, const file_type type, std::string& err_msg)
01568 {
01569 arma_extra_debug_sigprint();
01570
01571 switch(type)
01572 {
01573 case auto_detect:
01574 return diskio::load_auto_detect(x, name, err_msg);
01575 break;
01576
01577 case arma_binary:
01578 return diskio::load_arma_binary(x, name, err_msg);
01579 break;
01580
01581 case ppm_binary:
01582 return diskio::load_ppm_binary(x, name, err_msg);
01583 break;
01584
01585 default:
01586 err_msg = " [unsupported type] filename = ";
01587 return false;
01588 }
01589 }
01590
01591
01592
01593 template<typename eT>
01594 inline
01595 bool
01596 field_aux::load(field< Mat<eT> >& x, std::istream& is, const file_type type, std::string& err_msg)
01597 {
01598 arma_extra_debug_sigprint();
01599
01600 switch(type)
01601 {
01602 case auto_detect:
01603 return diskio::load_auto_detect(x, is, err_msg);
01604 break;
01605
01606 case arma_binary:
01607 return diskio::load_arma_binary(x, is, err_msg);
01608 break;
01609
01610 case ppm_binary:
01611 return diskio::load_ppm_binary(x, is, err_msg);
01612 break;
01613
01614 default:
01615 err_msg = " [unsupported type] filename = ";
01616 return false;
01617 }
01618 }
01619
01620
01621
01622 template<typename eT>
01623 inline
01624 bool
01625 field_aux::save(const field< Col<eT> >& x, const std::string& name, const file_type type, std::string& err_msg)
01626 {
01627 arma_extra_debug_sigprint();
01628
01629 switch(type)
01630 {
01631 case arma_binary:
01632 return diskio::save_arma_binary(x, name);
01633 break;
01634
01635 case ppm_binary:
01636 return diskio::save_ppm_binary(x, name);
01637 break;
01638
01639 default:
01640 err_msg = " [unsupported type] filename = ";
01641 return false;
01642 }
01643 }
01644
01645
01646
01647 template<typename eT>
01648 inline
01649 bool
01650 field_aux::save(const field< Col<eT> >& x, std::ostream& os, const file_type type, std::string& err_msg)
01651 {
01652 arma_extra_debug_sigprint();
01653
01654 switch(type)
01655 {
01656 case arma_binary:
01657 return diskio::save_arma_binary(x, os);
01658 break;
01659
01660 case ppm_binary:
01661 return diskio::save_ppm_binary(x, os);
01662 break;
01663
01664 default:
01665 err_msg = " [unsupported type] filename = ";
01666 return false;
01667 }
01668 }
01669
01670
01671
01672 template<typename eT>
01673 inline
01674 bool
01675 field_aux::load(field< Col<eT> >& x, const std::string& name, const file_type type, std::string& err_msg)
01676 {
01677 arma_extra_debug_sigprint();
01678
01679 switch(type)
01680 {
01681 case auto_detect:
01682 return diskio::load_auto_detect(x, name, err_msg);
01683 break;
01684
01685 case arma_binary:
01686 return diskio::load_arma_binary(x, name, err_msg);
01687 break;
01688
01689 case ppm_binary:
01690 return diskio::load_ppm_binary(x, name, err_msg);
01691 break;
01692
01693 default:
01694 err_msg = " [unsupported type] filename = ";
01695 return false;
01696 }
01697 }
01698
01699
01700
01701 template<typename eT>
01702 inline
01703 bool
01704 field_aux::load(field< Col<eT> >& x, std::istream& is, const file_type type, std::string& err_msg)
01705 {
01706 arma_extra_debug_sigprint();
01707
01708 switch(type)
01709 {
01710 case auto_detect:
01711 return diskio::load_auto_detect(x, is, err_msg);
01712 break;
01713
01714 case arma_binary:
01715 return diskio::load_arma_binary(x, is, err_msg);
01716 break;
01717
01718 case ppm_binary:
01719 return diskio::load_ppm_binary(x, is, err_msg);
01720 break;
01721
01722 default:
01723 err_msg = " [unsupported type] filename = ";
01724 return false;
01725 }
01726 }
01727
01728
01729
01730 template<typename eT>
01731 inline
01732 bool
01733 field_aux::save(const field< Row<eT> >& x, const std::string& name, const file_type type, std::string& err_msg)
01734 {
01735 arma_extra_debug_sigprint();
01736
01737 switch(type)
01738 {
01739 case arma_binary:
01740 return diskio::save_arma_binary(x, name, err_msg);
01741 break;
01742
01743 case ppm_binary:
01744 return diskio::save_ppm_binary(x, name, err_msg);
01745 break;
01746
01747 default:
01748 err_msg = " [unsupported type] filename = ";
01749 return false;
01750 }
01751 }
01752
01753
01754
01755 template<typename eT>
01756 inline
01757 bool
01758 field_aux::save(const field< Row<eT> >& x, std::ostream& os, const file_type type, std::string& err_msg)
01759 {
01760 arma_extra_debug_sigprint();
01761
01762 switch(type)
01763 {
01764 case arma_binary:
01765 return diskio::save_arma_binary(x, os, err_msg);
01766 break;
01767
01768 case ppm_binary:
01769 return diskio::save_ppm_binary(x, os, err_msg);
01770 break;
01771
01772 default:
01773 err_msg = " [unsupported type] filename = ";
01774 return false;
01775 }
01776 }
01777
01778
01779
01780 template<typename eT>
01781 inline
01782 bool
01783 field_aux::load(field< Row<eT> >& x, const std::string& name, const file_type type, std::string& err_msg)
01784 {
01785 arma_extra_debug_sigprint();
01786
01787 switch(type)
01788 {
01789 case auto_detect:
01790 return diskio::load_auto_detect(x, name, err_msg);
01791 break;
01792
01793 case arma_binary:
01794 return diskio::load_arma_binary(x, name, err_msg);
01795 break;
01796
01797 case ppm_binary:
01798 return diskio::load_ppm_binary(x, name, err_msg);
01799 break;
01800
01801 default:
01802 err_msg = " [unsupported type] filename = ";
01803 return false;
01804 }
01805 }
01806
01807
01808
01809 template<typename eT>
01810 inline
01811 bool
01812 field_aux::load(field< Row<eT> >& x, std::istream& is, const file_type type, std::string& err_msg)
01813 {
01814 arma_extra_debug_sigprint();
01815
01816 switch(type)
01817 {
01818 case auto_detect:
01819 return diskio::load_auto_detect(x, is, err_msg);
01820 break;
01821
01822 case arma_binary:
01823 return diskio::load_arma_binary(x, is, err_msg);
01824 break;
01825
01826 case ppm_binary:
01827 return diskio::load_ppm_binary(x, is, err_msg);
01828 break;
01829
01830 default:
01831 err_msg = " [unsupported type] filename = ";
01832 return false;
01833 }
01834 }
01835
01836
01837
01838 template<typename eT>
01839 inline
01840 bool
01841 field_aux::save(const field< Cube<eT> >& x, const std::string& name, const file_type type, std::string& err_msg)
01842 {
01843 arma_extra_debug_sigprint();
01844
01845 switch(type)
01846 {
01847 case arma_binary:
01848 return diskio::save_arma_binary(x, name, err_msg);
01849 break;
01850
01851 case ppm_binary:
01852 return diskio::save_ppm_binary(x, name, err_msg);
01853 break;
01854
01855 default:
01856 err_msg = " [unsupported type] filename = ";
01857 return false;
01858 }
01859 }
01860
01861
01862
01863 template<typename eT>
01864 inline
01865 bool
01866 field_aux::save(const field< Cube<eT> >& x, std::ostream& os, const file_type type, std::string& err_msg)
01867 {
01868 arma_extra_debug_sigprint();
01869
01870 switch(type)
01871 {
01872 case arma_binary:
01873 return diskio::save_arma_binary(x, os, err_msg);
01874 break;
01875
01876 case ppm_binary:
01877 return diskio::save_ppm_binary(x, os, err_msg);
01878 break;
01879
01880 default:
01881 err_msg = " [unsupported type] filename = ";
01882 return false;
01883 }
01884 }
01885
01886
01887
01888 template<typename eT>
01889 inline
01890 bool
01891 field_aux::load(field< Cube<eT> >& x, const std::string& name, const file_type type, std::string& err_msg)
01892 {
01893 arma_extra_debug_sigprint();
01894
01895 switch(type)
01896 {
01897 case auto_detect:
01898 return diskio::load_auto_detect(x, name, err_msg);
01899 break;
01900
01901 case arma_binary:
01902 return diskio::load_arma_binary(x, name, err_msg);
01903 break;
01904
01905 case ppm_binary:
01906 return diskio::load_ppm_binary(x, name, err_msg);
01907 break;
01908
01909 default:
01910 err_msg = " [unsupported type] filename = ";
01911 return false;
01912 }
01913 }
01914
01915
01916
01917 template<typename eT>
01918 inline
01919 bool
01920 field_aux::load(field< Cube<eT> >& x, std::istream& is, const file_type type, std::string& err_msg)
01921 {
01922 arma_extra_debug_sigprint();
01923
01924 switch(type)
01925 {
01926 case auto_detect:
01927 return diskio::load_auto_detect(x, is, err_msg);
01928 break;
01929
01930 case arma_binary:
01931 return diskio::load_arma_binary(x, is, err_msg);
01932 break;
01933
01934 case ppm_binary:
01935 return diskio::load_ppm_binary(x, is, err_msg);
01936 break;
01937
01938 default:
01939 err_msg = " [unsupported type] filename = ";
01940 return false;
01941 }
01942 }
01943
01944
01945
01946 inline
01947 bool
01948 field_aux::save(const field< std::string >& x, const std::string& name, const file_type type, std::string& err_msg)
01949 {
01950 arma_extra_debug_sigprint();
01951
01952 arma_ignore(type);
01953
01954 err_msg.clear();
01955
01956 return diskio::save_std_string(x, name);
01957 }
01958
01959
01960
01961 inline
01962 bool
01963 field_aux::save(const field< std::string >& x, std::ostream& os, const file_type type, std::string& err_msg)
01964 {
01965 arma_extra_debug_sigprint();
01966
01967 arma_ignore(type);
01968
01969 err_msg.clear();
01970
01971 return diskio::save_std_string(x, os);
01972 }
01973
01974
01975
01976 inline
01977 bool
01978 field_aux::load(field< std::string >& x, const std::string& name, const file_type type, std::string& err_msg)
01979 {
01980 arma_extra_debug_sigprint();
01981
01982 arma_ignore(type);
01983
01984 return diskio::load_std_string(x, name, err_msg);
01985 }
01986
01987
01988
01989 inline
01990 bool
01991 field_aux::load(field< std::string >& x, std::istream& is, const file_type type, std::string& err_msg)
01992 {
01993 arma_extra_debug_sigprint();
01994
01995 arma_ignore(type);
01996
01997 return diskio::load_std_string(x, is, err_msg);
01998 }
01999
02000
02001
02002 #ifdef ARMA_EXTRA_FIELD_MEAT
02003 #include ARMA_INCFILE_WRAP(ARMA_EXTRA_FIELD_MEAT)
02004 #endif
02005
02006
02007