1 // The template and inlines for the -*- C++ -*- internal _Meta class.
3 // Copyright (C) 1997-1999 Cygnus Solutions
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
36 // Implementing a loosened valarray return value is tricky.
37 // First we need to meet 26.3.1/3: we should not add more than
38 // two levels of template nesting. Therefore we resort to template
39 // template to "flatten" loosened return value types.
40 // At some point we use partial specialization to remove one level
41 // template nesting due to _Expr<>
45 // This class is NOT defined. It doesn't need to.
46 template<typename _Tp1
, typename _Tp2
> class _Constant
;
49 // Unary function application closure.
51 template<class _Dom
> class _UnFunBase
{
53 typedef typename
_Dom::value_type value_type
;
54 typedef value_type _Vt
;
56 _UnFunBase (const _Dom
& __e
, _Vt
__f(_Vt
))
57 : _M_expr(__e
), _M_func(__f
) {}
59 _Vt
operator[] (size_t __i
) const { return _M_func(_M_expr
[__i
]); }
60 size_t size () const { return _M_expr
.size(); }
67 template<template<class, class> class _Meta
, class _Dom
> class _UnFunClos
;
70 struct _UnFunClos
<_Expr
,_Dom
> : _UnFunBase
<_Dom
> {
71 typedef _UnFunBase
<_Dom
> _Base
;
72 typedef typename
_Base::value_type value_type
;
74 _UnFunClos (const _Dom
& __e
, value_type
__f(value_type
))
78 template<typename _Tp
>
79 struct _UnFunClos
<_ValArray
,_Tp
> : _UnFunBase
<valarray
<_Tp
> > {
80 typedef _UnFunBase
<valarray
<_Tp
> > _Base
;
81 typedef typename
_Base::value_type value_type
;
83 _UnFunClos (const valarray
<_Tp
>& __v
, _Tp
__f(_Tp
))
88 // Binary function application closure.
90 template<template<class, class> class _Meta1
,
91 template<class, class> class Meta2
,
92 class _Dom1
, class _Dom2
> class _BinFunClos
;
94 template<class _Dom1
, class _Dom2
> class _BinFunBase
{
96 typedef typename
_Dom1::value_type value_type
;
97 typedef value_type _Vt
;
99 _BinFunBase (const _Dom1
& __e1
, const _Dom2
& __e2
,
101 : _M_expr1 (__e1
), _M_expr2 (__e2
), _M_func (__f
) {}
103 value_type
operator[] (size_t __i
) const
104 { return _M_func (_M_expr1
[__i
], _M_expr2
[__i
]); }
105 size_t size () const { return _M_expr1
.size (); }
108 const _Dom1
& _M_expr1
;
109 const _Dom2
& _M_expr2
;
110 _Vt (*_M_func
)(_Vt
, _Vt
);
113 template<class _Dom
> class _BinFunBase1
{
115 typedef typename
_Dom::value_type value_type
;
116 typedef value_type _Vt
;
118 _BinFunBase1 (const _Vt
& __c
, const _Dom
& __e
, _Vt
__f(_Vt
, _Vt
))
119 : _M_expr1 (__c
), _M_expr2 (__e
), _M_func (__f
) {}
121 value_type
operator[] (size_t __i
) const
122 { return _M_func (_M_expr1
, _M_expr2
[__i
]); }
123 size_t size () const { return _M_expr2
.size (); }
127 const _Dom
& _M_expr2
;
128 _Vt (*_M_func
)(_Vt
, _Vt
);
131 template<class _Dom
> class _BinFunBase2
{
133 typedef typename
_Dom::value_type value_type
;
134 typedef value_type _Vt
;
136 _BinFunBase2 (const _Dom
& __e
, const _Vt
& __c
, _Vt
__f(_Vt
, _Vt
))
137 : _M_expr1 (__e
), _M_expr2 (__c
), _M_func (__f
) {}
139 value_type
operator[] (size_t __i
) const
140 { return _M_func (_M_expr1
[__i
], _M_expr2
); }
141 size_t size () const { return _M_expr1
.size (); }
144 const _Dom
& _M_expr1
;
146 _Vt (*_M_func
)(_Vt
, _Vt
);
149 template<class _Dom1
, class _Dom2
>
150 struct _BinFunClos
<_Expr
,_Expr
,_Dom1
,_Dom2
> : _BinFunBase
<_Dom1
,_Dom2
> {
151 typedef _BinFunBase
<_Dom1
,_Dom2
> _Base
;
152 typedef typename
_Base::value_type value_type
;
153 typedef value_type _Tp
;
155 _BinFunClos (const _Dom1
& __e1
, const _Dom2
& __e2
,
157 : _Base (__e1
, __e2
, __f
) {}
160 template<typename _Tp
>
161 struct _BinFunClos
<_ValArray
,_ValArray
,_Tp
,_Tp
>
162 : _BinFunBase
<valarray
<_Tp
>, valarray
<_Tp
> > {
163 typedef _BinFunBase
<valarray
<_Tp
>, valarray
<_Tp
> > _Base
;
164 typedef _Tp value_type
;
166 _BinFunClos (const valarray
<_Tp
>& __v
, const valarray
<_Tp
>& __w
,
168 : _Base (__v
, __w
, __f
) {}
172 struct _BinFunClos
<_Expr
,_ValArray
,_Dom
,typename
_Dom::value_type
>
173 : _BinFunBase
<_Dom
,valarray
<typename
_Dom::value_type
> > {
174 typedef typename
_Dom::value_type _Tp
;
175 typedef _BinFunBase
<_Dom
,valarray
<_Tp
> > _Base
;
176 typedef _Tp value_type
;
178 _BinFunClos (const _Dom
& __e
, const valarray
<_Tp
>& __v
,
180 : _Base (__e
, __v
, __f
) {}
184 struct _BinFunClos
<_ValArray
,_Expr
,typename
_Dom::value_type
,_Dom
>
185 : _BinFunBase
<valarray
<typename
_Dom::value_type
>,_Dom
> {
186 typedef typename
_Dom::value_type _Tp
;
187 typedef _BinFunBase
<_Dom
,valarray
<_Tp
> > _Base
;
188 typedef _Tp value_type
;
190 _BinFunClos (const valarray
<_Tp
>& __v
, const _Dom
& __e
,
192 : _Base (__v
, __e
, __f
) {}
196 struct _BinFunClos
<_Expr
,_Constant
,_Dom
,typename
_Dom::value_type
>
197 : _BinFunBase2
<_Dom
> {
198 typedef typename
_Dom::value_type _Tp
;
199 typedef _Tp value_type
;
200 typedef _BinFunBase2
<_Dom
> _Base
;
202 _BinFunClos (const _Dom
& __e
, const _Tp
& __t
, _Tp
__f (_Tp
, _Tp
))
203 : _Base (__e
, __t
, __f
) {}
207 struct _BinFunClos
<_Constant
,_Expr
,_Dom
,typename
_Dom::value_type
>
208 : _BinFunBase1
<_Dom
> {
209 typedef typename
_Dom::value_type _Tp
;
210 typedef _Tp value_type
;
211 typedef _BinFunBase1
<_Dom
> _Base
;
213 _BinFunClos (const _Tp
& __t
, const _Dom
& __e
, _Tp
__f (_Tp
, _Tp
))
214 : _Base (__t
, __e
, __f
) {}
217 template<typename _Tp
>
218 struct _BinFunClos
<_ValArray
,_Constant
,_Tp
,_Tp
>
219 : _BinFunBase2
<valarray
<_Tp
> > {
220 typedef _BinFunBase2
<valarray
<_Tp
> > _Base
;
221 typedef _Tp value_type
;
223 _BinFunClos (const valarray
<_Tp
>& __v
, const _Tp
& __t
,
225 : _Base (__v
, __t
, __f
) {}
228 template<typename _Tp
>
229 struct _BinFunClos
<_Constant
,_ValArray
,_Tp
,_Tp
>
230 : _BinFunBase1
<valarray
<_Tp
> > {
231 typedef _BinFunBase1
<valarray
<_Tp
> > _Base
;
232 typedef _Tp value_type
;
234 _BinFunClos (const _Tp
& __t
, const valarray
<_Tp
>& __v
,
236 : _Base (__t
, __v
, __f
) {}
240 // Apply function taking a value/const reference closure
243 template<typename _Dom
, typename _Arg
> class _FunBase
{
245 typedef typename
_Dom::value_type value_type
;
247 _FunBase (const _Dom
& __e
, value_type
__f(_Arg
))
248 : _M_expr (__e
), _M_func (__f
) {}
250 value_type
operator[] (size_t __i
) const
251 { return _M_func (_M_expr
[__i
]); }
252 size_t size() const { return _M_expr
.size ();}
256 value_type (*_M_func
)(_Arg
);
260 struct _ValFunClos
<_Expr
,_Dom
>
261 : _FunBase
<_Dom
, typename
_Dom::value_type
> {
262 typedef _FunBase
<_Dom
, typename
_Dom::value_type
> _Base
;
263 typedef typename
_Base::value_type value_type
;
264 typedef value_type _Tp
;
266 _ValFunClos (const _Dom
& __e
, _Tp
__f (_Tp
)) : _Base (__e
, __f
) {}
269 template<typename _Tp
>
270 struct _ValFunClos
<_ValArray
,_Tp
>
271 : _FunBase
<valarray
<_Tp
>, _Tp
> {
272 typedef _FunBase
<valarray
<_Tp
>, _Tp
> _Base
;
273 typedef _Tp value_type
;
275 _ValFunClos (const valarray
<_Tp
>& __v
, _Tp
__f(_Tp
))
276 : _Base (__v
, __f
) {}
280 struct _RefFunClos
<_Expr
,_Dom
> :
281 _FunBase
<_Dom
, const typename
_Dom::value_type
&> {
282 typedef _FunBase
<_Dom
, const typename
_Dom::value_type
&> _Base
;
283 typedef typename
_Base::value_type value_type
;
284 typedef value_type _Tp
;
286 _RefFunClos (const _Dom
& __e
, _Tp
__f (const _Tp
&))
287 : _Base (__e
, __f
) {}
290 template<typename _Tp
>
291 struct _RefFunClos
<_ValArray
,_Tp
>
292 : _FunBase
<valarray
<_Tp
>, const _Tp
&> {
293 typedef _FunBase
<valarray
<_Tp
>, const _Tp
&> _Base
;
294 typedef _Tp value_type
;
296 _RefFunClos (const valarray
<_Tp
>& __v
, _Tp
__f(const _Tp
&))
297 : _Base (__v
, __f
) {}
301 // Unary expression closure.
304 template<template<class> class _Oper
, typename _Arg
>
307 typedef _Oper
<typename
_Arg::value_type
> _Op
;
308 typedef typename
_Op::result_type value_type
;
310 _UnBase (const _Arg
& __e
) : _M_expr(__e
) {}
311 value_type
operator[] (size_t) const;
312 size_t size () const { return _M_expr
.size (); }
318 template<template<class> class _Oper
, typename _Arg
>
319 inline typename _UnBase
<_Oper
, _Arg
>::value_type
320 _UnBase
<_Oper
, _Arg
>::operator[] (size_t __i
) const
321 { return _Op() (_M_expr
[__i
]); }
323 template<template<class> class _Oper
, class _Dom
>
324 struct _UnClos
<_Oper
, _Expr
, _Dom
> : _UnBase
<_Oper
, _Dom
> {
326 typedef _UnBase
<_Oper
, _Dom
> _Base
;
327 typedef typename
_Base::value_type value_type
;
329 _UnClos (const _Arg
& __e
) : _Base(__e
) {}
332 template<template<class> class _Oper
, typename _Tp
>
333 struct _UnClos
<_Oper
, _ValArray
, _Tp
> : _UnBase
<_Oper
, valarray
<_Tp
> > {
334 typedef valarray
<_Tp
> _Arg
;
335 typedef _UnBase
<_Oper
, valarray
<_Tp
> > _Base
;
336 typedef typename
_Base::value_type value_type
;
338 _UnClos (const _Arg
& __e
) : _Base(__e
) {}
343 // Binary expression closure.
346 template<template<class> class _Oper
, typename _FirstArg
, typename _SecondArg
>
349 typedef _Oper
<typename
_FirstArg::value_type
> _Op
;
350 typedef typename
_Op::result_type value_type
;
352 _BinBase (const _FirstArg
& __e1
, const _SecondArg
& __e2
)
353 : _M_expr1 (__e1
), _M_expr2 (__e2
) {}
354 value_type
operator[] (size_t) const;
355 size_t size () const { return _M_expr1
.size (); }
358 const _FirstArg
& _M_expr1
;
359 const _SecondArg
& _M_expr2
;
362 template<template<class> class _Oper
, typename _FirstArg
, typename _SecondArg
>
363 inline typename _BinBase
<_Oper
,_FirstArg
,_SecondArg
>::value_type
364 _BinBase
<_Oper
,_FirstArg
,_SecondArg
>::operator[] (size_t __i
) const
365 { return _Op() (_M_expr1
[__i
], _M_expr2
[__i
]); }
368 template<template<class> class _Oper
, class _Clos
>
371 typedef typename
_Clos::value_type _Vt
;
372 typedef _Oper
<_Vt
> _Op
;
373 typedef typename
_Op::result_type value_type
;
375 _BinBase2 (const _Clos
& __e
, const _Vt
& __t
)
376 : _M_expr1 (__e
), _M_expr2 (__t
) {}
377 value_type
operator[] (size_t) const;
378 size_t size () const { return _M_expr1
.size (); }
381 const _Clos
& _M_expr1
;
385 template<template<class> class _Oper
, class _Clos
>
386 inline typename _BinBase2
<_Oper
,_Clos
>::value_type
387 _BinBase2
<_Oper
,_Clos
>::operator[] (size_t __i
) const
388 { return _Op() (_M_expr1
[__i
], _M_expr2
); }
391 template<template<class> class _Oper
, class _Clos
>
394 typedef typename
_Clos::value_type _Vt
;
395 typedef _Oper
<_Vt
> _Op
;
396 typedef typename
_Op::result_type value_type
;
398 _BinBase1 (const _Vt
& __t
, const _Clos
& __e
)
399 : _M_expr1 (__t
), _M_expr2 (__e
) {}
400 value_type
operator[] (size_t) const;
401 size_t size () const { return _M_expr2
.size (); }
405 const _Clos
& _M_expr2
;
408 template<template<class> class _Oper
, class _Clos
>
410 _BinBase1
<_Oper
,_Clos
>::value_type
411 _BinBase1
<_Oper
,_Clos
>:: operator[] (size_t __i
) const
412 { return _Op() (_M_expr1
, _M_expr2
[__i
]); }
415 template<template<class> class _Oper
, class _Dom1
, class _Dom2
>
416 struct _BinClos
<_Oper
, _Expr
, _Expr
, _Dom1
, _Dom2
>
417 : _BinBase
<_Oper
,_Dom1
,_Dom2
> {
418 typedef _BinBase
<_Oper
,_Dom1
,_Dom2
> _Base
;
419 typedef typename
_Base::value_type value_type
;
421 _BinClos(const _Dom1
& __e1
, const _Dom2
& __e2
) : _Base(__e1
, __e2
) {}
424 template<template<class> class _Oper
, typename _Tp
>
425 struct _BinClos
<_Oper
,_ValArray
,_ValArray
,_Tp
,_Tp
>
426 : _BinBase
<_Oper
,valarray
<_Tp
>,valarray
<_Tp
> > {
427 typedef _BinBase
<_Oper
,valarray
<_Tp
>,valarray
<_Tp
> > _Base
;
428 typedef _Tp value_type
;
430 _BinClos (const valarray
<_Tp
>& __v
, const valarray
<_Tp
>& __w
)
431 : _Base (__v
, __w
) {}
434 template<template<class> class _Oper
, class _Dom
>
435 struct _BinClos
<_Oper
,_Expr
,_ValArray
,_Dom
,typename
_Dom::value_type
>
436 : _BinBase
<_Oper
,_Dom
,valarray
<typename
_Dom::value_type
> > {
437 typedef typename
_Dom::value_type _Tp
;
438 typedef _BinBase
<_Oper
,_Dom
,valarray
<_Tp
> > _Base
;
439 typedef typename
_Base::value_type value_type
;
441 _BinClos(const _Dom
& __e1
, const valarray
<_Tp
>& __e2
)
442 : _Base (__e1
, __e2
) {}
445 template<template<class> class _Oper
, class _Dom
>
446 struct _BinClos
<_Oper
,_ValArray
,_Expr
,typename
_Dom::value_type
,_Dom
>
447 : _BinBase
<_Oper
,valarray
<typename
_Dom::value_type
>,_Dom
> {
448 typedef typename
_Dom::value_type _Tp
;
449 typedef _BinBase
<_Oper
,valarray
<_Tp
>,_Dom
> _Base
;
450 typedef typename
_Base::value_type value_type
;
452 _BinClos (const valarray
<_Tp
>& __e1
, const _Dom
& __e2
)
453 : _Base (__e1
, __e2
) {}
456 template<template<class> class _Oper
, class _Dom
>
457 struct _BinClos
<_Oper
,_Expr
,_Constant
,_Dom
,typename
_Dom::value_type
>
458 : _BinBase2
<_Oper
,_Dom
> {
459 typedef typename
_Dom::value_type _Tp
;
460 typedef _BinBase2
<_Oper
,_Dom
> _Base
;
461 typedef typename
_Base::value_type value_type
;
463 _BinClos (const _Dom
& __e1
, const _Tp
& __e2
) : _Base (__e1
, __e2
) {}
466 template<template<class> class _Oper
, class _Dom
>
467 struct _BinClos
<_Oper
,_Constant
,_Expr
,typename
_Dom::value_type
,_Dom
>
468 : _BinBase1
<_Oper
,_Dom
> {
469 typedef typename
_Dom::value_type _Tp
;
470 typedef _BinBase1
<_Oper
,_Dom
> _Base
;
471 typedef typename
_Base::value_type value_type
;
473 _BinClos (const _Tp
& __e1
, const _Dom
& __e2
) : _Base (__e1
, __e2
) {}
476 template<template<class> class _Oper
, typename _Tp
>
477 struct _BinClos
<_Oper
,_ValArray
,_Constant
,_Tp
,_Tp
>
478 : _BinBase2
<_Oper
,valarray
<_Tp
> > {
479 typedef _BinBase2
<_Oper
,valarray
<_Tp
> > _Base
;
480 typedef typename
_Base::value_type value_type
;
482 _BinClos (const valarray
<_Tp
>& __v
, const _Tp
& __t
)
483 : _Base (__v
, __t
) {}
486 template<template<class> class _Oper
, typename _Tp
>
487 struct _BinClos
<_Oper
,_Constant
,_ValArray
,_Tp
,_Tp
>
488 : _BinBase1
<_Oper
,valarray
<_Tp
> > {
489 typedef _BinBase1
<_Oper
,valarray
<_Tp
> > _Base
;
490 typedef typename
_Base::value_type value_type
;
492 _BinClos (const _Tp
& __t
, const valarray
<_Tp
>& __v
)
493 : _Base (__t
, __v
) {}
498 // slice_array closure.
500 template<typename _Dom
> class _SBase
{
502 typedef typename
_Dom::value_type value_type
;
504 _SBase (const _Dom
& __e
, const slice
& __s
)
505 : _M_expr (__e
), _M_slice (__s
) {}
506 value_type
operator[] (size_t __i
) const
507 { return _M_expr
[_M_slice
.start () + __i
* _M_slice
.stride ()]; }
508 size_t size() const { return _M_slice
.size (); }
512 const slice
& _M_slice
;
515 template<typename _Tp
> class _SBase
<_Array
<_Tp
> > {
517 typedef _Tp value_type
;
519 _SBase (_Array
<_Tp
> __a
, const slice
& __s
)
520 : _M_array (__a
._M_data
+__s
.start()), _M_size (__s
.size()),
521 _M_stride (__s
.stride()) {}
522 value_type
operator[] (size_t __i
) const
523 { return _M_array
._M_data
[__i
* _M_stride
]; }
524 size_t size() const { return _M_size
; }
527 const _Array
<_Tp
> _M_array
;
528 const size_t _M_size
;
529 const size_t _M_stride
;
532 template<class _Dom
> struct _SClos
<_Expr
,_Dom
> : _SBase
<_Dom
> {
533 typedef _SBase
<_Dom
> _Base
;
534 typedef typename
_Base::value_type value_type
;
536 _SClos (const _Dom
& __e
, const slice
& __s
) : _Base (__e
, __s
) {}
539 template<typename _Tp
>
540 struct _SClos
<_ValArray
,_Tp
> : _SBase
<_Array
<_Tp
> > {
541 typedef _SBase
<_Array
<_Tp
> > _Base
;
542 typedef _Tp value_type
;
544 _SClos (_Array
<_Tp
> __a
, const slice
& __s
) : _Base (__a
, __s
) {}
548 // gslice_array closure.
550 template<class _Dom
> class _GBase
{
552 typedef typename
_Dom::value_type value_type
;
554 _GBase (const _Dom
& __e
, const valarray
<size_t>& __i
)
555 : _M_expr (__e
), _M_index(__i
) {}
556 value_type
operator[] (size_t __i
) const
557 { return _M_expr
[_M_index
[__i
]]; }
558 size_t size () const { return _M_index
.size(); }
562 const valarray
<size_t>& _M_index
;
565 template<typename _Tp
> class _GBase
<_Array
<_Tp
> > {
567 typedef _Tp value_type
;
569 _GBase (_Array
<_Tp
> __a
, const valarray
<size_t>& __i
)
570 : _M_array (__a
), _M_index(__i
) {}
571 value_type
operator[] (size_t __i
) const
572 { return _M_array
._M_data
[_M_index
[__i
]]; }
573 size_t size () const { return _M_index
.size(); }
576 const _Array
<_Tp
> _M_array
;
577 const valarray
<size_t>& _M_index
;
580 template<class _Dom
> struct _GClos
<_Expr
,_Dom
> : _GBase
<_Dom
> {
581 typedef _GBase
<_Dom
> _Base
;
582 typedef typename
_Base::value_type value_type
;
584 _GClos (const _Dom
& __e
, const valarray
<size_t>& __i
)
585 : _Base (__e
, __i
) {}
588 template<typename _Tp
>
589 struct _GClos
<_ValArray
,_Tp
> : _GBase
<_Array
<_Tp
> > {
590 typedef _GBase
<_Array
<_Tp
> > _Base
;
591 typedef typename
_Base::value_type value_type
;
593 _GClos (_Array
<_Tp
> __a
, const valarray
<size_t>& __i
)
594 : _Base (__a
, __i
) {}
598 // indirect_array closure
601 template<class _Dom
> class _IBase
{
603 typedef typename
_Dom::value_type value_type
;
605 _IBase (const _Dom
& __e
, const valarray
<size_t>& __i
)
606 : _M_expr (__e
), _M_index (__i
) {}
607 value_type
operator[] (size_t __i
) const
608 { return _M_expr
[_M_index
[__i
]]; }
609 size_t size() const { return _M_index
.size(); }
613 const valarray
<size_t>& _M_index
;
616 template<class _Dom
> struct _IClos
<_Expr
,_Dom
> : _IBase
<_Dom
> {
617 typedef _IBase
<_Dom
> _Base
;
618 typedef typename
_Base::value_type value_type
;
620 _IClos (const _Dom
& __e
, const valarray
<size_t>& __i
)
621 : _Base (__e
, __i
) {}
624 template<typename _Tp
>
625 struct _IClos
<_ValArray
,_Tp
> : _IBase
<valarray
<_Tp
> > {
626 typedef _IBase
<valarray
<_Tp
> > _Base
;
627 typedef _Tp value_type
;
629 _IClos (const valarray
<_Tp
>& __a
, const valarray
<size_t>& __i
)
630 : _Base (__a
, __i
) {}
636 template<class _Clos
, typename _Tp
> class _Expr
{
638 typedef _Tp value_type
;
640 _Expr (const _Clos
&);
642 const _Clos
& operator() () const;
644 value_type
operator[] (size_t) const;
645 valarray
<value_type
> operator[] (slice
) const;
646 valarray
<value_type
> operator[] (const gslice
&) const;
647 valarray
<value_type
> operator[] (const valarray
<bool>&) const;
648 valarray
<value_type
> operator[] (const valarray
<size_t>&) const;
650 _Expr
<_UnClos
<_Unary_plus
,_Expr
,_Clos
>, value_type
>
653 _Expr
<_UnClos
<negate
,_Expr
,_Clos
>, value_type
>
656 _Expr
<_UnClos
<_Bitwise_not
,_Expr
,_Clos
>, value_type
>
659 _Expr
<_UnClos
<logical_not
,_Expr
,_Clos
>, bool>
662 size_t size () const;
663 value_type
sum () const;
665 valarray
<value_type
> shift (int) const;
666 valarray
<value_type
> cshift (int) const;
667 // _Meta<_ApplyFunctionWithValue<_Expr>, value_type>
668 // apply (value_type _M_func (value_type)) const;
669 // _Meta<_ApplyFunctionWithConstRef<_Expr>, value_type>
670 // apply (value_type _M_func (const value_type&)) const;
673 const _Clos _M_closure
;
676 template<class _Clos
, typename _Tp
>
678 _Expr
<_Clos
,_Tp
>::_Expr (const _Clos
& __c
) : _M_closure(__c
) {}
680 template<class _Clos
, typename _Tp
>
682 _Expr
<_Clos
,_Tp
>::operator() () const
683 { return _M_closure
; }
685 template<class _Clos
, typename _Tp
>
687 _Expr
<_Clos
,_Tp
>::operator[] (size_t __i
) const
688 { return _M_closure
[__i
]; }
690 template<class _Clos
, typename _Tp
>
692 _Expr
<_Clos
,_Tp
>::operator[] (slice __s
) const
693 { return _M_closure
[__s
]; }
695 template<class _Clos
, typename _Tp
>
697 _Expr
<_Clos
,_Tp
>::operator[] (const gslice
& __gs
) const
698 { return _M_closure
[__gs
]; }
700 template<class _Clos
, typename _Tp
>
702 _Expr
<_Clos
,_Tp
>::operator[] (const valarray
<bool>& __m
) const
703 { return _M_closure
[__m
]; }
705 template<class _Clos
, typename _Tp
>
707 _Expr
<_Clos
,_Tp
>::operator[] (const valarray
<size_t>& __i
) const
708 { return _M_closure
[__i
]; }
710 template<class _Clos
, typename _Tp
>
712 _Expr
<_Clos
,_Tp
>::size () const { return _M_closure
.size (); }
714 // XXX: replace this with a more robust summation algorithm.
715 template<class _Clos
, typename _Tp
>
717 _Expr
<_Clos
,_Tp
>::sum () const
720 size_t __n (_M_closure
.size ());
721 for (size_t __i
=0; __i
<__n
; ++__i
) __s
+= _M_closure
[__i
];
725 template<class _Dom
, typename _Tp
>
727 min (const _Expr
<_Dom
,_Tp
>& __e
)
729 size_t __s (__e
.size ());
731 for (size_t __i
=1; __i
<__s
; ++__i
)
732 if (__m
> __e
[__i
]) __m
= __e
[__i
];
736 template<class _Dom
, typename _Tp
>
738 max (const _Expr
<_Dom
,_Tp
>& __e
)
740 size_t __s (__e
.size());
742 for (size_t __i
=1; __i
<__s
; ++__i
)
743 if (__m
< __e
[__i
]) __m
= __e
[__i
];
747 template<class _Dom
, typename _Tp
>
748 inline _Expr
<_UnClos
<logical_not
,_Expr
,_Dom
>, bool>
749 _Expr
<_Dom
,_Tp
>::operator! () const
751 typedef _UnClos
<logical_not
,_Expr
,_Dom
> _Closure
;
752 return _Expr
<_Closure
,_Tp
> (_Closure(this->_M_closure
));
755 #define _DEFINE_EXPR_UNARY_OPERATOR(_Op, _Name) \
756 template<class _Dom, typename _Tp> \
757 inline _Expr<_UnClos<_Name,_Expr,_Dom>,_Tp> \
758 _Expr<_Dom,_Tp>::operator _Op () const \
760 typedef _UnClos<_Name,_Expr,_Dom> _Closure; \
761 return _Expr<_Closure,_Tp> (_Closure (this->_M_closure)); \
764 _DEFINE_EXPR_UNARY_OPERATOR(+, _Unary_plus
)
765 _DEFINE_EXPR_UNARY_OPERATOR(-, negate
)
766 _DEFINE_EXPR_UNARY_OPERATOR(~, _Bitwise_not
)
768 #undef _DEFINE_EXPR_UNARY_OPERATOR
771 #define _DEFINE_EXPR_BINARY_OPERATOR(_Op, _Name) \
772 template<class _Dom1, class _Dom2> \
773 inline _Expr<_BinClos<_Name,_Expr,_Expr,_Dom1,_Dom2>, \
774 typename _Name<typename _Dom1::value_type>::result_type> \
775 operator _Op (const _Expr<_Dom1,typename _Dom1::value_type>& __v, \
776 const _Expr<_Dom2,typename _Dom2::value_type>& __w) \
778 typedef typename _Dom1::value_type _Arg; \
779 typedef typename _Name<_Arg>::result_type _Value; \
780 typedef _BinClos<_Name,_Expr,_Expr,_Dom1,_Dom2> _Closure; \
781 return _Expr<_Closure,_Value> (_Closure (__v (), __w ())); \
784 template<class _Dom> \
785 inline _Expr<_BinClos<_Name,_Expr,_Constant,_Dom,typename _Dom::value_type>, \
786 typename _Name<typename _Dom::value_type>::result_type> \
787 operator _Op (const _Expr<_Dom,typename _Dom::value_type>& __v, \
788 const typename _Dom::value_type& __t) \
790 typedef typename _Dom::value_type _Arg; \
791 typedef typename _Name<_Arg>::result_type _Value; \
792 typedef _BinClos<_Name,_Expr,_Constant,_Dom,_Arg> _Closure; \
793 return _Expr<_Closure,_Value> (_Closure (__v (), __t)); \
796 template<class _Dom> \
797 inline _Expr<_BinClos<_Name,_Constant,_Expr,typename _Dom::value_type,_Dom>, \
798 typename _Name<typename _Dom::value_type>::result_type> \
799 operator _Op (const typename _Dom::value_type& __t, \
800 const _Expr<_Dom,typename _Dom::value_type>& __v) \
802 typedef typename _Dom::value_type _Arg; \
803 typedef typename _Name<_Arg>::result_type _Value; \
804 typedef _BinClos<_Name,_Constant,_Expr,_Arg,_Dom> _Closure; \
805 return _Expr<_Closure,_Value> (_Closure (__t, __v ())); \
808 template<class _Dom> \
809 inline _Expr<_BinClos<_Name,_Expr,_ValArray,_Dom,typename _Dom::value_type>, \
810 typename _Name<typename _Dom::value_type>::result_type> \
811 operator _Op (const _Expr<_Dom,typename _Dom::value_type>& __e, \
812 const valarray<typename _Dom::value_type>& __v) \
814 typedef typename _Dom::value_type _Arg; \
815 typedef typename _Name<_Arg>::result_type _Value; \
816 typedef _BinClos<_Name,_Expr,_ValArray,_Dom,_Arg> _Closure; \
817 return _Expr<_Closure,_Value> (_Closure (__e (), __v)); \
820 template<class _Dom> \
821 inline _Expr<_BinClos<_Name,_ValArray,_Expr,typename _Dom::value_type,_Dom>, \
822 typename _Name<typename _Dom::value_type>::result_type> \
823 operator _Op (const valarray<typename _Dom::value_type>& __v, \
824 const _Expr<_Dom,typename _Dom::value_type>& __e) \
826 typedef typename _Dom::value_type _Tp; \
827 typedef typename _Name<_Tp>::result_type _Value; \
828 typedef _BinClos<_Name,_ValArray,_Expr,_Tp,_Dom> _Closure; \
829 return _Expr<_Closure,_Value> (_Closure (__v, __e ())); \
832 _DEFINE_EXPR_BINARY_OPERATOR(+, plus
)
833 _DEFINE_EXPR_BINARY_OPERATOR(-, minus
)
834 _DEFINE_EXPR_BINARY_OPERATOR(*, multiplies
)
835 _DEFINE_EXPR_BINARY_OPERATOR(/, divides
)
836 _DEFINE_EXPR_BINARY_OPERATOR(%, modulus
)
837 _DEFINE_EXPR_BINARY_OPERATOR(^, _Bitwise_xor
)
838 _DEFINE_EXPR_BINARY_OPERATOR(&, _Bitwise_and
)
839 _DEFINE_EXPR_BINARY_OPERATOR(|, _Bitwise_or
)
840 _DEFINE_EXPR_BINARY_OPERATOR(<<, _Shift_left
)
841 _DEFINE_EXPR_BINARY_OPERATOR(>>, _Shift_right
)
843 #undef _DEFINE_EXPR_BINARY_OPERATOR
845 #define _DEFINE_EXPR_RELATIONAL_OPERATOR(_Op, _Name) \
846 template<class _Dom1, class _Dom2> \
847 inline _Expr<_BinClos<_Name,_Expr,_Expr,_Dom1,_Dom2>, bool> \
848 operator _Op (const _Expr<_Dom1,typename _Dom1::value_type>& __v, \
849 const _Expr<_Dom2,typename _Dom2::value_type>& __w) \
851 typedef typename _Dom1::value_type _Arg; \
852 typedef _BinClos<_Name,_Expr,_Expr,_Dom1,_Dom2> _Closure; \
853 return _Expr<_Closure,bool> (_Closure (__v (), __w ())); \
856 template<class _Dom> \
857 inline _Expr<_BinClos<_Name,_Expr,_Constant,_Dom,typename _Dom::value_type>, \
859 operator _Op (const _Expr<_Dom,typename _Dom::value_type>& __v, \
860 const typename _Dom::value_type& __t) \
862 typedef typename _Dom::value_type _Arg; \
863 typedef _BinClos<_Name,_Expr,_Constant,_Dom,_Arg> _Closure; \
864 return _Expr<_Closure,bool> (_Closure (__v (), __t)); \
867 template<class _Dom> \
868 inline _Expr<_BinClos<_Name,_Constant,_Expr,typename _Dom::value_type,_Dom>, \
870 operator _Op (const typename _Dom::value_type& __t, \
871 const _Expr<_Dom,typename _Dom::value_type>& __v) \
873 typedef typename _Dom::value_type _Arg; \
874 typedef _BinClos<_Name,_Constant,_Expr,_Arg,_Dom> _Closure; \
875 return _Expr<_Closure,bool> (_Closure (__t, __v ())); \
878 template<class _Dom> \
879 inline _Expr<_BinClos<_Name,_Expr,_ValArray,_Dom,typename _Dom::value_type>, \
881 operator _Op (const _Expr<_Dom,typename _Dom::value_type>& __e, \
882 const valarray<typename _Dom::value_type>& __v) \
884 typedef typename _Dom::value_type _Tp; \
885 typedef _BinClos<_Name,_Expr,_ValArray,_Dom,_Tp> _Closure; \
886 return _Expr<_Closure,bool> (_Closure (__e (), __v)); \
889 template<class _Dom> \
890 inline _Expr<_BinClos<_Name,_ValArray,_Expr,typename _Dom::value_type,_Dom>, \
892 operator _Op (const valarray<typename _Dom::value_type>& __v, \
893 const _Expr<_Dom,typename _Dom::value_type>& __e) \
895 typedef typename _Dom::value_type _Tp; \
896 typedef _BinClos<_Name,_ValArray,_Expr,_Tp,_Dom> _Closure; \
897 return _Expr<_Closure,bool> (_Closure (__v, __e ())); \
900 _DEFINE_EXPR_RELATIONAL_OPERATOR(&&, logical_and
)
901 _DEFINE_EXPR_RELATIONAL_OPERATOR(||, logical_or
)
902 _DEFINE_EXPR_RELATIONAL_OPERATOR(==, equal_to
)
903 _DEFINE_EXPR_RELATIONAL_OPERATOR(!=, not_equal_to
)
904 _DEFINE_EXPR_RELATIONAL_OPERATOR(<, less
)
905 _DEFINE_EXPR_RELATIONAL_OPERATOR(>, greater
)
906 _DEFINE_EXPR_RELATIONAL_OPERATOR(<=, less_equal
)
907 _DEFINE_EXPR_RELATIONAL_OPERATOR(>=, greater_equal
)
909 #undef _DEFINE_EXPR_RELATIONAL_OPERATOR
913 #define _DEFINE_EXPR_UNARY_FUNCTION(_Name) \
914 template<class _Dom> \
915 inline _Expr<_UnFunClos<_Expr,_Dom>,typename _Dom::value_type> \
916 _Name(const _Expr<_Dom,typename _Dom::value_type>& __e) \
918 typedef typename _Dom::value_type _Tp; \
919 typedef _UnFunClos<_Expr,_Dom> _Closure; \
920 return _Expr<_Closure,_Tp>(_Closure(__e(), (_Tp(*)(_Tp))(&_Name))); \
923 template<typename _Tp> \
924 inline _Expr<_UnFunClos<_ValArray,_Tp>,_Tp> \
925 _Name(const valarray<_Tp>& __v) \
927 typedef _UnFunClos<_ValArray,_Tp> _Closure; \
928 return _Expr<_Closure,_Tp> (_Closure (__v, (_Tp(*)(_Tp))(&_Name))); \
932 _DEFINE_EXPR_UNARY_FUNCTION(abs
)
933 _DEFINE_EXPR_UNARY_FUNCTION(cos
)
934 _DEFINE_EXPR_UNARY_FUNCTION(acos
)
935 _DEFINE_EXPR_UNARY_FUNCTION(cosh
)
936 _DEFINE_EXPR_UNARY_FUNCTION(sin
)
937 _DEFINE_EXPR_UNARY_FUNCTION(asin
)
938 _DEFINE_EXPR_UNARY_FUNCTION(sinh
)
939 _DEFINE_EXPR_UNARY_FUNCTION(tan
)
940 _DEFINE_EXPR_UNARY_FUNCTION(tanh
)
941 _DEFINE_EXPR_UNARY_FUNCTION(atan
)
942 _DEFINE_EXPR_UNARY_FUNCTION(exp
)
943 _DEFINE_EXPR_UNARY_FUNCTION(log
)
944 _DEFINE_EXPR_UNARY_FUNCTION(log10
)
945 _DEFINE_EXPR_UNARY_FUNCTION(sqrt
)
947 #undef _DEFINE_EXPR_UNARY_FUNCTION
950 #define _DEFINE_EXPR_BINARY_FUNCTION(_Name) \
951 template<class _Dom1, class _Dom2> \
952 inline _Expr<_BinFunClos<_Expr,_Expr,_Dom1,_Dom2>,typename _Dom1::value_type>\
953 _Name (const _Expr<_Dom1,typename _Dom1::value_type>& __e1, \
954 const _Expr<_Dom2,typename _Dom2::value_type>& __e2) \
956 typedef typename _Dom1::value_type _Tp; \
957 typedef _BinFunClos<_Expr,_Expr,_Dom1,_Dom2> _Closure; \
958 return _Expr<_Closure,_Tp> \
959 (_Closure (__e1 (), __e2 (), (_Tp(*)(_Tp, _Tp))(&_Name))); \
962 template<class _Dom> \
963 inline _Expr<_BinFunClos<_Expr,_ValArray,_Dom,typename _Dom::value_type>, \
964 typename _Dom::value_type> \
965 _Name (const _Expr<_Dom,typename _Dom::value_type>& __e, \
966 const valarray<typename _Dom::value_type>& __v) \
968 typedef typename _Dom::value_type _Tp; \
969 typedef _BinFunClos<_Expr,_ValArray,_Dom,_Tp> _Closure; \
970 return _Expr<_Closure,_Tp> \
971 (_Closure (__e (), __v, (_Tp(*)(_Tp, _Tp))(&_Name))); \
974 template<class _Dom> \
975 inline _Expr<_BinFunClos<_ValArray,_Expr,typename _Dom::value_type,_Dom>, \
976 typename _Dom::value_type> \
977 _Name (const valarray<typename _Dom::valarray>& __v, \
978 const _Expr<_Dom,typename _Dom::value_type>& __e) \
980 typedef typename _Dom::value_type _Tp; \
981 typedef _BinFunClos<_ValArray,_Expr,_Tp,_Dom> _Closure; \
982 return _Expr<_Closure,_Tp> \
983 (_Closure (__v, __e (), (_Tp(*)(_Tp, _Tp))(&_Name))); \
986 template<class _Dom> \
987 inline _Expr<_BinFunClos<_Expr,_Constant,_Dom,typename _Dom::value_type>, \
988 typename _Dom::value_type> \
989 _Name (const _Expr<_Dom, typename _Dom::value_type>& __e, \
990 const typename _Dom::value_type& __t) \
992 typedef typename _Dom::value_type _Tp; \
993 typedef _BinFunClos<_Expr,_Constant,_Dom,_Tp> _Closure; \
994 return _Expr<_Closure,_Tp> \
995 (_Closure (__e (), __t, (_Tp(*)(_Tp, _Tp))(&_Name))); \
998 template<class _Dom> \
999 inline _Expr<_BinFunClos<_Constant,_Expr,typename _Dom::value_type,_Dom>, \
1000 typename _Dom::value_type> \
1001 _Name (const typename _Dom::value_type& __t, \
1002 const _Expr<_Dom,typename _Dom::value_type>& __e) \
1004 typedef typename _Dom::value_type _Tp; \
1005 typedef _BinFunClos<_Constant,_Expr,_Tp,_Dom> _Closure; \
1006 return _Expr<_Closure,_Tp> \
1007 (_Closure (__t, __e (), (_Tp(*)(_Tp, _Tp))(&_Name))); \
1010 template<typename _Tp> \
1011 inline _Expr<_BinFunClos<_ValArray,_ValArray,_Tp,_Tp>, _Tp> \
1012 _Name (const valarray<_Tp>& __v, const valarray<_Tp>& __w) \
1014 typedef _BinFunClos<_ValArray,_ValArray,_Tp,_Tp> _Closure; \
1015 return _Expr<_Closure,_Tp> \
1016 (_Closure (__v, __w, (_Tp(*)(_Tp,_Tp))(&_Name))); \
1019 template<typename _Tp> \
1020 inline _Expr<_BinFunClos<_ValArray,_Constant,_Tp,_Tp>,_Tp> \
1021 _Name (const valarray<_Tp>& __v, const _Tp& __t) \
1023 typedef _BinFunClos<_ValArray,_Constant,_Tp,_Tp> _Closure; \
1024 return _Expr<_Closure,_Tp> \
1025 (_Closure (__v, __t, (_Tp(*)(_Tp,_Tp))(&_Name))); \
1028 template<typename _Tp> \
1029 inline _Expr<_BinFunClos<_Constant,_ValArray,_Tp,_Tp>,_Tp> \
1030 _Name (const _Tp& __t, const valarray<_Tp>& __v) \
1032 typedef _BinFunClos<_Constant,_ValArray,_Tp,_Tp> _Closure; \
1033 return _Expr<_Closure,_Tp> \
1034 (_Closure (__t, __v, (_Tp(*)(_Tp,_Tp))(&_Name))); \
1037 _DEFINE_EXPR_BINARY_FUNCTION(atan2
)
1038 _DEFINE_EXPR_BINARY_FUNCTION(pow
)
1040 #undef _DEFINE_EXPR_BINARY_FUNCTION
1042 #endif // _CPP_VALARRAY_META_H