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