00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifndef _VALARRAY_AFTER_H
00034 #define _VALARRAY_AFTER_H 1
00035
00036 #pragma GCC system_header
00037
00038 namespace std _GLIBCXX_VISIBILITY(default)
00039 {
00040 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00041
00042
00043
00044
00045 template<class _Dom>
00046 class _GBase
00047 {
00048 public:
00049 typedef typename _Dom::value_type value_type;
00050
00051 _GBase (const _Dom& __e, const valarray<size_t>& __i)
00052 : _M_expr (__e), _M_index(__i) {}
00053
00054 value_type
00055 operator[] (size_t __i) const
00056 { return _M_expr[_M_index[__i]]; }
00057
00058 size_t
00059 size () const
00060 { return _M_index.size(); }
00061
00062 private:
00063 const _Dom& _M_expr;
00064 const valarray<size_t>& _M_index;
00065 };
00066
00067 template<typename _Tp>
00068 class _GBase<_Array<_Tp> >
00069 {
00070 public:
00071 typedef _Tp value_type;
00072
00073 _GBase (_Array<_Tp> __a, const valarray<size_t>& __i)
00074 : _M_array (__a), _M_index(__i) {}
00075
00076 value_type
00077 operator[] (size_t __i) const
00078 { return _M_array._M_data[_M_index[__i]]; }
00079
00080 size_t
00081 size () const
00082 { return _M_index.size(); }
00083
00084 private:
00085 const _Array<_Tp> _M_array;
00086 const valarray<size_t>& _M_index;
00087 };
00088
00089 template<class _Dom>
00090 struct _GClos<_Expr, _Dom>
00091 : _GBase<_Dom>
00092 {
00093 typedef _GBase<_Dom> _Base;
00094 typedef typename _Base::value_type value_type;
00095
00096 _GClos (const _Dom& __e, const valarray<size_t>& __i)
00097 : _Base (__e, __i) {}
00098 };
00099
00100 template<typename _Tp>
00101 struct _GClos<_ValArray, _Tp>
00102 : _GBase<_Array<_Tp> >
00103 {
00104 typedef _GBase<_Array<_Tp> > _Base;
00105 typedef typename _Base::value_type value_type;
00106
00107 _GClos (_Array<_Tp> __a, const valarray<size_t>& __i)
00108 : _Base (__a, __i) {}
00109 };
00110
00111
00112
00113
00114 template<class _Dom>
00115 class _IBase
00116 {
00117 public:
00118 typedef typename _Dom::value_type value_type;
00119
00120 _IBase (const _Dom& __e, const valarray<size_t>& __i)
00121 : _M_expr (__e), _M_index (__i) {}
00122
00123 value_type
00124 operator[] (size_t __i) const
00125 { return _M_expr[_M_index[__i]]; }
00126
00127 size_t
00128 size() const
00129 { return _M_index.size(); }
00130
00131 private:
00132 const _Dom& _M_expr;
00133 const valarray<size_t>& _M_index;
00134 };
00135
00136 template<class _Dom>
00137 struct _IClos<_Expr, _Dom>
00138 : _IBase<_Dom>
00139 {
00140 typedef _IBase<_Dom> _Base;
00141 typedef typename _Base::value_type value_type;
00142
00143 _IClos (const _Dom& __e, const valarray<size_t>& __i)
00144 : _Base (__e, __i) {}
00145 };
00146
00147 template<typename _Tp>
00148 struct _IClos<_ValArray, _Tp>
00149 : _IBase<valarray<_Tp> >
00150 {
00151 typedef _IBase<valarray<_Tp> > _Base;
00152 typedef _Tp value_type;
00153
00154 _IClos (const valarray<_Tp>& __a, const valarray<size_t>& __i)
00155 : _Base (__a, __i) {}
00156 };
00157
00158
00159
00160
00161 template<class _Clos, typename _Tp>
00162 class _Expr
00163 {
00164 public:
00165 typedef _Tp value_type;
00166
00167 _Expr(const _Clos&);
00168
00169 const _Clos& operator()() const;
00170
00171 value_type operator[](size_t) const;
00172 valarray<value_type> operator[](slice) const;
00173 valarray<value_type> operator[](const gslice&) const;
00174 valarray<value_type> operator[](const valarray<bool>&) const;
00175 valarray<value_type> operator[](const valarray<size_t>&) const;
00176
00177 _Expr<_UnClos<__unary_plus, std::_Expr, _Clos>, value_type>
00178 operator+() const;
00179
00180 _Expr<_UnClos<__negate, std::_Expr, _Clos>, value_type>
00181 operator-() const;
00182
00183 _Expr<_UnClos<__bitwise_not, std::_Expr, _Clos>, value_type>
00184 operator~() const;
00185
00186 _Expr<_UnClos<__logical_not, std::_Expr, _Clos>, bool>
00187 operator!() const;
00188
00189 size_t size() const;
00190 value_type sum() const;
00191
00192 valarray<value_type> shift(int) const;
00193 valarray<value_type> cshift(int) const;
00194
00195 value_type min() const;
00196 value_type max() const;
00197
00198 valarray<value_type> apply(value_type (*)(const value_type&)) const;
00199 valarray<value_type> apply(value_type (*)(value_type)) const;
00200
00201 private:
00202 const _Clos _M_closure;
00203 };
00204
00205 template<class _Clos, typename _Tp>
00206 inline
00207 _Expr<_Clos, _Tp>::_Expr(const _Clos& __c) : _M_closure(__c) {}
00208
00209 template<class _Clos, typename _Tp>
00210 inline const _Clos&
00211 _Expr<_Clos, _Tp>::operator()() const
00212 { return _M_closure; }
00213
00214 template<class _Clos, typename _Tp>
00215 inline _Tp
00216 _Expr<_Clos, _Tp>::operator[](size_t __i) const
00217 { return _M_closure[__i]; }
00218
00219 template<class _Clos, typename _Tp>
00220 inline valarray<_Tp>
00221 _Expr<_Clos, _Tp>::operator[](slice __s) const
00222 {
00223 valarray<_Tp> __v = valarray<_Tp>(*this)[__s];
00224 return __v;
00225 }
00226
00227 template<class _Clos, typename _Tp>
00228 inline valarray<_Tp>
00229 _Expr<_Clos, _Tp>::operator[](const gslice& __gs) const
00230 {
00231 valarray<_Tp> __v = valarray<_Tp>(*this)[__gs];
00232 return __v;
00233 }
00234
00235 template<class _Clos, typename _Tp>
00236 inline valarray<_Tp>
00237 _Expr<_Clos, _Tp>::operator[](const valarray<bool>& __m) const
00238 {
00239 valarray<_Tp> __v = valarray<_Tp>(*this)[__m];
00240 return __v;
00241 }
00242
00243 template<class _Clos, typename _Tp>
00244 inline valarray<_Tp>
00245 _Expr<_Clos, _Tp>::operator[](const valarray<size_t>& __i) const
00246 {
00247 valarray<_Tp> __v = valarray<_Tp>(*this)[__i];
00248 return __v;
00249 }
00250
00251 template<class _Clos, typename _Tp>
00252 inline size_t
00253 _Expr<_Clos, _Tp>::size() const
00254 { return _M_closure.size(); }
00255
00256 template<class _Clos, typename _Tp>
00257 inline valarray<_Tp>
00258 _Expr<_Clos, _Tp>::shift(int __n) const
00259 {
00260 valarray<_Tp> __v = valarray<_Tp>(*this).shift(__n);
00261 return __v;
00262 }
00263
00264 template<class _Clos, typename _Tp>
00265 inline valarray<_Tp>
00266 _Expr<_Clos, _Tp>::cshift(int __n) const
00267 {
00268 valarray<_Tp> __v = valarray<_Tp>(*this).cshift(__n);
00269 return __v;
00270 }
00271
00272 template<class _Clos, typename _Tp>
00273 inline valarray<_Tp>
00274 _Expr<_Clos, _Tp>::apply(_Tp __f(const _Tp&)) const
00275 {
00276 valarray<_Tp> __v = valarray<_Tp>(*this).apply(__f);
00277 return __v;
00278 }
00279
00280 template<class _Clos, typename _Tp>
00281 inline valarray<_Tp>
00282 _Expr<_Clos, _Tp>::apply(_Tp __f(_Tp)) const
00283 {
00284 valarray<_Tp> __v = valarray<_Tp>(*this).apply(__f);
00285 return __v;
00286 }
00287
00288
00289 template<class _Clos, typename _Tp>
00290 inline _Tp
00291 _Expr<_Clos, _Tp>::sum() const
00292 {
00293 size_t __n = _M_closure.size();
00294 if (__n == 0)
00295 return _Tp();
00296 else
00297 {
00298 _Tp __s = _M_closure[--__n];
00299 while (__n != 0)
00300 __s += _M_closure[--__n];
00301 return __s;
00302 }
00303 }
00304
00305 template<class _Clos, typename _Tp>
00306 inline _Tp
00307 _Expr<_Clos, _Tp>::min() const
00308 { return __valarray_min(_M_closure); }
00309
00310 template<class _Clos, typename _Tp>
00311 inline _Tp
00312 _Expr<_Clos, _Tp>::max() const
00313 { return __valarray_max(_M_closure); }
00314
00315 template<class _Dom, typename _Tp>
00316 inline _Expr<_UnClos<__logical_not, _Expr, _Dom>, bool>
00317 _Expr<_Dom, _Tp>::operator!() const
00318 {
00319 typedef _UnClos<__logical_not, std::_Expr, _Dom> _Closure;
00320 return _Expr<_Closure, bool>(_Closure(this->_M_closure));
00321 }
00322
00323 #define _DEFINE_EXPR_UNARY_OPERATOR(_Op, _Name) \
00324 template<class _Dom, typename _Tp> \
00325 inline _Expr<_UnClos<_Name, std::_Expr, _Dom>, _Tp> \
00326 _Expr<_Dom, _Tp>::operator _Op() const \
00327 { \
00328 typedef _UnClos<_Name, std::_Expr, _Dom> _Closure; \
00329 return _Expr<_Closure, _Tp>(_Closure(this->_M_closure)); \
00330 }
00331
00332 _DEFINE_EXPR_UNARY_OPERATOR(+, __unary_plus)
00333 _DEFINE_EXPR_UNARY_OPERATOR(-, __negate)
00334 _DEFINE_EXPR_UNARY_OPERATOR(~, __bitwise_not)
00335
00336 #undef _DEFINE_EXPR_UNARY_OPERATOR
00337
00338 #define _DEFINE_EXPR_BINARY_OPERATOR(_Op, _Name) \
00339 template<class _Dom1, class _Dom2> \
00340 inline _Expr<_BinClos<_Name, _Expr, _Expr, _Dom1, _Dom2>, \
00341 typename __fun<_Name, typename _Dom1::value_type>::result_type> \
00342 operator _Op(const _Expr<_Dom1, typename _Dom1::value_type>& __v, \
00343 const _Expr<_Dom2, typename _Dom2::value_type>& __w) \
00344 { \
00345 typedef typename _Dom1::value_type _Arg; \
00346 typedef typename __fun<_Name, _Arg>::result_type _Value; \
00347 typedef _BinClos<_Name, _Expr, _Expr, _Dom1, _Dom2> _Closure; \
00348 return _Expr<_Closure, _Value>(_Closure(__v(), __w())); \
00349 } \
00350 \
00351 template<class _Dom> \
00352 inline _Expr<_BinClos<_Name, _Expr, _Constant, _Dom, \
00353 typename _Dom::value_type>, \
00354 typename __fun<_Name, typename _Dom::value_type>::result_type> \
00355 operator _Op(const _Expr<_Dom, typename _Dom::value_type>& __v, \
00356 const typename _Dom::value_type& __t) \
00357 { \
00358 typedef typename _Dom::value_type _Arg; \
00359 typedef typename __fun<_Name, _Arg>::result_type _Value; \
00360 typedef _BinClos<_Name, _Expr, _Constant, _Dom, _Arg> _Closure; \
00361 return _Expr<_Closure, _Value>(_Closure(__v(), __t)); \
00362 } \
00363 \
00364 template<class _Dom> \
00365 inline _Expr<_BinClos<_Name, _Constant, _Expr, \
00366 typename _Dom::value_type, _Dom>, \
00367 typename __fun<_Name, typename _Dom::value_type>::result_type> \
00368 operator _Op(const typename _Dom::value_type& __t, \
00369 const _Expr<_Dom, typename _Dom::value_type>& __v) \
00370 { \
00371 typedef typename _Dom::value_type _Arg; \
00372 typedef typename __fun<_Name, _Arg>::result_type _Value; \
00373 typedef _BinClos<_Name, _Constant, _Expr, _Arg, _Dom> _Closure; \
00374 return _Expr<_Closure, _Value>(_Closure(__t, __v())); \
00375 } \
00376 \
00377 template<class _Dom> \
00378 inline _Expr<_BinClos<_Name, _Expr, _ValArray, \
00379 _Dom, typename _Dom::value_type>, \
00380 typename __fun<_Name, typename _Dom::value_type>::result_type> \
00381 operator _Op(const _Expr<_Dom,typename _Dom::value_type>& __e, \
00382 const valarray<typename _Dom::value_type>& __v) \
00383 { \
00384 typedef typename _Dom::value_type _Arg; \
00385 typedef typename __fun<_Name, _Arg>::result_type _Value; \
00386 typedef _BinClos<_Name, _Expr, _ValArray, _Dom, _Arg> _Closure; \
00387 return _Expr<_Closure, _Value>(_Closure(__e(), __v)); \
00388 } \
00389 \
00390 template<class _Dom> \
00391 inline _Expr<_BinClos<_Name, _ValArray, _Expr, \
00392 typename _Dom::value_type, _Dom>, \
00393 typename __fun<_Name, typename _Dom::value_type>::result_type> \
00394 operator _Op(const valarray<typename _Dom::value_type>& __v, \
00395 const _Expr<_Dom, typename _Dom::value_type>& __e) \
00396 { \
00397 typedef typename _Dom::value_type _Tp; \
00398 typedef typename __fun<_Name, _Tp>::result_type _Value; \
00399 typedef _BinClos<_Name, _ValArray, _Expr, _Tp, _Dom> _Closure; \
00400 return _Expr<_Closure, _Value>(_Closure(__v, __e ())); \
00401 }
00402
00403 _DEFINE_EXPR_BINARY_OPERATOR(+, __plus)
00404 _DEFINE_EXPR_BINARY_OPERATOR(-, __minus)
00405 _DEFINE_EXPR_BINARY_OPERATOR(*, __multiplies)
00406 _DEFINE_EXPR_BINARY_OPERATOR(/, __divides)
00407 _DEFINE_EXPR_BINARY_OPERATOR(%, __modulus)
00408 _DEFINE_EXPR_BINARY_OPERATOR(^, __bitwise_xor)
00409 _DEFINE_EXPR_BINARY_OPERATOR(&, __bitwise_and)
00410 _DEFINE_EXPR_BINARY_OPERATOR(|, __bitwise_or)
00411 _DEFINE_EXPR_BINARY_OPERATOR(<<, __shift_left)
00412 _DEFINE_EXPR_BINARY_OPERATOR(>>, __shift_right)
00413 _DEFINE_EXPR_BINARY_OPERATOR(&&, __logical_and)
00414 _DEFINE_EXPR_BINARY_OPERATOR(||, __logical_or)
00415 _DEFINE_EXPR_BINARY_OPERATOR(==, __equal_to)
00416 _DEFINE_EXPR_BINARY_OPERATOR(!=, __not_equal_to)
00417 _DEFINE_EXPR_BINARY_OPERATOR(<, __less)
00418 _DEFINE_EXPR_BINARY_OPERATOR(>, __greater)
00419 _DEFINE_EXPR_BINARY_OPERATOR(<=, __less_equal)
00420 _DEFINE_EXPR_BINARY_OPERATOR(>=, __greater_equal)
00421
00422 #undef _DEFINE_EXPR_BINARY_OPERATOR
00423
00424 #define _DEFINE_EXPR_UNARY_FUNCTION(_Name, _UName) \
00425 template<class _Dom> \
00426 inline _Expr<_UnClos<_UName, _Expr, _Dom>, \
00427 typename _Dom::value_type> \
00428 _Name(const _Expr<_Dom, typename _Dom::value_type>& __e) \
00429 { \
00430 typedef typename _Dom::value_type _Tp; \
00431 typedef _UnClos<_UName, _Expr, _Dom> _Closure; \
00432 return _Expr<_Closure, _Tp>(_Closure(__e())); \
00433 } \
00434 \
00435 template<typename _Tp> \
00436 inline _Expr<_UnClos<_UName, _ValArray, _Tp>, _Tp> \
00437 _Name(const valarray<_Tp>& __v) \
00438 { \
00439 typedef _UnClos<_UName, _ValArray, _Tp> _Closure; \
00440 return _Expr<_Closure, _Tp>(_Closure(__v)); \
00441 }
00442
00443 _DEFINE_EXPR_UNARY_FUNCTION(abs, _Abs)
00444 _DEFINE_EXPR_UNARY_FUNCTION(cos, _Cos)
00445 _DEFINE_EXPR_UNARY_FUNCTION(acos, _Acos)
00446 _DEFINE_EXPR_UNARY_FUNCTION(cosh, _Cosh)
00447 _DEFINE_EXPR_UNARY_FUNCTION(sin, _Sin)
00448 _DEFINE_EXPR_UNARY_FUNCTION(asin, _Asin)
00449 _DEFINE_EXPR_UNARY_FUNCTION(sinh, _Sinh)
00450 _DEFINE_EXPR_UNARY_FUNCTION(tan, _Tan)
00451 _DEFINE_EXPR_UNARY_FUNCTION(tanh, _Tanh)
00452 _DEFINE_EXPR_UNARY_FUNCTION(atan, _Atan)
00453 _DEFINE_EXPR_UNARY_FUNCTION(exp, _Exp)
00454 _DEFINE_EXPR_UNARY_FUNCTION(log, _Log)
00455 _DEFINE_EXPR_UNARY_FUNCTION(log10, _Log10)
00456 _DEFINE_EXPR_UNARY_FUNCTION(sqrt, _Sqrt)
00457
00458 #undef _DEFINE_EXPR_UNARY_FUNCTION
00459
00460 #define _DEFINE_EXPR_BINARY_FUNCTION(_Fun, _UFun) \
00461 template<class _Dom1, class _Dom2> \
00462 inline _Expr<_BinClos<_UFun, _Expr, _Expr, _Dom1, _Dom2>, \
00463 typename _Dom1::value_type> \
00464 _Fun(const _Expr<_Dom1, typename _Dom1::value_type>& __e1, \
00465 const _Expr<_Dom2, typename _Dom2::value_type>& __e2) \
00466 { \
00467 typedef typename _Dom1::value_type _Tp; \
00468 typedef _BinClos<_UFun, _Expr, _Expr, _Dom1, _Dom2> _Closure; \
00469 return _Expr<_Closure, _Tp>(_Closure(__e1(), __e2())); \
00470 } \
00471 \
00472 template<class _Dom> \
00473 inline _Expr<_BinClos<_UFun, _Expr, _ValArray, _Dom, \
00474 typename _Dom::value_type>, \
00475 typename _Dom::value_type> \
00476 _Fun(const _Expr<_Dom, typename _Dom::value_type>& __e, \
00477 const valarray<typename _Dom::value_type>& __v) \
00478 { \
00479 typedef typename _Dom::value_type _Tp; \
00480 typedef _BinClos<_UFun, _Expr, _ValArray, _Dom, _Tp> _Closure; \
00481 return _Expr<_Closure, _Tp>(_Closure(__e(), __v)); \
00482 } \
00483 \
00484 template<class _Dom> \
00485 inline _Expr<_BinClos<_UFun, _ValArray, _Expr, \
00486 typename _Dom::value_type, _Dom>, \
00487 typename _Dom::value_type> \
00488 _Fun(const valarray<typename _Dom::valarray>& __v, \
00489 const _Expr<_Dom, typename _Dom::value_type>& __e) \
00490 { \
00491 typedef typename _Dom::value_type _Tp; \
00492 typedef _BinClos<_UFun, _ValArray, _Expr, _Tp, _Dom> _Closure; \
00493 return _Expr<_Closure, _Tp>(_Closure(__v, __e())); \
00494 } \
00495 \
00496 template<class _Dom> \
00497 inline _Expr<_BinClos<_UFun, _Expr, _Constant, _Dom, \
00498 typename _Dom::value_type>, \
00499 typename _Dom::value_type> \
00500 _Fun(const _Expr<_Dom, typename _Dom::value_type>& __e, \
00501 const typename _Dom::value_type& __t) \
00502 { \
00503 typedef typename _Dom::value_type _Tp; \
00504 typedef _BinClos<_UFun, _Expr, _Constant, _Dom, _Tp> _Closure; \
00505 return _Expr<_Closure, _Tp>(_Closure(__e(), __t)); \
00506 } \
00507 \
00508 template<class _Dom> \
00509 inline _Expr<_BinClos<_UFun, _Constant, _Expr, \
00510 typename _Dom::value_type, _Dom>, \
00511 typename _Dom::value_type> \
00512 _Fun(const typename _Dom::value_type& __t, \
00513 const _Expr<_Dom, typename _Dom::value_type>& __e) \
00514 { \
00515 typedef typename _Dom::value_type _Tp; \
00516 typedef _BinClos<_UFun, _Constant, _Expr, _Tp, _Dom> _Closure; \
00517 return _Expr<_Closure, _Tp>(_Closure(__t, __e())); \
00518 } \
00519 \
00520 template<typename _Tp> \
00521 inline _Expr<_BinClos<_UFun, _ValArray, _ValArray, _Tp, _Tp>, _Tp> \
00522 _Fun(const valarray<_Tp>& __v, const valarray<_Tp>& __w) \
00523 { \
00524 typedef _BinClos<_UFun, _ValArray, _ValArray, _Tp, _Tp> _Closure;\
00525 return _Expr<_Closure, _Tp>(_Closure(__v, __w)); \
00526 } \
00527 \
00528 template<typename _Tp> \
00529 inline _Expr<_BinClos<_UFun, _ValArray, _Constant, _Tp, _Tp>, _Tp> \
00530 _Fun(const valarray<_Tp>& __v, const _Tp& __t) \
00531 { \
00532 typedef _BinClos<_UFun, _ValArray, _Constant, _Tp, _Tp> _Closure;\
00533 return _Expr<_Closure, _Tp>(_Closure(__v, __t)); \
00534 } \
00535 \
00536 template<typename _Tp> \
00537 inline _Expr<_BinClos<_UFun, _Constant, _ValArray, _Tp, _Tp>, _Tp> \
00538 _Fun(const _Tp& __t, const valarray<_Tp>& __v) \
00539 { \
00540 typedef _BinClos<_UFun, _Constant, _ValArray, _Tp, _Tp> _Closure;\
00541 return _Expr<_Closure, _Tp>(_Closure(__t, __v)); \
00542 }
00543
00544 _DEFINE_EXPR_BINARY_FUNCTION(atan2, _Atan2)
00545 _DEFINE_EXPR_BINARY_FUNCTION(pow, _Pow)
00546
00547 #undef _DEFINE_EXPR_BINARY_FUNCTION
00548
00549 _GLIBCXX_END_NAMESPACE_VERSION
00550 }
00551
00552 #endif