From: Gabriel Dos Reis Date: Wed, 10 Jul 2002 12:06:37 +0000 (+0000) Subject: valarray_meta.h (_UnFunBase): Take a second template parameter. X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=c963e6b00c244d9aa733397d194c6e3a1510f67c;p=gcc.git valarray_meta.h (_UnFunBase): Take a second template parameter. * include/bits/valarray_meta.h (_UnFunBase): Take a second template parameter. Remove _M_func data member. (_UnFunClos): Take a third template parameter. (__abs, __cos, __acos, __cosh, __sin, __asin, __sinh, __tan, __atan, __tanh, __exp, __log, __log10, __sqrt): New classes. (_DEFINE_EXPR_UNARY_OPERATOR): Adjust definition. From-SVN: r55365 --- diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index c056f78376d..60eb56fb035 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,12 @@ +2002-07-10 Gabriel Dos Reis + + * include/bits/valarray_meta.h (_UnFunBase): Take a second + template parameter. Remove _M_func data member. + (_UnFunClos): Take a third template parameter. + (__abs, __cos, __acos, __cosh, __sin, __asin, __sinh, __tan, + __atan, __tanh, __exp, __log, __log10, __sqrt): New classes. + (_DEFINE_EXPR_UNARY_OPERATOR): Adjust definition. + 2002-07-08 Eric Christopher * configure.target: Fix comment for mips atomicity. Add diff --git a/libstdc++-v3/include/bits/valarray_meta.h b/libstdc++-v3/include/bits/valarray_meta.h index f2926c09035..689f715b5c1 100644 --- a/libstdc++-v3/include/bits/valarray_meta.h +++ b/libstdc++-v3/include/bits/valarray_meta.h @@ -58,41 +58,132 @@ namespace std // // Unary function application closure. // - template class _UnFunBase { + template class _UnFunBase + { public: - typedef typename _Dom::value_type value_type; - typedef value_type _Vt; - - _UnFunBase (const _Dom& __e, _Vt __f(_Vt)) - : _M_expr(__e), _M_func(__f) {} - - _Vt operator[] (size_t __i) const { return _M_func(_M_expr[__i]); } - size_t size () const { return _M_expr.size(); } - + typedef typename _Dom::value_type value_type; + typedef value_type _Vt; + + explicit _UnFunBase (const _Dom& __e) : _M_expr(__e) {} + + _Vt operator[] (size_t __i) const { return _Op()(_M_expr[__i]); } + size_t size () const { return _M_expr.size(); } + private: - const _Dom& _M_expr; - _Vt (*_M_func)(_Vt); + const _Dom& _M_expr; }; - template class _Meta, class _Dom> - class _UnFunClos; + // Implementations of unary functions applied to valarray<>s. + // I use hard-coded object functions here instead of a generic + // approach like pointers to function: + // 1) correctness: some functions take references, others values. + // we can't deduce the correct type afterwards. + // 2) efficiency -- object functions can be easily inlined + // 3) be Koenig-lookup-friendly + + struct __abs + { + template + _Tp operator()(const _Tp& __t) const { return abs(__t); } + }; + + struct __cos + { + template + _Tp operator()(const _Tp& __t) const { return cos(__t); } + }; + + struct __acos + { + template + _Tp operator()(const _Tp& __t) const { return acos(__t); } + }; + + struct __cosh + { + template + _Tp operator()(const _Tp& __t) const { return cosh(__t); } + }; + + struct __sin + { + template + _Tp operator()(const _Tp& __t) const { return sin(__t); } + }; + + struct __asin + { + template + _Tp operator()(const _Tp& __t) const { return asin(__t); } + }; + + struct __sinh + { + template + _Tp operator()(const _Tp& __t) const { return sinh(__t); } + }; + + struct __tan + { + template + _Tp operator()(const _Tp& __t) const { return tan(__t); } + }; + + struct __atan + { + template + _Tp operator()(const _Tp& __t) const { return atan(__t); } + }; + + struct __tanh + { + template + _Tp operator()(const _Tp& __t) const { return tanh(__t); } + }; + + struct __exp + { + template + _Tp operator()(const _Tp& __t) const { return exp(__t); } + }; + + struct __log + { + template + _Tp operator()(const _Tp& __t) const { return log(__t); } + }; + + struct __log10 + { + template + _Tp operator()(const _Tp& __t) const { return log10(__t); } + }; + + struct __sqrt + { + template + _Tp operator()(const _Tp& __t) const { return sqrt(__t); } + }; + + template class _Meta, class _Dom, typename _Op> + class _UnFunClos; - template - struct _UnFunClos<_Expr,_Dom> : _UnFunBase<_Dom> { - typedef _UnFunBase<_Dom> _Base; - typedef typename _Base::value_type value_type; - - _UnFunClos (const _Dom& __e, value_type __f(value_type)) - : _Base (__e, __f) {} + template + struct _UnFunClos<_Expr,_Dom, _Op> : _UnFunBase<_Dom, _Op> + { + typedef _UnFunBase<_Dom, _Op> _Base; + typedef typename _Base::value_type value_type; + + explicit _UnFunClos (const _Dom& __e) : _Base (__e) {} }; - template - struct _UnFunClos<_ValArray,_Tp> : _UnFunBase > { - typedef _UnFunBase > _Base; - typedef typename _Base::value_type value_type; - - _UnFunClos (const valarray<_Tp>& __v, _Tp __f(_Tp)) - : _Base (__v, __f) {} + template + struct _UnFunClos<_ValArray,_Tp, _Op> : _UnFunBase, _Op> + { + typedef _UnFunBase, _Op> _Base; + typedef typename _Base::value_type value_type; + + explicit _UnFunClos (const valarray<_Tp>& __v) : _Base (__v) {} }; // @@ -938,20 +1029,20 @@ operator _Op (const valarray& __v, \ #define _DEFINE_EXPR_UNARY_FUNCTION(_Name) \ template \ -inline _Expr<_UnFunClos<_Expr,_Dom>,typename _Dom::value_type> \ +inline _Expr<_UnFunClos<_Expr,_Dom,__##_Name>,typename _Dom::value_type>\ _Name(const _Expr<_Dom,typename _Dom::value_type>& __e) \ { \ typedef typename _Dom::value_type _Tp; \ - typedef _UnFunClos<_Expr,_Dom> _Closure; \ - return _Expr<_Closure,_Tp>(_Closure(__e(), (_Tp(*)(_Tp))(&_Name))); \ + typedef _UnFunClos<_Expr,_Dom,__##_Name> _Closure; \ + return _Expr<_Closure,_Tp>(_Closure(__e())); \ } \ \ template \ -inline _Expr<_UnFunClos<_ValArray,_Tp>,_Tp> \ +inline _Expr<_UnFunClos<_ValArray,_Tp,__##_Name>,_Tp> \ _Name(const valarray<_Tp>& __v) \ { \ - typedef _UnFunClos<_ValArray,_Tp> _Closure; \ - return _Expr<_Closure,_Tp> (_Closure (__v, (_Tp(*)(_Tp))(&_Name))); \ + typedef _UnFunClos<_ValArray,_Tp,__##_Name> _Closure; \ + return _Expr<_Closure,_Tp>(_Closure (__v)); \ }