1 // The template and inlines for the -*- C++ -*- internal _Meta class.
3 // Copyright (C) 1997-1999, 2000 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 2, 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 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@cmla.ens-cachan.fr>
32 #ifndef _CPP_VALARRAY_META_H
33 #define _CPP_VALARRAY_META_H 1
38 // Implementing a loosened valarray return value is tricky.
39 // First we need to meet 26.3.1/3: we should not add more than
40 // two levels of template nesting. Therefore we resort to template
41 // template to "flatten" loosened return value types.
42 // At some point we use partial specialization to remove one level
43 // template nesting due to _Expr<>
47 // This class is NOT defined. It doesn't need to.
48 template<typename _Tp1, typename _Tp2> class _Constant;
51 // Unary function application closure.
53 template<class _Dom> class _UnFunBase {
55 typedef typename _Dom::value_type value_type;
56 typedef value_type _Vt;
58 _UnFunBase (const _Dom& __e, _Vt __f(_Vt))
59 : _M_expr(__e), _M_func(__f) {}
61 _Vt operator[] (size_t __i) const { return _M_func(_M_expr[__i]); }
62 size_t size () const { return _M_expr.size(); }
69 template<template<class, class> class _Meta, class _Dom>
73 struct _UnFunClos<_Expr,_Dom> : _UnFunBase<_Dom> {
74 typedef _UnFunBase<_Dom> _Base;
75 typedef typename _Base::value_type value_type;
77 _UnFunClos (const _Dom& __e, value_type __f(value_type))
81 template<typename _Tp>
82 struct _UnFunClos<_ValArray,_Tp> : _UnFunBase<valarray<_Tp> > {
83 typedef _UnFunBase<valarray<_Tp> > _Base;
84 typedef typename _Base::value_type value_type;
86 _UnFunClos (const valarray<_Tp>& __v, _Tp __f(_Tp))
91 // Binary function application closure.
93 template<template<class, class> class _Meta1,
94 template<class, class> class Meta2,
95 class _Dom1, class _Dom2> class _BinFunClos;
97 template<class _Dom1, class _Dom2> class _BinFunBase {
99 typedef typename _Dom1::value_type value_type;
100 typedef value_type _Vt;
102 _BinFunBase (const _Dom1& __e1, const _Dom2& __e2,
104 : _M_expr1 (__e1), _M_expr2 (__e2), _M_func (__f) {}
106 value_type operator[] (size_t __i) const
107 { return _M_func (_M_expr1[__i], _M_expr2[__i]); }
108 size_t size () const { return _M_expr1.size (); }
111 const _Dom1& _M_expr1;
112 const _Dom2& _M_expr2;
113 _Vt (*_M_func)(_Vt, _Vt);
116 template<class _Dom> class _BinFunBase1 {
118 typedef typename _Dom::value_type value_type ;
119 typedef value_type _Vt;
121 _BinFunBase1 (const _Vt& __c, const _Dom& __e, _Vt __f(_Vt, _Vt))
122 : _M_expr1 (__c), _M_expr2 (__e), _M_func (__f) {}
124 value_type operator[] (size_t __i) const
125 { return _M_func (_M_expr1, _M_expr2[__i]); }
126 size_t size () const { return _M_expr2.size (); }
130 const _Dom& _M_expr2;
131 _Vt (*_M_func)(_Vt, _Vt);
134 template<class _Dom> class _BinFunBase2 {
136 typedef typename _Dom::value_type value_type;
137 typedef value_type _Vt;
139 _BinFunBase2 (const _Dom& __e, const _Vt& __c, _Vt __f(_Vt, _Vt))
140 : _M_expr1 (__e), _M_expr2 (__c), _M_func (__f) {}
142 value_type operator[] (size_t __i) const
143 { return _M_func (_M_expr1[__i], _M_expr2); }
144 size_t size () const { return _M_expr1.size (); }
147 const _Dom& _M_expr1;
149 _Vt (*_M_func)(_Vt, _Vt);
152 template<class _Dom1, class _Dom2>
153 struct _BinFunClos<_Expr,_Expr,_Dom1,_Dom2> : _BinFunBase<_Dom1,_Dom2> {
154 typedef _BinFunBase<_Dom1,_Dom2> _Base;
155 typedef typename _Base::value_type value_type;
156 typedef value_type _Tp;
158 _BinFunClos (const _Dom1& __e1, const _Dom2& __e2,
160 : _Base (__e1, __e2, __f) {}
163 template<typename _Tp>
164 struct _BinFunClos<_ValArray,_ValArray,_Tp,_Tp>
165 : _BinFunBase<valarray<_Tp>, valarray<_Tp> > {
166 typedef _BinFunBase<valarray<_Tp>, valarray<_Tp> > _Base;
167 typedef _Tp value_type;
169 _BinFunClos (const valarray<_Tp>& __v, const valarray<_Tp>& __w,
171 : _Base (__v, __w, __f) {}
175 struct _BinFunClos<_Expr,_ValArray,_Dom,typename _Dom::value_type>
176 : _BinFunBase<_Dom,valarray<typename _Dom::value_type> > {
177 typedef typename _Dom::value_type _Tp;
178 typedef _BinFunBase<_Dom,valarray<_Tp> > _Base;
179 typedef _Tp value_type;
181 _BinFunClos (const _Dom& __e, const valarray<_Tp>& __v,
183 : _Base (__e, __v, __f) {}
187 struct _BinFunClos<_ValArray,_Expr,typename _Dom::value_type,_Dom>
188 : _BinFunBase<valarray<typename _Dom::value_type>,_Dom> {
189 typedef typename _Dom::value_type _Tp;
190 typedef _BinFunBase<_Dom,valarray<_Tp> > _Base;
191 typedef _Tp value_type;
193 _BinFunClos (const valarray<_Tp>& __v, const _Dom& __e,
195 : _Base (__v, __e, __f) {}
199 struct _BinFunClos<_Expr,_Constant,_Dom,typename _Dom::value_type>
200 : _BinFunBase2<_Dom> {
201 typedef typename _Dom::value_type _Tp;
202 typedef _Tp value_type;
203 typedef _BinFunBase2<_Dom> _Base;
205 _BinFunClos (const _Dom& __e, const _Tp& __t, _Tp __f (_Tp, _Tp))
206 : _Base (__e, __t, __f) {}
210 struct _BinFunClos<_Constant,_Expr,_Dom,typename _Dom::value_type>
211 : _BinFunBase1<_Dom> {
212 typedef typename _Dom::value_type _Tp;
213 typedef _Tp value_type;
214 typedef _BinFunBase1<_Dom> _Base;
216 _BinFunClos (const _Tp& __t, const _Dom& __e, _Tp __f (_Tp, _Tp))
217 : _Base (__t, __e, __f) {}
220 template<typename _Tp>
221 struct _BinFunClos<_ValArray,_Constant,_Tp,_Tp>
222 : _BinFunBase2<valarray<_Tp> > {
223 typedef _BinFunBase2<valarray<_Tp> > _Base;
224 typedef _Tp value_type;
226 _BinFunClos (const valarray<_Tp>& __v, const _Tp& __t,
228 : _Base (__v, __t, __f) {}
231 template<typename _Tp>
232 struct _BinFunClos<_Constant,_ValArray,_Tp,_Tp>
233 : _BinFunBase1<valarray<_Tp> > {
234 typedef _BinFunBase1<valarray<_Tp> > _Base;
235 typedef _Tp value_type;
237 _BinFunClos (const _Tp& __t, const valarray<_Tp>& __v,
239 : _Base (__t, __v, __f) {}
243 // Apply function taking a value/const reference closure
246 template<typename _Dom, typename _Arg> class _FunBase {
248 typedef typename _Dom::value_type value_type;
250 _FunBase (const _Dom& __e, value_type __f(_Arg))
251 : _M_expr (__e), _M_func (__f) {}
253 value_type operator[] (size_t __i) const
254 { return _M_func (_M_expr[__i]); }
255 size_t size() const { return _M_expr.size ();}
259 value_type (*_M_func)(_Arg);
263 struct _ValFunClos<_Expr,_Dom>
264 : _FunBase<_Dom, typename _Dom::value_type> {
265 typedef _FunBase<_Dom, typename _Dom::value_type> _Base;
266 typedef typename _Base::value_type value_type;
267 typedef value_type _Tp;
269 _ValFunClos (const _Dom& __e, _Tp __f (_Tp)) : _Base (__e, __f) {}
272 template<typename _Tp>
273 struct _ValFunClos<_ValArray,_Tp>
274 : _FunBase<valarray<_Tp>, _Tp> {
275 typedef _FunBase<valarray<_Tp>, _Tp> _Base;
276 typedef _Tp value_type;
278 _ValFunClos (const valarray<_Tp>& __v, _Tp __f(_Tp))
279 : _Base (__v, __f) {}
283 struct _RefFunClos<_Expr,_Dom> :
284 _FunBase<_Dom, const typename _Dom::value_type&> {
285 typedef _FunBase<_Dom, const typename _Dom::value_type&> _Base;
286 typedef typename _Base::value_type value_type;
287 typedef value_type _Tp;
289 _RefFunClos (const _Dom& __e, _Tp __f (const _Tp&))
290 : _Base (__e, __f) {}
293 template<typename _Tp>
294 struct _RefFunClos<_ValArray,_Tp>
295 : _FunBase<valarray<_Tp>, const _Tp&> {
296 typedef _FunBase<valarray<_Tp>, const _Tp&> _Base;
297 typedef _Tp value_type;
299 _RefFunClos (const valarray<_Tp>& __v, _Tp __f(const _Tp&))
300 : _Base (__e, __f) {}
304 // Unary expression closure.
307 template<template<class> class _Oper, typename _Arg>
310 typedef _Oper<typename _Arg::value_type> _Op;
311 typedef typename _Op::result_type value_type;
313 _UnBase (const _Arg& __e) : _M_expr(__e) {}
314 value_type operator[] (size_t) const;
315 size_t size () const { return _M_expr.size (); }
321 template<template<class> class _Oper, typename _Arg>
322 inline typename _UnBase<_Oper, _Arg>::value_type
323 _UnBase<_Oper, _Arg>::operator[] (size_t __i) const
324 { return _Op() (_M_expr[__i]); }
326 template<template<class> class _Oper, class _Dom>
327 struct _UnClos<_Oper, _Expr, _Dom> : _UnBase<_Oper, _Dom> {
329 typedef _UnBase<_Oper, _Dom> _Base;
330 typedef typename _Base::value_type value_type;
332 _UnClos (const _Arg& __e) : _Base(__e) {}
335 template<template<class> class _Oper, typename _Tp>
336 struct _UnClos<_Oper, _ValArray, _Tp> : _UnBase<_Oper, valarray<_Tp> > {
337 typedef valarray<_Tp> _Arg;
338 typedef _UnBase<_Oper, valarray<_Tp> > _Base;
339 typedef typename _Base::value_type value_type;
341 _UnClos (const _Arg& __e) : _Base(__e) {}
346 // Binary expression closure.
349 template<template<class> class _Oper,
350 typename _FirstArg, typename _SecondArg>
353 typedef _Oper<typename _FirstArg::value_type> _Op;
354 typedef typename _Op::result_type value_type;
356 _BinBase (const _FirstArg& __e1, const _SecondArg& __e2)
357 : _M_expr1 (__e1), _M_expr2 (__e2) {}
358 value_type operator[] (size_t) const;
359 size_t size () const { return _M_expr1.size (); }
362 const _FirstArg& _M_expr1;
363 const _SecondArg& _M_expr2;
366 template<template<class> class _Oper,
367 typename _FirstArg, typename _SecondArg>
368 inline typename _BinBase<_Oper,_FirstArg,_SecondArg>::value_type
369 _BinBase<_Oper,_FirstArg,_SecondArg>::operator[] (size_t __i) const
370 { return _Op() (_M_expr1[__i], _M_expr2[__i]); }
373 template<template<class> class _Oper, class _Clos>
376 typedef typename _Clos::value_type _Vt;
377 typedef _Oper<_Vt> _Op;
378 typedef typename _Op::result_type value_type;
380 _BinBase2 (const _Clos& __e, const _Vt& __t)
381 : _M_expr1 (__e), _M_expr2 (__t) {}
382 value_type operator[] (size_t) const;
383 size_t size () const { return _M_expr1.size (); }
386 const _Clos& _M_expr1;
390 template<template<class> class _Oper, class _Clos>
391 inline typename _BinBase2<_Oper,_Clos>::value_type
392 _BinBase2<_Oper,_Clos>::operator[] (size_t __i) const
393 { return _Op() (_M_expr1[__i], _M_expr2); }
396 template<template<class> class _Oper, class _Clos>
399 typedef typename _Clos::value_type _Vt;
400 typedef _Oper<_Vt> _Op;
401 typedef typename _Op::result_type value_type;
403 _BinBase1 (const _Vt& __t, const _Clos& __e)
404 : _M_expr1 (__t), _M_expr2 (__e) {}
405 value_type operator[] (size_t) const;
406 size_t size () const { return _M_expr2.size (); }
410 const _Clos& _M_expr2;
413 template<template<class> class _Oper, class _Clos>
415 _BinBase1<_Oper,_Clos>::value_type
416 _BinBase1<_Oper,_Clos>:: operator[] (size_t __i) const
417 { return _Op() (_M_expr1, _M_expr2[__i]); }
420 template<template<class> class _Oper, class _Dom1, class _Dom2>
421 struct _BinClos<_Oper, _Expr, _Expr, _Dom1, _Dom2>
422 : _BinBase<_Oper,_Dom1,_Dom2> {
423 typedef _BinBase<_Oper,_Dom1,_Dom2> _Base;
424 typedef typename _Base::value_type value_type;
426 _BinClos(const _Dom1& __e1, const _Dom2& __e2) : _Base(__e1, __e2) {}
429 template<template<class> class _Oper, typename _Tp>
430 struct _BinClos<_Oper,_ValArray,_ValArray,_Tp,_Tp>
431 : _BinBase<_Oper,valarray<_Tp>,valarray<_Tp> > {
432 typedef _BinBase<_Oper,valarray<_Tp>,valarray<_Tp> > _Base;
433 typedef _Tp value_type;
435 _BinClos (const valarray<_Tp>& __v, const valarray<_Tp>& __w)
436 : _Base (__v, __w) {}
439 template<template<class> class _Oper, class _Dom>
440 struct _BinClos<_Oper,_Expr,_ValArray,_Dom,typename _Dom::value_type>
441 : _BinBase<_Oper,_Dom,valarray<typename _Dom::value_type> > {
442 typedef typename _Dom::value_type _Tp;
443 typedef _BinBase<_Oper,_Dom,valarray<_Tp> > _Base;
444 typedef typename _Base::value_type value_type;
446 _BinClos(const _Dom& __e1, const valarray<_Tp>& __e2)
447 : _Base (__e1, __e2) {}
450 template<template<class> class _Oper, class _Dom>
451 struct _BinClos<_Oper,_ValArray,_Expr,typename _Dom::value_type,_Dom>
452 : _BinBase<_Oper,valarray<typename _Dom::value_type>,_Dom> {
453 typedef typename _Dom::value_type _Tp;
454 typedef _BinBase<_Oper,valarray<_Tp>,_Dom> _Base;
455 typedef typename _Base::value_type value_type;
457 _BinClos (const valarray<_Tp>& __e1, const _Dom& __e2)
458 : _Base (__e1, __e2) {}
461 template<template<class> class _Oper, class _Dom>
462 struct _BinClos<_Oper,_Expr,_Constant,_Dom,typename _Dom::value_type>
463 : _BinBase2<_Oper,_Dom> {
464 typedef typename _Dom::value_type _Tp;
465 typedef _BinBase2<_Oper,_Dom> _Base;
466 typedef typename _Base::value_type value_type;
468 _BinClos (const _Dom& __e1, const _Tp& __e2) : _Base (__e1, __e2) {}
471 template<template<class> class _Oper, class _Dom>
472 struct _BinClos<_Oper,_Constant,_Expr,typename _Dom::value_type,_Dom>
473 : _BinBase1<_Oper,_Dom> {
474 typedef typename _Dom::value_type _Tp;
475 typedef _BinBase1<_Oper,_Dom> _Base;
476 typedef typename _Base::value_type value_type;
478 _BinClos (const _Tp& __e1, const _Dom& __e2) : _Base (__e1, __e2) {}
481 template<template<class> class _Oper, typename _Tp>
482 struct _BinClos<_Oper,_ValArray,_Constant,_Tp,_Tp>
483 : _BinBase2<_Oper,valarray<_Tp> > {
484 typedef _BinBase2<_Oper,valarray<_Tp> > _Base;
485 typedef typename _Base::value_type value_type;
487 _BinClos (const valarray<_Tp>& __v, const _Tp& __t)
488 : _Base (__v, __t) {}
491 template<template<class> class _Oper, typename _Tp>
492 struct _BinClos<_Oper,_Constant,_ValArray,_Tp,_Tp>
493 : _BinBase1<_Oper,valarray<_Tp> > {
494 typedef _BinBase1<_Oper,valarray<_Tp> > _Base;
495 typedef typename _Base::value_type value_type;
497 _BinClos (const _Tp& __t, const valarray<_Tp>& __v)
498 : _Base (__t, __v) {}
503 // slice_array closure.
505 template<typename _Dom> class _SBase {
507 typedef typename _Dom::value_type value_type;
509 _SBase (const _Dom& __e, const slice& __s)
510 : _M_expr (__e), _M_slice (__s) {}
511 value_type operator[] (size_t __i) const
512 { return _M_expr[_M_slice.start () + __i * _M_slice.stride ()]; }
513 size_t size() const { return _M_slice.size (); }
517 const slice& _M_slice;
520 template<typename _Tp> class _SBase<_Array<_Tp> > {
522 typedef _Tp value_type;
524 _SBase (_Array<_Tp> __a, const slice& __s)
525 : _M_array (__a._M_data+__s.start()), _M_size (__s.size()),
526 _M_stride (__s.stride()) {}
527 value_type operator[] (size_t __i) const
528 { return _M_array._M_data[__i * _M_stride]; }
529 size_t size() const { return _M_size; }
532 const _Array<_Tp> _M_array;
533 const size_t _M_size;
534 const size_t _M_stride;
537 template<class _Dom> struct _SClos<_Expr,_Dom> : _SBase<_Dom> {
538 typedef _SBase<_Dom> _Base;
539 typedef typename _Base::value_type value_type;
541 _SClos (const _Dom& __e, const slice& __s) : _Base (__e, __s) {}
544 template<typename _Tp>
545 struct _SClos<_ValArray,_Tp> : _SBase<_Array<_Tp> > {
546 typedef _SBase<_Array<_Tp> > _Base;
547 typedef _Tp value_type;
549 _SClos (_Array<_Tp> __a, const slice& __s) : _Base (__a, __s) {}
553 // gslice_array closure.
555 template<class _Dom> class _GBase {
557 typedef typename _Dom::value_type value_type;
559 _GBase (const _Dom& __e, const valarray<size_t>& __i)
560 : _M_expr (__e), _M_index(__i) {}
561 value_type operator[] (size_t __i) const
562 { return _M_expr[_M_index[__i]]; }
563 size_t size () const { return _M_index.size(); }
567 const valarray<size_t>& _M_index;
570 template<typename _Tp> class _GBase<_Array<_Tp> > {
572 typedef _Tp value_type;
574 _GBase (_Array<_Tp> __a, const valarray<size_t>& __i)
575 : _M_array (__a), _M_index(__i) {}
576 value_type operator[] (size_t __i) const
577 { return _M_array._M_data[_M_index[__i]]; }
578 size_t size () const { return _M_index.size(); }
581 const _Array<_Tp> _M_array;
582 const valarray<size_t>& _M_index;
585 template<class _Dom> struct _GClos<_Expr,_Dom> : _GBase<_Dom> {
586 typedef _GBase<_Dom> _Base;
587 typedef typename _Base::value_type value_type;
589 _GClos (const _Dom& __e, const valarray<size_t>& __i)
590 : _Base (__e, __i) {}
593 template<typename _Tp>
594 struct _GClos<_ValArray,_Tp> : _GBase<_Array<_Tp> > {
595 typedef _GBase<_Array<_Tp> > _Base;
596 typedef typename _Base::value_type value_type;
598 _GClos (_Array<_Tp> __a, const valarray<size_t>& __i)
599 : _Base (__a, __i) {}
603 // indirect_array closure
606 template<class _Dom> class _IBase {
608 typedef typename _Dom::value_type value_type;
610 _IBase (const _Dom& __e, const valarray<size_t>& __i)
611 : _M_expr (__e), _M_index (__i) {}
612 value_type operator[] (size_t __i) const
613 { return _M_expr[_M_index[__i]]; }
614 size_t size() const { return _M_index.size(); }
618 const valarray<size_t>& _M_index;
621 template<class _Dom> struct _IClos<_Expr,_Dom> : _IBase<_Dom> {
622 typedef _IBase<_Dom> _Base;
623 typedef typename _Base::value_type value_type;
625 _IClos (const _Dom& __e, const valarray<size_t>& __i)
626 : _Base (__e, __i) {}
629 template<typename _Tp>
630 struct _IClos<_ValArray,_Tp> : _IBase<valarray<_Tp> > {
631 typedef _IBase<valarray<_Tp> > _Base;
632 typedef _Tp value_type;
634 _IClos (const valarray<_Tp>& __a, const valarray<size_t>& __i)
635 : _Base (__a, __i) {}
641 template<class _Clos, typename _Tp> class _Expr {
643 typedef _Tp value_type;
645 _Expr (const _Clos&);
647 const _Clos& operator() () const;
649 value_type operator[] (size_t) const;
650 valarray<value_type> operator[] (slice) const;
651 valarray<value_type> operator[] (const gslice&) const;
652 valarray<value_type> operator[] (const valarray<bool>&) const;
653 valarray<value_type> operator[] (const valarray<size_t>&) const;
655 _Expr<_UnClos<_Unary_plus,_Expr,_Clos>, value_type>
658 _Expr<_UnClos<negate,_Expr,_Clos>, value_type>
661 _Expr<_UnClos<_Bitwise_not,_Expr,_Clos>, value_type>
664 _Expr<_UnClos<logical_not,_Expr,_Clos>, bool>
667 size_t size () const;
668 value_type sum () const;
670 valarray<value_type> shift (int) const;
671 valarray<value_type> cshift (int) const;
673 value_type min() const;
674 value_type max() const;
676 valarray<value_type> apply(value_type (*) (const value_type&)) const;
677 valarray<value_type> apply(value_type (*) (value_type)) const;
680 const _Clos _M_closure;
683 template<class _Clos, typename _Tp>
685 _Expr<_Clos,_Tp>::_Expr (const _Clos& __c) : _M_closure(__c) {}
687 template<class _Clos, typename _Tp>
689 _Expr<_Clos,_Tp>::operator() () const
690 { return _M_closure; }
692 template<class _Clos, typename _Tp>
694 _Expr<_Clos,_Tp>::operator[] (size_t __i) const
695 { return _M_closure[__i]; }
697 template<class _Clos, typename _Tp>
699 _Expr<_Clos,_Tp>::operator[] (slice __s) const
700 { return _M_closure[__s]; }
702 template<class _Clos, typename _Tp>
704 _Expr<_Clos,_Tp>::operator[] (const gslice& __gs) const
705 { return _M_closure[__gs]; }
707 template<class _Clos, typename _Tp>
709 _Expr<_Clos,_Tp>::operator[] (const valarray<bool>& __m) const
710 { return _M_closure[__m]; }
712 template<class _Clos, typename _Tp>
714 _Expr<_Clos,_Tp>::operator[] (const valarray<size_t>& __i) const
715 { return _M_closure[__i]; }
717 template<class _Clos, typename _Tp>
719 _Expr<_Clos,_Tp>::size () const { return _M_closure.size (); }
721 template<class _Clos, typename _Tp>
723 _Expr<_Clos, _Tp>::shift(int __n) const
724 { return valarray<_Tp>(_M_closure).shift(__n); }
726 template<class _Clos, typename _Tp>
728 _Expr<_Clos, _Tp>::cshift(int __n) const
729 { return valarray<_Tp>(_M_closure).cshift(__n); }
731 ttemplate<class _Clos, typename _Tp>
733 _Expr<_Clos, _Tp>::apply(_Tp __f(const _Tp&)) const
734 { return valarray<_Tp>(_M_closure).apply(__f); }
736 ttemplate<class _Clos, typename _Tp>
738 _Expr<_Clos, _Tp>::apply(_Tp __f(_Tp)) const
739 { return valarray<_Tp>(_M_closure).apply(__f); }
741 // XXX: replace this with a more robust summation algorithm.
742 template<class _Clos, typename _Tp>
744 _Expr<_Clos,_Tp>::sum () const
746 size_t __n = _M_closure.size();
747 if (__n == 0) return _Tp();
749 _Tp __s = _M_closure[--__n];
750 while (__n != 0) __s += _M_closure[--__n];
755 template<class _Clos, typename _Tp>
757 _Expr<_Clos, _Tp>::min() const
758 { return __valarray_min(_M_closure); }
760 template<class _Close, typename _Tp>
762 _Expr<_Clos, _Tp>::max() const
763 { return __valarray_max(_M_closure); }
765 template<class _Dom, typename _Tp>
766 inline _Expr<_UnClos<logical_not,_Expr,_Dom>, bool>
767 _Expr<_Dom,_Tp>::operator! () const
769 typedef _UnClos<logical_not,_Expr,_Dom> _Closure;
770 return _Expr<_Closure,_Tp> (_Closure(this->_M_closure));
773 #define _DEFINE_EXPR_UNARY_OPERATOR(_Op, _Name) \
774 template<class _Dom, typename _Tp> \
775 inline _Expr<_UnClos<_Name,_Expr,_Dom>,_Tp> \
776 _Expr<_Dom,_Tp>::operator _Op () const \
778 typedef _UnClos<_Name,_Expr,_Dom> _Closure; \
779 return _Expr<_Closure,_Tp> (_Closure (this->_M_closure)); \
782 _DEFINE_EXPR_UNARY_OPERATOR(+, _Unary_plus)
783 _DEFINE_EXPR_UNARY_OPERATOR(-, negate)
784 _DEFINE_EXPR_UNARY_OPERATOR(~, _Bitwise_not)
786 #undef _DEFINE_EXPR_UNARY_OPERATOR
789 #define _DEFINE_EXPR_BINARY_OPERATOR(_Op, _Name) \
790 template<class _Dom1, class _Dom2> \
791 inline _Expr<_BinClos<_Name,_Expr,_Expr,_Dom1,_Dom2>, \
792 typename _Name<typename _Dom1::value_type>::result_type> \
793 operator _Op (const _Expr<_Dom1,typename _Dom1::value_type>& __v, \
794 const _Expr<_Dom2,typename _Dom2::value_type>& __w) \
796 typedef typename _Dom1::value_type _Arg; \
797 typedef typename _Name<_Arg>::result_type _Value; \
798 typedef _BinClos<_Name,_Expr,_Expr,_Dom1,_Dom2> _Closure; \
799 return _Expr<_Closure,_Value> (_Closure (__v (), __w ())); \
802 template<class _Dom> \
803 inline _Expr<_BinClos<_Name,_Expr,_Constant,_Dom,typename _Dom::value_type>, \
804 typename _Name<typename _Dom::value_type>::result_type> \
805 operator _Op (const _Expr<_Dom,typename _Dom::value_type>& __v, \
806 const typename _Dom::value_type& __t) \
808 typedef typename _Dom::value_type _Arg; \
809 typedef typename _Name<_Arg>::result_type _Value; \
810 typedef _BinClos<_Name,_Expr,_Constant,_Dom,_Arg> _Closure; \
811 return _Expr<_Closure,_Value> (_Closure (__v (), __t)); \
814 template<class _Dom> \
815 inline _Expr<_BinClos<_Name,_Constant,_Expr,typename _Dom::value_type,_Dom>, \
816 typename _Name<typename _Dom::value_type>::result_type> \
817 operator _Op (const typename _Dom::value_type& __t, \
818 const _Expr<_Dom,typename _Dom::value_type>& __v) \
820 typedef typename _Dom::value_type _Arg; \
821 typedef typename _Name<_Arg>::result_type _Value; \
822 typedef _BinClos<_Name,_Constant,_Expr,_Arg,_Dom> _Closure; \
823 return _Expr<_Closure,_Value> (_Closure (__t, __v ())); \
826 template<class _Dom> \
827 inline _Expr<_BinClos<_Name,_Expr,_ValArray,_Dom,typename _Dom::value_type>, \
828 typename _Name<typename _Dom::value_type>::result_type> \
829 operator _Op (const _Expr<_Dom,typename _Dom::value_type>& __e, \
830 const valarray<typename _Dom::value_type>& __v) \
832 typedef typename _Dom::value_type _Arg; \
833 typedef typename _Name<_Arg>::result_type _Value; \
834 typedef _BinClos<_Name,_Expr,_ValArray,_Dom,_Arg> _Closure; \
835 return _Expr<_Closure,_Value> (_Closure (__e (), __v)); \
838 template<class _Dom> \
839 inline _Expr<_BinClos<_Name,_ValArray,_Expr,typename _Dom::value_type,_Dom>, \
840 typename _Name<typename _Dom::value_type>::result_type> \
841 operator _Op (const valarray<typename _Dom::value_type>& __v, \
842 const _Expr<_Dom,typename _Dom::value_type>& __e) \
844 typedef typename _Dom::value_type _Tp; \
845 typedef typename _Name<_Tp>::result_type _Value; \
846 typedef _BinClos<_Name,_ValArray,_Expr,_Tp,_Dom> _Closure; \
847 return _Expr<_Closure,_Value> (_Closure (__v, __e ())); \
850 _DEFINE_EXPR_BINARY_OPERATOR(+, plus)
851 _DEFINE_EXPR_BINARY_OPERATOR(-, minus)
852 _DEFINE_EXPR_BINARY_OPERATOR(*, multiplies)
853 _DEFINE_EXPR_BINARY_OPERATOR(/, divides)
854 _DEFINE_EXPR_BINARY_OPERATOR(%, modulus)
855 _DEFINE_EXPR_BINARY_OPERATOR(^, _Bitwise_xor)
856 _DEFINE_EXPR_BINARY_OPERATOR(&, _Bitwise_and)
857 _DEFINE_EXPR_BINARY_OPERATOR(|, _Bitwise_or)
858 _DEFINE_EXPR_BINARY_OPERATOR(<<, _Shift_left)
859 _DEFINE_EXPR_BINARY_OPERATOR(>>, _Shift_right)
861 #undef _DEFINE_EXPR_BINARY_OPERATOR
863 #define _DEFINE_EXPR_RELATIONAL_OPERATOR(_Op, _Name) \
864 template<class _Dom1, class _Dom2> \
865 inline _Expr<_BinClos<_Name,_Expr,_Expr,_Dom1,_Dom2>, bool> \
866 operator _Op (const _Expr<_Dom1,typename _Dom1::value_type>& __v, \
867 const _Expr<_Dom2,typename _Dom2::value_type>& __w) \
869 typedef typename _Dom1::value_type _Arg; \
870 typedef _BinClos<_Name,_Expr,_Expr,_Dom1,_Dom2> _Closure; \
871 return _Expr<_Closure,bool> (_Closure (__v (), __w ())); \
874 template<class _Dom> \
875 inline _Expr<_BinClos<_Name,_Expr,_Constant,_Dom,typename _Dom::value_type>, \
877 operator _Op (const _Expr<_Dom,typename _Dom::value_type>& __v, \
878 const typename _Dom::value_type& __t) \
880 typedef typename _Dom::value_type _Arg; \
881 typedef _BinClos<_Name,_Expr,_Constant,_Dom,_Arg> _Closure; \
882 return _Expr<_Closure,bool> (_Closure (__v (), __t)); \
885 template<class _Dom> \
886 inline _Expr<_BinClos<_Name,_Constant,_Expr,typename _Dom::value_type,_Dom>, \
888 operator _Op (const typename _Dom::value_type& __t, \
889 const _Expr<_Dom,typename _Dom::value_type>& __v) \
891 typedef typename _Dom::value_type _Arg; \
892 typedef _BinClos<_Name,_Constant,_Expr,_Arg,_Dom> _Closure; \
893 return _Expr<_Closure,bool> (_Closure (__t, __v ())); \
896 template<class _Dom> \
897 inline _Expr<_BinClos<_Name,_Expr,_ValArray,_Dom,typename _Dom::value_type>, \
899 operator _Op (const _Expr<_Dom,typename _Dom::value_type>& __e, \
900 const valarray<typename _Dom::value_type>& __v) \
902 typedef typename _Dom::value_type _Tp; \
903 typedef _BinClos<_Name,_Expr,_ValArray,_Dom,_Tp> _Closure; \
904 return _Expr<_Closure,bool> (_Closure (__e (), __v)); \
907 template<class _Dom> \
908 inline _Expr<_BinClos<_Name,_ValArray,_Expr,typename _Dom::value_type,_Dom>, \
910 operator _Op (const valarray<typename _Dom::value_type>& __v, \
911 const _Expr<_Dom,typename _Dom::value_type>& __e) \
913 typedef typename _Dom::value_type _Tp; \
914 typedef _BinClos<_Name,_ValArray,_Expr,_Tp,_Dom> _Closure; \
915 return _Expr<_Closure,bool> (_Closure (__v, __e ())); \
918 _DEFINE_EXPR_RELATIONAL_OPERATOR(&&, logical_and)
919 _DEFINE_EXPR_RELATIONAL_OPERATOR(||, logical_or)
920 _DEFINE_EXPR_RELATIONAL_OPERATOR(==, equal_to)
921 _DEFINE_EXPR_RELATIONAL_OPERATOR(!=, not_equal_to)
922 _DEFINE_EXPR_RELATIONAL_OPERATOR(<, less)
923 _DEFINE_EXPR_RELATIONAL_OPERATOR(>, greater)
924 _DEFINE_EXPR_RELATIONAL_OPERATOR(<=, less_equal)
925 _DEFINE_EXPR_RELATIONAL_OPERATOR(>=, greater_equal)
927 #undef _DEFINE_EXPR_RELATIONAL_OPERATOR
931 #define _DEFINE_EXPR_UNARY_FUNCTION(_Name) \
932 template<class _Dom> \
933 inline _Expr<_UnFunClos<_Expr,_Dom>,typename _Dom::value_type> \
934 _Name(const _Expr<_Dom,typename _Dom::value_type>& __e) \
936 typedef typename _Dom::value_type _Tp; \
937 typedef _UnFunClos<_Expr,_Dom> _Closure; \
938 return _Expr<_Closure,_Tp>(_Closure(__e(), (_Tp(*)(_Tp))(&_Name))); \
941 template<typename _Tp> \
942 inline _Expr<_UnFunClos<_ValArray,_Tp>,_Tp> \
943 _Name(const valarray<_Tp>& __v) \
945 typedef _UnFunClos<_ValArray,_Tp> _Closure; \
946 return _Expr<_Closure,_Tp> (_Closure (__v, (_Tp(*)(_Tp))(&_Name))); \
950 _DEFINE_EXPR_UNARY_FUNCTION(abs)
951 _DEFINE_EXPR_UNARY_FUNCTION(cos)
952 _DEFINE_EXPR_UNARY_FUNCTION(acos)
953 _DEFINE_EXPR_UNARY_FUNCTION(cosh)
954 _DEFINE_EXPR_UNARY_FUNCTION(sin)
955 _DEFINE_EXPR_UNARY_FUNCTION(asin)
956 _DEFINE_EXPR_UNARY_FUNCTION(sinh)
957 _DEFINE_EXPR_UNARY_FUNCTION(tan)
958 _DEFINE_EXPR_UNARY_FUNCTION(tanh)
959 _DEFINE_EXPR_UNARY_FUNCTION(atan)
960 _DEFINE_EXPR_UNARY_FUNCTION(exp)
961 _DEFINE_EXPR_UNARY_FUNCTION(log)
962 _DEFINE_EXPR_UNARY_FUNCTION(log10)
963 _DEFINE_EXPR_UNARY_FUNCTION(sqrt)
965 #undef _DEFINE_EXPR_UNARY_FUNCTION
968 #define _DEFINE_EXPR_BINARY_FUNCTION(_Name) \
969 template<class _Dom1, class _Dom2> \
970 inline _Expr<_BinFunClos<_Expr,_Expr,_Dom1,_Dom2>,typename _Dom1::value_type>\
971 _Name (const _Expr<_Dom1,typename _Dom1::value_type>& __e1, \
972 const _Expr<_Dom2,typename _Dom2::value_type>& __e2) \
974 typedef typename _Dom1::value_type _Tp; \
975 typedef _BinFunClos<_Expr,_Expr,_Dom1,_Dom2> _Closure; \
976 return _Expr<_Closure,_Tp> \
977 (_Closure (__e1 (), __e2 (), (_Tp(*)(_Tp, _Tp))(&_Name))); \
980 template<class _Dom> \
981 inline _Expr<_BinFunClos<_Expr,_ValArray,_Dom,typename _Dom::value_type>, \
982 typename _Dom::value_type> \
983 _Name (const _Expr<_Dom,typename _Dom::value_type>& __e, \
984 const valarray<typename _Dom::value_type>& __v) \
986 typedef typename _Dom::value_type _Tp; \
987 typedef _BinFunClos<_Expr,_ValArray,_Dom,_Tp> _Closure; \
988 return _Expr<_Closure,_Tp> \
989 (_Closure (__e (), __v, (_Tp(*)(_Tp, _Tp))(&_Name))); \
992 template<class _Dom> \
993 inline _Expr<_BinFunClos<_ValArray,_Expr,typename _Dom::value_type,_Dom>, \
994 typename _Dom::value_type> \
995 _Name (const valarray<typename _Dom::valarray>& __v, \
996 const _Expr<_Dom,typename _Dom::value_type>& __e) \
998 typedef typename _Dom::value_type _Tp; \
999 typedef _BinFunClos<_ValArray,_Expr,_Tp,_Dom> _Closure; \
1000 return _Expr<_Closure,_Tp> \
1001 (_Closure (__v, __e (), (_Tp(*)(_Tp, _Tp))(&_Name))); \
1004 template<class _Dom> \
1005 inline _Expr<_BinFunClos<_Expr,_Constant,_Dom,typename _Dom::value_type>, \
1006 typename _Dom::value_type> \
1007 _Name (const _Expr<_Dom, typename _Dom::value_type>& __e, \
1008 const typename _Dom::value_type& __t) \
1010 typedef typename _Dom::value_type _Tp; \
1011 typedef _BinFunClos<_Expr,_Constant,_Dom,_Tp> _Closure; \
1012 return _Expr<_Closure,_Tp> \
1013 (_Closure (__e (), __t, (_Tp(*)(_Tp, _Tp))(&_Name))); \
1016 template<class _Dom> \
1017 inline _Expr<_BinFunClos<_Constant,_Expr,typename _Dom::value_type,_Dom>, \
1018 typename _Dom::value_type> \
1019 _Name (const typename _Dom::value_type& __t, \
1020 const _Expr<_Dom,typename _Dom::value_type>& __e) \
1022 typedef typename _Dom::value_type _Tp; \
1023 typedef _BinFunClos<_Constant,_Expr,_Tp,_Dom> _Closure; \
1024 return _Expr<_Closure,_Tp> \
1025 (_Closure (__t, __e (), (_Tp(*)(_Tp, _Tp))(&_Name))); \
1028 template<typename _Tp> \
1029 inline _Expr<_BinFunClos<_ValArray,_ValArray,_Tp,_Tp>, _Tp> \
1030 _Name (const valarray<_Tp>& __v, const valarray<_Tp>& __w) \
1032 typedef _BinFunClos<_ValArray,_ValArray,_Tp,_Tp> _Closure; \
1033 return _Expr<_Closure,_Tp> \
1034 (_Closure (__v, __w, (_Tp(*)(_Tp,_Tp))(&_Name))); \
1037 template<typename _Tp> \
1038 inline _Expr<_BinFunClos<_ValArray,_Constant,_Tp,_Tp>,_Tp> \
1039 _Name (const valarray<_Tp>& __v, const _Tp& __t) \
1041 typedef _BinFunClos<_ValArray,_Constant,_Tp,_Tp> _Closure; \
1042 return _Expr<_Closure,_Tp> \
1043 (_Closure (__v, __t, (_Tp(*)(_Tp,_Tp))(&_Name))); \
1046 template<typename _Tp> \
1047 inline _Expr<_BinFunClos<_Constant,_ValArray,_Tp,_Tp>,_Tp> \
1048 _Name (const _Tp& __t, const valarray<_Tp>& __v) \
1050 typedef _BinFunClos<_Constant,_ValArray,_Tp,_Tp> _Closure; \
1051 return _Expr<_Closure,_Tp> \
1052 (_Closure (__t, __v, (_Tp(*)(_Tp,_Tp))(&_Name))); \
1055 _DEFINE_EXPR_BINARY_FUNCTION(atan2)
1056 _DEFINE_EXPR_BINARY_FUNCTION(pow)
1058 #undef _DEFINE_EXPR_BINARY_FUNCTION
1063 #endif /* _CPP_VALARRAY_META_H */