field_proto.hpp

Go to the documentation of this file.
00001 // Copyright (C) 2010 NICTA and the authors listed below
00002 // http://nicta.com.au
00003 // 
00004 // Authors:
00005 // - Conrad Sanderson (conradsand at ieee dot org)
00006 // - Ian Cullinan (ian dot cullinan at nicta dot com dot au)
00007 // 
00008 // This file is part of the Armadillo C++ library.
00009 // It is provided without any warranty of fitness
00010 // for any purpose. You can redistribute this file
00011 // and/or modify it under the terms of the GNU
00012 // Lesser General Public License (LGPL) as published
00013 // by the Free Software Foundation, either version 3
00014 // of the License or (at your option) any later version.
00015 // (see http://www.opensource.org/licenses for more info)
00016 
00017 
00018 //! \addtogroup field
00019 //! @{
00020 
00021 
00022 
00023 //! A lightweight 2D container for abitrary objects
00024 //! (the objects must have a copy constructor)
00025 
00026 template<typename oT>
00027 class field
00028   {
00029   public:
00030   
00031   typedef oT object_type;
00032   
00033   const u32 n_rows;     //!< number of rows in the field (read-only)
00034   const u32 n_cols;     //!< number of columns in the field (read-only)
00035   const u32 n_elem;     //!< number of elements in the field (read-only)
00036   
00037   
00038   private:
00039   
00040   arma_aligned oT** mem;             //!< pointer to memory used by the object
00041   arma_aligned oT*  mem_local[ 16 ]; //!< Internal memory, to avoid calling the 'new' operator for small amounts of memory
00042   
00043   
00044   public:
00045   
00046   inline ~field();
00047   inline  field();
00048   
00049   inline                  field(const field& x);
00050   inline const field& operator=(const field& x);
00051   
00052   inline                  field(const subview_field<oT>& x);
00053   inline const field& operator=(const subview_field<oT>& x);
00054   
00055   inline explicit field(const u32 n_elem_in);
00056   inline          field(const u32 n_rows_in, const u32 n_cols_in);
00057   
00058   inline void  set_size(const u32 n_obj_in);
00059   inline void  set_size(const u32 n_rows_in, const u32 n_cols_in);
00060   
00061   template<typename oT2>
00062   inline void copy_size(const field<oT2>& x);
00063   
00064   arma_inline       oT& operator[](const u32 i);
00065   arma_inline const oT& operator[](const u32 i) const;
00066   
00067   arma_inline       oT& operator()(const u32 i);
00068   arma_inline const oT& operator()(const u32 i) const;
00069   
00070   arma_inline       oT&         at(const u32 row, const u32 col);
00071   arma_inline const oT&         at(const u32 row, const u32 col) const;
00072   
00073   arma_inline       oT& operator()(const u32 row, const u32 col);
00074   arma_inline const oT& operator()(const u32 row, const u32 col) const;
00075   
00076   inline       subview_field<oT> row(const u32 row_num);
00077   inline const subview_field<oT> row(const u32 row_num) const;
00078   
00079   inline       subview_field<oT> col(const u32 col_num);
00080   inline const subview_field<oT> col(const u32 col_num) const;
00081   
00082   inline       subview_field<oT> rows(const u32 in_row1, const u32 in_row2);
00083   inline const subview_field<oT> rows(const u32 in_row1, const u32 in_row2) const;
00084   
00085   inline       subview_field<oT> cols(const u32 in_col1, const u32 in_col2);
00086   inline const subview_field<oT> cols(const u32 in_col1, const u32 in_col2) const;
00087   
00088   inline       subview_field<oT> subfield(const u32 in_row1, const u32 in_col1, const u32 in_row2, const u32 in_col2);
00089   inline const subview_field<oT> subfield(const u32 in_row1, const u32 in_col1, const u32 in_row2, const u32 in_col2) const;
00090   
00091   inline void print(const std::string extra_text = "") const;
00092   inline void print(std::ostream& user_stream, const std::string extra_text = "") const;
00093   
00094   inline void fill(const oT& x);
00095   
00096   inline void reset();
00097   inline void reset_objects();
00098   
00099   
00100   inline bool save(const std::string   name, const file_type type = arma_binary, const bool print_status = true) const;
00101   inline bool save(      std::ostream& os,   const file_type type = arma_binary, const bool print_status = true) const;
00102   
00103   inline bool load(const std::string   name, const file_type type = auto_detect, const bool print_status = true);
00104   inline bool load(      std::istream& is,   const file_type type = auto_detect, const bool print_status = true);
00105   
00106   
00107   inline bool quiet_save(const std::string   name, const file_type type = arma_binary) const;
00108   inline bool quiet_save(      std::ostream& os,   const file_type type = arma_binary) const;
00109   
00110   inline bool quiet_load(const std::string   name, const file_type type = auto_detect);
00111   inline bool quiet_load(      std::istream& is,   const file_type type = auto_detect);
00112   
00113   
00114   // iterators
00115   
00116   class iterator
00117     {
00118     public:
00119     
00120     inline iterator(field<oT>& in_M, const bool at_end = false);
00121     
00122     inline oT& operator* ();
00123     
00124     inline iterator& operator++();
00125     inline void      operator++(int);
00126     
00127     inline iterator& operator--();
00128     inline void      operator--(int);
00129     
00130     inline bool operator!=(const iterator& X) const;
00131     inline bool operator==(const iterator& X) const;
00132     
00133     arma_aligned field<oT>& M;
00134     arma_aligned u32        i;
00135     };
00136   
00137   
00138   class const_iterator
00139     {
00140     public:
00141     
00142     const_iterator(const field<oT>& in_M, const bool at_end = false);
00143     const_iterator(const iterator& X);
00144     
00145     inline const oT& operator*() const;
00146     
00147     inline const_iterator& operator++();
00148     inline void            operator++(int);
00149     
00150     inline const_iterator& operator--();
00151     inline void            operator--(int);
00152     
00153     inline bool operator!=(const const_iterator& X) const;
00154     inline bool operator==(const const_iterator& X) const;
00155     
00156     arma_aligned const field<oT>& M;
00157     arma_aligned       u32        i;
00158     };
00159   
00160   inline       iterator begin();
00161   inline const_iterator begin() const;
00162   
00163   inline       iterator end();
00164   inline const_iterator end()   const;
00165   
00166   
00167   private:
00168   
00169   inline void init(const field<oT>& x);
00170   inline void init(const u32 n_rows_in, const u32 n_cols_in);
00171   
00172   inline void delete_objects();
00173   inline void create_objects();
00174   
00175   friend class field_aux;
00176   friend class subview_field<oT>;
00177   };
00178 
00179 
00180 
00181 class field_aux
00182   {
00183   public:
00184   
00185   template<typename oT> inline static void reset_objects(field< oT >& x);
00186   template<typename eT> inline static void reset_objects(field< Mat<eT> >& x);
00187   template<typename eT> inline static void reset_objects(field< Col<eT> >& x);
00188   template<typename eT> inline static void reset_objects(field< Row<eT> >& x);
00189   template<typename eT> inline static void reset_objects(field< Cube<eT> >& x);
00190                         inline static void reset_objects(field< std::string >& x);
00191   
00192   
00193   template<typename oT> inline static bool save(const field< oT >& x,       const std::string& name, const file_type type, std::string& err_msg);
00194   template<typename oT> inline static bool save(const field< oT >& x,       std::ostream& os,        const file_type type, std::string& err_msg);
00195   template<typename oT> inline static bool load(      field< oT >& x,       const std::string& name, const file_type type, std::string& err_msg);
00196   template<typename oT> inline static bool load(      field< oT >& x,       std::istream& is,        const file_type type, std::string& err_msg);
00197 
00198   template<typename eT> inline static bool save(const field< Mat<eT> >& x,  const std::string& name, const file_type type, std::string& err_msg);
00199   template<typename eT> inline static bool save(const field< Mat<eT> >& x,  std::ostream& os,        const file_type type, std::string& err_msg);
00200   template<typename eT> inline static bool load(      field< Mat<eT> >& x,  const std::string& name, const file_type type, std::string& err_msg);
00201   template<typename eT> inline static bool load(      field< Mat<eT> >& x,  std::istream& is,        const file_type type, std::string& err_msg);
00202   
00203   template<typename eT> inline static bool save(const field< Col<eT> >& x,  const std::string& name, const file_type type, std::string& err_msg);
00204   template<typename eT> inline static bool save(const field< Col<eT> >& x,  std::ostream& os,        const file_type type, std::string& err_msg);
00205   template<typename eT> inline static bool load(      field< Col<eT> >& x,  const std::string& name, const file_type type, std::string& err_msg);
00206   template<typename eT> inline static bool load(      field< Col<eT> >& x,  std::istream& is,        const file_type type, std::string& err_msg);
00207   
00208   template<typename eT> inline static bool save(const field< Row<eT> >& x,  const std::string& name, const file_type type, std::string& err_msg);
00209   template<typename eT> inline static bool save(const field< Row<eT> >& x,  std::ostream& os,        const file_type type, std::string& err_msg);
00210   template<typename eT> inline static bool load(      field< Row<eT> >& x,  const std::string& name, const file_type type, std::string& err_msg);
00211   template<typename eT> inline static bool load(      field< Row<eT> >& x,  std::istream& is,        const file_type type, std::string& err_msg);
00212 
00213   template<typename eT> inline static bool save(const field< Cube<eT> >& x, const std::string& name, const file_type type, std::string& err_msg);
00214   template<typename eT> inline static bool save(const field< Cube<eT> >& x, std::ostream& os,        const file_type type, std::string& err_msg);
00215   template<typename eT> inline static bool load(      field< Cube<eT> >& x, const std::string& name, const file_type type, std::string& err_msg);
00216   template<typename eT> inline static bool load(      field< Cube<eT> >& x, std::istream& is,        const file_type type, std::string& err_msg);
00217   
00218   inline static bool save(const field< std::string >& x, const std::string& name, const file_type type, std::string& err_msg);
00219   inline static bool save(const field< std::string >& x, std::ostream& os,        const file_type type, std::string& err_msg);
00220   inline static bool load(      field< std::string >& x, const std::string& name, const file_type type, std::string& err_msg);
00221   inline static bool load(      field< std::string >& x, std::istream& is,        const file_type type, std::string& err_msg);
00222   
00223   };
00224 
00225 
00226 //! @}