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   // iterators
00108   
00109   class iterator
00110     {
00111     public:
00112     
00113     inline iterator(field<oT>& in_M, const bool at_end = false);
00114     
00115     inline oT& operator* ();
00116     
00117     inline iterator& operator++();
00118     inline void      operator++(int);
00119     
00120     inline iterator& operator--();
00121     inline void      operator--(int);
00122     
00123     inline bool operator!=(const iterator& X) const;
00124     inline bool operator==(const iterator& X) const;
00125     
00126     arma_aligned field<oT>& M;
00127     arma_aligned u32        i;
00128     };
00129   
00130   
00131   class const_iterator
00132     {
00133     public:
00134     
00135     const_iterator(const field<oT>& in_M, const bool at_end = false);
00136     const_iterator(const iterator& X);
00137     
00138     inline const oT& operator*() const;
00139     
00140     inline const_iterator& operator++();
00141     inline void            operator++(int);
00142     
00143     inline const_iterator& operator--();
00144     inline void            operator--(int);
00145     
00146     inline bool operator!=(const const_iterator& X) const;
00147     inline bool operator==(const const_iterator& X) const;
00148     
00149     arma_aligned const field<oT>& M;
00150     arma_aligned       u32        i;
00151     };
00152   
00153   inline       iterator begin();
00154   inline const_iterator begin() const;
00155   
00156   inline       iterator end();
00157   inline const_iterator end()   const;
00158   
00159   
00160   private:
00161   
00162   inline void init(const field<oT>& x);
00163   inline void init(const u32 n_rows_in, const u32 n_cols_in);
00164   
00165   inline void delete_objects();
00166   inline void create_objects();
00167   
00168   friend class field_aux;
00169   friend class subview_field<oT>;
00170   };
00171 
00172 
00173 
00174 class field_aux
00175   {
00176   public:
00177   
00178   template<typename oT> inline static void reset_objects(field< oT >& x);
00179   template<typename eT> inline static void reset_objects(field< Mat<eT> >& x);
00180   template<typename eT> inline static void reset_objects(field< Col<eT> >& x);
00181   template<typename eT> inline static void reset_objects(field< Row<eT> >& x);
00182   template<typename eT> inline static void reset_objects(field< Cube<eT> >& x);
00183                         inline static void reset_objects(field< std::string >& x);
00184   
00185   
00186   template<typename oT> inline static void save(const field< oT >& x,          const std::string& name, const file_type type);
00187   template<typename oT> inline static void save(const field< oT >& x,          std::ostream& os,        const file_type type);
00188   template<typename oT> inline static void load(      field< oT >& x,          const std::string& name, const file_type type);
00189   template<typename oT> inline static void load(      field< oT >& x,          std::istream& is,        const file_type type);
00190 
00191   template<typename eT> inline static void save(const field< Mat<eT> >& x,     const std::string& name, const file_type type);
00192   template<typename eT> inline static void save(const field< Mat<eT> >& x,     std::ostream& os,        const file_type type);
00193   template<typename eT> inline static void load(      field< Mat<eT> >& x,     const std::string& name, const file_type type);
00194   template<typename eT> inline static void load(      field< Mat<eT> >& x,     std::istream& is,        const file_type type);
00195   
00196   template<typename eT> inline static void save(const field< Col<eT> >& x,     const std::string& name, const file_type type);
00197   template<typename eT> inline static void save(const field< Col<eT> >& x,     std::ostream& os,        const file_type type);
00198   template<typename eT> inline static void load(      field< Col<eT> >& x,     const std::string& name, const file_type type);
00199   template<typename eT> inline static void load(      field< Col<eT> >& x,     std::istream& is,        const file_type type);
00200   
00201   template<typename eT> inline static void save(const field< Row<eT> >& x,     const std::string& name, const file_type type);
00202   template<typename eT> inline static void save(const field< Row<eT> >& x,     std::ostream& os,        const file_type type);
00203   template<typename eT> inline static void load(      field< Row<eT> >& x,     const std::string& name, const file_type type);
00204   template<typename eT> inline static void load(      field< Row<eT> >& x,     std::istream& is,        const file_type type);
00205 
00206   template<typename eT> inline static void save(const field< Cube<eT> >& x,    const std::string& name, const file_type type);
00207   template<typename eT> inline static void save(const field< Cube<eT> >& x,    std::ostream& os,        const file_type type);
00208   template<typename eT> inline static void load(      field< Cube<eT> >& x,    const std::string& name, const file_type type);
00209   template<typename eT> inline static void load(      field< Cube<eT> >& x,    std::istream& is,        const file_type type);
00210   
00211                         inline static void save(const field< std::string >& x, const std::string& name, const file_type type);
00212                         inline static void save(const field< std::string >& x, std::ostream& os,        const file_type type);
00213                         inline static void load(      field< std::string >& x, const std::string& name, const file_type type);
00214                         inline static void load(      field< std::string >& x, std::istream& is,        const file_type type);
00215   
00216   };
00217 
00218 
00219 //! @}