field_meat.hpp
Go to the documentation of this file.
1 // Copyright (C) 2008-2011 NICTA (www.nicta.com.au)
2 // Copyright (C) 2008-2011 Conrad Sanderson
3 // Copyright (C) 2009-2010 Ian Cullinan
4 //
5 // This file is part of the Armadillo C++ library.
6 // It is provided without any warranty of fitness
7 // for any purpose. You can redistribute this file
8 // and/or modify it under the terms of the GNU
9 // Lesser General Public License (LGPL) as published
10 // by the Free Software Foundation, either version 3
11 // of the License or (at your option) any later version.
12 // (see http://www.opensource.org/licenses for more info)
13 
14 
17 
18 
19 template<typename oT>
20 inline
22  {
24 
25  delete_objects();
26 
27  if(n_elem > sizeof(mem_local)/sizeof(oT*) )
28  {
29  delete [] mem;
30  }
31 
32  if(arma_config::debug == true)
33  {
34  // try to expose buggy user code that accesses deleted objects
35  access::rw(n_rows) = 0;
36  access::rw(n_cols) = 0;
37  access::rw(n_elem) = 0;
38  mem = 0;
39  }
40  }
41 
42 
43 
44 template<typename oT>
45 inline
47  : n_rows(0)
48  , n_cols(0)
49  , n_elem(0)
50  , mem(0)
51  {
53  }
54 
55 
56 
58 template<typename oT>
59 inline
61  : n_rows(0)
62  , n_cols(0)
63  , n_elem(0)
64  , mem(0)
65  {
66  arma_extra_debug_sigprint(arma_boost::format("this = %x x = %x") % this % &x);
67 
68  init(x);
69  }
70 
71 
72 
74 template<typename oT>
75 inline
76 const field<oT>&
78  {
80 
81  init(x);
82  return *this;
83  }
84 
85 
86 
88 template<typename oT>
89 inline
91  : n_rows(0)
92  , n_cols(0)
93  , n_elem(0)
94  , mem(0)
95  {
97 
98  this->operator=(X);
99  }
100 
101 
102 
104 template<typename oT>
105 inline
106 const field<oT>&
108  {
110 
111  subview_field<oT>::extract(*this, X);
112  return *this;
113  }
114 
115 
116 
119 template<typename oT>
120 inline
121 field<oT>::field(const uword n_elem_in)
122  : n_rows(0)
123  , n_cols(0)
124  , n_elem(0)
125  , mem(0)
126  {
128 
129  init(n_elem_in, 1);
130  }
131 
132 
133 
135 template<typename oT>
136 inline
137 field<oT>::field(const uword n_rows_in, const uword n_cols_in)
138  : n_rows(0)
139  , n_cols(0)
140  , n_elem(0)
141  , mem(0)
142  {
144 
145  init(n_rows_in, n_cols_in);
146  }
147 
148 
149 
152 template<typename oT>
153 inline
154 void
155 field<oT>::set_size(const uword n_elem_in)
156  {
157  arma_extra_debug_sigprint(arma_boost::format("n_elem_in = %d") % n_elem_in);
158 
159  init(n_elem_in, 1);
160  }
161 
162 
163 
165 template<typename oT>
166 inline
167 void
168 field<oT>::set_size(const uword n_rows_in, const uword n_cols_in)
169  {
170  arma_extra_debug_sigprint(arma_boost::format("n_rows_in = %d, n_cols_in = %d") % n_rows_in % n_cols_in);
171 
172  init(n_rows_in, n_cols_in);
173  }
174 
175 
176 
178 template<typename oT>
179 template<typename oT2>
180 inline
181 void
183  {
185 
186  init(x.n_rows, x.n_cols);
187  }
188 
189 
190 
192 template<typename oT>
194 oT&
196  {
197  return (*mem[i]);
198  }
199 
200 
201 
203 template<typename oT>
205 const oT&
207  {
208  return (*mem[i]);
209  }
210 
211 
212 
214 template<typename oT>
216 oT&
218  {
219  return (*mem[i]);
220  }
221 
222 
223 
225 template<typename oT>
227 const oT&
228 field<oT>::at(const uword i) const
229  {
230  return (*mem[i]);
231  }
232 
233 
234 
236 template<typename oT>
238 oT&
240  {
241  arma_debug_check( (i >= n_elem), "field::operator(): index out of bounds");
242  return (*mem[i]);
243  }
244 
245 
246 
248 template<typename oT>
250 const oT&
252  {
253  arma_debug_check( (i >= n_elem), "field::operator(): index out of bounds");
254  return (*mem[i]);
255  }
256 
257 
258 
260 template<typename oT>
262 oT&
263 field<oT>::operator() (const uword in_row, const uword in_col)
264  {
265  arma_debug_check( ((in_row >= n_rows) || (in_col >= n_cols)), "field::operator(): index out of bounds");
266  return (*mem[in_row + in_col*n_rows]);
267  }
268 
269 
270 
272 template<typename oT>
274 const oT&
275 field<oT>::operator() (const uword in_row, const uword in_col) const
276  {
277  arma_debug_check( ((in_row >= n_rows) || (in_col >= n_cols)), "field::operator(): index out of bounds");
278  return (*mem[in_row + in_col*n_rows]);
279  }
280 
281 
282 
284 template<typename oT>
286 oT&
287 field<oT>::at(const uword in_row, const uword in_col)
288  {
289  return (*mem[in_row + in_col*n_rows]);
290  }
291 
292 
293 
295 template<typename oT>
297 const oT&
298 field<oT>::at(const uword in_row, const uword in_col) const
299  {
300  return (*mem[in_row + in_col*n_rows]);
301  }
302 
303 
304 
305 template<typename oT>
306 inline
308 field<oT>::operator<<(const oT& val)
309  {
310  return field_injector< field<oT> >(*this, val);
311  }
312 
313 
314 
315 template<typename oT>
316 inline
319  {
320  return field_injector< field<oT> >(*this, x);
321  }
322 
323 
324 
326 template<typename oT>
327 inline
329 field<oT>::row(const uword row_num)
330  {
332 
333  arma_debug_check( (row_num >= n_rows), "field::row(): row out of bounds" );
334 
335  return subview_field<oT>(*this, row_num, 0, 1, n_cols);
336  }
337 
338 
339 
341 template<typename oT>
342 inline
343 const subview_field<oT>
344 field<oT>::row(const uword row_num) const
345  {
347 
348  arma_debug_check( (row_num >= n_rows), "field::row(): row out of bounds" );
349 
350  return subview_field<oT>(*this, row_num, 0, 1, n_cols);
351  }
352 
353 
354 
356 template<typename oT>
357 inline
359 field<oT>::col(const uword col_num)
360  {
362 
363  arma_debug_check( (col_num >= n_cols), "field::col(): out of bounds");
364 
365  return subview_field<oT>(*this, 0, col_num, n_rows, 1);
366  }
367 
368 
369 
371 template<typename oT>
372 inline
373 const subview_field<oT>
374 field<oT>::col(const uword col_num) const
375  {
377 
378  arma_debug_check( (col_num >= n_cols), "field::col(): out of bounds");
379 
380  return subview_field<oT>(*this, 0, col_num, n_rows, 1);
381  }
382 
383 
384 
386 template<typename oT>
387 inline
389 field<oT>::rows(const uword in_row1, const uword in_row2)
390  {
392 
394  (
395  ( (in_row1 > in_row2) || (in_row2 >= n_rows) ),
396  "field::rows(): indicies out of bounds or incorrectly used"
397  );
398 
399  const uword sub_n_rows = in_row2 - in_row1 + 1;
400 
401  return subview_field<oT>(*this, in_row1, 0, sub_n_rows, n_cols);
402  }
403 
404 
405 
407 template<typename oT>
408 inline
409 const subview_field<oT>
410 field<oT>::rows(const uword in_row1, const uword in_row2) const
411  {
413 
415  (
416  ( (in_row1 > in_row2) || (in_row2 >= n_rows) ),
417  "field::rows(): indicies out of bounds or incorrectly used"
418  );
419 
420  const uword sub_n_rows = in_row2 - in_row1 + 1;
421 
422  return subview_field<oT>(*this, in_row1, 0, sub_n_rows, n_cols);
423  }
424 
425 
426 
428 template<typename oT>
429 inline
431 field<oT>::cols(const uword in_col1, const uword in_col2)
432  {
434 
436  (
437  ( (in_col1 > in_col2) || (in_col2 >= n_cols) ),
438  "field::cols(): indicies out of bounds or incorrectly used"
439  );
440 
441  const uword sub_n_cols = in_col2 - in_col1 + 1;
442 
443  return subview_field<oT>(*this, 0, in_col1, n_rows, sub_n_cols);
444  }
445 
446 
447 
449 template<typename oT>
450 inline
451 const subview_field<oT>
452 field<oT>::cols(const uword in_col1, const uword in_col2) const
453  {
455 
457  (
458  ( (in_col1 > in_col2) || (in_col2 >= n_cols) ),
459  "field::cols(): indicies out of bounds or incorrectly used"
460  );
461 
462  const uword sub_n_cols = in_col2 - in_col1 + 1;
463 
464  return subview_field<oT>(*this, 0, in_col1, n_rows, sub_n_cols);
465  }
466 
467 
468 
470 template<typename oT>
471 inline
473 field<oT>::subfield(const uword in_row1, const uword in_col1, const uword in_row2, const uword in_col2)
474  {
476 
478  (
479  (in_row1 > in_row2) || (in_col1 > in_col2) || (in_row2 >= n_rows) || (in_col2 >= n_cols),
480  "field::subfield(): indices out of bounds or incorrectly used"
481  );
482 
483  const uword sub_n_rows = in_row2 - in_row1 + 1;
484  const uword sub_n_cols = in_col2 - in_col1 + 1;
485 
486  return subview_field<oT>(*this, in_row1, in_col1, sub_n_rows, sub_n_cols);
487  }
488 
489 
490 
492 template<typename oT>
493 inline
494 const subview_field<oT>
495 field<oT>::subfield(const uword in_row1, const uword in_col1, const uword in_row2, const uword in_col2) const
496  {
498 
500  (
501  (in_row1 > in_row2) || (in_col1 > in_col2) || (in_row2 >= n_rows) || (in_col2 >= n_cols),
502  "field::subfield(): indices out of bounds or incorrectly used"
503  );
504 
505  const uword sub_n_rows = in_row2 - in_row1 + 1;
506  const uword sub_n_cols = in_col2 - in_col1 + 1;
507 
508  return subview_field<oT>(*this, in_row1, in_col1, sub_n_rows, sub_n_cols);
509  }
510 
511 
512 
514 template<typename oT>
515 inline
517 field<oT>::subfield(const span& row_span, const span& col_span)
518  {
520 
521  const bool row_all = row_span.whole;
522  const bool col_all = col_span.whole;
523 
524  const uword local_n_rows = n_rows;
525  const uword local_n_cols = n_cols;
526 
527  const uword in_row1 = row_all ? 0 : row_span.a;
528  const uword in_row2 = row_span.b;
529  const uword sub_n_rows = row_all ? local_n_rows : in_row2 - in_row1 + 1;
530 
531  const uword in_col1 = col_all ? 0 : col_span.a;
532  const uword in_col2 = col_span.b;
533  const uword sub_n_cols = col_all ? local_n_cols : in_col2 - in_col1 + 1;
534 
536  (
537  ( row_all ? false : ((in_row1 > in_row2) || (in_row2 >= local_n_rows)) )
538  ||
539  ( col_all ? false : ((in_col1 > in_col2) || (in_col2 >= local_n_cols)) )
540  ,
541  "field::subfield(): indices out of bounds or incorrectly used"
542  );
543 
544  return subview_field<oT>(*this, in_row1, in_col1, sub_n_rows, sub_n_cols);
545  }
546 
547 
548 
550 template<typename oT>
551 inline
552 const subview_field<oT>
553 field<oT>::subfield(const span& row_span, const span& col_span) const
554  {
556 
557  const bool row_all = row_span.whole;
558  const bool col_all = col_span.whole;
559 
560  const uword local_n_rows = n_rows;
561  const uword local_n_cols = n_cols;
562 
563  const uword in_row1 = row_all ? 0 : row_span.a;
564  const uword in_row2 = row_span.b;
565  const uword sub_n_rows = row_all ? local_n_rows : in_row2 - in_row1 + 1;
566 
567  const uword in_col1 = col_all ? 0 : col_span.a;
568  const uword in_col2 = col_span.b;
569  const uword sub_n_cols = col_all ? local_n_cols : in_col2 - in_col1 + 1;
570 
572  (
573  ( row_all ? false : ((in_row1 > in_row2) || (in_row2 >= local_n_rows)) )
574  ||
575  ( col_all ? false : ((in_col1 > in_col2) || (in_col2 >= local_n_cols)) )
576  ,
577  "field::subfield(): indices out of bounds or incorrectly used"
578  );
579 
580  return subview_field<oT>(*this, in_row1, in_col1, sub_n_rows, sub_n_cols);
581  }
582 
583 
584 
585 template<typename oT>
586 inline
588 field<oT>::operator()(const span& row_span, const span& col_span)
589  {
591 
592  return (*this).subfield(row_span, col_span);
593  }
594 
595 
596 
597 template<typename oT>
598 inline
599 const subview_field<oT>
600 field<oT>::operator()(const span& row_span, const span& col_span) const
601  {
603 
604  return (*this).subfield(row_span, col_span);
605  }
606 
607 
608 
617 
618 template<typename oT>
619 inline
620 void
621 field<oT>::print(const std::string extra_text) const
622  {
624 
625  if(extra_text.length() != 0)
626  {
627  const std::streamsize orig_width = cout.width();
628 
629  cout << extra_text << '\n';
630 
631  cout.width(orig_width);
632  }
633 
634  arma_ostream::print(cout, *this);
635  }
636 
637 
638 
647 
648 template<typename oT>
649 inline
650 void
651 field<oT>::print(std::ostream& user_stream, const std::string extra_text) const
652  {
654 
655  if(extra_text.length() != 0)
656  {
657  const std::streamsize orig_width = user_stream.width();
658 
659  user_stream << extra_text << '\n';
660 
661  user_stream.width(orig_width);
662  }
663 
664  arma_ostream::print(user_stream, *this);
665  }
666 
667 
668 
670 template<typename oT>
671 inline
672 void
673 field<oT>::fill(const oT& x)
674  {
676 
677  field<oT>& t = *this;
678 
679  for(uword i=0; i<n_elem; ++i)
680  {
681  t[i] = x;
682  }
683  }
684 
685 
686 
688 template<typename oT>
689 inline
690 void
692  {
694 
695  init(0,0);
696  }
697 
698 
699 
701 template<typename oT>
702 inline
703 void
705  {
707 
709  }
710 
711 
712 
714 template<typename oT>
716 bool
718  {
719  return (n_elem == 0);
720  }
721 
722 
723 
725 template<typename oT>
728 bool
730  {
731  return (i < n_elem);
732  }
733 
734 
735 
737 template<typename oT>
740 bool
741 field<oT>::in_range(const span& x) const
742  {
744 
745  if(x.whole == true)
746  {
747  return true;
748  }
749  else
750  {
751  const uword a = x.a;
752  const uword b = x.b;
753 
754  return ( (a <= b) && (b < n_elem) );
755  }
756  }
757 
758 
759 
761 template<typename oT>
764 bool
765 field<oT>::in_range(const uword in_row, const uword in_col) const
766  {
767  return ( (in_row < n_rows) && (in_col < n_cols) );
768  }
769 
770 
771 
772 template<typename oT>
775 bool
776 field<oT>::in_range(const span& row_span, const uword in_col) const
777  {
779 
780  if(row_span.whole == true)
781  {
782  return (in_col < n_cols);
783  }
784  else
785  {
786  const uword in_row1 = row_span.a;
787  const uword in_row2 = row_span.b;
788 
789  return ( (in_row1 <= in_row2) && (in_row2 < n_rows) && (in_col < n_cols) );
790  }
791  }
792 
793 
794 
795 template<typename oT>
798 bool
799 field<oT>::in_range(const uword in_row, const span& col_span) const
800  {
802 
803  if(col_span.whole == true)
804  {
805  return (in_row < n_rows);
806  }
807  else
808  {
809  const uword in_col1 = col_span.a;
810  const uword in_col2 = col_span.b;
811 
812  return ( (in_row < n_rows) && (in_col1 <= in_col2) && (in_col2 < n_cols) );
813  }
814  }
815 
816 
817 
818 template<typename oT>
821 bool
822 field<oT>::in_range(const span& row_span, const span& col_span) const
823  {
825 
826  const uword in_row1 = row_span.a;
827  const uword in_row2 = row_span.b;
828 
829  const uword in_col1 = col_span.a;
830  const uword in_col2 = col_span.b;
831 
832  const bool rows_ok = row_span.whole ? true : ( (in_row1 <= in_row2) && (in_row2 < n_rows) );
833  const bool cols_ok = col_span.whole ? true : ( (in_col1 <= in_col2) && (in_col2 < n_cols) );
834 
835  return ( (rows_ok == true) && (cols_ok == true) );
836  }
837 
838 
839 
840 template<typename oT>
841 inline
842 bool
843 field<oT>::save(const std::string name, const file_type type, const bool print_status) const
844  {
846 
847  std::string err_msg;
848  const bool save_okay = field_aux::save(*this, name, type, err_msg);
849 
850  if( (print_status == true) && (save_okay == false) )
851  {
852  if(err_msg.length() > 0)
853  {
854  arma_warn(true, "field::save(): ", err_msg, name);
855  }
856  else
857  {
858  arma_warn(true, "field::save(): couldn't write to ", name);
859  }
860  }
861 
862  return save_okay;
863  }
864 
865 
866 
867 template<typename oT>
868 inline
869 bool
870 field<oT>::save(std::ostream& os, const file_type type, const bool print_status) const
871  {
873 
874  std::string err_msg;
875  const bool save_okay = field_aux::save(*this, os, type, err_msg);
876 
877  if( (print_status == true) && (save_okay == false) )
878  {
879  if(err_msg.length() > 0)
880  {
881  arma_warn(true, "field::save(): ", err_msg, "[ostream]");
882  }
883  else
884  {
885  arma_warn(true, "field::save(): couldn't write to [ostream]");
886  }
887  }
888 
889  return save_okay;
890  }
891 
892 
893 
894 template<typename oT>
895 inline
896 bool
897 field<oT>::load(const std::string name, const file_type type, const bool print_status)
898  {
900 
901  std::string err_msg;
902  const bool load_okay = field_aux::load(*this, name, type, err_msg);
903 
904  if( (print_status == true) && (load_okay == false) )
905  {
906  if(err_msg.length() > 0)
907  {
908  arma_warn(true, "field::load(): ", err_msg, name);
909  }
910  else
911  {
912  arma_warn(true, "field::load(): couldn't read from ", name);
913  }
914  }
915 
916  if(load_okay == false)
917  {
918  (*this).reset();
919  }
920 
921  return load_okay;
922  }
923 
924 
925 
926 template<typename oT>
927 inline
928 bool
929 field<oT>::load(std::istream& is, const file_type type, const bool print_status)
930  {
932 
933  std::string err_msg;
934  const bool load_okay = field_aux::load(*this, is, type, err_msg);
935 
936  if( (print_status == true) && (load_okay == false) )
937  {
938  if(err_msg.length() > 0)
939  {
940  arma_warn(true, "field::load(): ", err_msg, "[istream]");
941  }
942  else
943  {
944  arma_warn(true, "field::load(): couldn't read from [istream]");
945  }
946  }
947 
948  if(load_okay == false)
949  {
950  (*this).reset();
951  }
952 
953  return load_okay;
954  }
955 
956 
957 
958 template<typename oT>
959 inline
960 bool
961 field<oT>::quiet_save(const std::string name, const file_type type) const
962  {
964 
965  return (*this).save(name, type, false);
966  }
967 
968 
969 
970 template<typename oT>
971 inline
972 bool
973 field<oT>::quiet_save(std::ostream& os, const file_type type) const
974  {
976 
977  return (*this).save(os, type, false);
978  }
979 
980 
981 
982 template<typename oT>
983 inline
984 bool
985 field<oT>::quiet_load(const std::string name, const file_type type)
986  {
988 
989  return (*this).load(name, type, false);
990  }
991 
992 
993 
994 template<typename oT>
995 inline
996 bool
997 field<oT>::quiet_load(std::istream& is, const file_type type)
998  {
1000 
1001  return (*this).load(is, type, false);
1002  }
1003 
1004 
1005 
1007 template<typename oT>
1008 inline
1009 void
1011  {
1013 
1014  if(this != &x)
1015  {
1016  init(x.n_rows, x.n_cols);
1017 
1018  field& t = *this;
1019 
1020  for(uword col=0; col<x.n_cols; ++col)
1021  for(uword row=0; row<x.n_rows; ++row)
1022  {
1023  t.at(row,col) = x.at(row,col);
1024  }
1025  }
1026 
1027  }
1028 
1029 
1030 
1032 template<typename oT>
1033 inline
1034 void
1035 field<oT>::init(const uword n_rows_in, const uword n_cols_in)
1036  {
1037  arma_extra_debug_sigprint( arma_boost::format("n_rows_in = %d, n_cols_in = %d") % n_rows_in % n_cols_in );
1038 
1039  const uword n_elem_new = n_rows_in * n_cols_in;
1040 
1041  if(n_elem == n_elem_new)
1042  {
1043  // delete_objects();
1044  // create_objects();
1045  access::rw(n_rows) = n_rows_in;
1046  access::rw(n_cols) = n_cols_in;
1047  }
1048  else
1049  {
1050  delete_objects();
1051 
1052  if(n_elem > sizeof(mem_local)/sizeof(oT*) )
1053  {
1054  delete [] mem;
1055  }
1056 
1057  if(n_elem_new <= sizeof(mem_local)/sizeof(oT*) )
1058  {
1059  mem = mem_local;
1060  }
1061  else
1062  {
1063  mem = new(std::nothrow) oT* [n_elem_new];
1064  arma_check_bad_alloc( (mem == 0), "field::init(): out of memory" );
1065  }
1066 
1067  access::rw(n_elem) = n_elem_new;
1068 
1069  if(n_elem_new == 0)
1070  {
1071  access::rw(n_rows) = 0;
1072  access::rw(n_cols) = 0;
1073  }
1074  else
1075  {
1076  access::rw(n_rows) = n_rows_in;
1077  access::rw(n_cols) = n_cols_in;
1078  }
1079 
1080  create_objects();
1081 
1082  }
1083 
1084  }
1085 
1086 
1087 
1088 template<typename oT>
1089 inline
1090 void
1092  {
1094 
1095  for(uword i=0; i<n_elem; ++i)
1096  {
1097  if(mem[i] != 0)
1098  {
1099  delete mem[i];
1100  mem[i] = 0;
1101  }
1102  }
1103 
1104  }
1105 
1106 
1107 
1108 template<typename oT>
1109 inline
1110 void
1112  {
1114 
1115  for(uword i=0; i<n_elem; ++i)
1116  {
1117  mem[i] = new oT;
1118  }
1119 
1120  }
1121 
1122 
1123 
1124 template<typename oT>
1125 inline
1126 field<oT>::iterator::iterator(field<oT>& in_M, const bool at_end)
1127  : M(in_M)
1128  , i( (at_end == false) ? 0 : in_M.n_elem )
1129  {
1131  }
1132 
1133 
1134 
1135 template<typename oT>
1136 inline
1137 oT&
1139  {
1140  return M[i];
1141  }
1142 
1143 
1144 
1145 template<typename oT>
1146 inline
1147 typename field<oT>::iterator&
1149  {
1150  ++i;
1151 
1152  return *this;
1153  }
1154 
1155 
1156 
1157 template<typename oT>
1158 inline
1159 void
1161  {
1162  operator++();
1163  }
1164 
1165 
1166 
1167 template<typename oT>
1168 inline
1169 typename field<oT>::iterator&
1171  {
1172  if(i > 0)
1173  {
1174  --i;
1175  }
1176 
1177  return *this;
1178  }
1179 
1180 
1181 
1182 template<typename oT>
1183 inline
1184 void
1186  {
1187  operator--();
1188  }
1189 
1190 
1191 
1192 template<typename oT>
1193 inline
1194 bool
1196  {
1197  return (i != X.i);
1198  }
1199 
1200 
1201 
1202 template<typename oT>
1203 inline
1204 bool
1206  {
1207  return (i == X.i);
1208  }
1209 
1210 
1211 
1212 template<typename oT>
1213 inline
1215  : M(in_M)
1216  , i( (at_end == false) ? 0 : in_M.n_elem )
1217  {
1219  }
1220 
1221 
1222 
1223 template<typename oT>
1224 inline
1226  : M(X.M)
1227  , i(X.i)
1228  {
1230  }
1231 
1232 
1233 
1234 template<typename oT>
1235 inline
1236 const oT&
1238  {
1239  return M[i];
1240  }
1241 
1242 
1243 
1244 template<typename oT>
1245 inline
1246 typename field<oT>::const_iterator&
1248  {
1249  ++i;
1250 
1251  return *this;
1252  }
1253 
1254 
1255 
1256 template<typename oT>
1257 inline
1258 void
1260  {
1261  operator++();
1262  }
1263 
1264 
1265 
1266 template<typename oT>
1267 inline
1268 typename field<oT>::const_iterator&
1270  {
1271  if(i > 0)
1272  {
1273  --i;
1274  }
1275 
1276  return *this;
1277  }
1278 
1279 
1280 
1281 template<typename oT>
1282 inline
1283 void
1285  {
1286  operator--();
1287  }
1288 
1289 
1290 
1291 template<typename oT>
1292 inline
1293 bool
1295  {
1296  return (i != X.i);
1297  }
1298 
1299 
1300 
1301 template<typename oT>
1302 inline
1303 bool
1305  {
1306  return (i == X.i);
1307  }
1308 
1309 
1310 
1311 template<typename oT>
1312 inline
1313 typename field<oT>::iterator
1315  {
1317 
1318  return field<oT>::iterator(*this);
1319  }
1320 
1321 
1322 
1323 template<typename oT>
1324 inline
1327  {
1329 
1330  return field<oT>::const_iterator(*this);
1331  }
1332 
1333 
1334 
1335 template<typename oT>
1336 inline
1337 typename field<oT>::iterator
1339  {
1341 
1342  return field<oT>::iterator(*this, true);
1343  }
1344 
1345 
1346 
1347 template<typename oT>
1348 inline
1351  {
1353 
1354  return field<oT>::const_iterator(*this, true);
1355  }
1356 
1357 
1358 
1359 //
1360 //
1361 //
1362 
1363 
1364 
1365 template<typename oT>
1366 inline
1367 void
1369  {
1371 
1372  x.delete_objects();
1373  x.create_objects();
1374  }
1375 
1376 
1377 
1378 template<typename eT>
1379 inline
1380 void
1382  {
1384 
1385  for(uword i=0; i<x.n_elem; ++i)
1386  {
1387  (*(x.mem[i])).reset();
1388  }
1389  }
1390 
1391 
1392 
1393 template<typename eT>
1394 inline
1395 void
1397  {
1399 
1400  for(uword i=0; i<x.n_elem; ++i)
1401  {
1402  (*(x.mem[i])).reset();
1403  }
1404  }
1405 
1406 
1407 
1408 template<typename eT>
1409 inline
1410 void
1412  {
1414 
1415  for(uword i=0; i<x.n_elem; ++i)
1416  {
1417  (*(x.mem[i])).reset();
1418  }
1419  }
1420 
1421 
1422 
1423 template<typename eT>
1424 inline
1425 void
1427  {
1429 
1430  for(uword i=0; i<x.n_elem; ++i)
1431  {
1432  (*(x.mem[i])).reset();
1433  }
1434  }
1435 
1436 
1437 
1438 inline
1439 void
1441  {
1443 
1444  for(uword i=0; i<x.n_elem; ++i)
1445  {
1446  (*(x.mem[i])).clear();
1447  }
1448  }
1449 
1450 
1451 
1452 //
1453 //
1454 //
1455 
1456 
1457 
1458 template<typename oT>
1459 inline
1460 bool
1461 field_aux::save(const field<oT>& x, const std::string& name, const file_type type, std::string& err_msg)
1462  {
1464 
1465  err_msg = " [sorry, saving/loading this type of field is currently not supported] filename = ";
1466 
1467  return false;
1468  }
1469 
1470 
1471 
1472 template<typename oT>
1473 inline
1474 bool
1475 field_aux::save(const field<oT>& x, std::ostream& os, const file_type type, std::string& err_msg)
1476  {
1478 
1479  err_msg = " [sorry, saving/loading this type of field is currently not supported] filename = ";
1480 
1481  return false;
1482  }
1483 
1484 
1485 
1486 template<typename oT>
1487 inline
1488 bool
1489 field_aux::load(field<oT>& x, const std::string& name, const file_type type, std::string& err_msg)
1490  {
1492 
1493  err_msg = " [sorry, saving/loading this type of field is currently not supported] filename = ";
1494 
1495  return false;
1496  }
1497 
1498 
1499 
1500 template<typename oT>
1501 inline
1502 bool
1503 field_aux::load(field<oT>& x, std::istream& is, const file_type type, std::string& err_msg)
1504  {
1506 
1507  err_msg = " [sorry, saving/loading this type of field is currently not supported] filename = ";
1508 
1509  return false;
1510  }
1511 
1512 
1513 
1514 template<typename eT>
1515 inline
1516 bool
1517 field_aux::save(const field< Mat<eT> >& x, const std::string& name, const file_type type, std::string& err_msg)
1518  {
1520 
1521  switch(type)
1522  {
1523  case arma_binary:
1524  return diskio::save_arma_binary(x, name);
1525  break;
1526 
1527  case ppm_binary:
1528  return diskio::save_ppm_binary(x, name);
1529  break;
1530 
1531  default:
1532  err_msg = " [unsupported type] filename = ";
1533  return false;
1534  }
1535  }
1536 
1537 
1538 
1539 template<typename eT>
1540 inline
1541 bool
1542 field_aux::save(const field< Mat<eT> >& x, std::ostream& os, const file_type type, std::string& err_msg)
1543  {
1545 
1546  switch(type)
1547  {
1548  case arma_binary:
1549  return diskio::save_arma_binary(x, os);
1550  break;
1551 
1552  case ppm_binary:
1553  return diskio::save_ppm_binary(x, os);
1554  break;
1555 
1556  default:
1557  err_msg = " [unsupported type] filename = ";
1558  return false;
1559  }
1560  }
1561 
1562 
1563 
1564 template<typename eT>
1565 inline
1566 bool
1567 field_aux::load(field< Mat<eT> >& x, const std::string& name, const file_type type, std::string& err_msg)
1568  {
1570 
1571  switch(type)
1572  {
1573  case auto_detect:
1574  return diskio::load_auto_detect(x, name, err_msg);
1575  break;
1576 
1577  case arma_binary:
1578  return diskio::load_arma_binary(x, name, err_msg);
1579  break;
1580 
1581  case ppm_binary:
1582  return diskio::load_ppm_binary(x, name, err_msg);
1583  break;
1584 
1585  default:
1586  err_msg = " [unsupported type] filename = ";
1587  return false;
1588  }
1589  }
1590 
1591 
1592 
1593 template<typename eT>
1594 inline
1595 bool
1596 field_aux::load(field< Mat<eT> >& x, std::istream& is, const file_type type, std::string& err_msg)
1597  {
1599 
1600  switch(type)
1601  {
1602  case auto_detect:
1603  return diskio::load_auto_detect(x, is, err_msg);
1604  break;
1605 
1606  case arma_binary:
1607  return diskio::load_arma_binary(x, is, err_msg);
1608  break;
1609 
1610  case ppm_binary:
1611  return diskio::load_ppm_binary(x, is, err_msg);
1612  break;
1613 
1614  default:
1615  err_msg = " [unsupported type] filename = ";
1616  return false;
1617  }
1618  }
1619 
1620 
1621 
1622 template<typename eT>
1623 inline
1624 bool
1625 field_aux::save(const field< Col<eT> >& x, const std::string& name, const file_type type, std::string& err_msg)
1626  {
1628 
1629  switch(type)
1630  {
1631  case arma_binary:
1632  return diskio::save_arma_binary(x, name);
1633  break;
1634 
1635  case ppm_binary:
1636  return diskio::save_ppm_binary(x, name);
1637  break;
1638 
1639  default:
1640  err_msg = " [unsupported type] filename = ";
1641  return false;
1642  }
1643  }
1644 
1645 
1646 
1647 template<typename eT>
1648 inline
1649 bool
1650 field_aux::save(const field< Col<eT> >& x, std::ostream& os, const file_type type, std::string& err_msg)
1651  {
1653 
1654  switch(type)
1655  {
1656  case arma_binary:
1657  return diskio::save_arma_binary(x, os);
1658  break;
1659 
1660  case ppm_binary:
1661  return diskio::save_ppm_binary(x, os);
1662  break;
1663 
1664  default:
1665  err_msg = " [unsupported type] filename = ";
1666  return false;
1667  }
1668  }
1669 
1670 
1671 
1672 template<typename eT>
1673 inline
1674 bool
1675 field_aux::load(field< Col<eT> >& x, const std::string& name, const file_type type, std::string& err_msg)
1676  {
1678 
1679  switch(type)
1680  {
1681  case auto_detect:
1682  return diskio::load_auto_detect(x, name, err_msg);
1683  break;
1684 
1685  case arma_binary:
1686  return diskio::load_arma_binary(x, name, err_msg);
1687  break;
1688 
1689  case ppm_binary:
1690  return diskio::load_ppm_binary(x, name, err_msg);
1691  break;
1692 
1693  default:
1694  err_msg = " [unsupported type] filename = ";
1695  return false;
1696  }
1697  }
1698 
1699 
1700 
1701 template<typename eT>
1702 inline
1703 bool
1704 field_aux::load(field< Col<eT> >& x, std::istream& is, const file_type type, std::string& err_msg)
1705  {
1707 
1708  switch(type)
1709  {
1710  case auto_detect:
1711  return diskio::load_auto_detect(x, is, err_msg);
1712  break;
1713 
1714  case arma_binary:
1715  return diskio::load_arma_binary(x, is, err_msg);
1716  break;
1717 
1718  case ppm_binary:
1719  return diskio::load_ppm_binary(x, is, err_msg);
1720  break;
1721 
1722  default:
1723  err_msg = " [unsupported type] filename = ";
1724  return false;
1725  }
1726  }
1727 
1728 
1729 
1730 template<typename eT>
1731 inline
1732 bool
1733 field_aux::save(const field< Row<eT> >& x, const std::string& name, const file_type type, std::string& err_msg)
1734  {
1736 
1737  switch(type)
1738  {
1739  case arma_binary:
1740  return diskio::save_arma_binary(x, name, err_msg);
1741  break;
1742 
1743  case ppm_binary:
1744  return diskio::save_ppm_binary(x, name, err_msg);
1745  break;
1746 
1747  default:
1748  err_msg = " [unsupported type] filename = ";
1749  return false;
1750  }
1751  }
1752 
1753 
1754 
1755 template<typename eT>
1756 inline
1757 bool
1758 field_aux::save(const field< Row<eT> >& x, std::ostream& os, const file_type type, std::string& err_msg)
1759  {
1761 
1762  switch(type)
1763  {
1764  case arma_binary:
1765  return diskio::save_arma_binary(x, os, err_msg);
1766  break;
1767 
1768  case ppm_binary:
1769  return diskio::save_ppm_binary(x, os, err_msg);
1770  break;
1771 
1772  default:
1773  err_msg = " [unsupported type] filename = ";
1774  return false;
1775  }
1776  }
1777 
1778 
1779 
1780 template<typename eT>
1781 inline
1782 bool
1783 field_aux::load(field< Row<eT> >& x, const std::string& name, const file_type type, std::string& err_msg)
1784  {
1786 
1787  switch(type)
1788  {
1789  case auto_detect:
1790  return diskio::load_auto_detect(x, name, err_msg);
1791  break;
1792 
1793  case arma_binary:
1794  return diskio::load_arma_binary(x, name, err_msg);
1795  break;
1796 
1797  case ppm_binary:
1798  return diskio::load_ppm_binary(x, name, err_msg);
1799  break;
1800 
1801  default:
1802  err_msg = " [unsupported type] filename = ";
1803  return false;
1804  }
1805  }
1806 
1807 
1808 
1809 template<typename eT>
1810 inline
1811 bool
1812 field_aux::load(field< Row<eT> >& x, std::istream& is, const file_type type, std::string& err_msg)
1813  {
1815 
1816  switch(type)
1817  {
1818  case auto_detect:
1819  return diskio::load_auto_detect(x, is, err_msg);
1820  break;
1821 
1822  case arma_binary:
1823  return diskio::load_arma_binary(x, is, err_msg);
1824  break;
1825 
1826  case ppm_binary:
1827  return diskio::load_ppm_binary(x, is, err_msg);
1828  break;
1829 
1830  default:
1831  err_msg = " [unsupported type] filename = ";
1832  return false;
1833  }
1834  }
1835 
1836 
1837 
1838 template<typename eT>
1839 inline
1840 bool
1841 field_aux::save(const field< Cube<eT> >& x, const std::string& name, const file_type type, std::string& err_msg)
1842  {
1844 
1845  switch(type)
1846  {
1847  case arma_binary:
1848  return diskio::save_arma_binary(x, name, err_msg);
1849  break;
1850 
1851  case ppm_binary:
1852  return diskio::save_ppm_binary(x, name, err_msg);
1853  break;
1854 
1855  default:
1856  err_msg = " [unsupported type] filename = ";
1857  return false;
1858  }
1859  }
1860 
1861 
1862 
1863 template<typename eT>
1864 inline
1865 bool
1866 field_aux::save(const field< Cube<eT> >& x, std::ostream& os, const file_type type, std::string& err_msg)
1867  {
1869 
1870  switch(type)
1871  {
1872  case arma_binary:
1873  return diskio::save_arma_binary(x, os, err_msg);
1874  break;
1875 
1876  case ppm_binary:
1877  return diskio::save_ppm_binary(x, os, err_msg);
1878  break;
1879 
1880  default:
1881  err_msg = " [unsupported type] filename = ";
1882  return false;
1883  }
1884  }
1885 
1886 
1887 
1888 template<typename eT>
1889 inline
1890 bool
1891 field_aux::load(field< Cube<eT> >& x, const std::string& name, const file_type type, std::string& err_msg)
1892  {
1894 
1895  switch(type)
1896  {
1897  case auto_detect:
1898  return diskio::load_auto_detect(x, name, err_msg);
1899  break;
1900 
1901  case arma_binary:
1902  return diskio::load_arma_binary(x, name, err_msg);
1903  break;
1904 
1905  case ppm_binary:
1906  return diskio::load_ppm_binary(x, name, err_msg);
1907  break;
1908 
1909  default:
1910  err_msg = " [unsupported type] filename = ";
1911  return false;
1912  }
1913  }
1914 
1915 
1916 
1917 template<typename eT>
1918 inline
1919 bool
1920 field_aux::load(field< Cube<eT> >& x, std::istream& is, const file_type type, std::string& err_msg)
1921  {
1923 
1924  switch(type)
1925  {
1926  case auto_detect:
1927  return diskio::load_auto_detect(x, is, err_msg);
1928  break;
1929 
1930  case arma_binary:
1931  return diskio::load_arma_binary(x, is, err_msg);
1932  break;
1933 
1934  case ppm_binary:
1935  return diskio::load_ppm_binary(x, is, err_msg);
1936  break;
1937 
1938  default:
1939  err_msg = " [unsupported type] filename = ";
1940  return false;
1941  }
1942  }
1943 
1944 
1945 
1946 inline
1947 bool
1948 field_aux::save(const field< std::string >& x, const std::string& name, const file_type type, std::string& err_msg)
1949  {
1951 
1952  arma_ignore(type);
1953 
1954  err_msg.clear();
1955 
1956  return diskio::save_std_string(x, name);
1957  }
1958 
1959 
1960 
1961 inline
1962 bool
1963 field_aux::save(const field< std::string >& x, std::ostream& os, const file_type type, std::string& err_msg)
1964  {
1966 
1967  arma_ignore(type);
1968 
1969  err_msg.clear();
1970 
1971  return diskio::save_std_string(x, os);
1972  }
1973 
1974 
1975 
1976 inline
1977 bool
1978 field_aux::load(field< std::string >& x, const std::string& name, const file_type type, std::string& err_msg)
1979  {
1981 
1982  arma_ignore(type);
1983 
1984  return diskio::load_std_string(x, name, err_msg);
1985  }
1986 
1987 
1988 
1989 inline
1990 bool
1991 field_aux::load(field< std::string >& x, std::istream& is, const file_type type, std::string& err_msg)
1992  {
1994 
1995  arma_ignore(type);
1996 
1997  return diskio::load_std_string(x, is, err_msg);
1998  }
1999 
2000 
2001 
2002 #ifdef ARMA_EXTRA_FIELD_MEAT
2003  #include ARMA_INCFILE_WRAP(ARMA_EXTRA_FIELD_MEAT)
2004 #endif
2005 
2006 
2007 
Portable Pixel Map (colour image), used by the field and cube classes.
uword b
Definition: span.hpp:40
arma_inline arma_warn_unused bool in_range(const uword i) const
returns true if the given index is currently in range
Definition: field_meat.hpp:729
void create_objects()
const field & operator=(const field &x)
construct a field from a given field
Definition: field_meat.hpp:77
bool whole
Definition: span.hpp:41
static bool load_arma_binary(Mat< eT > &x, const std::string &name, std::string &err_msg)
arma_inline bool is_empty() const
returns true if the field has no objects
Definition: field_meat.hpp:717
void arma_hot arma_check_bad_alloc(const bool state, const T1 &x)
Definition: debug.hpp:368
bool operator==(const iterator &X) const
const oT & operator*() const
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
Definition: span.hpp:35
void set_size(const uword n_obj_in)
Definition: field_meat.hpp:155
const_iterator & operator--()
subview_field< oT > cols(const uword in_col1, const uword in_col2)
creation of subview_field (subfield comprised of specified columns)
Definition: field_meat.hpp:431
arma_aligned oT ** mem
pointer to memory used by the object
Definition: field_bones.hpp:44
arma_aligned uword i
subview_field< oT > rows(const uword in_row1, const uword in_row2)
creation of subview_field (subfield comprised of specified rows)
Definition: field_meat.hpp:389
iterator(field< oT > &in_M, const bool at_end=false)
u32 uword
Definition: typedef.hpp:85
~field()
Definition: field_meat.hpp:21
bool save(const std::string name, const file_type type=arma_binary, const bool print_status=true) const
Definition: field_meat.hpp:843
iterator end()
static bool load_std_string(field< std::string > &x, const std::string &name, std::string &err_msg)
Class for column vectors (matrices with only one column)
Definition: Col_bones.hpp:20
static bool save_ppm_binary(const Cube< T1 > &x, const std::string &final_name)
arma_inline oT & operator[](const uword i)
linear element accessor (treats the field as a vector); no bounds check
Definition: field_meat.hpp:195
iterator & operator++()
bool load(const std::string name, const file_type type=auto_detect, const bool print_status=true)
Definition: field_meat.hpp:897
static bool load_ppm_binary(Cube< T1 > &x, const std::string &final_name, std::string &err_msg)
static void print(std::ostream &o, const Mat< eT > &m, const bool modify)
Print a matrix to the specified stream.
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
file_type
uword a
Definition: span.hpp:39
bool operator!=(const iterator &X) const
bool quiet_save(const std::string name, const file_type type=arma_binary) const
Definition: field_meat.hpp:961
void reset_objects()
reset each object
Definition: field_meat.hpp:704
static void reset_objects(field< oT > &x)
void delete_objects()
const_iterator(const field< oT > &in_M, const bool at_end=false)
iterator begin()
#define arma_ignore(variable)
Dense cube class.
Definition: Cube_bones.hpp:30
arma_inline oT & operator()(const uword i)
linear element accessor (treats the field as a vector); bounds checking not done when ARMA_NO_DEBUG i...
Definition: field_meat.hpp:239
#define arma_extra_debug_sigprint_this
Definition: debug.hpp:1117
Armadillo binary format, with information about matrix type and size.
static bool load_auto_detect(Mat< eT > &x, const std::string &name, std::string &err_msg)
Try to load a matrix by automatically determining its type.
arma_aligned field< oT > & M
static bool save(const field< oT > &x, const std::string &name, const file_type type, std::string &err_msg)
void reset()
reset the field to an empty state (i.e. the field will have no objects)
Definition: field_meat.hpp:691
Class for row vectors (matrices with only one row)
void print(const std::string extra_text="") const
Definition: field_meat.hpp:621
arma_aligned const field< oT > & M
#define arma_extra_debug_sigprint
Definition: debug.hpp:1116
static bool save_std_string(const field< std::string > &x, const std::string &name)
static void extract(field< oT > &out, const subview_field &in)
X = Y.subfield(...)
#define arma_warn_unused
subview_field< oT > row(const uword row_num)
creation of subview_field (row of a field)
Definition: field_meat.hpp:329
subview_field< oT > subfield(const uword in_row1, const uword in_col1, const uword in_row2, const uword in_col2)
creation of subview_field (subfield with arbitrary dimensions)
Definition: field_meat.hpp:473
void arma_cold arma_warn(const bool state, const T1 &x)
print a message to the warn stream
Definition: debug.hpp:298
bool operator==(const const_iterator &X) const
bool quiet_load(const std::string name, const file_type type=auto_detect)
Definition: field_meat.hpp:985
Dense matrix class.
#define arma_inline
field_injector< field > operator<<(const oT &val)
Definition: field_meat.hpp:308
void fill(const oT &x)
fill the field with an object
Definition: field_meat.hpp:673
static bool load(field< oT > &x, const std::string &name, const file_type type, std::string &err_msg)
arma_aligned oT * mem_local[field_prealloc_n_elem::val]
Internal memory, to avoid calling the &#39;new&#39; operator for small amounts of memory. ...
Definition: field_bones.hpp:45
static const bool debug
Definition: arma_config.hpp:63
bool operator!=(const const_iterator &X) const
const_iterator & operator++()
iterator & operator--()
const uword n_elem
number of elements in the field (read-only)
Definition: field_bones.hpp:39
void init(const field< oT > &x)
construct a field from a given field
arma_aligned uword i
void copy_size(const field< oT2 > &x)
change the field to have the specified dimensions (data is not preserved)
Definition: field_meat.hpp:182
Automatically detect the file type (file must be one of the following types)
static bool save_arma_binary(const Mat< eT > &x, const std::string &final_name)
arma_inline oT & at(const uword i)
linear element accessor (treats the field as a vector); no bounds check
Definition: field_meat.hpp:217


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