max@0: // Copyright (C) 2008-2011 NICTA (www.nicta.com.au) max@0: // Copyright (C) 2008-2011 Conrad Sanderson max@0: // Copyright (C) 2009-2010 Ian Cullinan max@0: // max@0: // This file is part of the Armadillo C++ library. max@0: // It is provided without any warranty of fitness max@0: // for any purpose. You can redistribute this file max@0: // and/or modify it under the terms of the GNU max@0: // Lesser General Public License (LGPL) as published max@0: // by the Free Software Foundation, either version 3 max@0: // of the License or (at your option) any later version. max@0: // (see http://www.opensource.org/licenses for more info) max@0: max@0: max@0: //! \addtogroup field max@0: //! @{ max@0: max@0: max@0: max@0: struct field_prealloc_n_elem max@0: { max@0: static const uword val = 16; max@0: }; max@0: max@0: max@0: max@0: //! A lightweight 2D container for abitrary objects max@0: //! (the objects must have a copy constructor) max@0: max@0: template max@0: class field max@0: { max@0: public: max@0: max@0: typedef oT object_type; max@0: max@0: const uword n_rows; //!< number of rows in the field (read-only) max@0: const uword n_cols; //!< number of columns in the field (read-only) max@0: const uword n_elem; //!< number of elements in the field (read-only) max@0: max@0: max@0: private: max@0: max@0: arma_aligned oT** mem; //!< pointer to memory used by the object max@0: arma_aligned oT* mem_local[ field_prealloc_n_elem::val ]; max@0: //!< Internal memory, to avoid calling the 'new' operator for small amounts of memory max@0: max@0: max@0: public: max@0: max@0: inline ~field(); max@0: inline field(); max@0: max@0: inline field(const field& x); max@0: inline const field& operator=(const field& x); max@0: max@0: inline field(const subview_field& x); max@0: inline const field& operator=(const subview_field& x); max@0: max@0: inline explicit field(const uword n_elem_in); max@0: inline field(const uword n_rows_in, const uword n_cols_in); max@0: max@0: inline void set_size(const uword n_obj_in); max@0: inline void set_size(const uword n_rows_in, const uword n_cols_in); max@0: max@0: template max@0: inline void copy_size(const field& x); max@0: max@0: arma_inline oT& operator[](const uword i); max@0: arma_inline const oT& operator[](const uword i) const; max@0: max@0: arma_inline oT& at(const uword i); max@0: arma_inline const oT& at(const uword i) const; max@0: max@0: arma_inline oT& operator()(const uword i); max@0: arma_inline const oT& operator()(const uword i) const; max@0: max@0: arma_inline oT& at(const uword row, const uword col); max@0: arma_inline const oT& at(const uword row, const uword col) const; max@0: max@0: arma_inline oT& operator()(const uword row, const uword col); max@0: arma_inline const oT& operator()(const uword row, const uword col) const; max@0: max@0: inline field_injector operator<<(const oT& val); max@0: inline field_injector operator<<(const injector_end_of_row& x); max@0: max@0: max@0: inline subview_field row(const uword row_num); max@0: inline const subview_field row(const uword row_num) const; max@0: max@0: inline subview_field col(const uword col_num); max@0: inline const subview_field col(const uword col_num) const; max@0: max@0: inline subview_field rows(const uword in_row1, const uword in_row2); max@0: inline const subview_field rows(const uword in_row1, const uword in_row2) const; max@0: max@0: inline subview_field cols(const uword in_col1, const uword in_col2); max@0: inline const subview_field cols(const uword in_col1, const uword in_col2) const; max@0: max@0: inline subview_field subfield(const uword in_row1, const uword in_col1, const uword in_row2, const uword in_col2); max@0: inline const subview_field subfield(const uword in_row1, const uword in_col1, const uword in_row2, const uword in_col2) const; max@0: max@0: inline subview_field subfield (const span& row_span, const span& col_span); max@0: inline const subview_field subfield (const span& row_span, const span& col_span) const; max@0: max@0: inline subview_field operator()(const span& row_span, const span& col_span); max@0: inline const subview_field operator()(const span& row_span, const span& col_span) const; max@0: max@0: max@0: inline void print(const std::string extra_text = "") const; max@0: inline void print(std::ostream& user_stream, const std::string extra_text = "") const; max@0: max@0: inline void fill(const oT& x); max@0: max@0: inline void reset(); max@0: inline void reset_objects(); max@0: max@0: arma_inline bool is_empty() const; max@0: max@0: arma_inline arma_warn_unused bool in_range(const uword i) const; max@0: arma_inline arma_warn_unused bool in_range(const span& x) const; max@0: max@0: arma_inline arma_warn_unused bool in_range(const uword in_row, const uword in_col ) const; max@0: arma_inline arma_warn_unused bool in_range(const span& row_span, const uword in_col ) const; max@0: arma_inline arma_warn_unused bool in_range(const uword in_row, const span& col_span) const; max@0: arma_inline arma_warn_unused bool in_range(const span& row_span, const span& col_span) const; max@0: max@0: inline bool save(const std::string name, const file_type type = arma_binary, const bool print_status = true) const; max@0: inline bool save( std::ostream& os, const file_type type = arma_binary, const bool print_status = true) const; max@0: max@0: inline bool load(const std::string name, const file_type type = auto_detect, const bool print_status = true); max@0: inline bool load( std::istream& is, const file_type type = auto_detect, const bool print_status = true); max@0: max@0: max@0: inline bool quiet_save(const std::string name, const file_type type = arma_binary) const; max@0: inline bool quiet_save( std::ostream& os, const file_type type = arma_binary) const; max@0: max@0: inline bool quiet_load(const std::string name, const file_type type = auto_detect); max@0: inline bool quiet_load( std::istream& is, const file_type type = auto_detect); max@0: max@0: max@0: // iterators max@0: max@0: class iterator max@0: { max@0: public: max@0: max@0: inline iterator(field& in_M, const bool at_end = false); max@0: max@0: inline oT& operator* (); max@0: max@0: inline iterator& operator++(); max@0: inline void operator++(int); max@0: max@0: inline iterator& operator--(); max@0: inline void operator--(int); max@0: max@0: inline bool operator!=(const iterator& X) const; max@0: inline bool operator==(const iterator& X) const; max@0: max@0: arma_aligned field& M; max@0: arma_aligned uword i; max@0: }; max@0: max@0: max@0: class const_iterator max@0: { max@0: public: max@0: max@0: const_iterator(const field& in_M, const bool at_end = false); max@0: const_iterator(const iterator& X); max@0: max@0: inline const oT& operator*() const; max@0: max@0: inline const_iterator& operator++(); max@0: inline void operator++(int); max@0: max@0: inline const_iterator& operator--(); max@0: inline void operator--(int); max@0: max@0: inline bool operator!=(const const_iterator& X) const; max@0: inline bool operator==(const const_iterator& X) const; max@0: max@0: arma_aligned const field& M; max@0: arma_aligned uword i; max@0: }; max@0: max@0: inline iterator begin(); max@0: inline const_iterator begin() const; max@0: max@0: inline iterator end(); max@0: inline const_iterator end() const; max@0: max@0: max@0: private: max@0: max@0: inline void init(const field& x); max@0: inline void init(const uword n_rows_in, const uword n_cols_in); max@0: max@0: inline void delete_objects(); max@0: inline void create_objects(); max@0: max@0: friend class field_aux; max@0: friend class subview_field; max@0: max@0: max@0: public: max@0: max@0: #ifdef ARMA_EXTRA_FIELD_PROTO max@0: #include ARMA_INCFILE_WRAP(ARMA_EXTRA_FIELD_PROTO) max@0: #endif max@0: }; max@0: max@0: max@0: max@0: class field_aux max@0: { max@0: public: max@0: max@0: template inline static void reset_objects(field< oT >& x); max@0: template inline static void reset_objects(field< Mat >& x); max@0: template inline static void reset_objects(field< Col >& x); max@0: template inline static void reset_objects(field< Row >& x); max@0: template inline static void reset_objects(field< Cube >& x); max@0: inline static void reset_objects(field< std::string >& x); max@0: max@0: max@0: template inline static bool save(const field< oT >& x, const std::string& name, const file_type type, std::string& err_msg); max@0: template inline static bool save(const field< oT >& x, std::ostream& os, const file_type type, std::string& err_msg); max@0: template inline static bool load( field< oT >& x, const std::string& name, const file_type type, std::string& err_msg); max@0: template inline static bool load( field< oT >& x, std::istream& is, const file_type type, std::string& err_msg); max@0: max@0: template inline static bool save(const field< Mat >& x, const std::string& name, const file_type type, std::string& err_msg); max@0: template inline static bool save(const field< Mat >& x, std::ostream& os, const file_type type, std::string& err_msg); max@0: template inline static bool load( field< Mat >& x, const std::string& name, const file_type type, std::string& err_msg); max@0: template inline static bool load( field< Mat >& x, std::istream& is, const file_type type, std::string& err_msg); max@0: max@0: template inline static bool save(const field< Col >& x, const std::string& name, const file_type type, std::string& err_msg); max@0: template inline static bool save(const field< Col >& x, std::ostream& os, const file_type type, std::string& err_msg); max@0: template inline static bool load( field< Col >& x, const std::string& name, const file_type type, std::string& err_msg); max@0: template inline static bool load( field< Col >& x, std::istream& is, const file_type type, std::string& err_msg); max@0: max@0: template inline static bool save(const field< Row >& x, const std::string& name, const file_type type, std::string& err_msg); max@0: template inline static bool save(const field< Row >& x, std::ostream& os, const file_type type, std::string& err_msg); max@0: template inline static bool load( field< Row >& x, const std::string& name, const file_type type, std::string& err_msg); max@0: template inline static bool load( field< Row >& x, std::istream& is, const file_type type, std::string& err_msg); max@0: max@0: template inline static bool save(const field< Cube >& x, const std::string& name, const file_type type, std::string& err_msg); max@0: template inline static bool save(const field< Cube >& x, std::ostream& os, const file_type type, std::string& err_msg); max@0: template inline static bool load( field< Cube >& x, const std::string& name, const file_type type, std::string& err_msg); max@0: template inline static bool load( field< Cube >& x, std::istream& is, const file_type type, std::string& err_msg); max@0: max@0: inline static bool save(const field< std::string >& x, const std::string& name, const file_type type, std::string& err_msg); max@0: inline static bool save(const field< std::string >& x, std::ostream& os, const file_type type, std::string& err_msg); max@0: inline static bool load( field< std::string >& x, const std::string& name, const file_type type, std::string& err_msg); max@0: inline static bool load( field< std::string >& x, std::istream& is, const file_type type, std::string& err_msg); max@0: max@0: }; max@0: max@0: max@0: //! @}