Create startup files from the GCC sources and drop our versions.
[dragonfly.git] / contrib / gcc-4.0 / libstdc++-v3 / include / bits / valarray_after.h
1 // The template and inlines for the -*- C++ -*- internal _Meta class.
2
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library.  This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
10 // any later version.
11
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING.  If not, write to the Free
19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 // USA.
21
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction.  Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License.  This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
30
31 // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@cmla.ens-cachan.fr>
32
33 /** @file valarray_after.h
34  *  This is an internal header file, included by other library headers.
35  *  You should not attempt to use it directly.
36  */
37
38 #ifndef _VALARRAY_AFTER_H
39 #define _VALARRAY_AFTER_H 1
40
41 #pragma GCC system_header
42
43 namespace std
44 {
45   //
46   // gslice_array closure.
47   //
48   template<class _Dom>
49     class _GBase
50     {
51     public:
52       typedef typename _Dom::value_type value_type;
53       
54       _GBase (const _Dom& __e, const valarray<size_t>& __i)
55       : _M_expr (__e), _M_index(__i) {}
56       
57       value_type
58       operator[] (size_t __i) const
59       { return _M_expr[_M_index[__i]]; }
60       
61       size_t
62       size () const
63       { return _M_index.size(); }
64
65     private:
66       const _Dom&             _M_expr;
67       const valarray<size_t>& _M_index;
68     };
69
70   template<typename _Tp>
71     class _GBase<_Array<_Tp> >
72     {
73     public:
74       typedef _Tp value_type;
75       
76       _GBase (_Array<_Tp> __a, const valarray<size_t>& __i)
77       : _M_array (__a), _M_index(__i) {}
78       
79       value_type
80       operator[] (size_t __i) const
81       { return _M_array._M_data[_M_index[__i]]; }
82       
83       size_t
84       size () const
85       { return _M_index.size(); }
86
87     private:
88       const _Array<_Tp>       _M_array;
89       const valarray<size_t>& _M_index;
90     };
91
92   template<class _Dom>
93     struct _GClos<_Expr, _Dom>
94     : _GBase<_Dom>
95     {
96       typedef _GBase<_Dom> _Base;
97       typedef typename _Base::value_type value_type;
98       
99       _GClos (const _Dom& __e, const valarray<size_t>& __i)
100       : _Base (__e, __i) {}
101     };
102
103   template<typename _Tp>
104     struct _GClos<_ValArray, _Tp>
105     : _GBase<_Array<_Tp> >
106     {
107       typedef _GBase<_Array<_Tp> > _Base;
108       typedef typename _Base::value_type value_type;
109       
110       _GClos (_Array<_Tp> __a, const valarray<size_t>& __i)
111       : _Base (__a, __i) {}
112     };
113
114   //
115   // indirect_array closure
116   //
117   template<class _Dom>
118     class _IBase
119     {
120     public:
121       typedef typename _Dom::value_type value_type;
122
123       _IBase (const _Dom& __e, const valarray<size_t>& __i)
124       : _M_expr (__e), _M_index (__i) {}
125       
126       value_type
127       operator[] (size_t __i) const
128       { return _M_expr[_M_index[__i]]; }
129       
130       size_t
131       size() const
132       { return _M_index.size(); }
133
134     private:
135       const _Dom&             _M_expr;
136       const valarray<size_t>& _M_index;
137     };
138
139   template<class _Dom>
140     struct _IClos<_Expr, _Dom>
141     : _IBase<_Dom>
142     {
143       typedef _IBase<_Dom> _Base;
144       typedef typename _Base::value_type value_type;
145       
146       _IClos (const _Dom& __e, const valarray<size_t>& __i)
147       : _Base (__e, __i) {}
148     };
149
150   template<typename _Tp>
151     struct _IClos<_ValArray, _Tp>
152     : _IBase<valarray<_Tp> >
153     {
154       typedef _IBase<valarray<_Tp> > _Base;
155       typedef _Tp value_type;
156       
157       _IClos (const valarray<_Tp>& __a, const valarray<size_t>& __i)
158       : _Base (__a, __i) {}
159     };
160   
161   //
162   // class _Expr
163   //
164   template<class _Clos, typename _Tp>
165     class _Expr
166     {
167     public:
168       typedef _Tp value_type;
169
170       _Expr(const _Clos&);
171
172       const _Clos& operator()() const;
173
174       value_type operator[](size_t) const;
175       valarray<value_type> operator[](slice) const;
176       valarray<value_type> operator[](const gslice&) const;
177       valarray<value_type> operator[](const valarray<bool>&) const;
178       valarray<value_type> operator[](const valarray<size_t>&) const;
179
180       _Expr<_UnClos<__unary_plus, std::_Expr, _Clos>, value_type>
181       operator+() const;
182
183       _Expr<_UnClos<__negate, std::_Expr, _Clos>, value_type>
184       operator-() const;
185
186       _Expr<_UnClos<__bitwise_not, std::_Expr, _Clos>, value_type>
187       operator~() const;
188
189       _Expr<_UnClos<__logical_not, std::_Expr, _Clos>, bool>
190       operator!() const;
191
192       size_t size() const;
193       value_type sum() const;
194
195       valarray<value_type> shift(int) const;
196       valarray<value_type> cshift(int) const;
197
198       value_type min() const;
199       value_type max() const;
200
201       valarray<value_type> apply(value_type (*)(const value_type&)) const;
202       valarray<value_type> apply(value_type (*)(value_type)) const;
203
204     private:
205       const _Clos _M_closure;
206     };
207
208   template<class _Clos, typename _Tp>
209     inline
210     _Expr<_Clos, _Tp>::_Expr(const _Clos& __c) : _M_closure(__c) {}
211
212   template<class _Clos, typename _Tp>
213     inline const _Clos&
214     _Expr<_Clos, _Tp>::operator()() const
215     { return _M_closure; }
216
217   template<class _Clos, typename _Tp>
218     inline _Tp
219     _Expr<_Clos, _Tp>::operator[](size_t __i) const
220     { return _M_closure[__i]; }
221
222   template<class _Clos, typename _Tp>
223     inline valarray<_Tp>
224     _Expr<_Clos, _Tp>::operator[](slice __s) const
225     { return _M_closure[__s]; }
226
227   template<class _Clos, typename _Tp>
228     inline valarray<_Tp>
229     _Expr<_Clos, _Tp>::operator[](const gslice& __gs) const
230     { return _M_closure[__gs]; }
231
232   template<class _Clos, typename _Tp>
233     inline valarray<_Tp>
234     _Expr<_Clos, _Tp>::operator[](const valarray<bool>& __m) const
235     { return _M_closure[__m]; }
236
237   template<class _Clos, typename _Tp>
238     inline valarray<_Tp>
239     _Expr<_Clos, _Tp>::operator[](const valarray<size_t>& __i) const
240     { return _M_closure[__i]; }
241
242   template<class _Clos, typename _Tp>
243     inline size_t
244     _Expr<_Clos, _Tp>::size() const
245     { return _M_closure.size (); }
246
247   template<class _Clos, typename _Tp>
248     inline valarray<_Tp>
249     _Expr<_Clos, _Tp>::shift(int __n) const
250     { return valarray<_Tp>(_M_closure).shift(__n); }
251
252   template<class _Clos, typename _Tp>
253     inline valarray<_Tp>
254     _Expr<_Clos, _Tp>::cshift(int __n) const
255     { return valarray<_Tp>(_M_closure).cshift(__n); }
256
257   template<class _Clos, typename _Tp>
258     inline valarray<_Tp>
259     _Expr<_Clos, _Tp>::apply(_Tp __f(const _Tp&)) const
260     { return valarray<_Tp>(_M_closure).apply(__f); }
261
262   template<class _Clos, typename _Tp>
263     inline valarray<_Tp>
264     _Expr<_Clos, _Tp>::apply(_Tp __f(_Tp)) const
265     { return valarray<_Tp>(_M_closure).apply(__f); }
266
267   // XXX: replace this with a more robust summation algorithm.
268   template<class _Clos, typename _Tp>
269     inline _Tp
270     _Expr<_Clos, _Tp>::sum() const
271     {
272       size_t __n = _M_closure.size();
273       if (__n == 0)
274         return _Tp();
275       else
276         {
277           _Tp __s = _M_closure[--__n];
278           while (__n != 0)
279             __s += _M_closure[--__n];
280           return __s;
281         }
282     }
283
284   template<class _Clos, typename _Tp>
285     inline _Tp
286     _Expr<_Clos, _Tp>::min() const
287     { return __valarray_min(_M_closure); }
288
289   template<class _Clos, typename _Tp>
290     inline _Tp
291     _Expr<_Clos, _Tp>::max() const
292     { return __valarray_max(_M_closure); }
293
294   template<class _Dom, typename _Tp>
295     inline _Expr<_UnClos<__logical_not, _Expr, _Dom>, bool>
296     _Expr<_Dom, _Tp>::operator!() const
297     {
298       typedef _UnClos<__logical_not, std::_Expr, _Dom> _Closure;
299       return _Expr<_Closure, _Tp>(_Closure(this->_M_closure));
300     }
301
302 #define _DEFINE_EXPR_UNARY_OPERATOR(_Op, _Name)                           \
303   template<class _Dom, typename _Tp>                                      \
304     inline _Expr<_UnClos<_Name, std::_Expr, _Dom>, _Tp>                   \
305     _Expr<_Dom, _Tp>::operator _Op() const                                \
306     {                                                                     \
307       typedef _UnClos<_Name, std::_Expr, _Dom> _Closure;                  \
308       return _Expr<_Closure, _Tp>(_Closure(this->_M_closure));            \
309     }
310
311     _DEFINE_EXPR_UNARY_OPERATOR(+, __unary_plus)
312     _DEFINE_EXPR_UNARY_OPERATOR(-, __negate)
313     _DEFINE_EXPR_UNARY_OPERATOR(~, __bitwise_not)
314
315 #undef _DEFINE_EXPR_UNARY_OPERATOR
316
317 #define _DEFINE_EXPR_BINARY_OPERATOR(_Op, _Name)                        \
318   template<class _Dom1, class _Dom2>                                    \
319     inline _Expr<_BinClos<_Name, _Expr, _Expr, _Dom1, _Dom2>,           \
320            typename __fun<_Name, typename _Dom1::value_type>::result_type> \
321     operator _Op(const _Expr<_Dom1, typename _Dom1::value_type>& __v,   \
322                  const _Expr<_Dom2, typename _Dom2::value_type>& __w)   \
323     {                                                                   \
324       typedef typename _Dom1::value_type _Arg;                          \
325       typedef typename __fun<_Name, _Arg>::result_type _Value;          \
326       typedef _BinClos<_Name, _Expr, _Expr, _Dom1, _Dom2> _Closure;     \
327       return _Expr<_Closure, _Value>(_Closure(__v(), __w()));           \
328     }                                                                   \
329                                                                         \
330   template<class _Dom>                                                  \
331     inline _Expr<_BinClos<_Name, _Expr, _Constant, _Dom,                \
332                           typename _Dom::value_type>,                   \
333              typename __fun<_Name, typename _Dom::value_type>::result_type> \
334     operator _Op(const _Expr<_Dom, typename _Dom::value_type>& __v,     \
335                  const typename _Dom::value_type& __t)                  \
336     {                                                                   \
337       typedef typename _Dom::value_type _Arg;                           \
338       typedef typename __fun<_Name, _Arg>::result_type _Value;          \
339       typedef _BinClos<_Name, _Expr, _Constant, _Dom, _Arg> _Closure;   \
340       return _Expr<_Closure, _Value>(_Closure(__v(), __t));             \
341     }                                                                   \
342                                                                         \
343   template<class _Dom>                                                  \
344     inline _Expr<_BinClos<_Name, _Constant, _Expr,                      \
345                           typename _Dom::value_type, _Dom>,             \
346              typename __fun<_Name, typename _Dom::value_type>::result_type> \
347     operator _Op(const typename _Dom::value_type& __t,                  \
348                  const _Expr<_Dom, typename _Dom::value_type>& __v)     \
349     {                                                                   \
350       typedef typename _Dom::value_type _Arg;                           \
351       typedef typename __fun<_Name, _Arg>::result_type _Value;          \
352       typedef _BinClos<_Name, _Constant, _Expr, _Arg, _Dom> _Closure;   \
353       return _Expr<_Closure, _Value>(_Closure(__t, __v()));             \
354     }                                                                   \
355                                                                         \
356   template<class _Dom>                                                  \
357     inline _Expr<_BinClos<_Name, _Expr, _ValArray,                      \
358                           _Dom, typename _Dom::value_type>,             \
359              typename __fun<_Name, typename _Dom::value_type>::result_type> \
360     operator _Op(const _Expr<_Dom,typename _Dom::value_type>& __e,      \
361                  const valarray<typename _Dom::value_type>& __v)        \
362     {                                                                   \
363       typedef typename _Dom::value_type _Arg;                           \
364       typedef typename __fun<_Name, _Arg>::result_type _Value;          \
365       typedef _BinClos<_Name, _Expr, _ValArray, _Dom, _Arg> _Closure;   \
366       return _Expr<_Closure, _Value>(_Closure(__e(), __v));             \
367     }                                                                   \
368                                                                         \
369   template<class _Dom>                                                  \
370     inline _Expr<_BinClos<_Name, _ValArray, _Expr,                      \
371                  typename _Dom::value_type, _Dom>,                      \
372              typename __fun<_Name, typename _Dom::value_type>::result_type> \
373     operator _Op(const valarray<typename _Dom::value_type>& __v,        \
374                  const _Expr<_Dom, typename _Dom::value_type>& __e)     \
375     {                                                                   \
376       typedef typename _Dom::value_type _Tp;                            \
377       typedef typename __fun<_Name, _Tp>::result_type _Value;           \
378       typedef _BinClos<_Name, _ValArray, _Expr, _Tp, _Dom> _Closure;    \
379       return _Expr<_Closure, _Value>(_Closure(__v, __e ()));            \
380     }
381
382     _DEFINE_EXPR_BINARY_OPERATOR(+, __plus)
383     _DEFINE_EXPR_BINARY_OPERATOR(-, __minus)
384     _DEFINE_EXPR_BINARY_OPERATOR(*, __multiplies)
385     _DEFINE_EXPR_BINARY_OPERATOR(/, __divides)
386     _DEFINE_EXPR_BINARY_OPERATOR(%, __modulus)
387     _DEFINE_EXPR_BINARY_OPERATOR(^, __bitwise_xor)
388     _DEFINE_EXPR_BINARY_OPERATOR(&, __bitwise_and)
389     _DEFINE_EXPR_BINARY_OPERATOR(|, __bitwise_or)
390     _DEFINE_EXPR_BINARY_OPERATOR(<<, __shift_left)
391     _DEFINE_EXPR_BINARY_OPERATOR(>>, __shift_right)
392     _DEFINE_EXPR_BINARY_OPERATOR(&&, __logical_and)
393     _DEFINE_EXPR_BINARY_OPERATOR(||, __logical_or)
394     _DEFINE_EXPR_BINARY_OPERATOR(==, __equal_to)
395     _DEFINE_EXPR_BINARY_OPERATOR(!=, __not_equal_to)
396     _DEFINE_EXPR_BINARY_OPERATOR(<, __less)
397     _DEFINE_EXPR_BINARY_OPERATOR(>, __greater)
398     _DEFINE_EXPR_BINARY_OPERATOR(<=, __less_equal)
399     _DEFINE_EXPR_BINARY_OPERATOR(>=, __greater_equal)
400
401 #undef _DEFINE_EXPR_BINARY_OPERATOR
402
403 #define _DEFINE_EXPR_UNARY_FUNCTION(_Name)                               \
404   template<class _Dom>                                                   \
405     inline _Expr<_UnClos<__##_Name, _Expr, _Dom>,                        \
406                  typename _Dom::value_type>                              \
407     _Name(const _Expr<_Dom, typename _Dom::value_type>& __e)             \
408     {                                                                    \
409       typedef typename _Dom::value_type _Tp;                             \
410       typedef _UnClos<__##_Name, _Expr, _Dom> _Closure;                  \
411       return _Expr<_Closure, _Tp>(_Closure(__e()));                      \
412     }                                                                    \
413                                                                          \
414   template<typename _Tp>                                                 \
415     inline _Expr<_UnClos<__##_Name, _ValArray, _Tp>, _Tp>                \
416     _Name(const valarray<_Tp>& __v)                                      \
417     {                                                                    \
418       typedef _UnClos<__##_Name, _ValArray, _Tp> _Closure;               \
419       return _Expr<_Closure, _Tp>(_Closure(__v));                        \
420     }
421
422     _DEFINE_EXPR_UNARY_FUNCTION(abs)
423     _DEFINE_EXPR_UNARY_FUNCTION(cos)
424     _DEFINE_EXPR_UNARY_FUNCTION(acos)
425     _DEFINE_EXPR_UNARY_FUNCTION(cosh)
426     _DEFINE_EXPR_UNARY_FUNCTION(sin)
427     _DEFINE_EXPR_UNARY_FUNCTION(asin)
428     _DEFINE_EXPR_UNARY_FUNCTION(sinh)
429     _DEFINE_EXPR_UNARY_FUNCTION(tan)
430     _DEFINE_EXPR_UNARY_FUNCTION(tanh)
431     _DEFINE_EXPR_UNARY_FUNCTION(atan)
432     _DEFINE_EXPR_UNARY_FUNCTION(exp)
433     _DEFINE_EXPR_UNARY_FUNCTION(log)
434     _DEFINE_EXPR_UNARY_FUNCTION(log10)
435     _DEFINE_EXPR_UNARY_FUNCTION(sqrt)
436
437 #undef _DEFINE_EXPR_UNARY_FUNCTION
438
439 #define _DEFINE_EXPR_BINARY_FUNCTION(_Fun)                             \
440   template<class _Dom1, class _Dom2>                                   \
441     inline _Expr<_BinClos<__##_Fun, _Expr, _Expr, _Dom1, _Dom2>,       \
442                  typename _Dom1::value_type>                           \
443     _Fun(const _Expr<_Dom1, typename _Dom1::value_type>& __e1,         \
444           const _Expr<_Dom2, typename _Dom2::value_type>& __e2)        \
445     {                                                                  \
446       typedef typename _Dom1::value_type _Tp;                          \
447       typedef _BinClos<__##_Fun, _Expr, _Expr, _Dom1, _Dom2> _Closure; \
448       return _Expr<_Closure, _Tp>(_Closure(__e1(), __e2()));           \
449     }                                                                  \
450                                                                        \
451   template<class _Dom>                                                 \
452     inline _Expr<_BinClos<__##_Fun, _Expr, _ValArray, _Dom,            \
453                           typename _Dom::value_type>,                  \
454                  typename _Dom::value_type>                            \
455     _Fun(const _Expr<_Dom, typename _Dom::value_type>& __e,            \
456          const valarray<typename _Dom::value_type>& __v)               \
457     {                                                                  \
458       typedef typename _Dom::value_type _Tp;                           \
459       typedef _BinClos<__##_Fun, _Expr, _ValArray, _Dom, _Tp> _Closure; \
460       return _Expr<_Closure, _Tp>(_Closure(__e(), __v));               \
461     }                                                                  \
462                                                                        \
463   template<class _Dom>                                                 \
464     inline _Expr<_BinClos<__##_Fun, _ValArray, _Expr,                  \
465                           typename _Dom::value_type, _Dom>,            \
466                  typename _Dom::value_type>                            \
467     _Fun(const valarray<typename _Dom::valarray>& __v,                 \
468          const _Expr<_Dom, typename _Dom::value_type>& __e)            \
469     {                                                                  \
470       typedef typename _Dom::value_type _Tp;                           \
471       typedef _BinClos<__##_Fun, _ValArray, _Expr, _Tp, _Dom> _Closure; \
472       return _Expr<_Closure, _Tp>(_Closure(__v, __e()));               \
473     }                                                                  \
474                                                                        \
475   template<class _Dom>                                                 \
476     inline _Expr<_BinClos<__##_Fun, _Expr, _Constant, _Dom,            \
477                           typename _Dom::value_type>,                  \
478                  typename _Dom::value_type>                            \
479     _Fun(const _Expr<_Dom, typename _Dom::value_type>& __e,            \
480          const typename _Dom::value_type& __t)                         \
481     {                                                                  \
482       typedef typename _Dom::value_type _Tp;                           \
483       typedef _BinClos<__##_Fun, _Expr, _Constant, _Dom, _Tp> _Closure;\
484       return _Expr<_Closure, _Tp>(_Closure(__e(), __t));               \
485     }                                                                  \
486                                                                        \
487   template<class _Dom>                                                 \
488     inline _Expr<_BinClos<__##_Fun, _Constant, _Expr,                  \
489                           typename _Dom::value_type, _Dom>,            \
490                  typename _Dom::value_type>                            \
491     _Fun(const typename _Dom::value_type& __t,                         \
492          const _Expr<_Dom, typename _Dom::value_type>& __e)            \
493     {                                                                  \
494       typedef typename _Dom::value_type _Tp;                           \
495       typedef _BinClos<__##_Fun, _Constant, _Expr, _Tp, _Dom> _Closure; \
496       return _Expr<_Closure, _Tp>(_Closure(__t, __e()));               \
497     }                                                                  \
498                                                                        \
499   template<typename _Tp>                                               \
500     inline _Expr<_BinClos<__##_Fun, _ValArray, _ValArray, _Tp, _Tp>, _Tp> \
501     _Fun(const valarray<_Tp>& __v, const valarray<_Tp>& __w)           \
502     {                                                                  \
503       typedef _BinClos<__##_Fun, _ValArray, _ValArray, _Tp, _Tp> _Closure; \
504       return _Expr<_Closure, _Tp>(_Closure(__v, __w));                 \
505     }                                                                  \
506                                                                        \
507   template<typename _Tp>                                               \
508     inline _Expr<_BinClos<__##_Fun, _ValArray, _Constant, _Tp, _Tp>, _Tp> \
509     _Fun(const valarray<_Tp>& __v, const _Tp& __t)                     \
510     {                                                                  \
511       typedef _BinClos<__##_Fun, _ValArray, _Constant, _Tp, _Tp> _Closure; \
512       return _Expr<_Closure, _Tp>(_Closure(__v, __t));                 \
513     }                                                                  \
514                                                                        \
515   template<typename _Tp>                                               \
516     inline _Expr<_BinClos<__##_Fun, _Constant, _ValArray, _Tp, _Tp>, _Tp> \
517     _Fun(const _Tp& __t, const valarray<_Tp>& __v)                     \
518     {                                                                  \
519       typedef _BinClos<__##_Fun, _Constant, _ValArray, _Tp, _Tp> _Closure; \
520       return _Expr<_Closure, _Tp>(_Closure(__t, __v));                 \
521     }
522
523 _DEFINE_EXPR_BINARY_FUNCTION(atan2)
524 _DEFINE_EXPR_BINARY_FUNCTION(pow)
525
526 #undef _DEFINE_EXPR_BINARY_FUNCTION
527
528 } // std::
529
530 #endif /* _CPP_VALARRAY_AFTER_H */
531
532 // Local Variables:
533 // mode:c++
534 // End: