📄 map_view.hpp
字号:
// Software License for MTL// // Copyright (c) 2007 The Trustees of Indiana University. All rights reserved.// Authors: Peter Gottschling and Andrew Lumsdaine// // This file is part of the Matrix Template Library// // See also license.mtl.txt in the distribution.#ifndef MTL_MAP_VIEW_INCLUDE#define MTL_MAP_VIEW_INCLUDE#include <boost/shared_ptr.hpp>#include <boost/numeric/mtl/utility/category.hpp>#include <boost/numeric/mtl/utility/range_generator.hpp>#include <boost/numeric/mtl/utility/property_map.hpp>#include <boost/numeric/mtl/detail/crtp_base_matrix.hpp>#include <boost/numeric/mtl/operation/sub_matrix.hpp>#include <boost/numeric/mtl/operation/sfunctor.hpp>#include <boost/numeric/mtl/operation/tfunctor.hpp>#include <boost/numeric/mtl/operation/conj.hpp>#include <boost/numeric/mtl/matrix/mat_expr.hpp>namespace mtl { namespace matrix { namespace detail { // Forward declaration for friend declaration template <typename, typename> struct map_value;}}}namespace mtl { namespace matrix {template <typename Functor, typename Matrix> class map_view : public mtl::detail::const_crtp_base_matrix< map_view<Functor, Matrix>, typename Functor::result_type, typename Matrix::size_type >, public mat_expr< map_view<Functor, Matrix> >{ typedef map_view self; typedef matrix::mat_expr< self > expr_base;public: typedef Matrix other; typedef typename Matrix::orientation orientation; typedef typename Matrix::index_type index_type; typedef typename Functor::result_type value_type; typedef typename Functor::result_type const_reference_type; // Obsolete: typedef typename Functor::result_type const_access_type; typedef typename Matrix::key_type key_type; typedef typename Matrix::size_type size_type; typedef typename Matrix::dim_type::transposed_type dim_type; map_view (const Functor& functor, const other& ref) : expr_base(*this), functor(functor), ref(ref) {} map_view (const Functor& functor, boost::shared_ptr<Matrix> p) : expr_base(*this), functor(functor), my_copy(p), ref(*p) {} const_reference_type operator() (size_type r, size_type c) const { return functor(ref(r, c)); } size_type dim1() const { return ref.dim1(); } size_type dim2() const { return ref.dim2(); } dim_type dimensions() const { return ref.dimensions(); } size_type begin_row() const { return ref.begin_row(); } size_type end_row() const { return ref.end_row(); } size_type num_rows() const { return ref.num_rows(); } size_type begin_col() const { return ref.begin_col(); } size_type end_col() const { return ref.end_col(); } size_type num_cols() const { return ref.num_cols(); } template <typename, typename> friend struct detail::map_value; protected: boost::shared_ptr<Matrix> my_copy; public: Functor functor; const other& ref;}; // ================// Free functions// ================template <typename Functor, typename Matrix>typename map_view<Functor, Matrix>::size_typeinline num_rows(const map_view<Functor, Matrix>& matrix){ return matrix.num_rows();}template <typename Functor, typename Matrix>typename map_view<Functor, Matrix>::size_typeinline num_cols(const map_view<Functor, Matrix>& matrix){ return matrix.num_cols();}template <typename Functor, typename Matrix>typename map_view<Functor, Matrix>::size_typeinline size(const map_view<Functor, Matrix>& matrix){ return matrix.num_cols() * matrix.num_rows();} namespace detail { template <typename Functor, typename Matrix> struct map_value { typedef typename Matrix::key_type key_type; typedef typename matrix::map_view<Functor, Matrix>::value_type value_type; map_value(matrix::map_view<Functor, Matrix> const& map_matrix) : map_matrix(map_matrix), its_value(map_matrix.ref) {} value_type operator() (key_type const& key) const { return map_matrix.functor(its_value(key)); } protected: matrix::map_view<Functor, Matrix> const& map_matrix; typename traits::const_value<Matrix>::type its_value; }; } // detail}} // namespace mtl::matrixnamespace mtl { namespace traits { namespace detail { template <typename Functor, typename Matrix> struct mapped_row { typedef typename Matrix::key_type key_type; typedef typename Matrix::size_type size_type; explicit mapped_row(const matrix::map_view<Functor, Matrix>& view) : its_row(view.ref) {} size_type operator() (key_type const& key) const { return its_row(key); } protected: typename row<Matrix>::type its_row; }; template <typename Functor, typename Matrix> struct mapped_col { typedef typename Matrix::key_type key_type; typedef typename Matrix::size_type size_type; mapped_col(const matrix::map_view<Functor, Matrix>& view) : its_col(view.ref) {} size_type operator() (key_type const& key) const { return its_col(key); } protected: typename col<Matrix>::type its_col; }; } // namespace detail template <typename Functor, typename Matrix> struct row<matrix::map_view<Functor, Matrix> > { typedef detail::mapped_row<Functor, Matrix> type; }; template <typename Functor, typename Matrix> struct col<matrix::map_view<Functor, Matrix> > { typedef detail::mapped_col<Functor, Matrix> type; }; template <typename Functor, typename Matrix> struct const_value<matrix::map_view<Functor, Matrix> > { typedef matrix::detail::map_value<Functor, Matrix> type; }; // ================ // Range generators // ================ // Use range_generator of original matrix template <typename Tag, typename Functor, typename Matrix> struct range_generator<Tag, matrix::map_view<Functor, Matrix> > : public detail::referred_range_generator<matrix::map_view<Functor, Matrix>, range_generator<Tag, Matrix> > {}; // To disambigue template <typename Functor, typename Matrix> struct range_generator<tag::major, matrix::map_view<Functor, Matrix> > : public detail::referred_range_generator<matrix::map_view<Functor, Matrix>, range_generator<tag::major, Matrix> > {};}} // mtl::traitsnamespace mtl {// ==========// Sub matrix// ==========template <typename Functor, typename Matrix>struct sub_matrix_t< matrix::map_view<Functor, Matrix> >{ typedef matrix::map_view<Functor, Matrix> matrix_type; // Mapping of sub-matrix type typedef typename sub_matrix_t<Matrix>::const_sub_matrix_type ref_sub_type; typedef matrix::map_view<Functor, ref_sub_type> const_sub_matrix_type; typedef typename matrix_type::size_type size_type; const_sub_matrix_type operator()(matrix_type const& matrix, size_type begin_r, size_type end_r, size_type begin_c, size_type end_c) { typedef boost::shared_ptr<ref_sub_type> pointer_type; // Submatrix of referred matrix // Create a submatrix, whos address will be kept by map_view // Functor is copied from view pointer_type p(new ref_sub_type(sub_matrix(matrix.ref, begin_r, end_r, begin_c, end_c))); return const_sub_matrix_type(matrix.functor, p); }};} // namespace mtlnamespace mtl { namespace matrix {template <typename Scaling, typename Matrix>struct scaled_view : public map_view<tfunctor::scale<Scaling, typename Matrix::value_type>, Matrix>{ typedef tfunctor::scale<Scaling, typename Matrix::value_type> functor_type; typedef map_view<functor_type, Matrix> base; scaled_view(const Scaling& scaling, const Matrix& matrix) : base(functor_type(scaling), matrix) {} scaled_view(const Scaling& scaling, boost::shared_ptr<Matrix> p) : base(functor_type(scaling), p) {}};template <typename Matrix>struct conj_view : public map_view<sfunctor::conj<typename Matrix::value_type>, Matrix>{ typedef sfunctor::conj<typename Matrix::value_type> functor_type; typedef map_view<functor_type, Matrix> base; conj_view(const Matrix& matrix) : base(functor_type(), matrix) {} conj_view(boost::shared_ptr<Matrix> p) : base(functor_type(), p) {}};}} // namespace mtl::matrixnamespace mtl {template <typename Scaling, typename Matrix>struct sub_matrix_t< matrix::scaled_view<Scaling, Matrix> > : public sub_matrix_t< matrix::map_view<tfunctor::scale<Scaling, typename Matrix::value_type>, Matrix> >{};template <typename Matrix>struct sub_matrix_t< matrix::conj_view<Matrix> > : public sub_matrix_t< matrix::map_view<sfunctor::conj<typename Matrix::value_type>, Matrix> >{};} // namespace mtl#endif // MTL_MAP_VIEW_INCLUDE
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -