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   
00060   arma_inline       oT& operator[](const u32 i);
00061   arma_inline const oT& operator[](const u32 i) const;
00062   
00063   arma_inline       oT& operator()(const u32 i);
00064   arma_inline const oT& operator()(const u32 i) const;
00065   
00066   arma_inline       oT&         at(const u32 row, const u32 col);
00067   arma_inline const oT&         at(const u32 row, const u32 col) const;
00068   
00069   arma_inline       oT& operator()(const u32 row, const u32 col);
00070   arma_inline const oT& operator()(const u32 row, const u32 col) const;
00071   
00072   inline       subview_field<oT> row(const u32 row_num);
00073   inline const subview_field<oT> row(const u32 row_num) const;
00074   
00075   inline       subview_field<oT> col(const u32 col_num);
00076   inline const subview_field<oT> col(const u32 col_num) const;
00077   
00078   inline       subview_field<oT> rows(const u32 in_row1, const u32 in_row2);
00079   inline const subview_field<oT> rows(const u32 in_row1, const u32 in_row2) const;
00080   
00081   inline       subview_field<oT> cols(const u32 in_col1, const u32 in_col2);
00082   inline const subview_field<oT> cols(const u32 in_col1, const u32 in_col2) const;
00083   
00084   inline       subview_field<oT> subfield(const u32 in_row1, const u32 in_col1, const u32 in_row2, const u32 in_col2);
00085   inline const subview_field<oT> subfield(const u32 in_row1, const u32 in_col1, const u32 in_row2, const u32 in_col2) const;
00086   
00087   inline void print(const std::string extra_text = "") const;
00088   
00089   inline void fill(const oT& x);
00090   
00091   inline void reset();
00092   inline void reset_objects();
00093   
00094   inline void save(const std::string name, const file_type type = arma_binary) const;
00095   inline void load(const std::string name, const file_type type = auto_detect);
00096   
00097   
00098   private:
00099   
00100   inline void init(const field<oT>& x);
00101   inline void init(const u32 n_rows_in, const u32 n_cols_in);
00102   
00103   inline void delete_objects();
00104   inline void create_objects();
00105   
00106   friend class field_aux;
00107   friend class subview_field<oT>;
00108   };
00109 
00110 
00111 
00112 class field_aux
00113   {
00114   public:
00115   
00116   template<typename oT>
00117   inline static void reset_objects(field< oT >& x);
00118   
00119   template<typename eT>
00120   inline static void reset_objects(field< Mat<eT> >& x);
00121   
00122   template<typename eT>
00123   inline static void reset_objects(field< Col<eT> >& x);
00124   
00125   template<typename eT>
00126   inline static void reset_objects(field< Row<eT> >& x);
00127   
00128   inline static void reset_objects(field< std::string >& x);
00129   
00130   
00131   template<typename oT>
00132   inline static void save(const field< oT >& x,          const std::string& name, const file_type type);
00133   
00134   template<typename oT>
00135   inline static void load(      field< oT >& x,          const std::string& name, const file_type type);
00136   
00137   template<typename eT>
00138   inline static void save(const field< Mat<eT> >& x,     const std::string& name, const file_type type);
00139 
00140   template<typename eT>
00141   inline static void load(      field< Mat<eT> >& x,     const std::string& name, const file_type type);
00142   
00143   template<typename eT>
00144   inline static void save(const field< Col<eT> >& x,     const std::string& name, const file_type type);
00145 
00146   template<typename eT>
00147   inline static void load(      field< Col<eT> >& x,     const std::string& name, const file_type type);
00148   
00149   template<typename eT>
00150   inline static void save(const field< Row<eT> >& x,     const std::string& name, const file_type type);
00151 
00152   template<typename eT>
00153   inline static void load(      field< Row<eT> >& x,     const std::string& name, const file_type type);
00154   
00155   
00156   inline static void save(const field< std::string >& x, const std::string& name, const file_type type);
00157   inline static void load(      field< std::string >& x, const std::string& name, const file_type type);
00158   
00159   
00160   
00161   };
00162 
00163 
00164 //! @}