max@0: // Copyright (C) 2008-2011 NICTA (www.nicta.com.au) max@0: // Copyright (C) 2008-2011 Conrad Sanderson 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 Cube max@0: //! @{ max@0: max@0: max@0: max@0: struct Cube_prealloc max@0: { max@0: static const uword mat_ptrs_size = 4; max@0: static const uword mem_n_elem = 64; max@0: }; max@0: max@0: max@0: max@0: //! Dense cube class max@0: max@0: template max@0: class Cube : public BaseCube< eT, Cube > max@0: { max@0: public: max@0: max@0: typedef eT elem_type; //!< the type of elements stored in the cube max@0: typedef typename get_pod_type::result pod_type; //!< if eT is non-complex, pod_type is same as eT. otherwise, pod_type is the underlying type used by std::complex max@0: max@0: const uword n_rows; //!< number of rows in each slice (read-only) max@0: const uword n_cols; //!< number of columns in each slice (read-only) max@0: const uword n_elem_slice; //!< number of elements in each slice (read-only) max@0: const uword n_slices; //!< number of slices in the cube (read-only) max@0: const uword n_elem; //!< number of elements in the cube (read-only) max@0: const uword mem_state; max@0: max@0: // mem_state = 0: normal cube that can be resized; max@0: // mem_state = 1: use auxiliary memory until change in the number of elements is requested; max@0: // mem_state = 2: use auxiliary memory and don't allow the number of elements to be changed; max@0: // mem_state = 3: fixed size (e.g. via template based size specification). max@0: max@0: max@0: arma_aligned const Mat** const mat_ptrs; //!< pointer to an array containing pointers to Mat instances (one for each slice) max@0: arma_aligned const eT* const mem; //!< pointer to the memory used by the cube (memory is read-only) max@0: max@0: protected: max@0: arma_aligned Mat* mat_ptrs_local[ Cube_prealloc::mat_ptrs_size ]; max@0: arma_aligned eT mem_local[ Cube_prealloc::mem_n_elem ]; max@0: max@0: max@0: public: max@0: max@0: inline ~Cube(); max@0: inline Cube(); max@0: max@0: inline Cube(const uword in_rows, const uword in_cols, const uword in_slices); max@0: max@0: inline Cube( eT* aux_mem, const uword aux_n_rows, const uword aux_n_cols, const uword aux_n_slices, const bool copy_aux_mem = true, const bool strict = true); max@0: inline Cube(const eT* aux_mem, const uword aux_n_rows, const uword aux_n_cols, const uword aux_n_slices); max@0: max@0: arma_inline const Cube& operator=(const eT val); max@0: arma_inline const Cube& operator+=(const eT val); max@0: arma_inline const Cube& operator-=(const eT val); max@0: arma_inline const Cube& operator*=(const eT val); max@0: arma_inline const Cube& operator/=(const eT val); max@0: max@0: inline Cube(const Cube& m); max@0: inline const Cube& operator=(const Cube& m); max@0: inline const Cube& operator+=(const Cube& m); max@0: inline const Cube& operator-=(const Cube& m); max@0: inline const Cube& operator%=(const Cube& m); max@0: inline const Cube& operator/=(const Cube& m); max@0: max@0: template max@0: inline explicit Cube(const BaseCube& A, const BaseCube& B); max@0: max@0: inline Cube(const subview_cube& X); max@0: inline const Cube& operator=(const subview_cube& X); max@0: inline const Cube& operator+=(const subview_cube& X); max@0: inline const Cube& operator-=(const subview_cube& X); max@0: inline const Cube& operator%=(const subview_cube& X); max@0: inline const Cube& operator/=(const subview_cube& X); max@0: max@0: arma_inline Mat& slice(const uword in_slice); max@0: arma_inline const Mat& slice(const uword in_slice) const; max@0: max@0: arma_inline subview_cube slices(const uword in_slice1, const uword in_slice2); max@0: arma_inline const subview_cube slices(const uword in_slice1, const uword in_slice2) const; max@0: max@0: arma_inline subview_cube subcube(const uword in_row1, const uword in_col1, const uword in_slice1, const uword in_row2, const uword in_col2, const uword in_slice2); max@0: arma_inline const subview_cube subcube(const uword in_row1, const uword in_col1, const uword in_slice1, const uword in_row2, const uword in_col2, const uword in_slice2) const; max@0: max@0: inline subview_cube subcube(const span& row_span, const span& col_span, const span& slice_span); max@0: inline const subview_cube subcube(const span& row_span, const span& col_span, const span& slice_span) const; max@0: max@0: inline subview_cube operator()(const span& row_span, const span& col_span, const span& slice_span); max@0: inline const subview_cube operator()(const span& row_span, const span& col_span, const span& slice_span) const; max@0: max@0: max@0: inline void shed_slice(const uword slice_num); max@0: max@0: inline void shed_slices(const uword in_slice1, const uword in_slice2); max@0: max@0: inline void insert_slices(const uword slice_num, const uword N, const bool set_to_zero = true); max@0: max@0: template max@0: inline void insert_slices(const uword row_num, const BaseCube& X); max@0: max@0: max@0: template inline Cube(const GenCube& X); max@0: template inline const Cube& operator=(const GenCube& X); max@0: template inline const Cube& operator+=(const GenCube& X); max@0: template inline const Cube& operator-=(const GenCube& X); max@0: template inline const Cube& operator%=(const GenCube& X); max@0: template inline const Cube& operator/=(const GenCube& X); max@0: max@0: template inline Cube(const OpCube& X); max@0: template inline const Cube& operator=(const OpCube& X); max@0: template inline const Cube& operator+=(const OpCube& X); max@0: template inline const Cube& operator-=(const OpCube& X); max@0: template inline const Cube& operator%=(const OpCube& X); max@0: template inline const Cube& operator/=(const OpCube& X); max@0: max@0: template inline Cube(const eOpCube& X); max@0: template inline const Cube& operator=(const eOpCube& X); max@0: template inline const Cube& operator+=(const eOpCube& X); max@0: template inline const Cube& operator-=(const eOpCube& X); max@0: template inline const Cube& operator%=(const eOpCube& X); max@0: template inline const Cube& operator/=(const eOpCube& X); max@0: max@0: template inline Cube(const mtOpCube& X); max@0: template inline const Cube& operator=(const mtOpCube& X); max@0: template inline const Cube& operator+=(const mtOpCube& X); max@0: template inline const Cube& operator-=(const mtOpCube& X); max@0: template inline const Cube& operator%=(const mtOpCube& X); max@0: template inline const Cube& operator/=(const mtOpCube& X); max@0: max@0: template inline Cube(const GlueCube& X); max@0: template inline const Cube& operator=(const GlueCube& X); max@0: template inline const Cube& operator+=(const GlueCube& X); max@0: template inline const Cube& operator-=(const GlueCube& X); max@0: template inline const Cube& operator%=(const GlueCube& X); max@0: template inline const Cube& operator/=(const GlueCube& X); max@0: max@0: template inline Cube(const eGlueCube& X); max@0: template inline const Cube& operator=(const eGlueCube& X); max@0: template inline const Cube& operator+=(const eGlueCube& X); max@0: template inline const Cube& operator-=(const eGlueCube& X); max@0: template inline const Cube& operator%=(const eGlueCube& X); max@0: template inline const Cube& operator/=(const eGlueCube& X); max@0: max@0: template inline Cube(const mtGlueCube& X); max@0: template inline const Cube& operator=(const mtGlueCube& X); max@0: template inline const Cube& operator+=(const mtGlueCube& X); max@0: template inline const Cube& operator-=(const mtGlueCube& X); max@0: template inline const Cube& operator%=(const mtGlueCube& X); max@0: template inline const Cube& operator/=(const mtGlueCube& X); max@0: max@0: max@0: arma_inline arma_warn_unused eT& operator[] (const uword i); max@0: arma_inline arma_warn_unused eT operator[] (const uword i) const; max@0: max@0: arma_inline arma_warn_unused eT& at(const uword i); max@0: arma_inline arma_warn_unused eT at(const uword i) const; max@0: max@0: arma_inline arma_warn_unused eT& operator() (const uword i); max@0: arma_inline arma_warn_unused eT operator() (const uword i) const; max@0: max@0: arma_inline arma_warn_unused eT& at (const uword in_row, const uword in_col, const uword in_slice); max@0: arma_inline arma_warn_unused eT at (const uword in_row, const uword in_col, const uword in_slice) const; max@0: max@0: arma_inline arma_warn_unused eT& operator() (const uword in_row, const uword in_col, const uword in_slice); max@0: arma_inline arma_warn_unused eT operator() (const uword in_row, const uword in_col, const uword in_slice) const; max@0: max@0: arma_inline const Cube& operator++(); max@0: arma_inline void operator++(int); max@0: max@0: arma_inline const Cube& operator--(); max@0: arma_inline void operator--(int); max@0: max@0: arma_inline arma_warn_unused bool is_finite() const; max@0: arma_inline arma_warn_unused 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 uword in_slice) const; max@0: inline arma_warn_unused bool in_range(const span& row_span, const span& col_span, const span& slice_span) const; max@0: max@0: arma_inline arma_warn_unused eT* memptr(); max@0: arma_inline arma_warn_unused const eT* memptr() const; max@0: max@0: arma_inline arma_warn_unused eT* slice_memptr(const uword slice); max@0: arma_inline arma_warn_unused const eT* slice_memptr(const uword slice) const; max@0: max@0: arma_inline arma_warn_unused eT* slice_colptr(const uword in_slice, const uword in_col); max@0: arma_inline arma_warn_unused const eT* slice_colptr(const uword in_slice, const uword in_col) const; max@0: max@0: inline void impl_print(const std::string& extra_text) const; max@0: inline void impl_print(std::ostream& user_stream, const std::string& extra_text) const; max@0: max@0: inline void impl_raw_print(const std::string& extra_text) const; max@0: inline void impl_raw_print(std::ostream& user_stream, const std::string& extra_text) const; max@0: max@0: inline void set_size(const uword in_rows, const uword in_cols, const uword in_slices); max@0: inline void reshape(const uword in_rows, const uword in_cols, const uword in_slices, const uword dim = 0); max@0: inline void resize(const uword in_rows, const uword in_cols, const uword in_slices); max@0: max@0: template inline void copy_size(const Cube& m); max@0: max@0: inline const Cube& fill(const eT val); max@0: max@0: inline const Cube& zeros(); max@0: inline const Cube& zeros(const uword in_rows, const uword in_cols, const uword in_slices); max@0: max@0: inline const Cube& ones(); max@0: inline const Cube& ones(const uword in_rows, const uword in_cols, const uword in_slices); max@0: max@0: inline const Cube& randu(); max@0: inline const Cube& randu(const uword in_rows, const uword in_cols, const uword in_slices); max@0: max@0: inline const Cube& randn(); max@0: inline const Cube& randn(const uword in_rows, const uword in_cols, const uword in_slices); max@0: max@0: inline void reset(); max@0: max@0: max@0: template inline void set_real(const BaseCube& X); max@0: template inline void set_imag(const BaseCube& X); max@0: max@0: max@0: inline arma_warn_unused eT min() const; max@0: inline arma_warn_unused eT max() const; max@0: max@0: inline eT min(uword& index_of_min_val) const; max@0: inline eT max(uword& index_of_max_val) const; max@0: max@0: inline eT min(uword& row_of_min_val, uword& col_of_min_val, uword& slice_of_min_val) const; max@0: inline eT max(uword& row_of_max_val, uword& col_of_max_val, uword& slice_of_max_val) const; max@0: 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: 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: typedef eT* iterator; max@0: typedef const eT* const_iterator; max@0: max@0: typedef eT* slice_iterator; max@0: typedef const eT* const_slice_iterator; 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: inline slice_iterator begin_slice(const uword slice_num); max@0: inline const_slice_iterator begin_slice(const uword slice_num) const; max@0: max@0: inline slice_iterator end_slice(const uword slice_num); max@0: inline const_slice_iterator end_slice(const uword slice_num) const; max@0: max@0: max@0: template max@0: class fixed : public Cube max@0: { max@0: private: max@0: max@0: static const uword fixed_n_elem = fixed_n_rows * fixed_n_cols * fixed_n_slices; max@0: max@0: arma_aligned Mat* mat_ptrs_local_extra[ (fixed_n_slices > Cube_prealloc::mat_ptrs_size) ? fixed_n_slices : 1 ]; max@0: arma_aligned eT mem_local_extra [ (fixed_n_elem > Cube_prealloc::mem_n_elem) ? fixed_n_elem : 1 ]; max@0: max@0: arma_inline void mem_setup(); max@0: max@0: max@0: public: max@0: max@0: inline fixed() { mem_setup(); } max@0: max@0: inline const Cube& operator=(const eT val) { mem_setup(); Cube::operator=(val); return *this; } max@0: max@0: template max@0: inline fixed(const BaseCube& A) { mem_setup(); Cube::operator=(A.get_ref()); } max@0: max@0: template max@0: inline const Cube& operator=(const BaseCube& A) { Cube::operator=(A.get_ref()); return *this; } max@0: max@0: template max@0: inline explicit fixed(const BaseCube& A, const BaseCube& B) { mem_setup(); Cube::init(A,B); } max@0: }; max@0: max@0: max@0: protected: max@0: max@0: inline void init_cold(); max@0: inline void init_warm(const uword in_rows, const uword in_cols, const uword in_slices); max@0: max@0: template max@0: inline void init(const BaseCube& A, const BaseCube& B); max@0: max@0: inline void steal_mem(Cube& X); max@0: max@0: inline void delete_mat(); max@0: inline void create_mat(); max@0: max@0: friend class glue_join; max@0: friend class op_reshape; max@0: friend class op_resize; max@0: max@0: max@0: public: max@0: max@0: #ifdef ARMA_EXTRA_CUBE_PROTO max@0: #include ARMA_INCFILE_WRAP(ARMA_EXTRA_CUBE_PROTO) max@0: #endif max@0: }; max@0: max@0: max@0: max@0: class Cube_aux max@0: { max@0: public: max@0: max@0: template arma_inline static void prefix_pp(Cube& x); max@0: template arma_inline static void prefix_pp(Cube< std::complex >& x); max@0: max@0: template arma_inline static void postfix_pp(Cube& x); max@0: template arma_inline static void postfix_pp(Cube< std::complex >& x); max@0: max@0: template arma_inline static void prefix_mm(Cube& x); max@0: template arma_inline static void prefix_mm(Cube< std::complex >& x); max@0: max@0: template arma_inline static void postfix_mm(Cube& x); max@0: template arma_inline static void postfix_mm(Cube< std::complex >& x); max@0: max@0: template inline static void set_real(Cube& out, const BaseCube& X); max@0: template inline static void set_imag(Cube& out, const BaseCube& X); max@0: max@0: template inline static void set_real(Cube< std::complex >& out, const BaseCube< T,T1>& X); max@0: template inline static void set_imag(Cube< std::complex >& out, const BaseCube< T,T1>& X); max@0: }; max@0: max@0: max@0: max@0: //! @}