Chris@49: // Copyright (C) 2008-2011 NICTA (www.nicta.com.au) Chris@49: // Copyright (C) 2008-2013 Conrad Sanderson Chris@49: // Copyright (C) 2009-2010 Ian Cullinan Chris@49: // Chris@49: // This Source Code Form is subject to the terms of the Mozilla Public Chris@49: // License, v. 2.0. If a copy of the MPL was not distributed with this Chris@49: // file, You can obtain one at http://mozilla.org/MPL/2.0/. Chris@49: Chris@49: Chris@49: //! \addtogroup field Chris@49: //! @{ Chris@49: Chris@49: Chris@49: Chris@49: struct field_prealloc_n_elem Chris@49: { Chris@49: static const uword val = 16; Chris@49: }; Chris@49: Chris@49: Chris@49: Chris@49: //! A lightweight 2D container for abitrary objects Chris@49: //! (the objects must have a copy constructor) Chris@49: Chris@49: template Chris@49: class field Chris@49: { Chris@49: public: Chris@49: Chris@49: typedef oT object_type; Chris@49: Chris@49: const uword n_rows; //!< number of rows in the field (read-only) Chris@49: const uword n_cols; //!< number of columns in the field (read-only) Chris@49: const uword n_elem; //!< number of elements in the field (read-only) Chris@49: Chris@49: Chris@49: private: Chris@49: Chris@49: arma_aligned oT** mem; //!< pointer to memory used by the object Chris@49: arma_aligned oT* mem_local[ field_prealloc_n_elem::val ]; //!< Internal memory, to avoid calling the 'new' operator for small amounts of memory Chris@49: Chris@49: Chris@49: public: Chris@49: Chris@49: inline ~field(); Chris@49: inline field(); Chris@49: Chris@49: inline field(const field& x); Chris@49: inline const field& operator=(const field& x); Chris@49: Chris@49: inline field(const subview_field& x); Chris@49: inline const field& operator=(const subview_field& x); Chris@49: Chris@49: inline explicit field(const uword n_elem_in); Chris@49: inline field(const uword n_rows_in, const uword n_cols_in); Chris@49: Chris@49: inline void set_size(const uword n_obj_in); Chris@49: inline void set_size(const uword n_rows_in, const uword n_cols_in); Chris@49: Chris@49: template Chris@49: inline void copy_size(const field& x); Chris@49: Chris@49: arma_inline oT& operator[](const uword i); Chris@49: arma_inline const oT& operator[](const uword i) const; Chris@49: Chris@49: arma_inline oT& at(const uword i); Chris@49: arma_inline const oT& at(const uword i) const; Chris@49: Chris@49: arma_inline oT& operator()(const uword i); Chris@49: arma_inline const oT& operator()(const uword i) const; Chris@49: Chris@49: arma_inline oT& at(const uword row, const uword col); Chris@49: arma_inline const oT& at(const uword row, const uword col) const; Chris@49: Chris@49: arma_inline oT& operator()(const uword row, const uword col); Chris@49: arma_inline const oT& operator()(const uword row, const uword col) const; Chris@49: Chris@49: inline field_injector operator<<(const oT& val); Chris@49: inline field_injector operator<<(const injector_end_of_row<>& x); Chris@49: Chris@49: Chris@49: inline subview_field row(const uword row_num); Chris@49: inline const subview_field row(const uword row_num) const; Chris@49: Chris@49: inline subview_field col(const uword col_num); Chris@49: inline const subview_field col(const uword col_num) const; Chris@49: Chris@49: inline subview_field rows(const uword in_row1, const uword in_row2); Chris@49: inline const subview_field rows(const uword in_row1, const uword in_row2) const; Chris@49: Chris@49: inline subview_field cols(const uword in_col1, const uword in_col2); Chris@49: inline const subview_field cols(const uword in_col1, const uword in_col2) const; Chris@49: Chris@49: inline subview_field subfield(const uword in_row1, const uword in_col1, const uword in_row2, const uword in_col2); Chris@49: inline const subview_field subfield(const uword in_row1, const uword in_col1, const uword in_row2, const uword in_col2) const; Chris@49: Chris@49: inline subview_field subfield (const span& row_span, const span& col_span); Chris@49: inline const subview_field subfield (const span& row_span, const span& col_span) const; Chris@49: Chris@49: inline subview_field operator()(const span& row_span, const span& col_span); Chris@49: inline const subview_field operator()(const span& row_span, const span& col_span) const; Chris@49: Chris@49: Chris@49: inline void print(const std::string extra_text = "") const; Chris@49: inline void print(std::ostream& user_stream, const std::string extra_text = "") const; Chris@49: Chris@49: inline void fill(const oT& x); Chris@49: Chris@49: inline void reset(); Chris@49: inline void reset_objects(); Chris@49: Chris@49: arma_inline bool is_empty() const; Chris@49: Chris@49: arma_inline arma_warn_unused bool in_range(const uword i) const; Chris@49: arma_inline arma_warn_unused bool in_range(const span& x) const; Chris@49: Chris@49: arma_inline arma_warn_unused bool in_range(const uword in_row, const uword in_col) const; Chris@49: arma_inline arma_warn_unused bool in_range(const span& row_span, const uword in_col) const; Chris@49: arma_inline arma_warn_unused bool in_range(const uword in_row, const span& col_span) const; Chris@49: arma_inline arma_warn_unused bool in_range(const span& row_span, const span& col_span) const; Chris@49: Chris@49: inline bool save(const std::string name, const file_type type = arma_binary, const bool print_status = true) const; Chris@49: inline bool save( std::ostream& os, const file_type type = arma_binary, const bool print_status = true) const; Chris@49: Chris@49: inline bool load(const std::string name, const file_type type = auto_detect, const bool print_status = true); Chris@49: inline bool load( std::istream& is, const file_type type = auto_detect, const bool print_status = true); Chris@49: Chris@49: Chris@49: inline bool quiet_save(const std::string name, const file_type type = arma_binary) const; Chris@49: inline bool quiet_save( std::ostream& os, const file_type type = arma_binary) const; Chris@49: Chris@49: inline bool quiet_load(const std::string name, const file_type type = auto_detect); Chris@49: inline bool quiet_load( std::istream& is, const file_type type = auto_detect); Chris@49: Chris@49: Chris@49: // for container-like functionality Chris@49: Chris@49: typedef oT value_type; Chris@49: typedef uword size_type; Chris@49: Chris@49: Chris@49: class iterator Chris@49: { Chris@49: public: Chris@49: Chris@49: inline iterator(field& in_M, const bool at_end = false); Chris@49: Chris@49: inline oT& operator* (); Chris@49: Chris@49: inline iterator& operator++(); Chris@49: inline void operator++(int); Chris@49: Chris@49: inline iterator& operator--(); Chris@49: inline void operator--(int); Chris@49: Chris@49: inline bool operator!=(const iterator& X) const; Chris@49: inline bool operator==(const iterator& X) const; Chris@49: Chris@49: arma_aligned field& M; Chris@49: arma_aligned uword i; Chris@49: }; Chris@49: Chris@49: Chris@49: class const_iterator Chris@49: { Chris@49: public: Chris@49: Chris@49: const_iterator(const field& in_M, const bool at_end = false); Chris@49: const_iterator(const iterator& X); Chris@49: Chris@49: inline const oT& operator*() const; Chris@49: Chris@49: inline const_iterator& operator++(); Chris@49: inline void operator++(int); Chris@49: Chris@49: inline const_iterator& operator--(); Chris@49: inline void operator--(int); Chris@49: Chris@49: inline bool operator!=(const const_iterator& X) const; Chris@49: inline bool operator==(const const_iterator& X) const; Chris@49: Chris@49: arma_aligned const field& M; Chris@49: arma_aligned uword i; Chris@49: }; Chris@49: Chris@49: inline iterator begin(); Chris@49: inline const_iterator begin() const; Chris@49: inline const_iterator cbegin() const; Chris@49: Chris@49: inline iterator end(); Chris@49: inline const_iterator end() const; Chris@49: inline const_iterator cend() const; Chris@49: Chris@49: inline void clear(); Chris@49: inline bool empty() const; Chris@49: inline uword size() const; Chris@49: Chris@49: Chris@49: private: Chris@49: Chris@49: inline void init(const field& x); Chris@49: inline void init(const uword n_rows_in, const uword n_cols_in); Chris@49: Chris@49: inline void delete_objects(); Chris@49: inline void create_objects(); Chris@49: Chris@49: friend class field_aux; Chris@49: friend class subview_field; Chris@49: Chris@49: Chris@49: public: Chris@49: Chris@49: #ifdef ARMA_EXTRA_FIELD_PROTO Chris@49: #include ARMA_INCFILE_WRAP(ARMA_EXTRA_FIELD_PROTO) Chris@49: #endif Chris@49: }; Chris@49: Chris@49: Chris@49: Chris@49: class field_aux Chris@49: { Chris@49: public: Chris@49: Chris@49: template inline static void reset_objects(field< oT >& x); Chris@49: template inline static void reset_objects(field< Mat >& x); Chris@49: template inline static void reset_objects(field< Col >& x); Chris@49: template inline static void reset_objects(field< Row >& x); Chris@49: template inline static void reset_objects(field< Cube >& x); Chris@49: inline static void reset_objects(field< std::string >& x); Chris@49: Chris@49: Chris@49: template inline static bool save(const field< oT >& x, const std::string& name, const file_type type, std::string& err_msg); Chris@49: template inline static bool save(const field< oT >& x, std::ostream& os, const file_type type, std::string& err_msg); Chris@49: template inline static bool load( field< oT >& x, const std::string& name, const file_type type, std::string& err_msg); Chris@49: template inline static bool load( field< oT >& x, std::istream& is, const file_type type, std::string& err_msg); Chris@49: Chris@49: template inline static bool save(const field< Mat >& x, const std::string& name, const file_type type, std::string& err_msg); Chris@49: template inline static bool save(const field< Mat >& x, std::ostream& os, const file_type type, std::string& err_msg); Chris@49: template inline static bool load( field< Mat >& x, const std::string& name, const file_type type, std::string& err_msg); Chris@49: template inline static bool load( field< Mat >& x, std::istream& is, const file_type type, std::string& err_msg); Chris@49: Chris@49: template inline static bool save(const field< Col >& x, const std::string& name, const file_type type, std::string& err_msg); Chris@49: template inline static bool save(const field< Col >& x, std::ostream& os, const file_type type, std::string& err_msg); Chris@49: template inline static bool load( field< Col >& x, const std::string& name, const file_type type, std::string& err_msg); Chris@49: template inline static bool load( field< Col >& x, std::istream& is, const file_type type, std::string& err_msg); Chris@49: Chris@49: template inline static bool save(const field< Row >& x, const std::string& name, const file_type type, std::string& err_msg); Chris@49: template inline static bool save(const field< Row >& x, std::ostream& os, const file_type type, std::string& err_msg); Chris@49: template inline static bool load( field< Row >& x, const std::string& name, const file_type type, std::string& err_msg); Chris@49: template inline static bool load( field< Row >& x, std::istream& is, const file_type type, std::string& err_msg); Chris@49: Chris@49: template inline static bool save(const field< Cube >& x, const std::string& name, const file_type type, std::string& err_msg); Chris@49: template inline static bool save(const field< Cube >& x, std::ostream& os, const file_type type, std::string& err_msg); Chris@49: template inline static bool load( field< Cube >& x, const std::string& name, const file_type type, std::string& err_msg); Chris@49: template inline static bool load( field< Cube >& x, std::istream& is, const file_type type, std::string& err_msg); Chris@49: Chris@49: inline static bool save(const field< std::string >& x, const std::string& name, const file_type type, std::string& err_msg); Chris@49: inline static bool save(const field< std::string >& x, std::ostream& os, const file_type type, std::string& err_msg); Chris@49: inline static bool load( field< std::string >& x, const std::string& name, const file_type type, std::string& err_msg); Chris@49: inline static bool load( field< std::string >& x, std::istream& is, const file_type type, std::string& err_msg); Chris@49: Chris@49: }; Chris@49: Chris@49: Chris@49: //! @}