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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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
225 { return _M_closure
[__s
]; }
227 template<class _Clos
, typename _Tp
>
229 _Expr
<_Clos
, _Tp
>::operator[](const gslice
& __gs
) const
230 { return _M_closure
[__gs
]; }
232 template<class _Clos
, typename _Tp
>
234 _Expr
<_Clos
, _Tp
>::operator[](const valarray
<bool>& __m
) const
235 { return _M_closure
[__m
]; }
237 template<class _Clos
, typename _Tp
>
239 _Expr
<_Clos
, _Tp
>::operator[](const valarray
<size_t>& __i
) const
240 { return _M_closure
[__i
]; }
242 template<class _Clos
, typename _Tp
>
244 _Expr
<_Clos
, _Tp
>::size() const
245 { return _M_closure
.size (); }
247 template<class _Clos
, typename _Tp
>
249 _Expr
<_Clos
, _Tp
>::shift(int __n
) const
250 { return valarray
<_Tp
>(_M_closure
).shift(__n
); }
252 template<class _Clos
, typename _Tp
>
254 _Expr
<_Clos
, _Tp
>::cshift(int __n
) const
255 { return valarray
<_Tp
>(_M_closure
).cshift(__n
); }
257 template<class _Clos
, typename _Tp
>
259 _Expr
<_Clos
, _Tp
>::apply(_Tp
__f(const _Tp
&)) const
260 { return valarray
<_Tp
>(_M_closure
).apply(__f
); }
262 template<class _Clos
, typename _Tp
>
264 _Expr
<_Clos
, _Tp
>::apply(_Tp
__f(_Tp
)) const
265 { return valarray
<_Tp
>(_M_closure
).apply(__f
); }
267 // XXX: replace this with a more robust summation algorithm.
268 template<class _Clos
, typename _Tp
>
270 _Expr
<_Clos
, _Tp
>::sum() const
272 size_t __n
= _M_closure
.size();
277 _Tp __s
= _M_closure
[--__n
];
279 __s
+= _M_closure
[--__n
];
284 template<class _Clos
, typename _Tp
>
286 _Expr
<_Clos
, _Tp
>::min() const
287 { return __valarray_min(_M_closure
); }
289 template<class _Clos
, typename _Tp
>
291 _Expr
<_Clos
, _Tp
>::max() const
292 { return __valarray_max(_M_closure
); }
294 template<class _Dom
, typename _Tp
>
295 inline _Expr
<_UnClos
<__logical_not
, _Expr
, _Dom
>, bool>
296 _Expr
<_Dom
, _Tp
>::operator!() const
298 typedef _UnClos
<__logical_not
, std::_Expr
, _Dom
> _Closure
;
299 return _Expr
<_Closure
, _Tp
>(_Closure(this->_M_closure
));
302 #define _DEFINE_EXPR_UNARY_OPERATOR(_Op, _Name) \
303 template<class _Dom, typename _Tp> \
304 inline _Expr<_UnClos<_Name, std::_Expr, _Dom>, _Tp> \
305 _Expr<_Dom, _Tp>::operator _Op() const \
307 typedef _UnClos<_Name, std::_Expr, _Dom> _Closure; \
308 return _Expr<_Closure, _Tp>(_Closure(this->_M_closure)); \
311 _DEFINE_EXPR_UNARY_OPERATOR(+, __unary_plus
)
312 _DEFINE_EXPR_UNARY_OPERATOR(-, __negate
)
313 _DEFINE_EXPR_UNARY_OPERATOR(~, __bitwise_not
)
315 #undef _DEFINE_EXPR_UNARY_OPERATOR
317 #define _DEFINE_EXPR_BINARY_OPERATOR(_Op, _Name) \
318 template<class _Dom1, class _Dom2> \
319 inline _Expr<_BinClos<_Name, _Expr, _Expr, _Dom1, _Dom2>, \
320 typename __fun<_Name, typename _Dom1::value_type>::result_type> \
321 operator _Op(const _Expr<_Dom1, typename _Dom1::value_type>& __v, \
322 const _Expr<_Dom2, typename _Dom2::value_type>& __w) \
324 typedef typename _Dom1::value_type _Arg; \
325 typedef typename __fun<_Name, _Arg>::result_type _Value; \
326 typedef _BinClos<_Name, _Expr, _Expr, _Dom1, _Dom2> _Closure; \
327 return _Expr<_Closure, _Value>(_Closure(__v(), __w())); \
330 template<class _Dom> \
331 inline _Expr<_BinClos<_Name, _Expr, _Constant, _Dom, \
332 typename _Dom::value_type>, \
333 typename __fun<_Name, typename _Dom::value_type>::result_type> \
334 operator _Op(const _Expr<_Dom, typename _Dom::value_type>& __v, \
335 const typename _Dom::value_type& __t) \
337 typedef typename _Dom::value_type _Arg; \
338 typedef typename __fun<_Name, _Arg>::result_type _Value; \
339 typedef _BinClos<_Name, _Expr, _Constant, _Dom, _Arg> _Closure; \
340 return _Expr<_Closure, _Value>(_Closure(__v(), __t)); \
343 template<class _Dom> \
344 inline _Expr<_BinClos<_Name, _Constant, _Expr, \
345 typename _Dom::value_type, _Dom>, \
346 typename __fun<_Name, typename _Dom::value_type>::result_type> \
347 operator _Op(const typename _Dom::value_type& __t, \
348 const _Expr<_Dom, typename _Dom::value_type>& __v) \
350 typedef typename _Dom::value_type _Arg; \
351 typedef typename __fun<_Name, _Arg>::result_type _Value; \
352 typedef _BinClos<_Name, _Constant, _Expr, _Arg, _Dom> _Closure; \
353 return _Expr<_Closure, _Value>(_Closure(__t, __v())); \
356 template<class _Dom> \
357 inline _Expr<_BinClos<_Name, _Expr, _ValArray, \
358 _Dom, typename _Dom::value_type>, \
359 typename __fun<_Name, typename _Dom::value_type>::result_type> \
360 operator _Op(const _Expr<_Dom,typename _Dom::value_type>& __e, \
361 const valarray<typename _Dom::value_type>& __v) \
363 typedef typename _Dom::value_type _Arg; \
364 typedef typename __fun<_Name, _Arg>::result_type _Value; \
365 typedef _BinClos<_Name, _Expr, _ValArray, _Dom, _Arg> _Closure; \
366 return _Expr<_Closure, _Value>(_Closure(__e(), __v)); \
369 template<class _Dom> \
370 inline _Expr<_BinClos<_Name, _ValArray, _Expr, \
371 typename _Dom::value_type, _Dom>, \
372 typename __fun<_Name, typename _Dom::value_type>::result_type> \
373 operator _Op(const valarray<typename _Dom::value_type>& __v, \
374 const _Expr<_Dom, typename _Dom::value_type>& __e) \
376 typedef typename _Dom::value_type _Tp; \
377 typedef typename __fun<_Name, _Tp>::result_type _Value; \
378 typedef _BinClos<_Name, _ValArray, _Expr, _Tp, _Dom> _Closure; \
379 return _Expr<_Closure, _Value>(_Closure(__v, __e ())); \
382 _DEFINE_EXPR_BINARY_OPERATOR(+, __plus
)
383 _DEFINE_EXPR_BINARY_OPERATOR(-, __minus
)
384 _DEFINE_EXPR_BINARY_OPERATOR(*, __multiplies
)
385 _DEFINE_EXPR_BINARY_OPERATOR(/, __divides
)
386 _DEFINE_EXPR_BINARY_OPERATOR(%, __modulus
)
387 _DEFINE_EXPR_BINARY_OPERATOR(^, __bitwise_xor
)
388 _DEFINE_EXPR_BINARY_OPERATOR(&, __bitwise_and
)
389 _DEFINE_EXPR_BINARY_OPERATOR(|, __bitwise_or
)
390 _DEFINE_EXPR_BINARY_OPERATOR(<<, __shift_left
)
391 _DEFINE_EXPR_BINARY_OPERATOR(>>, __shift_right
)
392 _DEFINE_EXPR_BINARY_OPERATOR(&&, __logical_and
)
393 _DEFINE_EXPR_BINARY_OPERATOR(||, __logical_or
)
394 _DEFINE_EXPR_BINARY_OPERATOR(==, __equal_to
)
395 _DEFINE_EXPR_BINARY_OPERATOR(!=, __not_equal_to
)
396 _DEFINE_EXPR_BINARY_OPERATOR(<, __less
)
397 _DEFINE_EXPR_BINARY_OPERATOR(>, __greater
)
398 _DEFINE_EXPR_BINARY_OPERATOR(<=, __less_equal
)
399 _DEFINE_EXPR_BINARY_OPERATOR(>=, __greater_equal
)
401 #undef _DEFINE_EXPR_BINARY_OPERATOR
403 #define _DEFINE_EXPR_UNARY_FUNCTION(_Name) \
404 template<class _Dom> \
405 inline _Expr<_UnClos<__##_Name, _Expr, _Dom>, \
406 typename _Dom::value_type> \
407 _Name(const _Expr<_Dom, typename _Dom::value_type>& __e) \
409 typedef typename _Dom::value_type _Tp; \
410 typedef _UnClos<__##_Name, _Expr, _Dom> _Closure; \
411 return _Expr<_Closure, _Tp>(_Closure(__e())); \
414 template<typename _Tp> \
415 inline _Expr<_UnClos<__##_Name, _ValArray, _Tp>, _Tp> \
416 _Name(const valarray<_Tp>& __v) \
418 typedef _UnClos<__##_Name, _ValArray, _Tp> _Closure; \
419 return _Expr<_Closure, _Tp>(_Closure(__v)); \
422 _DEFINE_EXPR_UNARY_FUNCTION(abs
)
423 _DEFINE_EXPR_UNARY_FUNCTION(cos
)
424 _DEFINE_EXPR_UNARY_FUNCTION(acos
)
425 _DEFINE_EXPR_UNARY_FUNCTION(cosh
)
426 _DEFINE_EXPR_UNARY_FUNCTION(sin
)
427 _DEFINE_EXPR_UNARY_FUNCTION(asin
)
428 _DEFINE_EXPR_UNARY_FUNCTION(sinh
)
429 _DEFINE_EXPR_UNARY_FUNCTION(tan
)
430 _DEFINE_EXPR_UNARY_FUNCTION(tanh
)
431 _DEFINE_EXPR_UNARY_FUNCTION(atan
)
432 _DEFINE_EXPR_UNARY_FUNCTION(exp
)
433 _DEFINE_EXPR_UNARY_FUNCTION(log
)
434 _DEFINE_EXPR_UNARY_FUNCTION(log10
)
435 _DEFINE_EXPR_UNARY_FUNCTION(sqrt
)
437 #undef _DEFINE_EXPR_UNARY_FUNCTION
439 #define _DEFINE_EXPR_BINARY_FUNCTION(_Fun) \
440 template<class _Dom1, class _Dom2> \
441 inline _Expr<_BinClos<__##_Fun, _Expr, _Expr, _Dom1, _Dom2>, \
442 typename _Dom1::value_type> \
443 _Fun(const _Expr<_Dom1, typename _Dom1::value_type>& __e1, \
444 const _Expr<_Dom2, typename _Dom2::value_type>& __e2) \
446 typedef typename _Dom1::value_type _Tp; \
447 typedef _BinClos<__##_Fun, _Expr, _Expr, _Dom1, _Dom2> _Closure; \
448 return _Expr<_Closure, _Tp>(_Closure(__e1(), __e2())); \
451 template<class _Dom> \
452 inline _Expr<_BinClos<__##_Fun, _Expr, _ValArray, _Dom, \
453 typename _Dom::value_type>, \
454 typename _Dom::value_type> \
455 _Fun(const _Expr<_Dom, typename _Dom::value_type>& __e, \
456 const valarray<typename _Dom::value_type>& __v) \
458 typedef typename _Dom::value_type _Tp; \
459 typedef _BinClos<__##_Fun, _Expr, _ValArray, _Dom, _Tp> _Closure; \
460 return _Expr<_Closure, _Tp>(_Closure(__e(), __v)); \
463 template<class _Dom> \
464 inline _Expr<_BinClos<__##_Fun, _ValArray, _Expr, \
465 typename _Dom::value_type, _Dom>, \
466 typename _Dom::value_type> \
467 _Fun(const valarray<typename _Dom::valarray>& __v, \
468 const _Expr<_Dom, typename _Dom::value_type>& __e) \
470 typedef typename _Dom::value_type _Tp; \
471 typedef _BinClos<__##_Fun, _ValArray, _Expr, _Tp, _Dom> _Closure; \
472 return _Expr<_Closure, _Tp>(_Closure(__v, __e())); \
475 template<class _Dom> \
476 inline _Expr<_BinClos<__##_Fun, _Expr, _Constant, _Dom, \
477 typename _Dom::value_type>, \
478 typename _Dom::value_type> \
479 _Fun(const _Expr<_Dom, typename _Dom::value_type>& __e, \
480 const typename _Dom::value_type& __t) \
482 typedef typename _Dom::value_type _Tp; \
483 typedef _BinClos<__##_Fun, _Expr, _Constant, _Dom, _Tp> _Closure;\
484 return _Expr<_Closure, _Tp>(_Closure(__e(), __t)); \
487 template<class _Dom> \
488 inline _Expr<_BinClos<__##_Fun, _Constant, _Expr, \
489 typename _Dom::value_type, _Dom>, \
490 typename _Dom::value_type> \
491 _Fun(const typename _Dom::value_type& __t, \
492 const _Expr<_Dom, typename _Dom::value_type>& __e) \
494 typedef typename _Dom::value_type _Tp; \
495 typedef _BinClos<__##_Fun, _Constant, _Expr, _Tp, _Dom> _Closure; \
496 return _Expr<_Closure, _Tp>(_Closure(__t, __e())); \
499 template<typename _Tp> \
500 inline _Expr<_BinClos<__##_Fun, _ValArray, _ValArray, _Tp, _Tp>, _Tp> \
501 _Fun(const valarray<_Tp>& __v, const valarray<_Tp>& __w) \
503 typedef _BinClos<__##_Fun, _ValArray, _ValArray, _Tp, _Tp> _Closure; \
504 return _Expr<_Closure, _Tp>(_Closure(__v, __w)); \
507 template<typename _Tp> \
508 inline _Expr<_BinClos<__##_Fun, _ValArray, _Constant, _Tp, _Tp>, _Tp> \
509 _Fun(const valarray<_Tp>& __v, const _Tp& __t) \
511 typedef _BinClos<__##_Fun, _ValArray, _Constant, _Tp, _Tp> _Closure; \
512 return _Expr<_Closure, _Tp>(_Closure(__v, __t)); \
515 template<typename _Tp> \
516 inline _Expr<_BinClos<__##_Fun, _Constant, _ValArray, _Tp, _Tp>, _Tp> \
517 _Fun(const _Tp& __t, const valarray<_Tp>& __v) \
519 typedef _BinClos<__##_Fun, _Constant, _ValArray, _Tp, _Tp> _Closure; \
520 return _Expr<_Closure, _Tp>(_Closure(__t, __v)); \
523 _DEFINE_EXPR_BINARY_FUNCTION(atan2
)
524 _DEFINE_EXPR_BINARY_FUNCTION(pow
)
526 #undef _DEFINE_EXPR_BINARY_FUNCTION
530 #endif /* _CPP_VALARRAY_AFTER_H */