field_proto.hpp

Go to the documentation of this file.
00001 // Copyright (C) 2009 NICTA
00002 // 
00003 // Authors:
00004 // - Conrad Sanderson (conradsand at ieee dot org)
00005 // 
00006 // This file is part of the Armadillo C++ library.
00007 // It is provided without any warranty of fitness
00008 // for any purpose. You can redistribute this file
00009 // and/or modify it under the terms of the GNU
00010 // Lesser General Public License (LGPL) as published
00011 // by the Free Software Foundation, either version 3
00012 // of the License or (at your option) any later version.
00013 // (see http://www.opensource.org/licenses for more info)
00014 
00015 
00016 //! \addtogroup field
00017 //! @{
00018 
00019 
00020 
00021 //! A lightweight 2D container for abitrary objects
00022 //! (the objects must have a copy constructor)
00023 
00024 template<typename oT>
00025 class field
00026   {
00027   public:
00028   
00029   typedef oT object_type;
00030   
00031   const u32 n_rows;     //!< number of rows in the field (read-only)
00032   const u32 n_cols;     //!< number of columns in the field (read-only)
00033   const u32 n_elem;     //!< number of elements in the field (read-only)
00034 
00035   private:
00036   
00037   //! pointer to memory used by the object
00038   arma_aligned oT** mem;
00039   arma_aligned oT*  mem_local[ 16 ];
00040   //!< Internal memory, to avoid calling the 'new' operator for small amounts of memory
00041   
00042   
00043   public:
00044   
00045   inline ~field();
00046   inline  field();
00047   
00048   inline                  field(const field& x);
00049   inline const field& operator=(const field& x);
00050   
00051   inline                  field(const subview_field<oT>& x);
00052   inline const field& operator=(const subview_field<oT>& x);
00053   
00054   inline explicit field(const u32 n_elem_in);
00055   inline          field(const u32 n_rows_in, const u32 n_cols_in);
00056   
00057   inline void  set_size(const u32 n_obj_in);
00058   inline void  set_size(const u32 n_rows_in, const u32 n_cols_in);
00059   inline void copy_size(const field& x);
00060   
00061   arma_inline       oT& operator[](const u32 i);
00062   arma_inline const oT& operator[](const u32 i) const;
00063   
00064   arma_inline       oT& operator()(const u32 i);
00065   arma_inline const oT& operator()(const u32 i) const;
00066   
00067   arma_inline       oT&         at(const u32 row, const u32 col);
00068   arma_inline const oT&         at(const u32 row, const u32 col) const;
00069   
00070   arma_inline       oT& operator()(const u32 row, const u32 col);
00071   arma_inline const oT& operator()(const u32 row, const u32 col) const;
00072   
00073   inline       subview_field<oT> row(const u32 row_num);
00074   inline const subview_field<oT> row(const u32 row_num) const;
00075   
00076   inline       subview_field<oT> col(const u32 col_num);
00077   inline const subview_field<oT> col(const u32 col_num) const;
00078   
00079   inline       subview_field<oT> rows(const u32 in_row1, const u32 in_row2);
00080   inline const subview_field<oT> rows(const u32 in_row1, const u32 in_row2) const;
00081   
00082   inline       subview_field<oT> cols(const u32 in_col1, const u32 in_col2);
00083   inline const subview_field<oT> cols(const u32 in_col1, const u32 in_col2) const;
00084   
00085   inline       subview_field<oT> subfield(const u32 in_row1, const u32 in_col1, const u32 in_row2, const u32 in_col2);
00086   inline const subview_field<oT> subfield(const u32 in_row1, const u32 in_col1, const u32 in_row2, const u32 in_col2) const;
00087   
00088   inline void print(const std::string extra_text = "") const;
00089   inline void print(std::ostream& user_stream, const std::string extra_text = "") const;
00090   
00091   inline void fill(const oT& x);
00092   
00093   inline void reset();
00094   inline void reset_objects();
00095   
00096   inline void save(const std::string name, const file_type type = arma_binary) const;
00097   inline void load(const std::string name, const file_type type = auto_detect);
00098   
00099   
00100   private:
00101   
00102   inline void init(const field<oT>& x);
00103   inline void init(const u32 n_rows_in, const u32 n_cols_in);
00104   
00105   inline void delete_objects();
00106   inline void create_objects();
00107   
00108   friend class field_aux;
00109   friend class subview_field<oT>;
00110   };
00111 
00112 
00113 
00114 class field_aux
00115   {
00116   public:
00117   
00118   template<typename oT> inline static void reset_objects(field< oT >& x);
00119   template<typename eT> inline static void reset_objects(field< Mat<eT> >& x);
00120   template<typename eT> inline static void reset_objects(field< Col<eT> >& x);
00121   template<typename eT> inline static void reset_objects(field< Row<eT> >& x);
00122   template<typename eT> inline static void reset_objects(field< Cube<eT> >& x);
00123                         inline static void reset_objects(field< std::string >& x);
00124   
00125   
00126   template<typename oT> inline static void save(const field< oT >& x,          const std::string& name, const file_type type);
00127   template<typename oT> inline static void load(      field< oT >& x,          const std::string& name, const file_type type);
00128 
00129   template<typename eT> inline static void save(const field< Mat<eT> >& x,     const std::string& name, const file_type type);
00130   template<typename eT> inline static void load(      field< Mat<eT> >& x,     const std::string& name, const file_type type);
00131   
00132   template<typename eT> inline static void save(const field< Col<eT> >& x,     const std::string& name, const file_type type);
00133   template<typename eT> inline static void load(      field< Col<eT> >& x,     const std::string& name, const file_type type);
00134   
00135   template<typename eT> inline static void save(const field< Row<eT> >& x,     const std::string& name, const file_type type);
00136   template<typename eT> inline static void load(      field< Row<eT> >& x,     const std::string& name, const file_type type);
00137 
00138   template<typename eT> inline static void save(const field< Cube<eT> >& x,    const std::string& name, const file_type type);
00139   template<typename eT> inline static void load(      field< Cube<eT> >& x,    const std::string& name, const file_type type);
00140   
00141                         inline static void save(const field< std::string >& x, const std::string& name, const file_type type);
00142                         inline static void load(      field< std::string >& x, const std::string& name, const file_type type);
00143   
00144   };
00145 
00146 
00147 //! @}