📄 vector_expression.hpp
字号:
//// Copyright (c) 2000-2002// Joerg Walter, Mathias Koch//// Permission to use, copy, modify, distribute and sell this software// and its documentation for any purpose is hereby granted without fee,// provided that the above copyright notice appear in all copies and// that both that copyright notice and this permission notice appear// in supporting documentation. The authors make no representations// about the suitability of this software for any purpose.// It is provided "as is" without express or implied warranty.//// The authors gratefully acknowledge the support of// GeNeSys mbH & Co. KG in producing this work.//#ifndef VECTOR_EXPRESSION_H#define VECTOR_EXPRESSION_H#include <boost/numeric/ublas/config.hpp>#include <boost/numeric/ublas/exception.hpp>#include <boost/numeric/ublas/functional.hpp>// Expression templates based on ideas of Todd Veldhuizen and Geoffrey Furnish// Iterators based on ideas of Jeremy Sieknamespace boost { namespace numeric { namespace ublas { // Base class for uBLAS staticaly derived expressions - see the Barton Nackman trick // Provides numeric properties for linear algebra template<class E> class ublas_expression { public: typedef E expression_type; /* FIXME expression properties are undefined due to a template instantiation order problem typedef typename E::type_category type_category; typedef typename E::value_type value_type; */ // Directly implement nonassignable - simplifes debugging call trace! protected: ublas_expression () {} ~ublas_expression () {} private: const ublas_expression& operator= (const ublas_expression &); }; // Base class for Scalar Expressions - see the Barton Nackman trick template<class E> class scalar_expression: public ublas_expression<E> { public: typedef E expression_type; typedef scalar_tag type_category; BOOST_UBLAS_INLINE const expression_type &operator () () const { return *static_cast<const expression_type *> (this); } BOOST_UBLAS_INLINE expression_type &operator () () { return *static_cast<expression_type *> (this); } }; template<class T> class scalar_reference: public scalar_expression<scalar_reference<T> > { public: typedef T value_type;#ifndef BOOST_UBLAS_CT_REFERENCE_BASE_TYPEDEFS typedef const value_type &const_reference; typedef value_type &reference;#else typedef const value_type &const_reference; typedef typename boost::mpl::if_<boost::is_const<T>, const_reference, value_type &>::type reference;#endif private: typedef scalar_reference<T> self_type; public: typedef const self_type const_closure_type; typedef const_closure_type closure_type; // Construction and destruction BOOST_UBLAS_INLINE scalar_reference (): t_ (nil_) {} BOOST_UBLAS_INLINE explicit scalar_reference (reference t): t_ (t) {} BOOST_UBLAS_INLINE operator value_type () const { return t_; } // Assignment BOOST_UBLAS_INLINE scalar_reference &operator = (const scalar_reference &s) { t_ = s.t_; return *this; } template<class AE> BOOST_UBLAS_INLINE scalar_reference &operator = (const scalar_expression<AE> &ae) { t_ = ae; return *this; } // Closure comparison BOOST_UBLAS_INLINE bool same_closure (const scalar_reference &sr) const { return &t_ == &sr.t_; } private: reference t_; static value_type nil_; }; template<class T> typename scalar_reference<T>::value_type scalar_reference<T>::nil_ = BOOST_UBLAS_TYPENAME scalar_reference<T>::value_type (); template<class T> class scalar_value: public scalar_expression<scalar_value<T> > { public: typedef T value_type;#ifndef BOOST_UBLAS_CT_REFERENCE_BASE_TYPEDEFS typedef const value_type &const_reference; typedef value_type &reference;#else typedef const value_type &const_reference; typedef typename boost::mpl::if_<boost::is_const<T>, const_reference, value_type &>::type reference;#endif private: typedef scalar_value<T> self_type; public:#ifndef BOOST_UBLAS_CT_REFERENCE_BASE_TYPEDEFS typedef const scalar_const_reference<const self_type> const_closure_type;#else typedef const scalar_reference<const self_type> const_closure_type;#endif typedef scalar_reference<self_type> closure_type; // Construction and destruction BOOST_UBLAS_INLINE scalar_value (): t_ () {} BOOST_UBLAS_INLINE scalar_value (const value_type &t): t_ (t) {} BOOST_UBLAS_INLINE operator value_type () const { return t_; } // Assignment BOOST_UBLAS_INLINE scalar_value &operator = (const scalar_value &s) { t_ = s.t_; return *this; } template<class AE> BOOST_UBLAS_INLINE scalar_value &operator = (const scalar_expression<AE> &ae) { t_ = ae; return *this; } // Closure comparison BOOST_UBLAS_INLINE bool same_closure (const scalar_value &sv) const { return this == &sv; // self closing on instances value } private: value_type t_; }; // Base class for Vector Expressions - see the Barton Nackman trick template<class E> class vector_expression: public ublas_expression<E> { public: BOOST_STATIC_CONSTANT (unsigned, complexity = 0); typedef E expression_type; typedef vector_tag type_category; typedef abstract_tag simd_category; typedef noalias_proxy<E> noalias_proxy_type; typedef const vector_range<const E> const_vector_range_type; typedef vector_range<E> vector_range_type; typedef const vector_slice<const E> const_vector_slice_type; typedef vector_slice<E> vector_slice_type; typedef const vector_indirect<const E> const_vector_indirect_type; typedef vector_indirect<E> vector_indirect_type; // This class could define an common interface for all // statically derived expression type classes. // Due to a compiler deficiency - one can not reference class typedefs of E // on MSVC 6.0 (error C2027) - we only implement the casts. BOOST_UBLAS_INLINE const expression_type &operator () () const { return *static_cast<const expression_type *> (this); } BOOST_UBLAS_INLINE expression_type &operator () () { return *static_cast<expression_type *> (this); } BOOST_UBLAS_INLINE noalias_proxy_type noalias () { return noalias_proxy_type (operator () ()); }#ifndef BOOST_UBLAS_NO_PROXY_SHORTCUTS BOOST_UBLAS_INLINE const_vector_range_type operator () (const range &r) const { return const_vector_range_type (operator () (), r); } BOOST_UBLAS_INLINE vector_range_type operator () (const range &r) { return vector_range_type (operator () (), r); } BOOST_UBLAS_INLINE const_vector_slice_type operator () (const slice &s) const { return const_vector_slice_type (operator () (), s); } BOOST_UBLAS_INLINE vector_slice_type operator () (const slice &s) { return vector_slice_type (operator () (), s); } template<class A> BOOST_UBLAS_INLINE const_vector_indirect_type operator () (const indirect_array<A> &ia) const { return const_vector_indirect_type (operator () (), ia); } template<class A> BOOST_UBLAS_INLINE vector_indirect_type operator () (const indirect_array<A> &ia) { return vector_indirect_type (operator () (), ia); }#else BOOST_UBLAS_INLINE const_vector_range_type project (const range &r) const { return const_vector_range_type (operator () (), r); } BOOST_UBLAS_INLINE vector_range_type project (const range &r) { return vector_range_type (operator () (), r); } BOOST_UBLAS_INLINE const_vector_slice_type project (const slice &s) const { return const_vector_slice_type (operator () (), s); } BOOST_UBLAS_INLINE vector_slice_type project (const slice &s) { return vector_slice_type (operator () (), s); } template<class A> BOOST_UBLAS_INLINE const_vector_indirect_type project (const indirect_array<A> &ia) const { return const_vector_indirect_type (operator () (), ia); } template<class A> BOOST_UBLAS_INLINE vector_indirect_type project (const indirect_array<A> &ia) { return vector_indirect_type (operator () (), ia); }#endif }; template<class T> class vector_reference: public vector_expression<vector_reference<T> > { public:#ifndef BOOST_UBLAS_NO_PROXY_SHORTCUTS BOOST_UBLAS_USING vector_expression<vector_reference<T> >::operator ();#endif typedef typename T::size_type size_type; typedef typename T::difference_type difference_type; typedef typename T::value_type value_type;#ifndef BOOST_UBLAS_CT_REFERENCE_BASE_TYPEDEFS typedef typename T::const_reference const_reference; typedef typename T::reference reference;#else typedef typename T::const_reference const_reference; typedef typename boost::mpl::if_<boost::is_const<T>, typename T::const_reference, typename T::reference>::type reference;#endif private: typedef vector_reference<T> self_type; public: typedef T refered_type; typedef const self_type const_closure_type; typedef const_closure_type closure_type; typedef typename T::storage_category storage_category; typedef typename T::simd_category simd_category; // Construction and destruction BOOST_UBLAS_INLINE vector_reference (): e_ (nil_) {} BOOST_UBLAS_INLINE explicit vector_reference (refered_type &e): e_ (e) {} // Accessors BOOST_UBLAS_INLINE size_type size () const { return expression ().size (); }#ifndef BOOST_UBLAS_NESTED_CLASS_DR45 private:#endif // Expression accessors - const correct BOOST_UBLAS_INLINE const refered_type &expression () const { return e_; } BOOST_UBLAS_INLINE refered_type &expression () { return e_; } public: // Element access#ifndef BOOST_UBLAS_REFERENCE_CONST_MEMBER BOOST_UBLAS_INLINE const_reference operator () (size_type i) const { return expression () (i); } BOOST_UBLAS_INLINE reference operator () (size_type i) { return expression () (i); } BOOST_UBLAS_INLINE const_reference operator [] (size_type i) const { return expression () [i]; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -