1 // The template and inlines for the -*- C++ -*- internal _Meta class.
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
4 // 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 2, 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 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
31 // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@cmla.ens-cachan.fr>
33 /** @file valarray_after.h
34 * This is an internal header file, included by other library headers.
35 * You should not attempt to use it directly.
38 #ifndef _VALARRAY_AFTER_H
39 #define _VALARRAY_AFTER_H 1
41 #pragma GCC system_header
46 // gslice_array closure.
52 typedef typename
_Dom::value_type value_type
;
54 _GBase (const _Dom
& __e
, const valarray
<size_t>& __i
)
55 : _M_expr (__e
), _M_index(__i
) {}
58 operator[] (size_t __i
) const
59 { return _M_expr
[_M_index
[__i
]]; }
63 { return _M_index
.size(); }
67 const valarray
<size_t>& _M_index
;
70 template<typename _Tp
>
71 class _GBase
<_Array
<_Tp
> >
74 typedef _Tp value_type
;
76 _GBase (_Array
<_Tp
> __a
, const valarray
<size_t>& __i
)
77 : _M_array (__a
), _M_index(__i
) {}
80 operator[] (size_t __i
) const
81 { return _M_array
._M_data
[_M_index
[__i
]]; }
85 { return _M_index
.size(); }
88 const _Array
<_Tp
> _M_array
;
89 const valarray
<size_t>& _M_index
;
93 struct _GClos
<_Expr
, _Dom
>
96 typedef _GBase
<_Dom
> _Base
;
97 typedef typename
_Base::value_type value_type
;
99 _GClos (const _Dom
& __e
, const valarray
<size_t>& __i
)
100 : _Base (__e
, __i
) {}
103 template<typename _Tp
>
104 struct _GClos
<_ValArray
, _Tp
>
105 : _GBase
<_Array
<_Tp
> >
107 typedef _GBase
<_Array
<_Tp
> > _Base
;
108 typedef typename
_Base::value_type value_type
;
110 _GClos (_Array
<_Tp
> __a
, const valarray
<size_t>& __i
)
111 : _Base (__a
, __i
) {}
115 // indirect_array closure
121 typedef typename
_Dom::value_type value_type
;
123 _IBase (const _Dom
& __e
, const valarray
<size_t>& __i
)
124 : _M_expr (__e
), _M_index (__i
) {}
127 operator[] (size_t __i
) const
128 { return _M_expr
[_M_index
[__i
]]; }
132 { return _M_index
.size(); }
136 const valarray
<size_t>& _M_index
;
140 struct _IClos
<_Expr
, _Dom
>
143 typedef _IBase
<_Dom
> _Base
;
144 typedef typename
_Base::value_type value_type
;
146 _IClos (const _Dom
& __e
, const valarray
<size_t>& __i
)
147 : _Base (__e
, __i
) {}
150 template<typename _Tp
>
151 struct _IClos
<_ValArray
, _Tp
>
152 : _IBase
<valarray
<_Tp
> >
154 typedef _IBase
<valarray
<_Tp
> > _Base
;
155 typedef _Tp value_type
;
157 _IClos (const valarray
<_Tp
>& __a
, const valarray
<size_t>& __i
)
158 : _Base (__a
, __i
) {}
164 template<class _Clos
, typename _Tp
>
168 typedef _Tp value_type
;
172 const _Clos
& operator()() const;
174 value_type
operator[](size_t) const;
175 valarray
<value_type
> operator[](slice
) const;
176 valarray
<value_type
> operator[](const gslice
&) const;
177 valarray
<value_type
> operator[](const valarray
<bool>&) const;
178 valarray
<value_type
> operator[](const valarray
<size_t>&) const;
180 _Expr
<_UnClos
<__unary_plus
, std::_Expr
, _Clos
>, value_type
>
183 _Expr
<_UnClos
<__negate
, std::_Expr
, _Clos
>, value_type
>
186 _Expr
<_UnClos
<__bitwise_not
, std::_Expr
, _Clos
>, value_type
>
189 _Expr
<_UnClos
<__logical_not
, std::_Expr
, _Clos
>, bool>
193 value_type
sum() const;
195 valarray
<value_type
> shift(int) const;
196 valarray
<value_type
> cshift(int) const;
198 value_type
min() const;
199 value_type
max() const;
201 valarray
<value_type
> apply(value_type (*)(const value_type
&)) const;
202 valarray
<value_type
> apply(value_type (*)(value_type
)) const;
205 const _Clos _M_closure
;
208 template<class _Clos
, typename _Tp
>
210 _Expr
<_Clos
, _Tp
>::_Expr(const _Clos
& __c
) : _M_closure(__c
) {}
212 template<class _Clos
, typename _Tp
>
214 _Expr
<_Clos
, _Tp
>::operator()() const
215 { return _M_closure
; }
217 template<class _Clos
, typename _Tp
>
219 _Expr
<_Clos
, _Tp
>::operator[](size_t __i
) const
220 { return _M_closure
[__i
]; }
222 template<class _Clos
, typename _Tp
>
224 _Expr
<_Clos
, _Tp
>::operator[](slice __s
) const
226 valarray
<_Tp
> __v
= valarray
<_Tp
>(*this)[__s
];
230 template<class _Clos
, typename _Tp
>
232 _Expr
<_Clos
, _Tp
>::operator[](const gslice
& __gs
) const
234 valarray
<_Tp
> __v
= valarray
<_Tp
>(*this)[__gs
];
238 template<class _Clos
, typename _Tp
>
240 _Expr
<_Clos
, _Tp
>::operator[](const valarray
<bool>& __m
) const
242 valarray
<_Tp
> __v
= valarray
<_Tp
>(*this)[__m
];
246 template<class _Clos
, typename _Tp
>
248 _Expr
<_Clos
, _Tp
>::operator[](const valarray
<size_t>& __i
) const
250 valarray
<_Tp
> __v
= valarray
<_Tp
>(*this)[__i
];
254 template<class _Clos
, typename _Tp
>
256 _Expr
<_Clos
, _Tp
>::size() const
257 { return _M_closure
.size(); }
259 template<class _Clos
, typename _Tp
>
261 _Expr
<_Clos
, _Tp
>::shift(int __n
) const
263 valarray
<_Tp
> __v
= valarray
<_Tp
>(*this).shift(__n
);
267 template<class _Clos
, typename _Tp
>
269 _Expr
<_Clos
, _Tp
>::cshift(int __n
) const
271 valarray
<_Tp
> __v
= valarray
<_Tp
>(*this).cshift(__n
);
275 template<class _Clos
, typename _Tp
>
277 _Expr
<_Clos
, _Tp
>::apply(_Tp
__f(const _Tp
&)) const
279 valarray
<_Tp
> __v
= valarray
<_Tp
>(*this).apply(__f
);
283 template<class _Clos
, typename _Tp
>
285 _Expr
<_Clos
, _Tp
>::apply(_Tp
__f(_Tp
)) const
287 valarray
<_Tp
> __v
= valarray
<_Tp
>(*this).apply(__f
);
291 // XXX: replace this with a more robust summation algorithm.
292 template<class _Clos
, typename _Tp
>
294 _Expr
<_Clos
, _Tp
>::sum() const
296 size_t __n
= _M_closure
.size();
301 _Tp __s
= _M_closure
[--__n
];
303 __s
+= _M_closure
[--__n
];
308 template<class _Clos
, typename _Tp
>
310 _Expr
<_Clos
, _Tp
>::min() const
311 { return __valarray_min(_M_closure
); }
313 template<class _Clos
, typename _Tp
>
315 _Expr
<_Clos
, _Tp
>::max() const
316 { return __valarray_max(_M_closure
); }
318 template<class _Dom
, typename _Tp
>
319 inline _Expr
<_UnClos
<__logical_not
, _Expr
, _Dom
>, bool>
320 _Expr
<_Dom
, _Tp
>::operator!() const
322 typedef _UnClos
<__logical_not
, std::_Expr
, _Dom
> _Closure
;
323 return _Expr
<_Closure
, _Tp
>(_Closure(this->_M_closure
));
326 #define _DEFINE_EXPR_UNARY_OPERATOR(_Op, _Name) \
327 template<class _Dom, typename _Tp> \
328 inline _Expr<_UnClos<_Name, std::_Expr, _Dom>, _Tp> \
329 _Expr<_Dom, _Tp>::operator _Op() const \
331 typedef _UnClos<_Name, std::_Expr, _Dom> _Closure; \
332 return _Expr<_Closure, _Tp>(_Closure(this->_M_closure)); \
335 _DEFINE_EXPR_UNARY_OPERATOR(+, __unary_plus
)
336 _DEFINE_EXPR_UNARY_OPERATOR(-, __negate
)
337 _DEFINE_EXPR_UNARY_OPERATOR(~, __bitwise_not
)
339 #undef _DEFINE_EXPR_UNARY_OPERATOR
341 #define _DEFINE_EXPR_BINARY_OPERATOR(_Op, _Name) \
342 template<class _Dom1, class _Dom2> \
343 inline _Expr<_BinClos<_Name, _Expr, _Expr, _Dom1, _Dom2>, \
344 typename __fun<_Name, typename _Dom1::value_type>::result_type> \
345 operator _Op(const _Expr<_Dom1, typename _Dom1::value_type>& __v, \
346 const _Expr<_Dom2, typename _Dom2::value_type>& __w) \
348 typedef typename _Dom1::value_type _Arg; \
349 typedef typename __fun<_Name, _Arg>::result_type _Value; \
350 typedef _BinClos<_Name, _Expr, _Expr, _Dom1, _Dom2> _Closure; \
351 return _Expr<_Closure, _Value>(_Closure(__v(), __w())); \
354 template<class _Dom> \
355 inline _Expr<_BinClos<_Name, _Expr, _Constant, _Dom, \
356 typename _Dom::value_type>, \
357 typename __fun<_Name, typename _Dom::value_type>::result_type> \
358 operator _Op(const _Expr<_Dom, typename _Dom::value_type>& __v, \
359 const typename _Dom::value_type& __t) \
361 typedef typename _Dom::value_type _Arg; \
362 typedef typename __fun<_Name, _Arg>::result_type _Value; \
363 typedef _BinClos<_Name, _Expr, _Constant, _Dom, _Arg> _Closure; \
364 return _Expr<_Closure, _Value>(_Closure(__v(), __t)); \
367 template<class _Dom> \
368 inline _Expr<_BinClos<_Name, _Constant, _Expr, \
369 typename _Dom::value_type, _Dom>, \
370 typename __fun<_Name, typename _Dom::value_type>::result_type> \
371 operator _Op(const typename _Dom::value_type& __t, \
372 const _Expr<_Dom, typename _Dom::value_type>& __v) \
374 typedef typename _Dom::value_type _Arg; \
375 typedef typename __fun<_Name, _Arg>::result_type _Value; \
376 typedef _BinClos<_Name, _Constant, _Expr, _Arg, _Dom> _Closure; \
377 return _Expr<_Closure, _Value>(_Closure(__t, __v())); \
380 template<class _Dom> \
381 inline _Expr<_BinClos<_Name, _Expr, _ValArray, \
382 _Dom, typename _Dom::value_type>, \
383 typename __fun<_Name, typename _Dom::value_type>::result_type> \
384 operator _Op(const _Expr<_Dom,typename _Dom::value_type>& __e, \
385 const valarray<typename _Dom::value_type>& __v) \
387 typedef typename _Dom::value_type _Arg; \
388 typedef typename __fun<_Name, _Arg>::result_type _Value; \
389 typedef _BinClos<_Name, _Expr, _ValArray, _Dom, _Arg> _Closure; \
390 return _Expr<_Closure, _Value>(_Closure(__e(), __v)); \
393 template<class _Dom> \
394 inline _Expr<_BinClos<_Name, _ValArray, _Expr, \
395 typename _Dom::value_type, _Dom>, \
396 typename __fun<_Name, typename _Dom::value_type>::result_type> \
397 operator _Op(const valarray<typename _Dom::value_type>& __v, \
398 const _Expr<_Dom, typename _Dom::value_type>& __e) \
400 typedef typename _Dom::value_type _Tp; \
401 typedef typename __fun<_Name, _Tp>::result_type _Value; \
402 typedef _BinClos<_Name, _ValArray, _Expr, _Tp, _Dom> _Closure; \
403 return _Expr<_Closure, _Value>(_Closure(__v, __e ())); \
406 _DEFINE_EXPR_BINARY_OPERATOR(+, __plus
)
407 _DEFINE_EXPR_BINARY_OPERATOR(-, __minus
)
408 _DEFINE_EXPR_BINARY_OPERATOR(*, __multiplies
)
409 _DEFINE_EXPR_BINARY_OPERATOR(/, __divides
)
410 _DEFINE_EXPR_BINARY_OPERATOR(%, __modulus
)
411 _DEFINE_EXPR_BINARY_OPERATOR(^, __bitwise_xor
)
412 _DEFINE_EXPR_BINARY_OPERATOR(&, __bitwise_and
)
413 _DEFINE_EXPR_BINARY_OPERATOR(|, __bitwise_or
)
414 _DEFINE_EXPR_BINARY_OPERATOR(<<, __shift_left
)
415 _DEFINE_EXPR_BINARY_OPERATOR(>>, __shift_right
)
416 _DEFINE_EXPR_BINARY_OPERATOR(&&, __logical_and
)
417 _DEFINE_EXPR_BINARY_OPERATOR(||, __logical_or
)
418 _DEFINE_EXPR_BINARY_OPERATOR(==, __equal_to
)
419 _DEFINE_EXPR_BINARY_OPERATOR(!=, __not_equal_to
)
420 _DEFINE_EXPR_BINARY_OPERATOR(<, __less
)
421 _DEFINE_EXPR_BINARY_OPERATOR(>, __greater
)
422 _DEFINE_EXPR_BINARY_OPERATOR(<=, __less_equal
)
423 _DEFINE_EXPR_BINARY_OPERATOR(>=, __greater_equal
)
425 #undef _DEFINE_EXPR_BINARY_OPERATOR
427 #define _DEFINE_EXPR_UNARY_FUNCTION(_Name) \
428 template<class _Dom> \
429 inline _Expr<_UnClos<__##_Name, _Expr, _Dom>, \
430 typename _Dom::value_type> \
431 _Name(const _Expr<_Dom, typename _Dom::value_type>& __e) \
433 typedef typename _Dom::value_type _Tp; \
434 typedef _UnClos<__##_Name, _Expr, _Dom> _Closure; \
435 return _Expr<_Closure, _Tp>(_Closure(__e())); \
438 template<typename _Tp> \
439 inline _Expr<_UnClos<__##_Name, _ValArray, _Tp>, _Tp> \
440 _Name(const valarray<_Tp>& __v) \
442 typedef _UnClos<__##_Name, _ValArray, _Tp> _Closure; \
443 return _Expr<_Closure, _Tp>(_Closure(__v)); \
446 _DEFINE_EXPR_UNARY_FUNCTION(abs
)
447 _DEFINE_EXPR_UNARY_FUNCTION(cos
)
448 _DEFINE_EXPR_UNARY_FUNCTION(acos
)
449 _DEFINE_EXPR_UNARY_FUNCTION(cosh
)
450 _DEFINE_EXPR_UNARY_FUNCTION(sin
)
451 _DEFINE_EXPR_UNARY_FUNCTION(asin
)
452 _DEFINE_EXPR_UNARY_FUNCTION(sinh
)
453 _DEFINE_EXPR_UNARY_FUNCTION(tan
)
454 _DEFINE_EXPR_UNARY_FUNCTION(tanh
)
455 _DEFINE_EXPR_UNARY_FUNCTION(atan
)
456 _DEFINE_EXPR_UNARY_FUNCTION(exp
)
457 _DEFINE_EXPR_UNARY_FUNCTION(log
)
458 _DEFINE_EXPR_UNARY_FUNCTION(log10
)
459 _DEFINE_EXPR_UNARY_FUNCTION(sqrt
)
461 #undef _DEFINE_EXPR_UNARY_FUNCTION
463 #define _DEFINE_EXPR_BINARY_FUNCTION(_Fun) \
464 template<class _Dom1, class _Dom2> \
465 inline _Expr<_BinClos<__##_Fun, _Expr, _Expr, _Dom1, _Dom2>, \
466 typename _Dom1::value_type> \
467 _Fun(const _Expr<_Dom1, typename _Dom1::value_type>& __e1, \
468 const _Expr<_Dom2, typename _Dom2::value_type>& __e2) \
470 typedef typename _Dom1::value_type _Tp; \
471 typedef _BinClos<__##_Fun, _Expr, _Expr, _Dom1, _Dom2> _Closure; \
472 return _Expr<_Closure, _Tp>(_Closure(__e1(), __e2())); \
475 template<class _Dom> \
476 inline _Expr<_BinClos<__##_Fun, _Expr, _ValArray, _Dom, \
477 typename _Dom::value_type>, \
478 typename _Dom::value_type> \
479 _Fun(const _Expr<_Dom, typename _Dom::value_type>& __e, \
480 const valarray<typename _Dom::value_type>& __v) \
482 typedef typename _Dom::value_type _Tp; \
483 typedef _BinClos<__##_Fun, _Expr, _ValArray, _Dom, _Tp> _Closure; \
484 return _Expr<_Closure, _Tp>(_Closure(__e(), __v)); \
487 template<class _Dom> \
488 inline _Expr<_BinClos<__##_Fun, _ValArray, _Expr, \
489 typename _Dom::value_type, _Dom>, \
490 typename _Dom::value_type> \
491 _Fun(const valarray<typename _Dom::valarray>& __v, \
492 const _Expr<_Dom, typename _Dom::value_type>& __e) \
494 typedef typename _Dom::value_type _Tp; \
495 typedef _BinClos<__##_Fun, _ValArray, _Expr, _Tp, _Dom> _Closure; \
496 return _Expr<_Closure, _Tp>(_Closure(__v, __e())); \
499 template<class _Dom> \
500 inline _Expr<_BinClos<__##_Fun, _Expr, _Constant, _Dom, \
501 typename _Dom::value_type>, \
502 typename _Dom::value_type> \
503 _Fun(const _Expr<_Dom, typename _Dom::value_type>& __e, \
504 const typename _Dom::value_type& __t) \
506 typedef typename _Dom::value_type _Tp; \
507 typedef _BinClos<__##_Fun, _Expr, _Constant, _Dom, _Tp> _Closure;\
508 return _Expr<_Closure, _Tp>(_Closure(__e(), __t)); \
511 template<class _Dom> \
512 inline _Expr<_BinClos<__##_Fun, _Constant, _Expr, \
513 typename _Dom::value_type, _Dom>, \
514 typename _Dom::value_type> \
515 _Fun(const typename _Dom::value_type& __t, \
516 const _Expr<_Dom, typename _Dom::value_type>& __e) \
518 typedef typename _Dom::value_type _Tp; \
519 typedef _BinClos<__##_Fun, _Constant, _Expr, _Tp, _Dom> _Closure; \
520 return _Expr<_Closure, _Tp>(_Closure(__t, __e())); \
523 template<typename _Tp> \
524 inline _Expr<_BinClos<__##_Fun, _ValArray, _ValArray, _Tp, _Tp>, _Tp> \
525 _Fun(const valarray<_Tp>& __v, const valarray<_Tp>& __w) \
527 typedef _BinClos<__##_Fun, _ValArray, _ValArray, _Tp, _Tp> _Closure; \
528 return _Expr<_Closure, _Tp>(_Closure(__v, __w)); \
531 template<typename _Tp> \
532 inline _Expr<_BinClos<__##_Fun, _ValArray, _Constant, _Tp, _Tp>, _Tp> \
533 _Fun(const valarray<_Tp>& __v, const _Tp& __t) \
535 typedef _BinClos<__##_Fun, _ValArray, _Constant, _Tp, _Tp> _Closure; \
536 return _Expr<_Closure, _Tp>(_Closure(__v, __t)); \
539 template<typename _Tp> \
540 inline _Expr<_BinClos<__##_Fun, _Constant, _ValArray, _Tp, _Tp>, _Tp> \
541 _Fun(const _Tp& __t, const valarray<_Tp>& __v) \
543 typedef _BinClos<__##_Fun, _Constant, _ValArray, _Tp, _Tp> _Closure; \
544 return _Expr<_Closure, _Tp>(_Closure(__t, __v)); \
547 _DEFINE_EXPR_BINARY_FUNCTION(atan2
)
548 _DEFINE_EXPR_BINARY_FUNCTION(pow
)
550 #undef _DEFINE_EXPR_BINARY_FUNCTION
554 #endif /* _CPP_VALARRAY_AFTER_H */