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, 2011 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_before.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_BEFORE_H
34 #define _VALARRAY_BEFORE_H 1
36 #pragma GCC system_header
38 #include <bits/slice_array.h>
40 namespace std
_GLIBCXX_VISIBILITY(default)
42 _GLIBCXX_BEGIN_NAMESPACE_VERSION
45 // Implementing a loosened valarray return value is tricky.
46 // First we need to meet 26.3.1/3: we should not add more than
47 // two levels of template nesting. Therefore we resort to template
48 // template to "flatten" loosened return value types.
49 // At some point we use partial specialization to remove one level
50 // template nesting due to _Expr<>
53 // This class is NOT defined. It doesn't need to.
54 template<typename _Tp1
, typename _Tp2
> class _Constant
;
56 // Implementations of unary functions applied to valarray<>s.
57 // I use hard-coded object functions here instead of a generic
58 // approach like pointers to function:
59 // 1) correctness: some functions take references, others values.
60 // we can't deduce the correct type afterwards.
61 // 2) efficiency -- object functions can be easily inlined
62 // 3) be Koenig-lookup-friendly
66 template<typename _Tp
>
67 _Tp
operator()(const _Tp
& __t
) const
73 template<typename _Tp
>
74 _Tp
operator()(const _Tp
& __t
) const
80 template<typename _Tp
>
81 _Tp
operator()(const _Tp
& __t
) const
87 template<typename _Tp
>
88 _Tp
operator()(const _Tp
& __t
) const
94 template<typename _Tp
>
95 _Tp
operator()(const _Tp
& __t
) const
101 template<typename _Tp
>
102 _Tp
operator()(const _Tp
& __t
) const
103 { return asin(__t
); }
108 template<typename _Tp
>
109 _Tp
operator()(const _Tp
& __t
) const
110 { return sinh(__t
); }
115 template<typename _Tp
>
116 _Tp
operator()(const _Tp
& __t
) const
122 template<typename _Tp
>
123 _Tp
operator()(const _Tp
& __t
) const
124 { return atan(__t
); }
129 template<typename _Tp
>
130 _Tp
operator()(const _Tp
& __t
) const
131 { return tanh(__t
); }
136 template<typename _Tp
>
137 _Tp
operator()(const _Tp
& __t
) const
143 template<typename _Tp
>
144 _Tp
operator()(const _Tp
& __t
) const
150 template<typename _Tp
>
151 _Tp
operator()(const _Tp
& __t
) const
152 { return log10(__t
); }
157 template<typename _Tp
>
158 _Tp
operator()(const _Tp
& __t
) const
159 { return sqrt(__t
); }
162 // In the past, we used to tailor operator applications semantics
163 // to the specialization of standard function objects (i.e. plus<>, etc.)
164 // That is incorrect. Therefore we provide our own surrogates.
168 template<typename _Tp
>
169 _Tp
operator()(const _Tp
& __t
) const
175 template<typename _Tp
>
176 _Tp
operator()(const _Tp
& __t
) const
182 template<typename _Tp
>
183 _Tp
operator()(const _Tp
& __t
) const
189 template<typename _Tp
>
190 _Tp
operator()(const _Tp
& __x
, const _Tp
& __y
) const
191 { return __x
+ __y
; }
196 template<typename _Tp
>
197 _Tp
operator()(const _Tp
& __x
, const _Tp
& __y
) const
198 { return __x
- __y
; }
203 template<typename _Tp
>
204 _Tp
operator()(const _Tp
& __x
, const _Tp
& __y
) const
205 { return __x
* __y
; }
210 template<typename _Tp
>
211 _Tp
operator()(const _Tp
& __x
, const _Tp
& __y
) const
212 { return __x
/ __y
; }
217 template<typename _Tp
>
218 _Tp
operator()(const _Tp
& __x
, const _Tp
& __y
) const
219 { return __x
% __y
; }
224 template<typename _Tp
>
225 _Tp
operator()(const _Tp
& __x
, const _Tp
& __y
) const
226 { return __x
^ __y
; }
231 template<typename _Tp
>
232 _Tp
operator()(const _Tp
& __x
, const _Tp
& __y
) const
233 { return __x
& __y
; }
238 template<typename _Tp
>
239 _Tp
operator()(const _Tp
& __x
, const _Tp
& __y
) const
240 { return __x
| __y
; }
245 template<typename _Tp
>
246 _Tp
operator()(const _Tp
& __x
, const _Tp
& __y
) const
247 { return __x
<< __y
; }
252 template<typename _Tp
>
253 _Tp
operator()(const _Tp
& __x
, const _Tp
& __y
) const
254 { return __x
>> __y
; }
259 template<typename _Tp
>
260 bool operator()(const _Tp
& __x
, const _Tp
& __y
) const
261 { return __x
&& __y
; }
266 template<typename _Tp
>
267 bool operator()(const _Tp
& __x
, const _Tp
& __y
) const
268 { return __x
|| __y
; }
273 template<typename _Tp
>
274 bool operator()(const _Tp
& __x
) const
280 template<typename _Tp
>
281 bool operator()(const _Tp
& __x
, const _Tp
& __y
) const
282 { return __x
== __y
; }
285 struct __not_equal_to
287 template<typename _Tp
>
288 bool operator()(const _Tp
& __x
, const _Tp
& __y
) const
289 { return __x
!= __y
; }
294 template<typename _Tp
>
295 bool operator()(const _Tp
& __x
, const _Tp
& __y
) const
296 { return __x
< __y
; }
301 template<typename _Tp
>
302 bool operator()(const _Tp
& __x
, const _Tp
& __y
) const
303 { return __x
> __y
; }
308 template<typename _Tp
>
309 bool operator()(const _Tp
& __x
, const _Tp
& __y
) const
310 { return __x
<= __y
; }
313 struct __greater_equal
315 template<typename _Tp
>
316 bool operator()(const _Tp
& __x
, const _Tp
& __y
) const
317 { return __x
>= __y
; }
320 // The few binary functions we miss.
323 template<typename _Tp
>
324 _Tp
operator()(const _Tp
& __x
, const _Tp
& __y
) const
325 { return atan2(__x
, __y
); }
330 template<typename _Tp
>
331 _Tp
operator()(const _Tp
& __x
, const _Tp
& __y
) const
332 { return pow(__x
, __y
); }
336 // We need these bits in order to recover the return type of
337 // some functions/operators now that we're no longer using
338 // function templates.
339 template<typename
, typename _Tp
>
342 typedef _Tp result_type
;
345 // several specializations for relational operators.
346 template<typename _Tp
>
347 struct __fun
<__logical_not
, _Tp
>
349 typedef bool result_type
;
352 template<typename _Tp
>
353 struct __fun
<__logical_and
, _Tp
>
355 typedef bool result_type
;
358 template<typename _Tp
>
359 struct __fun
<__logical_or
, _Tp
>
361 typedef bool result_type
;
364 template<typename _Tp
>
365 struct __fun
<__less
, _Tp
>
367 typedef bool result_type
;
370 template<typename _Tp
>
371 struct __fun
<__greater
, _Tp
>
373 typedef bool result_type
;
376 template<typename _Tp
>
377 struct __fun
<__less_equal
, _Tp
>
379 typedef bool result_type
;
382 template<typename _Tp
>
383 struct __fun
<__greater_equal
, _Tp
>
385 typedef bool result_type
;
388 template<typename _Tp
>
389 struct __fun
<__equal_to
, _Tp
>
391 typedef bool result_type
;
394 template<typename _Tp
>
395 struct __fun
<__not_equal_to
, _Tp
>
397 typedef bool result_type
;
401 // Apply function taking a value/const reference closure
404 template<typename _Dom
, typename _Arg
>
408 typedef typename
_Dom::value_type value_type
;
410 _FunBase(const _Dom
& __e
, value_type
__f(_Arg
))
411 : _M_expr(__e
), _M_func(__f
) {}
413 value_type
operator[](size_t __i
) const
414 { return _M_func (_M_expr
[__i
]); }
416 size_t size() const { return _M_expr
.size ();}
420 value_type (*_M_func
)(_Arg
);
424 struct _ValFunClos
<_Expr
,_Dom
> : _FunBase
<_Dom
, typename
_Dom::value_type
>
426 typedef _FunBase
<_Dom
, typename
_Dom::value_type
> _Base
;
427 typedef typename
_Base::value_type value_type
;
428 typedef value_type _Tp
;
430 _ValFunClos(const _Dom
& __e
, _Tp
__f(_Tp
)) : _Base(__e
, __f
) {}
433 template<typename _Tp
>
434 struct _ValFunClos
<_ValArray
,_Tp
> : _FunBase
<valarray
<_Tp
>, _Tp
>
436 typedef _FunBase
<valarray
<_Tp
>, _Tp
> _Base
;
437 typedef _Tp value_type
;
439 _ValFunClos(const valarray
<_Tp
>& __v
, _Tp
__f(_Tp
)) : _Base(__v
, __f
) {}
443 struct _RefFunClos
<_Expr
, _Dom
>
444 : _FunBase
<_Dom
, const typename
_Dom::value_type
&>
446 typedef _FunBase
<_Dom
, const typename
_Dom::value_type
&> _Base
;
447 typedef typename
_Base::value_type value_type
;
448 typedef value_type _Tp
;
450 _RefFunClos(const _Dom
& __e
, _Tp
__f(const _Tp
&))
454 template<typename _Tp
>
455 struct _RefFunClos
<_ValArray
, _Tp
>
456 : _FunBase
<valarray
<_Tp
>, const _Tp
&>
458 typedef _FunBase
<valarray
<_Tp
>, const _Tp
&> _Base
;
459 typedef _Tp value_type
;
461 _RefFunClos(const valarray
<_Tp
>& __v
, _Tp
__f(const _Tp
&))
466 // Unary expression closure.
469 template<class _Oper
, class _Arg
>
473 typedef typename
_Arg::value_type _Vt
;
474 typedef typename __fun
<_Oper
, _Vt
>::result_type value_type
;
476 _UnBase(const _Arg
& __e
) : _M_expr(__e
) {}
478 value_type
operator[](size_t __i
) const
479 { return _Oper()(_M_expr
[__i
]); }
481 size_t size() const { return _M_expr
.size(); }
487 template<class _Oper
, class _Dom
>
488 struct _UnClos
<_Oper
, _Expr
, _Dom
>
489 : _UnBase
<_Oper
, _Dom
>
492 typedef _UnBase
<_Oper
, _Dom
> _Base
;
493 typedef typename
_Base::value_type value_type
;
495 _UnClos(const _Arg
& __e
) : _Base(__e
) {}
498 template<class _Oper
, typename _Tp
>
499 struct _UnClos
<_Oper
, _ValArray
, _Tp
>
500 : _UnBase
<_Oper
, valarray
<_Tp
> >
502 typedef valarray
<_Tp
> _Arg
;
503 typedef _UnBase
<_Oper
, valarray
<_Tp
> > _Base
;
504 typedef typename
_Base::value_type value_type
;
506 _UnClos(const _Arg
& __e
) : _Base(__e
) {}
511 // Binary expression closure.
514 template<class _Oper
, class _FirstArg
, class _SecondArg
>
518 typedef typename
_FirstArg::value_type _Vt
;
519 typedef typename __fun
<_Oper
, _Vt
>::result_type value_type
;
521 _BinBase(const _FirstArg
& __e1
, const _SecondArg
& __e2
)
522 : _M_expr1(__e1
), _M_expr2(__e2
) {}
524 value_type
operator[](size_t __i
) const
525 { return _Oper()(_M_expr1
[__i
], _M_expr2
[__i
]); }
527 size_t size() const { return _M_expr1
.size(); }
530 const _FirstArg
& _M_expr1
;
531 const _SecondArg
& _M_expr2
;
535 template<class _Oper
, class _Clos
>
539 typedef typename
_Clos::value_type _Vt
;
540 typedef typename __fun
<_Oper
, _Vt
>::result_type value_type
;
542 _BinBase2(const _Clos
& __e
, const _Vt
& __t
)
543 : _M_expr1(__e
), _M_expr2(__t
) {}
545 value_type
operator[](size_t __i
) const
546 { return _Oper()(_M_expr1
[__i
], _M_expr2
); }
548 size_t size() const { return _M_expr1
.size(); }
551 const _Clos
& _M_expr1
;
555 template<class _Oper
, class _Clos
>
559 typedef typename
_Clos::value_type _Vt
;
560 typedef typename __fun
<_Oper
, _Vt
>::result_type value_type
;
562 _BinBase1(const _Vt
& __t
, const _Clos
& __e
)
563 : _M_expr1(__t
), _M_expr2(__e
) {}
565 value_type
operator[](size_t __i
) const
566 { return _Oper()(_M_expr1
, _M_expr2
[__i
]); }
568 size_t size() const { return _M_expr2
.size(); }
572 const _Clos
& _M_expr2
;
575 template<class _Oper
, class _Dom1
, class _Dom2
>
576 struct _BinClos
<_Oper
, _Expr
, _Expr
, _Dom1
, _Dom2
>
577 : _BinBase
<_Oper
, _Dom1
, _Dom2
>
579 typedef _BinBase
<_Oper
, _Dom1
, _Dom2
> _Base
;
580 typedef typename
_Base::value_type value_type
;
582 _BinClos(const _Dom1
& __e1
, const _Dom2
& __e2
) : _Base(__e1
, __e2
) {}
585 template<class _Oper
, typename _Tp
>
586 struct _BinClos
<_Oper
,_ValArray
, _ValArray
, _Tp
, _Tp
>
587 : _BinBase
<_Oper
, valarray
<_Tp
>, valarray
<_Tp
> >
589 typedef _BinBase
<_Oper
, valarray
<_Tp
>, valarray
<_Tp
> > _Base
;
590 typedef typename
_Base::value_type value_type
;
592 _BinClos(const valarray
<_Tp
>& __v
, const valarray
<_Tp
>& __w
)
596 template<class _Oper
, class _Dom
>
597 struct _BinClos
<_Oper
, _Expr
, _ValArray
, _Dom
, typename
_Dom::value_type
>
598 : _BinBase
<_Oper
, _Dom
, valarray
<typename
_Dom::value_type
> >
600 typedef typename
_Dom::value_type _Tp
;
601 typedef _BinBase
<_Oper
,_Dom
,valarray
<_Tp
> > _Base
;
602 typedef typename
_Base::value_type value_type
;
604 _BinClos(const _Dom
& __e1
, const valarray
<_Tp
>& __e2
)
605 : _Base(__e1
, __e2
) {}
608 template<class _Oper
, class _Dom
>
609 struct _BinClos
<_Oper
, _ValArray
, _Expr
, typename
_Dom::value_type
, _Dom
>
610 : _BinBase
<_Oper
, valarray
<typename
_Dom::value_type
>,_Dom
>
612 typedef typename
_Dom::value_type _Tp
;
613 typedef _BinBase
<_Oper
, valarray
<_Tp
>, _Dom
> _Base
;
614 typedef typename
_Base::value_type value_type
;
616 _BinClos(const valarray
<_Tp
>& __e1
, const _Dom
& __e2
)
617 : _Base(__e1
, __e2
) {}
620 template<class _Oper
, class _Dom
>
621 struct _BinClos
<_Oper
, _Expr
, _Constant
, _Dom
, typename
_Dom::value_type
>
622 : _BinBase2
<_Oper
, _Dom
>
624 typedef typename
_Dom::value_type _Tp
;
625 typedef _BinBase2
<_Oper
,_Dom
> _Base
;
626 typedef typename
_Base::value_type value_type
;
628 _BinClos(const _Dom
& __e1
, const _Tp
& __e2
) : _Base(__e1
, __e2
) {}
631 template<class _Oper
, class _Dom
>
632 struct _BinClos
<_Oper
, _Constant
, _Expr
, typename
_Dom::value_type
, _Dom
>
633 : _BinBase1
<_Oper
, _Dom
>
635 typedef typename
_Dom::value_type _Tp
;
636 typedef _BinBase1
<_Oper
, _Dom
> _Base
;
637 typedef typename
_Base::value_type value_type
;
639 _BinClos(const _Tp
& __e1
, const _Dom
& __e2
) : _Base(__e1
, __e2
) {}
642 template<class _Oper
, typename _Tp
>
643 struct _BinClos
<_Oper
, _ValArray
, _Constant
, _Tp
, _Tp
>
644 : _BinBase2
<_Oper
, valarray
<_Tp
> >
646 typedef _BinBase2
<_Oper
,valarray
<_Tp
> > _Base
;
647 typedef typename
_Base::value_type value_type
;
649 _BinClos(const valarray
<_Tp
>& __v
, const _Tp
& __t
) : _Base(__v
, __t
) {}
652 template<class _Oper
, typename _Tp
>
653 struct _BinClos
<_Oper
, _Constant
, _ValArray
, _Tp
, _Tp
>
654 : _BinBase1
<_Oper
, valarray
<_Tp
> >
656 typedef _BinBase1
<_Oper
, valarray
<_Tp
> > _Base
;
657 typedef typename
_Base::value_type value_type
;
659 _BinClos(const _Tp
& __t
, const valarray
<_Tp
>& __v
) : _Base(__t
, __v
) {}
663 // slice_array closure.
665 template<typename _Dom
>
669 typedef typename
_Dom::value_type value_type
;
671 _SBase (const _Dom
& __e
, const slice
& __s
)
672 : _M_expr (__e
), _M_slice (__s
) {}
675 operator[] (size_t __i
) const
676 { return _M_expr
[_M_slice
.start () + __i
* _M_slice
.stride ()]; }
680 { return _M_slice
.size (); }
684 const slice
& _M_slice
;
687 template<typename _Tp
>
688 class _SBase
<_Array
<_Tp
> >
691 typedef _Tp value_type
;
693 _SBase (_Array
<_Tp
> __a
, const slice
& __s
)
694 : _M_array (__a
._M_data
+__s
.start()), _M_size (__s
.size()),
695 _M_stride (__s
.stride()) {}
698 operator[] (size_t __i
) const
699 { return _M_array
._M_data
[__i
* _M_stride
]; }
706 const _Array
<_Tp
> _M_array
;
707 const size_t _M_size
;
708 const size_t _M_stride
;
712 struct _SClos
<_Expr
, _Dom
>
715 typedef _SBase
<_Dom
> _Base
;
716 typedef typename
_Base::value_type value_type
;
718 _SClos (const _Dom
& __e
, const slice
& __s
) : _Base (__e
, __s
) {}
721 template<typename _Tp
>
722 struct _SClos
<_ValArray
, _Tp
>
723 : _SBase
<_Array
<_Tp
> >
725 typedef _SBase
<_Array
<_Tp
> > _Base
;
726 typedef _Tp value_type
;
728 _SClos (_Array
<_Tp
> __a
, const slice
& __s
) : _Base (__a
, __s
) {}
731 _GLIBCXX_END_NAMESPACE_VERSION
734 #endif /* _CPP_VALARRAY_BEFORE_H */