]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/bits/valarray_meta.h
include: New directory.
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / valarray_meta.h
CommitLineData
725dc051
BK
1// The template and inlines for the -*- C++ -*- internal _Meta class.
2
3// Copyright (C) 1997-1999, 2000 Free Software Foundation, Inc.
4//
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)
9// any later version.
10
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.
15
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,
19// USA.
20
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.
29
30// Written by Gabriel Dos Reis <Gabriel.Dos-Reis@cmla.ens-cachan.fr>
31
32#ifndef _CPP_VALARRAY_META_H
33#define _CPP_VALARRAY_META_H 1
34
35namespace std {
36
37 //
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<>
44 //
45
46
47 // This class is NOT defined. It doesn't need to.
48 template<typename _Tp1, typename _Tp2> class _Constant;
49
50 //
51 // Unary function application closure.
52 //
53 template<class _Dom> class _UnFunBase {
54 public:
55 typedef typename _Dom::value_type value_type;
56 typedef value_type _Vt;
57
58 _UnFunBase (const _Dom& __e, _Vt __f(_Vt))
59 : _M_expr(__e), _M_func(__f) {}
60
61 _Vt operator[] (size_t __i) const { return _M_func(_M_expr[__i]); }
62 size_t size () const { return _M_expr.size(); }
63
64 private:
65 const _Dom& _M_expr;
66 _Vt (*_M_func)(_Vt);
67 };
68
69 template<template<class, class> class _Meta, class _Dom>
70 class _UnFunClos;
71
72 template<class _Dom>
73 struct _UnFunClos<_Expr,_Dom> : _UnFunBase<_Dom> {
74 typedef _UnFunBase<_Dom> _Base;
75 typedef typename _Base::value_type value_type;
76
77 _UnFunClos (const _Dom& __e, value_type __f(value_type))
78 : _Base (__e, __f) {}
79 };
80
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;
85
86 _UnFunClos (const valarray<_Tp>& __v, _Tp __f(_Tp))
87 : _Base (__v, __f) {}
88 };
89
90 //
91 // Binary function application closure.
92 //
93 template<template<class, class> class _Meta1,
94 template<class, class> class Meta2,
95 class _Dom1, class _Dom2> class _BinFunClos;
96
97 template<class _Dom1, class _Dom2> class _BinFunBase {
98 public:
99 typedef typename _Dom1::value_type value_type;
100 typedef value_type _Vt;
101
102 _BinFunBase (const _Dom1& __e1, const _Dom2& __e2,
103 _Vt __f (_Vt, _Vt))
104 : _M_expr1 (__e1), _M_expr2 (__e2), _M_func (__f) {}
105
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 (); }
109
110 private:
111 const _Dom1& _M_expr1;
112 const _Dom2& _M_expr2;
113 _Vt (*_M_func)(_Vt, _Vt);
114 };
115
116 template<class _Dom> class _BinFunBase1 {
117 public:
118 typedef typename _Dom::value_type value_type ;
119 typedef value_type _Vt;
120
121 _BinFunBase1 (const _Vt& __c, const _Dom& __e, _Vt __f(_Vt, _Vt))
122 : _M_expr1 (__c), _M_expr2 (__e), _M_func (__f) {}
123
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 (); }
127
128 private:
129 const _Vt& _M_expr1;
130 const _Dom& _M_expr2;
131 _Vt (*_M_func)(_Vt, _Vt);
132 };
133
134 template<class _Dom> class _BinFunBase2 {
135 public:
136 typedef typename _Dom::value_type value_type;
137 typedef value_type _Vt;
138
139 _BinFunBase2 (const _Dom& __e, const _Vt& __c, _Vt __f(_Vt, _Vt))
140 : _M_expr1 (__e), _M_expr2 (__c), _M_func (__f) {}
141
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 (); }
145
146 private:
147 const _Dom& _M_expr1;
148 const _Vt& _M_expr2;
149 _Vt (*_M_func)(_Vt, _Vt);
150 };
151
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;
157
158 _BinFunClos (const _Dom1& __e1, const _Dom2& __e2,
159 _Tp __f(_Tp, _Tp))
160 : _Base (__e1, __e2, __f) {}
161 };
162
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;
168
169 _BinFunClos (const valarray<_Tp>& __v, const valarray<_Tp>& __w,
170 _Tp __f(_Tp, _Tp))
171 : _Base (__v, __w, __f) {}
172 };
173
174 template<class _Dom>
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;
180
181 _BinFunClos (const _Dom& __e, const valarray<_Tp>& __v,
182 _Tp __f(_Tp, _Tp))
183 : _Base (__e, __v, __f) {}
184 };
185
186 template<class _Dom>
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;
192
193 _BinFunClos (const valarray<_Tp>& __v, const _Dom& __e,
194 _Tp __f(_Tp, _Tp))
195 : _Base (__v, __e, __f) {}
196 };
197
198 template<class _Dom>
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;
204
205 _BinFunClos (const _Dom& __e, const _Tp& __t, _Tp __f (_Tp, _Tp))
206 : _Base (__e, __t, __f) {}
207 };
208
209 template<class _Dom>
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;
215
216 _BinFunClos (const _Tp& __t, const _Dom& __e, _Tp __f (_Tp, _Tp))
217 : _Base (__t, __e, __f) {}
218 };
219
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;
225
226 _BinFunClos (const valarray<_Tp>& __v, const _Tp& __t,
227 _Tp __f(_Tp, _Tp))
228 : _Base (__v, __t, __f) {}
229 };
230
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;
236
237 _BinFunClos (const _Tp& __t, const valarray<_Tp>& __v,
238 _Tp __f (_Tp, _Tp))
239 : _Base (__t, __v, __f) {}
240 };
241
242 //
243 // Apply function taking a value/const reference closure
244 //
245
246 template<typename _Dom, typename _Arg> class _FunBase {
247 public:
248 typedef typename _Dom::value_type value_type;
249
250 _FunBase (const _Dom& __e, value_type __f(_Arg))
251 : _M_expr (__e), _M_func (__f) {}
252
253 value_type operator[] (size_t __i) const
254 { return _M_func (_M_expr[__i]); }
255 size_t size() const { return _M_expr.size ();}
256
257 private:
258 const _Dom& _M_expr;
259 value_type (*_M_func)(_Arg);
260 };
261
262 template<class _Dom>
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;
268
269 _ValFunClos (const _Dom& __e, _Tp __f (_Tp)) : _Base (__e, __f) {}
270 };
271
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;
277
278 _ValFunClos (const valarray<_Tp>& __v, _Tp __f(_Tp))
279 : _Base (__v, __f) {}
280 };
281
282 template<class _Dom>
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;
288
289 _RefFunClos (const _Dom& __e, _Tp __f (const _Tp&))
290 : _Base (__e, __f) {}
291 };
292
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;
298
299 _RefFunClos (const valarray<_Tp>& __v, _Tp __f(const _Tp&))
300 : _Base (__e, __f) {}
301 };
302
303 //
304 // Unary expression closure.
305 //
306
307 template<template<class> class _Oper, typename _Arg>
308 class _UnBase {
309 public:
310 typedef _Oper<typename _Arg::value_type> _Op;
311 typedef typename _Op::result_type value_type;
312
313 _UnBase (const _Arg& __e) : _M_expr(__e) {}
314 value_type operator[] (size_t) const;
315 size_t size () const { return _M_expr.size (); }
316
317 private:
318 const _Arg& _M_expr;
319 };
320
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]); }
325
326 template<template<class> class _Oper, class _Dom>
327 struct _UnClos<_Oper, _Expr, _Dom> : _UnBase<_Oper, _Dom> {
328 typedef _Dom _Arg;
329 typedef _UnBase<_Oper, _Dom> _Base;
330 typedef typename _Base::value_type value_type;
331
332 _UnClos (const _Arg& __e) : _Base(__e) {}
333 };
334
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;
340
341 _UnClos (const _Arg& __e) : _Base(__e) {}
342 };
343
344
345 //
346 // Binary expression closure.
347 //
348
349 template<template<class> class _Oper,
350 typename _FirstArg, typename _SecondArg>
351 class _BinBase {
352 public:
353 typedef _Oper<typename _FirstArg::value_type> _Op;
354 typedef typename _Op::result_type value_type;
355
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 (); }
360
361 private:
362 const _FirstArg& _M_expr1;
363 const _SecondArg& _M_expr2;
364 };
365
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]); }
371
372
373 template<template<class> class _Oper, class _Clos>
374 class _BinBase2 {
375 public:
376 typedef typename _Clos::value_type _Vt;
377 typedef _Oper<_Vt> _Op;
378 typedef typename _Op::result_type value_type;
379
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 (); }
384
385 private:
386 const _Clos& _M_expr1;
387 const _Vt& _M_expr2;
388 };
389
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); }
394
395
396 template<template<class> class _Oper, class _Clos>
397 class _BinBase1 {
398 public:
399 typedef typename _Clos::value_type _Vt;
400 typedef _Oper<_Vt> _Op;
401 typedef typename _Op::result_type value_type;
402
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 (); }
407
408 private:
409 const _Vt& _M_expr1;
410 const _Clos& _M_expr2;
411 };
412
413 template<template<class> class _Oper, class _Clos>
414 inline typename
415 _BinBase1<_Oper,_Clos>::value_type
416 _BinBase1<_Oper,_Clos>:: operator[] (size_t __i) const
417 { return _Op() (_M_expr1, _M_expr2[__i]); }
418
419
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;
425
426 _BinClos(const _Dom1& __e1, const _Dom2& __e2) : _Base(__e1, __e2) {}
427 };
428
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;
434
435 _BinClos (const valarray<_Tp>& __v, const valarray<_Tp>& __w)
436 : _Base (__v, __w) {}
437 };
438
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;
445
446 _BinClos(const _Dom& __e1, const valarray<_Tp>& __e2)
447 : _Base (__e1, __e2) {}
448 };
449
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;
456
457 _BinClos (const valarray<_Tp>& __e1, const _Dom& __e2)
458 : _Base (__e1, __e2) {}
459 };
460
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;
467
468 _BinClos (const _Dom& __e1, const _Tp& __e2) : _Base (__e1, __e2) {}
469 };
470
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;
477
478 _BinClos (const _Tp& __e1, const _Dom& __e2) : _Base (__e1, __e2) {}
479 };
480
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;
486
487 _BinClos (const valarray<_Tp>& __v, const _Tp& __t)
488 : _Base (__v, __t) {}
489 };
490
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;
496
497 _BinClos (const _Tp& __t, const valarray<_Tp>& __v)
498 : _Base (__t, __v) {}
499 };
500
501
502 //
503 // slice_array closure.
504 //
505 template<typename _Dom> class _SBase {
506 public:
507 typedef typename _Dom::value_type value_type;
508
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 (); }
514
515 private:
516 const _Dom& _M_expr;
517 const slice& _M_slice;
518 };
519
520 template<typename _Tp> class _SBase<_Array<_Tp> > {
521 public:
522 typedef _Tp value_type;
523
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; }
530
531 private:
532 const _Array<_Tp> _M_array;
533 const size_t _M_size;
534 const size_t _M_stride;
535 };
536
537 template<class _Dom> struct _SClos<_Expr,_Dom> : _SBase<_Dom> {
538 typedef _SBase<_Dom> _Base;
539 typedef typename _Base::value_type value_type;
540
541 _SClos (const _Dom& __e, const slice& __s) : _Base (__e, __s) {}
542 };
543
544 template<typename _Tp>
545 struct _SClos<_ValArray,_Tp> : _SBase<_Array<_Tp> > {
546 typedef _SBase<_Array<_Tp> > _Base;
547 typedef _Tp value_type;
548
549 _SClos (_Array<_Tp> __a, const slice& __s) : _Base (__a, __s) {}
550 };
551
552 //
553 // gslice_array closure.
554 //
555 template<class _Dom> class _GBase {
556 public:
557 typedef typename _Dom::value_type value_type;
558
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(); }
564
565 private:
566 const _Dom& _M_expr;
567 const valarray<size_t>& _M_index;
568 };
569
570 template<typename _Tp> class _GBase<_Array<_Tp> > {
571 public:
572 typedef _Tp value_type;
573
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(); }
579
580 private:
581 const _Array<_Tp> _M_array;
582 const valarray<size_t>& _M_index;
583 };
584
585 template<class _Dom> struct _GClos<_Expr,_Dom> : _GBase<_Dom> {
586 typedef _GBase<_Dom> _Base;
587 typedef typename _Base::value_type value_type;
588
589 _GClos (const _Dom& __e, const valarray<size_t>& __i)
590 : _Base (__e, __i) {}
591 };
592
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;
597
598 _GClos (_Array<_Tp> __a, const valarray<size_t>& __i)
599 : _Base (__a, __i) {}
600 };
601
602 //
603 // indirect_array closure
604 //
605
606 template<class _Dom> class _IBase {
607 public:
608 typedef typename _Dom::value_type value_type;
609
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(); }
615
616 private:
617 const _Dom& _M_expr;
618 const valarray<size_t>& _M_index;
619 };
620
621 template<class _Dom> struct _IClos<_Expr,_Dom> : _IBase<_Dom> {
622 typedef _IBase<_Dom> _Base;
623 typedef typename _Base::value_type value_type;
624
625 _IClos (const _Dom& __e, const valarray<size_t>& __i)
626 : _Base (__e, __i) {}
627 };
628
629 template<typename _Tp>
630 struct _IClos<_ValArray,_Tp> : _IBase<valarray<_Tp> > {
631 typedef _IBase<valarray<_Tp> > _Base;
632 typedef _Tp value_type;
633
634 _IClos (const valarray<_Tp>& __a, const valarray<size_t>& __i)
635 : _Base (__a, __i) {}
636 };
637
638 //
639 // class _Expr
640 //
641 template<class _Clos, typename _Tp> class _Expr {
642 public:
643 typedef _Tp value_type;
644
645 _Expr (const _Clos&);
646
647 const _Clos& operator() () const;
648
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;
654
655 _Expr<_UnClos<_Unary_plus,_Expr,_Clos>, value_type>
656 operator+ () const;
657
658 _Expr<_UnClos<negate,_Expr,_Clos>, value_type>
659 operator- () const;
660
661 _Expr<_UnClos<_Bitwise_not,_Expr,_Clos>, value_type>
662 operator~ () const;
663
664 _Expr<_UnClos<logical_not,_Expr,_Clos>, bool>
665 operator! () const;
666
667 size_t size () const;
668 value_type sum () const;
669
670 valarray<value_type> shift (int) const;
671 valarray<value_type> cshift (int) const;
672
673 value_type min() const;
674 value_type max() const;
675
676 valarray<value_type> apply(value_type (*) (const value_type&)) const;
677 valarray<value_type> apply(value_type (*) (value_type)) const;
678
679 private:
680 const _Clos _M_closure;
681 };
682
683 template<class _Clos, typename _Tp>
684 inline
685 _Expr<_Clos,_Tp>::_Expr (const _Clos& __c) : _M_closure(__c) {}
686
687 template<class _Clos, typename _Tp>
688 inline const _Clos&
689 _Expr<_Clos,_Tp>::operator() () const
690 { return _M_closure; }
691
692 template<class _Clos, typename _Tp>
693 inline _Tp
694 _Expr<_Clos,_Tp>::operator[] (size_t __i) const
695 { return _M_closure[__i]; }
696
697 template<class _Clos, typename _Tp>
698 inline valarray<_Tp>
699 _Expr<_Clos,_Tp>::operator[] (slice __s) const
700 { return _M_closure[__s]; }
701
702 template<class _Clos, typename _Tp>
703 inline valarray<_Tp>
704 _Expr<_Clos,_Tp>::operator[] (const gslice& __gs) const
705 { return _M_closure[__gs]; }
706
707 template<class _Clos, typename _Tp>
708 inline valarray<_Tp>
709 _Expr<_Clos,_Tp>::operator[] (const valarray<bool>& __m) const
710 { return _M_closure[__m]; }
711
712 template<class _Clos, typename _Tp>
713 inline valarray<_Tp>
714 _Expr<_Clos,_Tp>::operator[] (const valarray<size_t>& __i) const
715 { return _M_closure[__i]; }
716
717 template<class _Clos, typename _Tp>
718 inline size_t
719 _Expr<_Clos,_Tp>::size () const { return _M_closure.size (); }
720
721 template<class _Clos, typename _Tp>
722 inline valarray<_Tp>
723 _Expr<_Clos, _Tp>::shift(int __n) const
724 { return valarray<_Tp>(_M_closure).shift(__n); }
725
726 template<class _Clos, typename _Tp>
727 inline valarray<_Tp>
728 _Expr<_Clos, _Tp>::cshift(int __n) const
729 { return valarray<_Tp>(_M_closure).cshift(__n); }
730
731 template<class _Clos, typename _Tp>
732 inline valarray<_Tp>
733 _Expr<_Clos, _Tp>::apply(_Tp __f(const _Tp&)) const
734 { return valarray<_Tp>(_M_closure).apply(__f); }
735
736 template<class _Clos, typename _Tp>
737 inline valarray<_Tp>
738 _Expr<_Clos, _Tp>::apply(_Tp __f(_Tp)) const
739 { return valarray<_Tp>(_M_closure).apply(__f); }
740
741 // XXX: replace this with a more robust summation algorithm.
742 template<class _Clos, typename _Tp>
743 inline _Tp
744 _Expr<_Clos,_Tp>::sum () const
745 {
746 size_t __n = _M_closure.size();
747 if (__n == 0) return _Tp();
748 else {
749 _Tp __s = _M_closure[--__n];
750 while (__n != 0) __s += _M_closure[--__n];
751 return __s;
752 }
753 }
754
755 template<class _Clos, typename _Tp>
756 inline _Tp
757 _Expr<_Clos, _Tp>::min() const
758 { return __valarray_min(_M_closure); }
759
760 template<class _Clos, typename _Tp>
761 inline _Tp
762 _Expr<_Clos, _Tp>::max() const
763 { return __valarray_max(_M_closure); }
764
765 template<class _Dom, typename _Tp>
766 inline _Expr<_UnClos<logical_not,_Expr,_Dom>, bool>
767 _Expr<_Dom,_Tp>::operator! () const
768 {
769 typedef _UnClos<logical_not,_Expr,_Dom> _Closure;
770 return _Expr<_Closure,_Tp> (_Closure(this->_M_closure));
771 }
772
773#define _DEFINE_EXPR_UNARY_OPERATOR(_Op, _Name) \
774template<class _Dom, typename _Tp> \
775inline _Expr<_UnClos<_Name,_Expr,_Dom>,_Tp> \
776_Expr<_Dom,_Tp>::operator _Op () const \
777{ \
778 typedef _UnClos<_Name,_Expr,_Dom> _Closure; \
779 return _Expr<_Closure,_Tp> (_Closure (this->_M_closure)); \
780}
781
782 _DEFINE_EXPR_UNARY_OPERATOR(+, _Unary_plus)
783 _DEFINE_EXPR_UNARY_OPERATOR(-, negate)
784 _DEFINE_EXPR_UNARY_OPERATOR(~, _Bitwise_not)
785
786#undef _DEFINE_EXPR_UNARY_OPERATOR
787
788
789#define _DEFINE_EXPR_BINARY_OPERATOR(_Op, _Name) \
790template<class _Dom1, class _Dom2> \
791inline _Expr<_BinClos<_Name,_Expr,_Expr,_Dom1,_Dom2>, \
792 typename _Name<typename _Dom1::value_type>::result_type> \
793operator _Op (const _Expr<_Dom1,typename _Dom1::value_type>& __v, \
794 const _Expr<_Dom2,typename _Dom2::value_type>& __w) \
795{ \
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 ())); \
800} \
801 \
802template<class _Dom> \
803inline _Expr<_BinClos<_Name,_Expr,_Constant,_Dom,typename _Dom::value_type>, \
804 typename _Name<typename _Dom::value_type>::result_type> \
805operator _Op (const _Expr<_Dom,typename _Dom::value_type>& __v, \
806 const typename _Dom::value_type& __t) \
807{ \
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)); \
812} \
813 \
814template<class _Dom> \
815inline _Expr<_BinClos<_Name,_Constant,_Expr,typename _Dom::value_type,_Dom>, \
816 typename _Name<typename _Dom::value_type>::result_type> \
817operator _Op (const typename _Dom::value_type& __t, \
818 const _Expr<_Dom,typename _Dom::value_type>& __v) \
819{ \
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 ())); \
824} \
825 \
826template<class _Dom> \
827inline _Expr<_BinClos<_Name,_Expr,_ValArray,_Dom,typename _Dom::value_type>, \
828 typename _Name<typename _Dom::value_type>::result_type> \
829operator _Op (const _Expr<_Dom,typename _Dom::value_type>& __e, \
830 const valarray<typename _Dom::value_type>& __v) \
831{ \
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)); \
836} \
837 \
838template<class _Dom> \
839inline _Expr<_BinClos<_Name,_ValArray,_Expr,typename _Dom::value_type,_Dom>, \
840 typename _Name<typename _Dom::value_type>::result_type> \
841operator _Op (const valarray<typename _Dom::value_type>& __v, \
842 const _Expr<_Dom,typename _Dom::value_type>& __e) \
843{ \
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 ())); \
848}
849
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)
860
861#undef _DEFINE_EXPR_BINARY_OPERATOR
862
863#define _DEFINE_EXPR_RELATIONAL_OPERATOR(_Op, _Name) \
864template<class _Dom1, class _Dom2> \
865inline _Expr<_BinClos<_Name,_Expr,_Expr,_Dom1,_Dom2>, bool> \
866operator _Op (const _Expr<_Dom1,typename _Dom1::value_type>& __v, \
867 const _Expr<_Dom2,typename _Dom2::value_type>& __w) \
868{ \
869 typedef typename _Dom1::value_type _Arg; \
870 typedef _BinClos<_Name,_Expr,_Expr,_Dom1,_Dom2> _Closure; \
871 return _Expr<_Closure,bool> (_Closure (__v (), __w ())); \
872} \
873 \
874template<class _Dom> \
875inline _Expr<_BinClos<_Name,_Expr,_Constant,_Dom,typename _Dom::value_type>, \
876 bool> \
877operator _Op (const _Expr<_Dom,typename _Dom::value_type>& __v, \
878 const typename _Dom::value_type& __t) \
879{ \
880 typedef typename _Dom::value_type _Arg; \
881 typedef _BinClos<_Name,_Expr,_Constant,_Dom,_Arg> _Closure; \
882 return _Expr<_Closure,bool> (_Closure (__v (), __t)); \
883} \
884 \
885template<class _Dom> \
886inline _Expr<_BinClos<_Name,_Constant,_Expr,typename _Dom::value_type,_Dom>, \
887 bool> \
888operator _Op (const typename _Dom::value_type& __t, \
889 const _Expr<_Dom,typename _Dom::value_type>& __v) \
890{ \
891 typedef typename _Dom::value_type _Arg; \
892 typedef _BinClos<_Name,_Constant,_Expr,_Arg,_Dom> _Closure; \
893 return _Expr<_Closure,bool> (_Closure (__t, __v ())); \
894} \
895 \
896template<class _Dom> \
897inline _Expr<_BinClos<_Name,_Expr,_ValArray,_Dom,typename _Dom::value_type>, \
898 bool> \
899operator _Op (const _Expr<_Dom,typename _Dom::value_type>& __e, \
900 const valarray<typename _Dom::value_type>& __v) \
901{ \
902 typedef typename _Dom::value_type _Tp; \
903 typedef _BinClos<_Name,_Expr,_ValArray,_Dom,_Tp> _Closure; \
904 return _Expr<_Closure,bool> (_Closure (__e (), __v)); \
905} \
906 \
907template<class _Dom> \
908inline _Expr<_BinClos<_Name,_ValArray,_Expr,typename _Dom::value_type,_Dom>, \
909 bool> \
910operator _Op (const valarray<typename _Dom::value_type>& __v, \
911 const _Expr<_Dom,typename _Dom::value_type>& __e) \
912{ \
913 typedef typename _Dom::value_type _Tp; \
914 typedef _BinClos<_Name,_ValArray,_Expr,_Tp,_Dom> _Closure; \
915 return _Expr<_Closure,bool> (_Closure (__v, __e ())); \
916}
917
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)
926
927#undef _DEFINE_EXPR_RELATIONAL_OPERATOR
928
929
930
931#define _DEFINE_EXPR_UNARY_FUNCTION(_Name) \
932template<class _Dom> \
933inline _Expr<_UnFunClos<_Expr,_Dom>,typename _Dom::value_type> \
934_Name(const _Expr<_Dom,typename _Dom::value_type>& __e) \
935{ \
936 typedef typename _Dom::value_type _Tp; \
937 typedef _UnFunClos<_Expr,_Dom> _Closure; \
938 return _Expr<_Closure,_Tp>(_Closure(__e(), (_Tp(*)(_Tp))(&_Name))); \
939} \
940 \
941template<typename _Tp> \
942inline _Expr<_UnFunClos<_ValArray,_Tp>,_Tp> \
943_Name(const valarray<_Tp>& __v) \
944{ \
945 typedef _UnFunClos<_ValArray,_Tp> _Closure; \
946 return _Expr<_Closure,_Tp> (_Closure (__v, (_Tp(*)(_Tp))(&_Name))); \
947}
948
949
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)
964
965#undef _DEFINE_EXPR_UNARY_FUNCTION
966
967
968#define _DEFINE_EXPR_BINARY_FUNCTION(_Name) \
969template<class _Dom1, class _Dom2> \
970inline _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) \
973{ \
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))); \
978} \
979 \
980template<class _Dom> \
981inline _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) \
985{ \
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))); \
990} \
991 \
992template<class _Dom> \
993inline _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) \
997{ \
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))); \
1002} \
1003 \
1004template<class _Dom> \
1005inline _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) \
1009{ \
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))); \
1014} \
1015 \
1016template<class _Dom> \
1017inline _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) \
1021{ \
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))); \
1026} \
1027 \
1028template<typename _Tp> \
1029inline _Expr<_BinFunClos<_ValArray,_ValArray,_Tp,_Tp>, _Tp> \
1030_Name (const valarray<_Tp>& __v, const valarray<_Tp>& __w) \
1031{ \
1032 typedef _BinFunClos<_ValArray,_ValArray,_Tp,_Tp> _Closure; \
1033 return _Expr<_Closure,_Tp> \
1034 (_Closure (__v, __w, (_Tp(*)(_Tp,_Tp))(&_Name))); \
1035} \
1036 \
1037template<typename _Tp> \
1038inline _Expr<_BinFunClos<_ValArray,_Constant,_Tp,_Tp>,_Tp> \
1039_Name (const valarray<_Tp>& __v, const _Tp& __t) \
1040{ \
1041 typedef _BinFunClos<_ValArray,_Constant,_Tp,_Tp> _Closure; \
1042 return _Expr<_Closure,_Tp> \
1043 (_Closure (__v, __t, (_Tp(*)(_Tp,_Tp))(&_Name))); \
1044} \
1045 \
1046template<typename _Tp> \
1047inline _Expr<_BinFunClos<_Constant,_ValArray,_Tp,_Tp>,_Tp> \
1048_Name (const _Tp& __t, const valarray<_Tp>& __v) \
1049{ \
1050 typedef _BinFunClos<_Constant,_ValArray,_Tp,_Tp> _Closure; \
1051 return _Expr<_Closure,_Tp> \
1052 (_Closure (__t, __v, (_Tp(*)(_Tp,_Tp))(&_Name))); \
1053}
1054
1055_DEFINE_EXPR_BINARY_FUNCTION(atan2)
1056_DEFINE_EXPR_BINARY_FUNCTION(pow)
1057
1058#undef _DEFINE_EXPR_BINARY_FUNCTION
1059
1060} // std::
1061
1062
1063#endif /* _CPP_VALARRAY_META_H */
1064
1065// Local Variables:
1066// mode:c++
1067// End: