1 // The template and inlines for the -*- C++ -*- internal _Meta class.
3 // Copyright (C) 1997-2020 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file bits/valarray_after.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{valarray}
30 // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@cmla.ens-cachan.fr>
32 #ifndef _VALARRAY_AFTER_H
33 #define _VALARRAY_AFTER_H 1
35 #pragma GCC system_header
37 namespace std
_GLIBCXX_VISIBILITY(default)
39 _GLIBCXX_BEGIN_NAMESPACE_VERSION
44 // gslice_array closure.
50 typedef typename
_Dom::value_type value_type
;
52 _GBase (const _Dom
& __e
, const valarray
<size_t>& __i
)
53 : _M_expr (__e
), _M_index(__i
) {}
56 operator[] (size_t __i
) const
57 { return _M_expr
[_M_index
[__i
]]; }
61 { return _M_index
.size(); }
64 typename _ValArrayRef
<_Dom
>::__type _M_expr
;
65 const valarray
<size_t>& _M_index
;
68 template<typename _Tp
>
69 class _GBase
<_Array
<_Tp
> >
72 typedef _Tp value_type
;
74 _GBase (_Array
<_Tp
> __a
, const valarray
<size_t>& __i
)
75 : _M_array (__a
), _M_index(__i
) {}
78 operator[] (size_t __i
) const
79 { return _M_array
._M_data
[_M_index
[__i
]]; }
83 { return _M_index
.size(); }
86 const _Array
<_Tp
> _M_array
;
87 const valarray
<size_t>& _M_index
;
91 struct _GClos
<_Expr
, _Dom
>
94 typedef _GBase
<_Dom
> _Base
;
95 typedef typename
_Base::value_type value_type
;
97 _GClos (const _Dom
& __e
, const valarray
<size_t>& __i
)
101 template<typename _Tp
>
102 struct _GClos
<_ValArray
, _Tp
>
103 : _GBase
<_Array
<_Tp
> >
105 typedef _GBase
<_Array
<_Tp
> > _Base
;
106 typedef typename
_Base::value_type value_type
;
108 _GClos (_Array
<_Tp
> __a
, const valarray
<size_t>& __i
)
109 : _Base (__a
, __i
) {}
113 // indirect_array closure
119 typedef typename
_Dom::value_type value_type
;
121 _IBase (const _Dom
& __e
, const valarray
<size_t>& __i
)
122 : _M_expr (__e
), _M_index (__i
) {}
125 operator[] (size_t __i
) const
126 { return _M_expr
[_M_index
[__i
]]; }
130 { return _M_index
.size(); }
133 typename _ValArrayRef
<_Dom
>::__type _M_expr
;
134 const valarray
<size_t>& _M_index
;
138 struct _IClos
<_Expr
, _Dom
>
141 typedef _IBase
<_Dom
> _Base
;
142 typedef typename
_Base::value_type value_type
;
144 _IClos (const _Dom
& __e
, const valarray
<size_t>& __i
)
145 : _Base (__e
, __i
) {}
148 template<typename _Tp
>
149 struct _IClos
<_ValArray
, _Tp
>
150 : _IBase
<valarray
<_Tp
> >
152 typedef _IBase
<valarray
<_Tp
> > _Base
;
153 typedef _Tp value_type
;
155 _IClos (const valarray
<_Tp
>& __a
, const valarray
<size_t>& __i
)
156 : _Base (__a
, __i
) {}
158 } // namespace __detail
163 template<class _Clos
, typename _Tp
>
167 typedef _Tp value_type
;
171 const _Clos
& operator()() const;
173 value_type
operator[](size_t) const;
174 valarray
<value_type
> operator[](slice
) const;
175 valarray
<value_type
> operator[](const gslice
&) const;
176 valarray
<value_type
> operator[](const valarray
<bool>&) const;
177 valarray
<value_type
> operator[](const valarray
<size_t>&) const;
179 _Expr
<_UnClos
<__unary_plus
, std::_Expr
, _Clos
>, value_type
>
182 _Expr
<_UnClos
<__negate
, std::_Expr
, _Clos
>, value_type
>
185 _Expr
<_UnClos
<__bitwise_not
, std::_Expr
, _Clos
>, value_type
>
188 _Expr
<_UnClos
<__logical_not
, std::_Expr
, _Clos
>, bool>
192 value_type
sum() const;
194 valarray
<value_type
> shift(int) const;
195 valarray
<value_type
> cshift(int) const;
197 value_type
min() const;
198 value_type
max() const;
200 valarray
<value_type
> apply(value_type (*)(const value_type
&)) const;
201 valarray
<value_type
> apply(value_type (*)(value_type
)) const;
204 const _Clos _M_closure
;
207 template<class _Clos
, typename _Tp
>
209 _Expr
<_Clos
, _Tp
>::_Expr(const _Clos
& __c
) : _M_closure(__c
) {}
211 template<class _Clos
, typename _Tp
>
213 _Expr
<_Clos
, _Tp
>::operator()() const
214 { return _M_closure
; }
216 template<class _Clos
, typename _Tp
>
218 _Expr
<_Clos
, _Tp
>::operator[](size_t __i
) const
219 { return _M_closure
[__i
]; }
221 template<class _Clos
, typename _Tp
>
223 _Expr
<_Clos
, _Tp
>::operator[](slice __s
) const
225 valarray
<_Tp
> __v
= valarray
<_Tp
>(*this)[__s
];
229 template<class _Clos
, typename _Tp
>
231 _Expr
<_Clos
, _Tp
>::operator[](const gslice
& __gs
) const
233 valarray
<_Tp
> __v
= valarray
<_Tp
>(*this)[__gs
];
237 template<class _Clos
, typename _Tp
>
239 _Expr
<_Clos
, _Tp
>::operator[](const valarray
<bool>& __m
) const
241 valarray
<_Tp
> __v
= valarray
<_Tp
>(*this)[__m
];
245 template<class _Clos
, typename _Tp
>
247 _Expr
<_Clos
, _Tp
>::operator[](const valarray
<size_t>& __i
) const
249 valarray
<_Tp
> __v
= valarray
<_Tp
>(*this)[__i
];
253 template<class _Clos
, typename _Tp
>
255 _Expr
<_Clos
, _Tp
>::size() const
256 { return _M_closure
.size(); }
258 template<class _Clos
, typename _Tp
>
260 _Expr
<_Clos
, _Tp
>::shift(int __n
) const
262 valarray
<_Tp
> __v
= valarray
<_Tp
>(*this).shift(__n
);
266 template<class _Clos
, typename _Tp
>
268 _Expr
<_Clos
, _Tp
>::cshift(int __n
) const
270 valarray
<_Tp
> __v
= valarray
<_Tp
>(*this).cshift(__n
);
274 template<class _Clos
, typename _Tp
>
276 _Expr
<_Clos
, _Tp
>::apply(_Tp
__f(const _Tp
&)) const
278 valarray
<_Tp
> __v
= valarray
<_Tp
>(*this).apply(__f
);
282 template<class _Clos
, typename _Tp
>
284 _Expr
<_Clos
, _Tp
>::apply(_Tp
__f(_Tp
)) const
286 valarray
<_Tp
> __v
= valarray
<_Tp
>(*this).apply(__f
);
290 // XXX: replace this with a more robust summation algorithm.
291 template<class _Clos
, typename _Tp
>
293 _Expr
<_Clos
, _Tp
>::sum() const
295 size_t __n
= _M_closure
.size();
300 _Tp __s
= _M_closure
[--__n
];
302 __s
+= _M_closure
[--__n
];
307 template<class _Clos
, typename _Tp
>
309 _Expr
<_Clos
, _Tp
>::min() const
310 { return __valarray_min(_M_closure
); }
312 template<class _Clos
, typename _Tp
>
314 _Expr
<_Clos
, _Tp
>::max() const
315 { return __valarray_max(_M_closure
); }
317 template<class _Dom
, typename _Tp
>
318 inline _Expr
<_UnClos
<__logical_not
, _Expr
, _Dom
>, bool>
319 _Expr
<_Dom
, _Tp
>::operator!() const
321 typedef _UnClos
<__logical_not
, std::_Expr
, _Dom
> _Closure
;
322 return _Expr
<_Closure
, bool>(_Closure(this->_M_closure
));
325 #define _DEFINE_EXPR_UNARY_OPERATOR(_Op, _Name) \
326 template<class _Dom, typename _Tp> \
327 inline _Expr<_UnClos<_Name, std::_Expr, _Dom>, _Tp> \
328 _Expr<_Dom, _Tp>::operator _Op() const \
330 typedef _UnClos<_Name, std::_Expr, _Dom> _Closure; \
331 return _Expr<_Closure, _Tp>(_Closure(this->_M_closure)); \
334 _DEFINE_EXPR_UNARY_OPERATOR(+, __unary_plus
)
335 _DEFINE_EXPR_UNARY_OPERATOR(-, __negate
)
336 _DEFINE_EXPR_UNARY_OPERATOR(~, __bitwise_not
)
338 #undef _DEFINE_EXPR_UNARY_OPERATOR
340 #define _DEFINE_EXPR_BINARY_OPERATOR(_Op, _Name) \
341 template<class _Dom1, class _Dom2> \
342 inline _Expr<_BinClos<_Name, _Expr, _Expr, _Dom1, _Dom2>, \
343 typename __fun<_Name, typename _Dom1::value_type>::result_type> \
344 operator _Op(const _Expr<_Dom1, typename _Dom1::value_type>& __v, \
345 const _Expr<_Dom2, typename _Dom2::value_type>& __w) \
347 typedef typename _Dom1::value_type _Arg; \
348 typedef typename __fun<_Name, _Arg>::result_type _Value; \
349 typedef _BinClos<_Name, _Expr, _Expr, _Dom1, _Dom2> _Closure; \
350 return _Expr<_Closure, _Value>(_Closure(__v(), __w())); \
353 template<class _Dom> \
354 inline _Expr<_BinClos<_Name, _Expr, _Constant, _Dom, \
355 typename _Dom::value_type>, \
356 typename __fun<_Name, typename _Dom::value_type>::result_type> \
357 operator _Op(const _Expr<_Dom, typename _Dom::value_type>& __v, \
358 const typename _Dom::value_type& __t) \
360 typedef typename _Dom::value_type _Arg; \
361 typedef typename __fun<_Name, _Arg>::result_type _Value; \
362 typedef _BinClos<_Name, _Expr, _Constant, _Dom, _Arg> _Closure; \
363 return _Expr<_Closure, _Value>(_Closure(__v(), __t)); \
366 template<class _Dom> \
367 inline _Expr<_BinClos<_Name, _Constant, _Expr, \
368 typename _Dom::value_type, _Dom>, \
369 typename __fun<_Name, typename _Dom::value_type>::result_type> \
370 operator _Op(const typename _Dom::value_type& __t, \
371 const _Expr<_Dom, typename _Dom::value_type>& __v) \
373 typedef typename _Dom::value_type _Arg; \
374 typedef typename __fun<_Name, _Arg>::result_type _Value; \
375 typedef _BinClos<_Name, _Constant, _Expr, _Arg, _Dom> _Closure; \
376 return _Expr<_Closure, _Value>(_Closure(__t, __v())); \
379 template<class _Dom> \
380 inline _Expr<_BinClos<_Name, _Expr, _ValArray, \
381 _Dom, typename _Dom::value_type>, \
382 typename __fun<_Name, typename _Dom::value_type>::result_type> \
383 operator _Op(const _Expr<_Dom,typename _Dom::value_type>& __e, \
384 const valarray<typename _Dom::value_type>& __v) \
386 typedef typename _Dom::value_type _Arg; \
387 typedef typename __fun<_Name, _Arg>::result_type _Value; \
388 typedef _BinClos<_Name, _Expr, _ValArray, _Dom, _Arg> _Closure; \
389 return _Expr<_Closure, _Value>(_Closure(__e(), __v)); \
392 template<class _Dom> \
393 inline _Expr<_BinClos<_Name, _ValArray, _Expr, \
394 typename _Dom::value_type, _Dom>, \
395 typename __fun<_Name, typename _Dom::value_type>::result_type> \
396 operator _Op(const valarray<typename _Dom::value_type>& __v, \
397 const _Expr<_Dom, typename _Dom::value_type>& __e) \
399 typedef typename _Dom::value_type _Tp; \
400 typedef typename __fun<_Name, _Tp>::result_type _Value; \
401 typedef _BinClos<_Name, _ValArray, _Expr, _Tp, _Dom> _Closure; \
402 return _Expr<_Closure, _Value>(_Closure(__v, __e ())); \
405 _DEFINE_EXPR_BINARY_OPERATOR(+, __plus
)
406 _DEFINE_EXPR_BINARY_OPERATOR(-, __minus
)
407 _DEFINE_EXPR_BINARY_OPERATOR(*, __multiplies
)
408 _DEFINE_EXPR_BINARY_OPERATOR(/, __divides
)
409 _DEFINE_EXPR_BINARY_OPERATOR(%, __modulus
)
410 _DEFINE_EXPR_BINARY_OPERATOR(^, __bitwise_xor
)
411 _DEFINE_EXPR_BINARY_OPERATOR(&, __bitwise_and
)
412 _DEFINE_EXPR_BINARY_OPERATOR(|, __bitwise_or
)
413 _DEFINE_EXPR_BINARY_OPERATOR(<<, __shift_left
)
414 _DEFINE_EXPR_BINARY_OPERATOR(>>, __shift_right
)
415 _DEFINE_EXPR_BINARY_OPERATOR(&&, __logical_and
)
416 _DEFINE_EXPR_BINARY_OPERATOR(||, __logical_or
)
417 _DEFINE_EXPR_BINARY_OPERATOR(==, __equal_to
)
418 _DEFINE_EXPR_BINARY_OPERATOR(!=, __not_equal_to
)
419 _DEFINE_EXPR_BINARY_OPERATOR(<, __less
)
420 _DEFINE_EXPR_BINARY_OPERATOR(>, __greater
)
421 _DEFINE_EXPR_BINARY_OPERATOR(<=, __less_equal
)
422 _DEFINE_EXPR_BINARY_OPERATOR(>=, __greater_equal
)
424 #undef _DEFINE_EXPR_BINARY_OPERATOR
426 #define _DEFINE_EXPR_UNARY_FUNCTION(_Name, _UName) \
427 template<class _Dom> \
428 inline _Expr<_UnClos<_UName, _Expr, _Dom>, \
429 typename _Dom::value_type> \
430 _Name(const _Expr<_Dom, typename _Dom::value_type>& __e) \
432 typedef typename _Dom::value_type _Tp; \
433 typedef _UnClos<_UName, _Expr, _Dom> _Closure; \
434 return _Expr<_Closure, _Tp>(_Closure(__e())); \
437 template<typename _Tp> \
438 inline _Expr<_UnClos<_UName, _ValArray, _Tp>, _Tp> \
439 _Name(const valarray<_Tp>& __v) \
441 typedef _UnClos<_UName, _ValArray, _Tp> _Closure; \
442 return _Expr<_Closure, _Tp>(_Closure(__v)); \
445 _DEFINE_EXPR_UNARY_FUNCTION(abs
, _Abs
)
446 _DEFINE_EXPR_UNARY_FUNCTION(cos
, _Cos
)
447 _DEFINE_EXPR_UNARY_FUNCTION(acos
, _Acos
)
448 _DEFINE_EXPR_UNARY_FUNCTION(cosh
, _Cosh
)
449 _DEFINE_EXPR_UNARY_FUNCTION(sin
, _Sin
)
450 _DEFINE_EXPR_UNARY_FUNCTION(asin
, _Asin
)
451 _DEFINE_EXPR_UNARY_FUNCTION(sinh
, _Sinh
)
452 _DEFINE_EXPR_UNARY_FUNCTION(tan
, _Tan
)
453 _DEFINE_EXPR_UNARY_FUNCTION(tanh
, _Tanh
)
454 _DEFINE_EXPR_UNARY_FUNCTION(atan
, _Atan
)
455 _DEFINE_EXPR_UNARY_FUNCTION(exp
, _Exp
)
456 _DEFINE_EXPR_UNARY_FUNCTION(log
, _Log
)
457 _DEFINE_EXPR_UNARY_FUNCTION(log10
, _Log10
)
458 _DEFINE_EXPR_UNARY_FUNCTION(sqrt
, _Sqrt
)
460 #undef _DEFINE_EXPR_UNARY_FUNCTION
462 #define _DEFINE_EXPR_BINARY_FUNCTION(_Fun, _UFun) \
463 template<class _Dom1, class _Dom2> \
464 inline _Expr<_BinClos<_UFun, _Expr, _Expr, _Dom1, _Dom2>, \
465 typename _Dom1::value_type> \
466 _Fun(const _Expr<_Dom1, typename _Dom1::value_type>& __e1, \
467 const _Expr<_Dom2, typename _Dom2::value_type>& __e2) \
469 typedef typename _Dom1::value_type _Tp; \
470 typedef _BinClos<_UFun, _Expr, _Expr, _Dom1, _Dom2> _Closure; \
471 return _Expr<_Closure, _Tp>(_Closure(__e1(), __e2())); \
474 template<class _Dom> \
475 inline _Expr<_BinClos<_UFun, _Expr, _ValArray, _Dom, \
476 typename _Dom::value_type>, \
477 typename _Dom::value_type> \
478 _Fun(const _Expr<_Dom, typename _Dom::value_type>& __e, \
479 const valarray<typename _Dom::value_type>& __v) \
481 typedef typename _Dom::value_type _Tp; \
482 typedef _BinClos<_UFun, _Expr, _ValArray, _Dom, _Tp> _Closure; \
483 return _Expr<_Closure, _Tp>(_Closure(__e(), __v)); \
486 template<class _Dom> \
487 inline _Expr<_BinClos<_UFun, _ValArray, _Expr, \
488 typename _Dom::value_type, _Dom>, \
489 typename _Dom::value_type> \
490 _Fun(const valarray<typename _Dom::valarray>& __v, \
491 const _Expr<_Dom, typename _Dom::value_type>& __e) \
493 typedef typename _Dom::value_type _Tp; \
494 typedef _BinClos<_UFun, _ValArray, _Expr, _Tp, _Dom> _Closure; \
495 return _Expr<_Closure, _Tp>(_Closure(__v, __e())); \
498 template<class _Dom> \
499 inline _Expr<_BinClos<_UFun, _Expr, _Constant, _Dom, \
500 typename _Dom::value_type>, \
501 typename _Dom::value_type> \
502 _Fun(const _Expr<_Dom, typename _Dom::value_type>& __e, \
503 const typename _Dom::value_type& __t) \
505 typedef typename _Dom::value_type _Tp; \
506 typedef _BinClos<_UFun, _Expr, _Constant, _Dom, _Tp> _Closure; \
507 return _Expr<_Closure, _Tp>(_Closure(__e(), __t)); \
510 template<class _Dom> \
511 inline _Expr<_BinClos<_UFun, _Constant, _Expr, \
512 typename _Dom::value_type, _Dom>, \
513 typename _Dom::value_type> \
514 _Fun(const typename _Dom::value_type& __t, \
515 const _Expr<_Dom, typename _Dom::value_type>& __e) \
517 typedef typename _Dom::value_type _Tp; \
518 typedef _BinClos<_UFun, _Constant, _Expr, _Tp, _Dom> _Closure; \
519 return _Expr<_Closure, _Tp>(_Closure(__t, __e())); \
522 template<typename _Tp> \
523 inline _Expr<_BinClos<_UFun, _ValArray, _ValArray, _Tp, _Tp>, _Tp> \
524 _Fun(const valarray<_Tp>& __v, const valarray<_Tp>& __w) \
526 typedef _BinClos<_UFun, _ValArray, _ValArray, _Tp, _Tp> _Closure;\
527 return _Expr<_Closure, _Tp>(_Closure(__v, __w)); \
530 template<typename _Tp> \
531 inline _Expr<_BinClos<_UFun, _ValArray, _Constant, _Tp, _Tp>, _Tp> \
532 _Fun(const valarray<_Tp>& __v, \
533 const typename valarray<_Tp>::value_type& __t) \
535 typedef _BinClos<_UFun, _ValArray, _Constant, _Tp, _Tp> _Closure;\
536 return _Expr<_Closure, _Tp>(_Closure(__v, __t)); \
539 template<typename _Tp> \
540 inline _Expr<_BinClos<_UFun, _Constant, _ValArray, _Tp, _Tp>, _Tp> \
541 _Fun(const typename valarray<_Tp>::value_type& __t, \
542 const valarray<_Tp>& __v) \
544 typedef _BinClos<_UFun, _Constant, _ValArray, _Tp, _Tp> _Closure;\
545 return _Expr<_Closure, _Tp>(_Closure(__t, __v)); \
548 _DEFINE_EXPR_BINARY_FUNCTION(atan2
, _Atan2
)
549 _DEFINE_EXPR_BINARY_FUNCTION(pow
, _Pow
)
551 #undef _DEFINE_EXPR_BINARY_FUNCTION
553 _GLIBCXX_END_NAMESPACE_VERSION
556 #endif /* _CPP_VALARRAY_AFTER_H */