1 // The template and inlines for the -*- C++ -*- internal _Meta class.
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
26 /** @file bits/valarray_after.h
27 * This is an internal header file, included by other library headers.
28 * Do not attempt to use it directly. @headername{valarray}
31 // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@cmla.ens-cachan.fr>
33 #ifndef _VALARRAY_AFTER_H
34 #define _VALARRAY_AFTER_H 1
36 #pragma GCC system_header
38 namespace std
_GLIBCXX_VISIBILITY(default)
40 _GLIBCXX_BEGIN_NAMESPACE_VERSION
43 // gslice_array closure.
49 typedef typename
_Dom::value_type value_type
;
51 _GBase (const _Dom
& __e
, const valarray
<size_t>& __i
)
52 : _M_expr (__e
), _M_index(__i
) {}
55 operator[] (size_t __i
) const
56 { return _M_expr
[_M_index
[__i
]]; }
60 { return _M_index
.size(); }
64 const valarray
<size_t>& _M_index
;
67 template<typename _Tp
>
68 class _GBase
<_Array
<_Tp
> >
71 typedef _Tp value_type
;
73 _GBase (_Array
<_Tp
> __a
, const valarray
<size_t>& __i
)
74 : _M_array (__a
), _M_index(__i
) {}
77 operator[] (size_t __i
) const
78 { return _M_array
._M_data
[_M_index
[__i
]]; }
82 { return _M_index
.size(); }
85 const _Array
<_Tp
> _M_array
;
86 const valarray
<size_t>& _M_index
;
90 struct _GClos
<_Expr
, _Dom
>
93 typedef _GBase
<_Dom
> _Base
;
94 typedef typename
_Base::value_type value_type
;
96 _GClos (const _Dom
& __e
, const valarray
<size_t>& __i
)
100 template<typename _Tp
>
101 struct _GClos
<_ValArray
, _Tp
>
102 : _GBase
<_Array
<_Tp
> >
104 typedef _GBase
<_Array
<_Tp
> > _Base
;
105 typedef typename
_Base::value_type value_type
;
107 _GClos (_Array
<_Tp
> __a
, const valarray
<size_t>& __i
)
108 : _Base (__a
, __i
) {}
112 // indirect_array closure
118 typedef typename
_Dom::value_type value_type
;
120 _IBase (const _Dom
& __e
, const valarray
<size_t>& __i
)
121 : _M_expr (__e
), _M_index (__i
) {}
124 operator[] (size_t __i
) const
125 { return _M_expr
[_M_index
[__i
]]; }
129 { return _M_index
.size(); }
133 const valarray
<size_t>& _M_index
;
137 struct _IClos
<_Expr
, _Dom
>
140 typedef _IBase
<_Dom
> _Base
;
141 typedef typename
_Base::value_type value_type
;
143 _IClos (const _Dom
& __e
, const valarray
<size_t>& __i
)
144 : _Base (__e
, __i
) {}
147 template<typename _Tp
>
148 struct _IClos
<_ValArray
, _Tp
>
149 : _IBase
<valarray
<_Tp
> >
151 typedef _IBase
<valarray
<_Tp
> > _Base
;
152 typedef _Tp value_type
;
154 _IClos (const valarray
<_Tp
>& __a
, const valarray
<size_t>& __i
)
155 : _Base (__a
, __i
) {}
161 template<class _Clos
, typename _Tp
>
165 typedef _Tp value_type
;
169 const _Clos
& operator()() const;
171 value_type
operator[](size_t) const;
172 valarray
<value_type
> operator[](slice
) const;
173 valarray
<value_type
> operator[](const gslice
&) const;
174 valarray
<value_type
> operator[](const valarray
<bool>&) const;
175 valarray
<value_type
> operator[](const valarray
<size_t>&) const;
177 _Expr
<_UnClos
<__unary_plus
, std::_Expr
, _Clos
>, value_type
>
180 _Expr
<_UnClos
<__negate
, std::_Expr
, _Clos
>, value_type
>
183 _Expr
<_UnClos
<__bitwise_not
, std::_Expr
, _Clos
>, value_type
>
186 _Expr
<_UnClos
<__logical_not
, std::_Expr
, _Clos
>, bool>
190 value_type
sum() const;
192 valarray
<value_type
> shift(int) const;
193 valarray
<value_type
> cshift(int) const;
195 value_type
min() const;
196 value_type
max() const;
198 valarray
<value_type
> apply(value_type (*)(const value_type
&)) const;
199 valarray
<value_type
> apply(value_type (*)(value_type
)) const;
202 const _Clos _M_closure
;
205 template<class _Clos
, typename _Tp
>
207 _Expr
<_Clos
, _Tp
>::_Expr(const _Clos
& __c
) : _M_closure(__c
) {}
209 template<class _Clos
, typename _Tp
>
211 _Expr
<_Clos
, _Tp
>::operator()() const
212 { return _M_closure
; }
214 template<class _Clos
, typename _Tp
>
216 _Expr
<_Clos
, _Tp
>::operator[](size_t __i
) const
217 { return _M_closure
[__i
]; }
219 template<class _Clos
, typename _Tp
>
221 _Expr
<_Clos
, _Tp
>::operator[](slice __s
) const
223 valarray
<_Tp
> __v
= valarray
<_Tp
>(*this)[__s
];
227 template<class _Clos
, typename _Tp
>
229 _Expr
<_Clos
, _Tp
>::operator[](const gslice
& __gs
) const
231 valarray
<_Tp
> __v
= valarray
<_Tp
>(*this)[__gs
];
235 template<class _Clos
, typename _Tp
>
237 _Expr
<_Clos
, _Tp
>::operator[](const valarray
<bool>& __m
) const
239 valarray
<_Tp
> __v
= valarray
<_Tp
>(*this)[__m
];
243 template<class _Clos
, typename _Tp
>
245 _Expr
<_Clos
, _Tp
>::operator[](const valarray
<size_t>& __i
) const
247 valarray
<_Tp
> __v
= valarray
<_Tp
>(*this)[__i
];
251 template<class _Clos
, typename _Tp
>
253 _Expr
<_Clos
, _Tp
>::size() const
254 { return _M_closure
.size(); }
256 template<class _Clos
, typename _Tp
>
258 _Expr
<_Clos
, _Tp
>::shift(int __n
) const
260 valarray
<_Tp
> __v
= valarray
<_Tp
>(*this).shift(__n
);
264 template<class _Clos
, typename _Tp
>
266 _Expr
<_Clos
, _Tp
>::cshift(int __n
) const
268 valarray
<_Tp
> __v
= valarray
<_Tp
>(*this).cshift(__n
);
272 template<class _Clos
, typename _Tp
>
274 _Expr
<_Clos
, _Tp
>::apply(_Tp
__f(const _Tp
&)) const
276 valarray
<_Tp
> __v
= valarray
<_Tp
>(*this).apply(__f
);
280 template<class _Clos
, typename _Tp
>
282 _Expr
<_Clos
, _Tp
>::apply(_Tp
__f(_Tp
)) const
284 valarray
<_Tp
> __v
= valarray
<_Tp
>(*this).apply(__f
);
288 // XXX: replace this with a more robust summation algorithm.
289 template<class _Clos
, typename _Tp
>
291 _Expr
<_Clos
, _Tp
>::sum() const
293 size_t __n
= _M_closure
.size();
298 _Tp __s
= _M_closure
[--__n
];
300 __s
+= _M_closure
[--__n
];
305 template<class _Clos
, typename _Tp
>
307 _Expr
<_Clos
, _Tp
>::min() const
308 { return __valarray_min(_M_closure
); }
310 template<class _Clos
, typename _Tp
>
312 _Expr
<_Clos
, _Tp
>::max() const
313 { return __valarray_max(_M_closure
); }
315 template<class _Dom
, typename _Tp
>
316 inline _Expr
<_UnClos
<__logical_not
, _Expr
, _Dom
>, bool>
317 _Expr
<_Dom
, _Tp
>::operator!() const
319 typedef _UnClos
<__logical_not
, std::_Expr
, _Dom
> _Closure
;
320 return _Expr
<_Closure
, bool>(_Closure(this->_M_closure
));
323 #define _DEFINE_EXPR_UNARY_OPERATOR(_Op, _Name) \
324 template<class _Dom, typename _Tp> \
325 inline _Expr<_UnClos<_Name, std::_Expr, _Dom>, _Tp> \
326 _Expr<_Dom, _Tp>::operator _Op() const \
328 typedef _UnClos<_Name, std::_Expr, _Dom> _Closure; \
329 return _Expr<_Closure, _Tp>(_Closure(this->_M_closure)); \
332 _DEFINE_EXPR_UNARY_OPERATOR(+, __unary_plus
)
333 _DEFINE_EXPR_UNARY_OPERATOR(-, __negate
)
334 _DEFINE_EXPR_UNARY_OPERATOR(~, __bitwise_not
)
336 #undef _DEFINE_EXPR_UNARY_OPERATOR
338 #define _DEFINE_EXPR_BINARY_OPERATOR(_Op, _Name) \
339 template<class _Dom1, class _Dom2> \
340 inline _Expr<_BinClos<_Name, _Expr, _Expr, _Dom1, _Dom2>, \
341 typename __fun<_Name, typename _Dom1::value_type>::result_type> \
342 operator _Op(const _Expr<_Dom1, typename _Dom1::value_type>& __v, \
343 const _Expr<_Dom2, typename _Dom2::value_type>& __w) \
345 typedef typename _Dom1::value_type _Arg; \
346 typedef typename __fun<_Name, _Arg>::result_type _Value; \
347 typedef _BinClos<_Name, _Expr, _Expr, _Dom1, _Dom2> _Closure; \
348 return _Expr<_Closure, _Value>(_Closure(__v(), __w())); \
351 template<class _Dom> \
352 inline _Expr<_BinClos<_Name, _Expr, _Constant, _Dom, \
353 typename _Dom::value_type>, \
354 typename __fun<_Name, typename _Dom::value_type>::result_type> \
355 operator _Op(const _Expr<_Dom, typename _Dom::value_type>& __v, \
356 const typename _Dom::value_type& __t) \
358 typedef typename _Dom::value_type _Arg; \
359 typedef typename __fun<_Name, _Arg>::result_type _Value; \
360 typedef _BinClos<_Name, _Expr, _Constant, _Dom, _Arg> _Closure; \
361 return _Expr<_Closure, _Value>(_Closure(__v(), __t)); \
364 template<class _Dom> \
365 inline _Expr<_BinClos<_Name, _Constant, _Expr, \
366 typename _Dom::value_type, _Dom>, \
367 typename __fun<_Name, typename _Dom::value_type>::result_type> \
368 operator _Op(const typename _Dom::value_type& __t, \
369 const _Expr<_Dom, typename _Dom::value_type>& __v) \
371 typedef typename _Dom::value_type _Arg; \
372 typedef typename __fun<_Name, _Arg>::result_type _Value; \
373 typedef _BinClos<_Name, _Constant, _Expr, _Arg, _Dom> _Closure; \
374 return _Expr<_Closure, _Value>(_Closure(__t, __v())); \
377 template<class _Dom> \
378 inline _Expr<_BinClos<_Name, _Expr, _ValArray, \
379 _Dom, typename _Dom::value_type>, \
380 typename __fun<_Name, typename _Dom::value_type>::result_type> \
381 operator _Op(const _Expr<_Dom,typename _Dom::value_type>& __e, \
382 const valarray<typename _Dom::value_type>& __v) \
384 typedef typename _Dom::value_type _Arg; \
385 typedef typename __fun<_Name, _Arg>::result_type _Value; \
386 typedef _BinClos<_Name, _Expr, _ValArray, _Dom, _Arg> _Closure; \
387 return _Expr<_Closure, _Value>(_Closure(__e(), __v)); \
390 template<class _Dom> \
391 inline _Expr<_BinClos<_Name, _ValArray, _Expr, \
392 typename _Dom::value_type, _Dom>, \
393 typename __fun<_Name, typename _Dom::value_type>::result_type> \
394 operator _Op(const valarray<typename _Dom::value_type>& __v, \
395 const _Expr<_Dom, typename _Dom::value_type>& __e) \
397 typedef typename _Dom::value_type _Tp; \
398 typedef typename __fun<_Name, _Tp>::result_type _Value; \
399 typedef _BinClos<_Name, _ValArray, _Expr, _Tp, _Dom> _Closure; \
400 return _Expr<_Closure, _Value>(_Closure(__v, __e ())); \
403 _DEFINE_EXPR_BINARY_OPERATOR(+, __plus
)
404 _DEFINE_EXPR_BINARY_OPERATOR(-, __minus
)
405 _DEFINE_EXPR_BINARY_OPERATOR(*, __multiplies
)
406 _DEFINE_EXPR_BINARY_OPERATOR(/, __divides
)
407 _DEFINE_EXPR_BINARY_OPERATOR(%, __modulus
)
408 _DEFINE_EXPR_BINARY_OPERATOR(^, __bitwise_xor
)
409 _DEFINE_EXPR_BINARY_OPERATOR(&, __bitwise_and
)
410 _DEFINE_EXPR_BINARY_OPERATOR(|, __bitwise_or
)
411 _DEFINE_EXPR_BINARY_OPERATOR(<<, __shift_left
)
412 _DEFINE_EXPR_BINARY_OPERATOR(>>, __shift_right
)
413 _DEFINE_EXPR_BINARY_OPERATOR(&&, __logical_and
)
414 _DEFINE_EXPR_BINARY_OPERATOR(||, __logical_or
)
415 _DEFINE_EXPR_BINARY_OPERATOR(==, __equal_to
)
416 _DEFINE_EXPR_BINARY_OPERATOR(!=, __not_equal_to
)
417 _DEFINE_EXPR_BINARY_OPERATOR(<, __less
)
418 _DEFINE_EXPR_BINARY_OPERATOR(>, __greater
)
419 _DEFINE_EXPR_BINARY_OPERATOR(<=, __less_equal
)
420 _DEFINE_EXPR_BINARY_OPERATOR(>=, __greater_equal
)
422 #undef _DEFINE_EXPR_BINARY_OPERATOR
424 #define _DEFINE_EXPR_UNARY_FUNCTION(_Name, _UName) \
425 template<class _Dom> \
426 inline _Expr<_UnClos<_UName, _Expr, _Dom>, \
427 typename _Dom::value_type> \
428 _Name(const _Expr<_Dom, typename _Dom::value_type>& __e) \
430 typedef typename _Dom::value_type _Tp; \
431 typedef _UnClos<_UName, _Expr, _Dom> _Closure; \
432 return _Expr<_Closure, _Tp>(_Closure(__e())); \
435 template<typename _Tp> \
436 inline _Expr<_UnClos<_UName, _ValArray, _Tp>, _Tp> \
437 _Name(const valarray<_Tp>& __v) \
439 typedef _UnClos<_UName, _ValArray, _Tp> _Closure; \
440 return _Expr<_Closure, _Tp>(_Closure(__v)); \
443 _DEFINE_EXPR_UNARY_FUNCTION(abs
, _Abs
)
444 _DEFINE_EXPR_UNARY_FUNCTION(cos
, _Cos
)
445 _DEFINE_EXPR_UNARY_FUNCTION(acos
, _Acos
)
446 _DEFINE_EXPR_UNARY_FUNCTION(cosh
, _Cosh
)
447 _DEFINE_EXPR_UNARY_FUNCTION(sin
, _Sin
)
448 _DEFINE_EXPR_UNARY_FUNCTION(asin
, _Asin
)
449 _DEFINE_EXPR_UNARY_FUNCTION(sinh
, _Sinh
)
450 _DEFINE_EXPR_UNARY_FUNCTION(tan
, _Tan
)
451 _DEFINE_EXPR_UNARY_FUNCTION(tanh
, _Tanh
)
452 _DEFINE_EXPR_UNARY_FUNCTION(atan
, _Atan
)
453 _DEFINE_EXPR_UNARY_FUNCTION(exp
, _Exp
)
454 _DEFINE_EXPR_UNARY_FUNCTION(log
, _Log
)
455 _DEFINE_EXPR_UNARY_FUNCTION(log10
, _Log10
)
456 _DEFINE_EXPR_UNARY_FUNCTION(sqrt
, _Sqrt
)
458 #undef _DEFINE_EXPR_UNARY_FUNCTION
460 #define _DEFINE_EXPR_BINARY_FUNCTION(_Fun, _UFun) \
461 template<class _Dom1, class _Dom2> \
462 inline _Expr<_BinClos<_UFun, _Expr, _Expr, _Dom1, _Dom2>, \
463 typename _Dom1::value_type> \
464 _Fun(const _Expr<_Dom1, typename _Dom1::value_type>& __e1, \
465 const _Expr<_Dom2, typename _Dom2::value_type>& __e2) \
467 typedef typename _Dom1::value_type _Tp; \
468 typedef _BinClos<_UFun, _Expr, _Expr, _Dom1, _Dom2> _Closure; \
469 return _Expr<_Closure, _Tp>(_Closure(__e1(), __e2())); \
472 template<class _Dom> \
473 inline _Expr<_BinClos<_UFun, _Expr, _ValArray, _Dom, \
474 typename _Dom::value_type>, \
475 typename _Dom::value_type> \
476 _Fun(const _Expr<_Dom, typename _Dom::value_type>& __e, \
477 const valarray<typename _Dom::value_type>& __v) \
479 typedef typename _Dom::value_type _Tp; \
480 typedef _BinClos<_UFun, _Expr, _ValArray, _Dom, _Tp> _Closure; \
481 return _Expr<_Closure, _Tp>(_Closure(__e(), __v)); \
484 template<class _Dom> \
485 inline _Expr<_BinClos<_UFun, _ValArray, _Expr, \
486 typename _Dom::value_type, _Dom>, \
487 typename _Dom::value_type> \
488 _Fun(const valarray<typename _Dom::valarray>& __v, \
489 const _Expr<_Dom, typename _Dom::value_type>& __e) \
491 typedef typename _Dom::value_type _Tp; \
492 typedef _BinClos<_UFun, _ValArray, _Expr, _Tp, _Dom> _Closure; \
493 return _Expr<_Closure, _Tp>(_Closure(__v, __e())); \
496 template<class _Dom> \
497 inline _Expr<_BinClos<_UFun, _Expr, _Constant, _Dom, \
498 typename _Dom::value_type>, \
499 typename _Dom::value_type> \
500 _Fun(const _Expr<_Dom, typename _Dom::value_type>& __e, \
501 const typename _Dom::value_type& __t) \
503 typedef typename _Dom::value_type _Tp; \
504 typedef _BinClos<_UFun, _Expr, _Constant, _Dom, _Tp> _Closure; \
505 return _Expr<_Closure, _Tp>(_Closure(__e(), __t)); \
508 template<class _Dom> \
509 inline _Expr<_BinClos<_UFun, _Constant, _Expr, \
510 typename _Dom::value_type, _Dom>, \
511 typename _Dom::value_type> \
512 _Fun(const typename _Dom::value_type& __t, \
513 const _Expr<_Dom, typename _Dom::value_type>& __e) \
515 typedef typename _Dom::value_type _Tp; \
516 typedef _BinClos<_UFun, _Constant, _Expr, _Tp, _Dom> _Closure; \
517 return _Expr<_Closure, _Tp>(_Closure(__t, __e())); \
520 template<typename _Tp> \
521 inline _Expr<_BinClos<_UFun, _ValArray, _ValArray, _Tp, _Tp>, _Tp> \
522 _Fun(const valarray<_Tp>& __v, const valarray<_Tp>& __w) \
524 typedef _BinClos<_UFun, _ValArray, _ValArray, _Tp, _Tp> _Closure;\
525 return _Expr<_Closure, _Tp>(_Closure(__v, __w)); \
528 template<typename _Tp> \
529 inline _Expr<_BinClos<_UFun, _ValArray, _Constant, _Tp, _Tp>, _Tp> \
530 _Fun(const valarray<_Tp>& __v, const _Tp& __t) \
532 typedef _BinClos<_UFun, _ValArray, _Constant, _Tp, _Tp> _Closure;\
533 return _Expr<_Closure, _Tp>(_Closure(__v, __t)); \
536 template<typename _Tp> \
537 inline _Expr<_BinClos<_UFun, _Constant, _ValArray, _Tp, _Tp>, _Tp> \
538 _Fun(const _Tp& __t, const valarray<_Tp>& __v) \
540 typedef _BinClos<_UFun, _Constant, _ValArray, _Tp, _Tp> _Closure;\
541 return _Expr<_Closure, _Tp>(_Closure(__t, __v)); \
544 _DEFINE_EXPR_BINARY_FUNCTION(atan2
, _Atan2
)
545 _DEFINE_EXPR_BINARY_FUNCTION(pow
, _Pow
)
547 #undef _DEFINE_EXPR_BINARY_FUNCTION
549 _GLIBCXX_END_NAMESPACE_VERSION
552 #endif /* _CPP_VALARRAY_AFTER_H */