1// The template and inlines for the -*- C++ -*- internal _Meta class.
2
3// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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/** @file valarray_after.h
32 *  This is an internal header file, included by other library headers.
33 *  You should not attempt to use it directly.
34 */
35
36// Written by Gabriel Dos Reis <Gabriel.Dos-Reis@cmla.ens-cachan.fr>
37
38#ifndef _VALARRAY_AFTER_H
39#define _VALARRAY_AFTER_H 1
40
41#pragma GCC system_header
42
43_GLIBCXX_BEGIN_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    {
226      valarray<_Tp> __v = valarray<_Tp>(*this)[__s];
227      return __v;
228    }
229
230  template<class _Clos, typename _Tp>
231    inline valarray<_Tp>
232    _Expr<_Clos, _Tp>::operator[](const gslice& __gs) const
233    {
234      valarray<_Tp> __v = valarray<_Tp>(*this)[__gs];
235      return __v;
236    }
237
238  template<class _Clos, typename _Tp>
239    inline valarray<_Tp>
240    _Expr<_Clos, _Tp>::operator[](const valarray<bool>& __m) const
241    {
242      valarray<_Tp> __v = valarray<_Tp>(*this)[__m];
243      return __v;
244    }
245
246  template<class _Clos, typename _Tp>
247    inline valarray<_Tp>
248    _Expr<_Clos, _Tp>::operator[](const valarray<size_t>& __i) const
249    {
250      valarray<_Tp> __v = valarray<_Tp>(*this)[__i];
251      return __v;
252    }
253
254  template<class _Clos, typename _Tp>
255    inline size_t
256    _Expr<_Clos, _Tp>::size() const
257    { return _M_closure.size(); }
258
259  template<class _Clos, typename _Tp>
260    inline valarray<_Tp>
261    _Expr<_Clos, _Tp>::shift(int __n) const
262    {
263      valarray<_Tp> __v = valarray<_Tp>(*this).shift(__n);
264      return __v;
265    }
266
267  template<class _Clos, typename _Tp>
268    inline valarray<_Tp>
269    _Expr<_Clos, _Tp>::cshift(int __n) const
270    {
271      valarray<_Tp> __v = valarray<_Tp>(*this).cshift(__n);
272      return __v;
273    }
274
275  template<class _Clos, typename _Tp>
276    inline valarray<_Tp>
277    _Expr<_Clos, _Tp>::apply(_Tp __f(const _Tp&)) const
278    {
279      valarray<_Tp> __v = valarray<_Tp>(*this).apply(__f);
280      return __v;
281    }
282
283  template<class _Clos, typename _Tp>
284    inline valarray<_Tp>
285    _Expr<_Clos, _Tp>::apply(_Tp __f(_Tp)) const
286    {
287      valarray<_Tp> __v = valarray<_Tp>(*this).apply(__f);
288      return __v;
289    }
290
291  // XXX: replace this with a more robust summation algorithm.
292  template<class _Clos, typename _Tp>
293    inline _Tp
294    _Expr<_Clos, _Tp>::sum() const
295    {
296      size_t __n = _M_closure.size();
297      if (__n == 0)
298	return _Tp();
299      else
300	{
301	  _Tp __s = _M_closure[--__n];
302	  while (__n != 0)
303	    __s += _M_closure[--__n];
304	  return __s;
305        }
306    }
307
308  template<class _Clos, typename _Tp>
309    inline _Tp
310    _Expr<_Clos, _Tp>::min() const
311    { return __valarray_min(_M_closure); }
312
313  template<class _Clos, typename _Tp>
314    inline _Tp
315    _Expr<_Clos, _Tp>::max() const
316    { return __valarray_max(_M_closure); }
317
318  template<class _Dom, typename _Tp>
319    inline _Expr<_UnClos<__logical_not, _Expr, _Dom>, bool>
320    _Expr<_Dom, _Tp>::operator!() const
321    {
322      typedef _UnClos<__logical_not, std::_Expr, _Dom> _Closure;
323      return _Expr<_Closure, _Tp>(_Closure(this->_M_closure));
324    }
325
326#define _DEFINE_EXPR_UNARY_OPERATOR(_Op, _Name)                           \
327  template<class _Dom, typename _Tp>                                      \
328    inline _Expr<_UnClos<_Name, std::_Expr, _Dom>, _Tp>                   \
329    _Expr<_Dom, _Tp>::operator _Op() const                                \
330    {                                                                     \
331      typedef _UnClos<_Name, std::_Expr, _Dom> _Closure;                  \
332      return _Expr<_Closure, _Tp>(_Closure(this->_M_closure));            \
333    }
334
335    _DEFINE_EXPR_UNARY_OPERATOR(+, __unary_plus)
336    _DEFINE_EXPR_UNARY_OPERATOR(-, __negate)
337    _DEFINE_EXPR_UNARY_OPERATOR(~, __bitwise_not)
338
339#undef _DEFINE_EXPR_UNARY_OPERATOR
340
341#define _DEFINE_EXPR_BINARY_OPERATOR(_Op, _Name)                        \
342  template<class _Dom1, class _Dom2>					\
343    inline _Expr<_BinClos<_Name, _Expr, _Expr, _Dom1, _Dom2>,           \
344           typename __fun<_Name, typename _Dom1::value_type>::result_type> \
345    operator _Op(const _Expr<_Dom1, typename _Dom1::value_type>& __v,   \
346	         const _Expr<_Dom2, typename _Dom2::value_type>& __w)   \
347    {                                                                   \
348      typedef typename _Dom1::value_type _Arg;                          \
349      typedef typename __fun<_Name, _Arg>::result_type _Value;          \
350      typedef _BinClos<_Name, _Expr, _Expr, _Dom1, _Dom2> _Closure;     \
351      return _Expr<_Closure, _Value>(_Closure(__v(), __w()));           \
352    }                                                                   \
353                                                                        \
354  template<class _Dom>                                                  \
355    inline _Expr<_BinClos<_Name, _Expr, _Constant, _Dom,                \
356                          typename _Dom::value_type>,                   \
357             typename __fun<_Name, typename _Dom::value_type>::result_type> \
358    operator _Op(const _Expr<_Dom, typename _Dom::value_type>& __v,     \
359                 const typename _Dom::value_type& __t)                  \
360    {                                                                   \
361      typedef typename _Dom::value_type _Arg;                           \
362      typedef typename __fun<_Name, _Arg>::result_type _Value;          \
363      typedef _BinClos<_Name, _Expr, _Constant, _Dom, _Arg> _Closure;   \
364      return _Expr<_Closure, _Value>(_Closure(__v(), __t));             \
365    }                                                                   \
366                                                                        \
367  template<class _Dom>                                                  \
368    inline _Expr<_BinClos<_Name, _Constant, _Expr,                      \
369                          typename _Dom::value_type, _Dom>,             \
370             typename __fun<_Name, typename _Dom::value_type>::result_type> \
371    operator _Op(const typename _Dom::value_type& __t,                  \
372                 const _Expr<_Dom, typename _Dom::value_type>& __v)     \
373    {                                                                   \
374      typedef typename _Dom::value_type _Arg;                           \
375      typedef typename __fun<_Name, _Arg>::result_type _Value;          \
376      typedef _BinClos<_Name, _Constant, _Expr, _Arg, _Dom> _Closure;   \
377      return _Expr<_Closure, _Value>(_Closure(__t, __v()));             \
378    }                                                                   \
379                                                                        \
380  template<class _Dom>                                                  \
381    inline _Expr<_BinClos<_Name, _Expr, _ValArray,                      \
382                          _Dom, typename _Dom::value_type>,             \
383             typename __fun<_Name, typename _Dom::value_type>::result_type> \
384    operator _Op(const _Expr<_Dom,typename _Dom::value_type>& __e,      \
385                 const valarray<typename _Dom::value_type>& __v)        \
386    {                                                                   \
387      typedef typename _Dom::value_type _Arg;                           \
388      typedef typename __fun<_Name, _Arg>::result_type _Value;          \
389      typedef _BinClos<_Name, _Expr, _ValArray, _Dom, _Arg> _Closure;   \
390      return _Expr<_Closure, _Value>(_Closure(__e(), __v));             \
391    }                                                                   \
392                                                                        \
393  template<class _Dom>                                                  \
394    inline _Expr<_BinClos<_Name, _ValArray, _Expr,                      \
395                 typename _Dom::value_type, _Dom>,                      \
396             typename __fun<_Name, typename _Dom::value_type>::result_type> \
397    operator _Op(const valarray<typename _Dom::value_type>& __v,        \
398                 const _Expr<_Dom, typename _Dom::value_type>& __e)     \
399    {                                                                   \
400      typedef typename _Dom::value_type _Tp;                            \
401      typedef typename __fun<_Name, _Tp>::result_type _Value;           \
402      typedef _BinClos<_Name, _ValArray, _Expr, _Tp, _Dom> _Closure;    \
403      return _Expr<_Closure, _Value>(_Closure(__v, __e ()));            \
404    }
405
406    _DEFINE_EXPR_BINARY_OPERATOR(+, __plus)
407    _DEFINE_EXPR_BINARY_OPERATOR(-, __minus)
408    _DEFINE_EXPR_BINARY_OPERATOR(*, __multiplies)
409    _DEFINE_EXPR_BINARY_OPERATOR(/, __divides)
410    _DEFINE_EXPR_BINARY_OPERATOR(%, __modulus)
411    _DEFINE_EXPR_BINARY_OPERATOR(^, __bitwise_xor)
412    _DEFINE_EXPR_BINARY_OPERATOR(&, __bitwise_and)
413    _DEFINE_EXPR_BINARY_OPERATOR(|, __bitwise_or)
414    _DEFINE_EXPR_BINARY_OPERATOR(<<, __shift_left)
415    _DEFINE_EXPR_BINARY_OPERATOR(>>, __shift_right)
416    _DEFINE_EXPR_BINARY_OPERATOR(&&, __logical_and)
417    _DEFINE_EXPR_BINARY_OPERATOR(||, __logical_or)
418    _DEFINE_EXPR_BINARY_OPERATOR(==, __equal_to)
419    _DEFINE_EXPR_BINARY_OPERATOR(!=, __not_equal_to)
420    _DEFINE_EXPR_BINARY_OPERATOR(<, __less)
421    _DEFINE_EXPR_BINARY_OPERATOR(>, __greater)
422    _DEFINE_EXPR_BINARY_OPERATOR(<=, __less_equal)
423    _DEFINE_EXPR_BINARY_OPERATOR(>=, __greater_equal)
424
425#undef _DEFINE_EXPR_BINARY_OPERATOR
426
427#define _DEFINE_EXPR_UNARY_FUNCTION(_Name)                               \
428  template<class _Dom>                                                   \
429    inline _Expr<_UnClos<__##_Name, _Expr, _Dom>,                        \
430                 typename _Dom::value_type>                              \
431    _Name(const _Expr<_Dom, typename _Dom::value_type>& __e)             \
432    {                                                                    \
433      typedef typename _Dom::value_type _Tp;                             \
434      typedef _UnClos<__##_Name, _Expr, _Dom> _Closure;                  \
435      return _Expr<_Closure, _Tp>(_Closure(__e()));                      \
436    }                                                                    \
437                                                                         \
438  template<typename _Tp>                                                 \
439    inline _Expr<_UnClos<__##_Name, _ValArray, _Tp>, _Tp>                \
440    _Name(const valarray<_Tp>& __v)                                      \
441    {                                                                    \
442      typedef _UnClos<__##_Name, _ValArray, _Tp> _Closure;               \
443      return _Expr<_Closure, _Tp>(_Closure(__v));                        \
444    }
445
446    _DEFINE_EXPR_UNARY_FUNCTION(abs)
447    _DEFINE_EXPR_UNARY_FUNCTION(cos)
448    _DEFINE_EXPR_UNARY_FUNCTION(acos)
449    _DEFINE_EXPR_UNARY_FUNCTION(cosh)
450    _DEFINE_EXPR_UNARY_FUNCTION(sin)
451    _DEFINE_EXPR_UNARY_FUNCTION(asin)
452    _DEFINE_EXPR_UNARY_FUNCTION(sinh)
453    _DEFINE_EXPR_UNARY_FUNCTION(tan)
454    _DEFINE_EXPR_UNARY_FUNCTION(tanh)
455    _DEFINE_EXPR_UNARY_FUNCTION(atan)
456    _DEFINE_EXPR_UNARY_FUNCTION(exp)
457    _DEFINE_EXPR_UNARY_FUNCTION(log)
458    _DEFINE_EXPR_UNARY_FUNCTION(log10)
459    _DEFINE_EXPR_UNARY_FUNCTION(sqrt)
460
461#undef _DEFINE_EXPR_UNARY_FUNCTION
462
463#define _DEFINE_EXPR_BINARY_FUNCTION(_Fun)                             \
464  template<class _Dom1, class _Dom2>                                   \
465    inline _Expr<_BinClos<__##_Fun, _Expr, _Expr, _Dom1, _Dom2>,       \
466		 typename _Dom1::value_type>                           \
467    _Fun(const _Expr<_Dom1, typename _Dom1::value_type>& __e1,         \
468	  const _Expr<_Dom2, typename _Dom2::value_type>& __e2)        \
469    {                                                                  \
470      typedef typename _Dom1::value_type _Tp;                          \
471      typedef _BinClos<__##_Fun, _Expr, _Expr, _Dom1, _Dom2> _Closure; \
472      return _Expr<_Closure, _Tp>(_Closure(__e1(), __e2()));           \
473    }                                                                  \
474                                                                       \
475  template<class _Dom>                                                 \
476    inline _Expr<_BinClos<__##_Fun, _Expr, _ValArray, _Dom,            \
477			  typename _Dom::value_type>,                  \
478		 typename _Dom::value_type>                            \
479    _Fun(const _Expr<_Dom, typename _Dom::value_type>& __e,            \
480	 const valarray<typename _Dom::value_type>& __v)               \
481    {                                                                  \
482      typedef typename _Dom::value_type _Tp;                           \
483      typedef _BinClos<__##_Fun, _Expr, _ValArray, _Dom, _Tp> _Closure; \
484      return _Expr<_Closure, _Tp>(_Closure(__e(), __v));               \
485    }                                                                  \
486                                                                       \
487  template<class _Dom>                                                 \
488    inline _Expr<_BinClos<__##_Fun, _ValArray, _Expr,                  \
489			  typename _Dom::value_type, _Dom>,            \
490		 typename _Dom::value_type>                            \
491    _Fun(const valarray<typename _Dom::valarray>& __v,                 \
492	 const _Expr<_Dom, typename _Dom::value_type>& __e)            \
493    {                                                                  \
494      typedef typename _Dom::value_type _Tp;                           \
495      typedef _BinClos<__##_Fun, _ValArray, _Expr, _Tp, _Dom> _Closure; \
496      return _Expr<_Closure, _Tp>(_Closure(__v, __e()));               \
497    }                                                                  \
498                                                                       \
499  template<class _Dom>                                                 \
500    inline _Expr<_BinClos<__##_Fun, _Expr, _Constant, _Dom,            \
501			  typename _Dom::value_type>,                  \
502		 typename _Dom::value_type>                            \
503    _Fun(const _Expr<_Dom, typename _Dom::value_type>& __e,            \
504	 const typename _Dom::value_type& __t)                         \
505    {                                                                  \
506      typedef typename _Dom::value_type _Tp;                           \
507      typedef _BinClos<__##_Fun, _Expr, _Constant, _Dom, _Tp> _Closure;\
508      return _Expr<_Closure, _Tp>(_Closure(__e(), __t));               \
509    }                                                                  \
510                                                                       \
511  template<class _Dom>                                                 \
512    inline _Expr<_BinClos<__##_Fun, _Constant, _Expr,                  \
513			  typename _Dom::value_type, _Dom>,            \
514		 typename _Dom::value_type>                            \
515    _Fun(const typename _Dom::value_type& __t,                         \
516	 const _Expr<_Dom, typename _Dom::value_type>& __e)            \
517    {                                                                  \
518      typedef typename _Dom::value_type _Tp;                           \
519      typedef _BinClos<__##_Fun, _Constant, _Expr, _Tp, _Dom> _Closure; \
520      return _Expr<_Closure, _Tp>(_Closure(__t, __e()));               \
521    }                                                                  \
522                                                                       \
523  template<typename _Tp>                                               \
524    inline _Expr<_BinClos<__##_Fun, _ValArray, _ValArray, _Tp, _Tp>, _Tp> \
525    _Fun(const valarray<_Tp>& __v, const valarray<_Tp>& __w)           \
526    {                                                                  \
527      typedef _BinClos<__##_Fun, _ValArray, _ValArray, _Tp, _Tp> _Closure; \
528      return _Expr<_Closure, _Tp>(_Closure(__v, __w));                 \
529    }                                                                  \
530                                                                       \
531  template<typename _Tp>                                               \
532    inline _Expr<_BinClos<__##_Fun, _ValArray, _Constant, _Tp, _Tp>, _Tp> \
533    _Fun(const valarray<_Tp>& __v, const _Tp& __t)                     \
534    {                                                                  \
535      typedef _BinClos<__##_Fun, _ValArray, _Constant, _Tp, _Tp> _Closure; \
536      return _Expr<_Closure, _Tp>(_Closure(__v, __t));                 \
537    }                                                                  \
538								       \
539  template<typename _Tp>                                               \
540    inline _Expr<_BinClos<__##_Fun, _Constant, _ValArray, _Tp, _Tp>, _Tp> \
541    _Fun(const _Tp& __t, const valarray<_Tp>& __v)                     \
542    {                                                                  \
543      typedef _BinClos<__##_Fun, _Constant, _ValArray, _Tp, _Tp> _Closure; \
544      return _Expr<_Closure, _Tp>(_Closure(__t, __v));                 \
545    }
546
547_DEFINE_EXPR_BINARY_FUNCTION(atan2)
548_DEFINE_EXPR_BINARY_FUNCTION(pow)
549
550#undef _DEFINE_EXPR_BINARY_FUNCTION
551
552_GLIBCXX_END_NAMESPACE
553
554#endif /* _CPP_VALARRAY_AFTER_H */
555