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   private:
00038   
00039   //! pointer to memory used by the object
00040   arma_aligned oT** mem;
00041   arma_aligned oT*  mem_local[ 16 ];
00042   //!< Internal memory, to avoid calling the 'new' operator for small amounts of memory
00043   
00044   
00045   public:
00046   
00047   inline ~field();
00048   inline  field();
00049   
00050   inline                  field(const field& x);
00051   inline const field& operator=(const field& x);
00052   
00053   inline                  field(const subview_field<oT>& x);
00054   inline const field& operator=(const subview_field<oT>& x);
00055   
00056   inline explicit field(const u32 n_elem_in);
00057   inline          field(const u32 n_rows_in, const u32 n_cols_in);
00058   
00059   inline void  set_size(const u32 n_obj_in);
00060   inline void  set_size(const u32 n_rows_in, const u32 n_cols_in);
00061   
00062   template<typename oT2>
00063   inline void copy_size(const field<oT2>& x);
00064   
00065   arma_inline       oT& operator[](const u32 i);
00066   arma_inline const oT& operator[](const u32 i) const;
00067   
00068   arma_inline       oT& operator()(const u32 i);
00069   arma_inline const oT& operator()(const u32 i) const;
00070   
00071   arma_inline       oT&         at(const u32 row, const u32 col);
00072   arma_inline const oT&         at(const u32 row, const u32 col) const;
00073   
00074   arma_inline       oT& operator()(const u32 row, const u32 col);
00075   arma_inline const oT& operator()(const u32 row, const u32 col) const;
00076   
00077   inline       subview_field<oT> row(const u32 row_num);
00078   inline const subview_field<oT> row(const u32 row_num) const;
00079   
00080   inline       subview_field<oT> col(const u32 col_num);
00081   inline const subview_field<oT> col(const u32 col_num) const;
00082   
00083   inline       subview_field<oT> rows(const u32 in_row1, const u32 in_row2);
00084   inline const subview_field<oT> rows(const u32 in_row1, const u32 in_row2) const;
00085   
00086   inline       subview_field<oT> cols(const u32 in_col1, const u32 in_col2);
00087   inline const subview_field<oT> cols(const u32 in_col1, const u32 in_col2) const;
00088   
00089   inline       subview_field<oT> subfield(const u32 in_row1, const u32 in_col1, const u32 in_row2, const u32 in_col2);
00090   inline const subview_field<oT> subfield(const u32 in_row1, const u32 in_col1, const u32 in_row2, const u32 in_col2) const;
00091   
00092   inline void print(const std::string extra_text = "") const;
00093   inline void print(std::ostream& user_stream, const std::string extra_text = "") const;
00094   
00095   inline void fill(const oT& x);
00096   
00097   inline void reset();
00098   inline void reset_objects();
00099   
00100   inline void save(const std::string   name, const file_type type = arma_binary) const;
00101   inline void save(      std::ostream& os,   const file_type type = arma_binary) const;
00102   
00103   inline void load(const std::string   name, const file_type type = auto_detect);
00104   inline void load(      std::istream& is,   const file_type type = auto_detect);
00105   
00106   
00107   private:
00108   
00109   inline void init(const field<oT>& x);
00110   inline void init(const u32 n_rows_in, const u32 n_cols_in);
00111   
00112   inline void delete_objects();
00113   inline void create_objects();
00114   
00115   friend class field_aux;
00116   friend class subview_field<oT>;
00117   };
00118 
00119 
00120 
00121 class field_aux
00122   {
00123   public:
00124   
00125   template<typename oT> inline static void reset_objects(field< oT >& x);
00126   template<typename eT> inline static void reset_objects(field< Mat<eT> >& x);
00127   template<typename eT> inline static void reset_objects(field< Col<eT> >& x);
00128   template<typename eT> inline static void reset_objects(field< Row<eT> >& x);
00129   template<typename eT> inline static void reset_objects(field< Cube<eT> >& x);
00130                         inline static void reset_objects(field< std::string >& x);
00131   
00132   
00133   template<typename oT> inline static void save(const field< oT >& x,          const std::string& name, const file_type type);
00134   template<typename oT> inline static void save(const field< oT >& x,          std::ostream& os,        const file_type type);
00135   template<typename oT> inline static void load(      field< oT >& x,          const std::string& name, const file_type type);
00136   template<typename oT> inline static void load(      field< oT >& x,          std::istream& is,        const file_type type);
00137 
00138   template<typename eT> inline static void save(const field< Mat<eT> >& x,     const std::string& name, const file_type type);
00139   template<typename eT> inline static void save(const field< Mat<eT> >& x,     std::ostream& os,        const file_type type);
00140   template<typename eT> inline static void load(      field< Mat<eT> >& x,     const std::string& name, const file_type type);
00141   template<typename eT> inline static void load(      field< Mat<eT> >& x,     std::istream& is,        const file_type type);
00142   
00143   template<typename eT> inline static void save(const field< Col<eT> >& x,     const std::string& name, const file_type type);
00144   template<typename eT> inline static void save(const field< Col<eT> >& x,     std::ostream& os,        const file_type type);
00145   template<typename eT> inline static void load(      field< Col<eT> >& x,     const std::string& name, const file_type type);
00146   template<typename eT> inline static void load(      field< Col<eT> >& x,     std::istream& is,        const file_type type);
00147   
00148   template<typename eT> inline static void save(const field< Row<eT> >& x,     const std::string& name, const file_type type);
00149   template<typename eT> inline static void save(const field< Row<eT> >& x,     std::ostream& os,        const file_type type);
00150   template<typename eT> inline static void load(      field< Row<eT> >& x,     const std::string& name, const file_type type);
00151   template<typename eT> inline static void load(      field< Row<eT> >& x,     std::istream& is,        const file_type type);
00152 
00153   template<typename eT> inline static void save(const field< Cube<eT> >& x,    const std::string& name, const file_type type);
00154   template<typename eT> inline static void save(const field< Cube<eT> >& x,    std::ostream& os,        const file_type type);
00155   template<typename eT> inline static void load(      field< Cube<eT> >& x,    const std::string& name, const file_type type);
00156   template<typename eT> inline static void load(      field< Cube<eT> >& x,    std::istream& is,        const file_type type);
00157   
00158                         inline static void save(const field< std::string >& x, const std::string& name, const file_type type);
00159                         inline static void save(const field< std::string >& x, std::ostream& os,        const file_type type);
00160                         inline static void load(      field< std::string >& x, const std::string& name, const file_type type);
00161                         inline static void load(      field< std::string >& x, std::istream& is,        const file_type type);
00162   
00163   };
00164 
00165 
00166 //! @}