// Copyright (C) 2008-2015 National ICT Australia (NICTA) // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. // ------------------------------------------------------------------- // // Written by Conrad Sanderson - http://conradsanderson.id.au //! \addtogroup Cube //! @{ struct Cube_prealloc { static const uword mat_ptrs_size = 4; static const uword mem_n_elem = 64; }; //! Dense cube class template class Cube : public BaseCube< eT, Cube > { public: typedef eT elem_type; //!< the type of elements stored in the cube typedef typename get_pod_type::result pod_type; //!< if eT is std::complex, pod_type is T; otherwise pod_type is eT const uword n_rows; //!< number of rows in each slice (read-only) const uword n_cols; //!< number of columns in each slice (read-only) const uword n_elem_slice; //!< number of elements in each slice (read-only) const uword n_slices; //!< number of slices in the cube (read-only) const uword n_elem; //!< number of elements in the cube (read-only) const uword mem_state; // mem_state = 0: normal cube which manages its own memory // mem_state = 1: use auxiliary memory until a size change // mem_state = 2: use auxiliary memory and don't allow the number of elements to be changed // mem_state = 3: fixed size (eg. via template based size specification) arma_aligned const eT* const mem; //!< pointer to the memory used for storing elements (memory is read-only) protected: arma_aligned const Mat** const mat_ptrs; arma_align_mem Mat* mat_ptrs_local[ Cube_prealloc::mat_ptrs_size ]; arma_align_mem eT mem_local[ Cube_prealloc::mem_n_elem ]; // local storage, for small cubes public: inline ~Cube(); inline Cube(); inline explicit Cube(const uword in_rows, const uword in_cols, const uword in_slices); inline explicit Cube(const SizeCube& s); template inline Cube(const uword in_rows, const uword in_cols, const uword in_slices, const fill::fill_class& f); template inline Cube(const SizeCube& s, const fill::fill_class& f); #if defined(ARMA_USE_CXX11) inline Cube(Cube&& m); inline const Cube& operator=(Cube&& m); #endif 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 = false, const bool prealloc_mat = false); inline Cube(const eT* aux_mem, const uword aux_n_rows, const uword aux_n_cols, const uword aux_n_slices); arma_inline const Cube& operator=(const eT val); arma_inline const Cube& operator+=(const eT val); arma_inline const Cube& operator-=(const eT val); arma_inline const Cube& operator*=(const eT val); arma_inline const Cube& operator/=(const eT val); inline Cube(const Cube& m); inline const Cube& operator=(const Cube& m); inline const Cube& operator+=(const Cube& m); inline const Cube& operator-=(const Cube& m); inline const Cube& operator%=(const Cube& m); inline const Cube& operator/=(const Cube& m); template inline explicit Cube(const BaseCube& A, const BaseCube& B); inline Cube(const subview_cube& X); inline const Cube& operator=(const subview_cube& X); inline const Cube& operator+=(const subview_cube& X); inline const Cube& operator-=(const subview_cube& X); inline const Cube& operator%=(const subview_cube& X); inline const Cube& operator/=(const subview_cube& X); arma_inline Mat& slice(const uword in_slice); arma_inline const Mat& slice(const uword in_slice) const; arma_inline subview_cube slices(const uword in_slice1, const uword in_slice2); arma_inline const subview_cube slices(const uword in_slice1, const uword in_slice2) const; 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); 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; inline subview_cube subcube(const uword in_row1, const uword in_col1, const uword in_slice1, const SizeCube& s); inline const subview_cube subcube(const uword in_row1, const uword in_col1, const uword in_slice1, const SizeCube& s) const; inline subview_cube subcube(const span& row_span, const span& col_span, const span& slice_span); inline const subview_cube subcube(const span& row_span, const span& col_span, const span& slice_span) const; inline subview_cube operator()(const span& row_span, const span& col_span, const span& slice_span); inline const subview_cube operator()(const span& row_span, const span& col_span, const span& slice_span) const; inline subview_cube operator()(const uword in_row1, const uword in_col1, const uword in_slice1, const SizeCube& s); inline const subview_cube operator()(const uword in_row1, const uword in_col1, const uword in_slice1, const SizeCube& s) const; arma_inline subview_cube tube(const uword in_row1, const uword in_col1); arma_inline const subview_cube tube(const uword in_row1, const uword in_col1) const; arma_inline subview_cube tube(const uword in_row1, const uword in_col1, const uword in_row2, const uword in_col2); arma_inline const subview_cube tube(const uword in_row1, const uword in_col1, const uword in_row2, const uword in_col2) const; arma_inline subview_cube tube(const uword in_row1, const uword in_col1, const SizeMat& s); arma_inline const subview_cube tube(const uword in_row1, const uword in_col1, const SizeMat& s) const; inline subview_cube tube(const span& row_span, const span& col_span); inline const subview_cube tube(const span& row_span, const span& col_span) const; template arma_inline subview_elem1 elem(const Base& a); template arma_inline const subview_elem1 elem(const Base& a) const; template arma_inline subview_elem1 operator()(const Base& a); template arma_inline const subview_elem1 operator()(const Base& a) const; arma_inline subview_cube_each1 each_slice(); arma_inline const subview_cube_each1 each_slice() const; template inline subview_cube_each2 each_slice(const Base& indices); template inline const subview_cube_each2 each_slice(const Base& indices) const; #if defined(ARMA_USE_CXX11) inline const Cube& each_slice(const std::function< void( Mat&) >& F); inline const Cube& each_slice(const std::function< void(const Mat&) >& F) const; #endif inline void shed_slice(const uword slice_num); inline void shed_slices(const uword in_slice1, const uword in_slice2); inline void insert_slices(const uword slice_num, const uword N, const bool set_to_zero = true); template inline void insert_slices(const uword row_num, const BaseCube& X); template inline Cube(const GenCube& X); template inline const Cube& operator=(const GenCube& X); template inline const Cube& operator+=(const GenCube& X); template inline const Cube& operator-=(const GenCube& X); template inline const Cube& operator%=(const GenCube& X); template inline const Cube& operator/=(const GenCube& X); template inline Cube(const OpCube& X); template inline const Cube& operator=(const OpCube& X); template inline const Cube& operator+=(const OpCube& X); template inline const Cube& operator-=(const OpCube& X); template inline const Cube& operator%=(const OpCube& X); template inline const Cube& operator/=(const OpCube& X); template inline Cube(const eOpCube& X); template inline const Cube& operator=(const eOpCube& X); template inline const Cube& operator+=(const eOpCube& X); template inline const Cube& operator-=(const eOpCube& X); template inline const Cube& operator%=(const eOpCube& X); template inline const Cube& operator/=(const eOpCube& X); template inline Cube(const mtOpCube& X); template inline const Cube& operator=(const mtOpCube& X); template inline const Cube& operator+=(const mtOpCube& X); template inline const Cube& operator-=(const mtOpCube& X); template inline const Cube& operator%=(const mtOpCube& X); template inline const Cube& operator/=(const mtOpCube& X); template inline Cube(const GlueCube& X); template inline const Cube& operator=(const GlueCube& X); template inline const Cube& operator+=(const GlueCube& X); template inline const Cube& operator-=(const GlueCube& X); template inline const Cube& operator%=(const GlueCube& X); template inline const Cube& operator/=(const GlueCube& X); template inline Cube(const eGlueCube& X); template inline const Cube& operator=(const eGlueCube& X); template inline const Cube& operator+=(const eGlueCube& X); template inline const Cube& operator-=(const eGlueCube& X); template inline const Cube& operator%=(const eGlueCube& X); template inline const Cube& operator/=(const eGlueCube& X); template inline Cube(const mtGlueCube& X); template inline const Cube& operator=(const mtGlueCube& X); template inline const Cube& operator+=(const mtGlueCube& X); template inline const Cube& operator-=(const mtGlueCube& X); template inline const Cube& operator%=(const mtGlueCube& X); template inline const Cube& operator/=(const mtGlueCube& X); arma_inline arma_warn_unused const eT& at_alt (const uword i) const; arma_inline arma_warn_unused eT& operator[] (const uword i); arma_inline arma_warn_unused const eT& operator[] (const uword i) const; arma_inline arma_warn_unused eT& at(const uword i); arma_inline arma_warn_unused const eT& at(const uword i) const; arma_inline arma_warn_unused eT& operator() (const uword i); arma_inline arma_warn_unused const eT& operator() (const uword i) const; arma_inline arma_warn_unused eT& at (const uword in_row, const uword in_col, const uword in_slice); arma_inline arma_warn_unused const eT& at (const uword in_row, const uword in_col, const uword in_slice) const; arma_inline arma_warn_unused eT& operator() (const uword in_row, const uword in_col, const uword in_slice); arma_inline arma_warn_unused const eT& operator() (const uword in_row, const uword in_col, const uword in_slice) const; arma_inline const Cube& operator++(); arma_inline void operator++(int); arma_inline const Cube& operator--(); arma_inline void operator--(int); arma_inline arma_warn_unused bool is_finite() const; arma_inline arma_warn_unused bool is_empty() const; inline arma_warn_unused bool has_inf() const; inline arma_warn_unused bool has_nan() const; arma_inline arma_warn_unused bool in_range(const uword i) const; arma_inline arma_warn_unused bool in_range(const span& x) const; arma_inline arma_warn_unused bool in_range(const uword in_row, const uword in_col, const uword in_slice) const; inline arma_warn_unused bool in_range(const span& row_span, const span& col_span, const span& slice_span) const; inline arma_warn_unused bool in_range(const uword in_row, const uword in_col, const uword in_slice, const SizeCube& s) const; arma_inline arma_warn_unused eT* memptr(); arma_inline arma_warn_unused const eT* memptr() const; arma_inline arma_warn_unused eT* slice_memptr(const uword slice); arma_inline arma_warn_unused const eT* slice_memptr(const uword slice) const; arma_inline arma_warn_unused eT* slice_colptr(const uword in_slice, const uword in_col); arma_inline arma_warn_unused const eT* slice_colptr(const uword in_slice, const uword in_col) const; inline void impl_print( const std::string& extra_text) const; inline void impl_print(std::ostream& user_stream, const std::string& extra_text) const; inline void impl_raw_print( const std::string& extra_text) const; inline void impl_raw_print(std::ostream& user_stream, const std::string& extra_text) const; inline void set_size(const uword in_rows, const uword in_cols, const uword in_slices); inline void reshape(const uword in_rows, const uword in_cols, const uword in_slices, const uword dim = 0); inline void resize(const uword in_rows, const uword in_cols, const uword in_slices); inline void set_size(const SizeCube& s); inline void reshape(const SizeCube& s); inline void resize(const SizeCube& s); template inline void copy_size(const Cube& m); template inline const Cube& for_each(functor F); template inline const Cube& for_each(functor F) const; template inline const Cube& transform(functor F); template inline const Cube& imbue(functor F); inline const Cube& fill(const eT val); inline const Cube& zeros(); inline const Cube& zeros(const uword in_rows, const uword in_cols, const uword in_slices); inline const Cube& zeros(const SizeCube& s); inline const Cube& ones(); inline const Cube& ones(const uword in_rows, const uword in_cols, const uword in_slices); inline const Cube& ones(const SizeCube& s); inline const Cube& randu(); inline const Cube& randu(const uword in_rows, const uword in_cols, const uword in_slices); inline const Cube& randu(const SizeCube& s); inline const Cube& randn(); inline const Cube& randn(const uword in_rows, const uword in_cols, const uword in_slices); inline const Cube& randn(const SizeCube& s); inline void reset(); template inline void set_real(const BaseCube& X); template inline void set_imag(const BaseCube& X); inline arma_warn_unused eT min() const; inline arma_warn_unused eT max() const; inline eT min(uword& index_of_min_val) const; inline eT max(uword& index_of_max_val) const; inline eT min(uword& row_of_min_val, uword& col_of_min_val, uword& slice_of_min_val) const; inline eT max(uword& row_of_max_val, uword& col_of_max_val, uword& slice_of_max_val) const; inline bool save(const std::string name, const file_type type = arma_binary, const bool print_status = true) const; inline bool save( std::ostream& os, const file_type type = arma_binary, const bool print_status = true) const; inline bool load(const std::string name, const file_type type = auto_detect, const bool print_status = true); inline bool load( std::istream& is, const file_type type = auto_detect, const bool print_status = true); inline bool quiet_save(const std::string name, const file_type type = arma_binary) const; inline bool quiet_save( std::ostream& os, const file_type type = arma_binary) const; inline bool quiet_load(const std::string name, const file_type type = auto_detect); inline bool quiet_load( std::istream& is, const file_type type = auto_detect); // iterators typedef eT* iterator; typedef const eT* const_iterator; typedef eT* slice_iterator; typedef const eT* const_slice_iterator; inline iterator begin(); inline const_iterator begin() const; inline const_iterator cbegin() const; inline iterator end(); inline const_iterator end() const; inline const_iterator cend() const; inline slice_iterator begin_slice(const uword slice_num); inline const_slice_iterator begin_slice(const uword slice_num) const; inline slice_iterator end_slice(const uword slice_num); inline const_slice_iterator end_slice(const uword slice_num) const; inline void clear(); inline bool empty() const; inline uword size() const; inline void swap(Cube& B); inline void steal_mem(Cube& X); //!< don't use this unless you're writing code internal to Armadillo template class fixed; protected: inline void init_cold(); inline void init_warm(const uword in_rows, const uword in_cols, const uword in_slices); template inline void init(const BaseCube& A, const BaseCube& B); inline void delete_mat(); inline void create_mat(); friend class glue_join; friend class op_reshape; friend class op_resize; friend class subview_cube; public: #ifdef ARMA_EXTRA_CUBE_PROTO #include ARMA_INCFILE_WRAP(ARMA_EXTRA_CUBE_PROTO) #endif }; template template class Cube::fixed : public Cube { private: static const uword fixed_n_elem = fixed_n_rows * fixed_n_cols * fixed_n_slices; static const uword fixed_n_elem_slice = fixed_n_rows * fixed_n_cols; static const bool use_extra = (fixed_n_elem > Cube_prealloc::mem_n_elem); arma_aligned Mat* mat_ptrs_local_extra[ (fixed_n_slices > Cube_prealloc::mat_ptrs_size) ? fixed_n_slices : 1 ]; arma_align_mem eT mem_local_extra [ use_extra ? fixed_n_elem : 1 ]; arma_inline void mem_setup(); public: inline fixed(); inline fixed(const fixed& X); template inline fixed(const fill::fill_class& f); template inline fixed(const BaseCube& A); template inline fixed(const BaseCube& A, const BaseCube& B); using Cube::operator=; using Cube::operator(); inline const Cube& operator=(const fixed& X); arma_inline arma_warn_unused eT& operator[] (const uword i); arma_inline arma_warn_unused const eT& operator[] (const uword i) const; arma_inline arma_warn_unused eT& at (const uword i); arma_inline arma_warn_unused const eT& at (const uword i) const; arma_inline arma_warn_unused eT& operator() (const uword i); arma_inline arma_warn_unused const eT& operator() (const uword i) const; arma_inline arma_warn_unused eT& at (const uword in_row, const uword in_col, const uword in_slice); arma_inline arma_warn_unused const eT& at (const uword in_row, const uword in_col, const uword in_slice) const; arma_inline arma_warn_unused eT& operator() (const uword in_row, const uword in_col, const uword in_slice); arma_inline arma_warn_unused const eT& operator() (const uword in_row, const uword in_col, const uword in_slice) const; }; class Cube_aux { public: template arma_inline static void prefix_pp(Cube& x); template arma_inline static void prefix_pp(Cube< std::complex >& x); template arma_inline static void postfix_pp(Cube& x); template arma_inline static void postfix_pp(Cube< std::complex >& x); template arma_inline static void prefix_mm(Cube& x); template arma_inline static void prefix_mm(Cube< std::complex >& x); template arma_inline static void postfix_mm(Cube& x); template arma_inline static void postfix_mm(Cube< std::complex >& x); template inline static void set_real(Cube& out, const BaseCube& X); template inline static void set_imag(Cube& out, const BaseCube& X); template inline static void set_real(Cube< std::complex >& out, const BaseCube< T,T1>& X); template inline static void set_imag(Cube< std::complex >& out, const BaseCube< T,T1>& X); }; //! @}