2002-01-04 Benjamin Kosnik <bkoz@redhat.com>
[official-gcc.git] / libstdc++-v3 / include / std / valarray
bloba3547080ed3a7201e99019811e99cb1fdeb5085f
1 // The template and inlines for the -*- C++ -*- valarray class.
3 // Copyright (C) 1997-1999, 2000, 2001 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.
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,
19 // USA.
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@DPTMaths.ENS-Cachan.Fr>
32 /** @file std_valarray.h
33  *  This is an internal header file, included by other library headers.
34  *  You should not attempt to use it directly.
35  */
37 #ifndef _CPP_VALARRAY
38 #define _CPP_VALARRAY 1
40 #pragma GCC system_header
42 #include <bits/c++config.h>
43 #include <cstddef>
44 #include <cmath>
45 #include <cstdlib>
46 #include <numeric>
47 #include <functional>
48 #include <algorithm>
50 namespace std
52     template<class _Clos, typename _Tp> class _Expr;
54     template<typename _Tp1, typename _Tp2> class _ValArray;    
56     template<template<class> class _Oper,
57         template<class, class> class _Meta, class _Dom> struct _UnClos;
59     template<template<class> class _Oper,
60         template<class, class> class _Meta1,
61         template<class, class> class _Meta2,
62         class _Dom1, class _Dom2> class _BinClos;
64     template<template<class, class> class _Meta, class _Dom> class _SClos;
66     template<template<class, class> class _Meta, class _Dom> class _GClos;
67     
68     template<template<class, class> class _Meta, class _Dom> class _IClos;
69     
70     template<template<class, class> class _Meta, class _Dom> class _ValFunClos;
72     template<template<class, class> class _Meta, class _Dom> class _RefFunClos;
74     template<class _Tp> struct _Unary_plus;
75     template<class _Tp> struct _Bitwise_and;
76     template<class _Tp> struct _Bitwise_or;
77     template<class _Tp> struct _Bitwise_xor;  
78     template<class _Tp> struct _Bitwise_not;
79     template<class _Tp> struct _Shift_left;
80     template<class _Tp> struct _Shift_right;
81   
82     template<class _Tp> class valarray;   // An array of type _Tp
83     class slice;                          // BLAS-like slice out of an array
84     template<class _Tp> class slice_array;
85     class gslice;                         // generalized slice out of an array
86     template<class _Tp> class gslice_array;
87     template<class _Tp> class mask_array;     // masked array
88     template<class _Tp> class indirect_array; // indirected array
90 } // namespace std
92 #include <bits/valarray_array.h>
93 #include <bits/valarray_meta.h>
94   
95 namespace std
97   template<class _Tp> class valarray
98   {
99   public:
100       typedef _Tp value_type;
102       // _lib.valarray.cons_ construct/destroy:
103       valarray();
104       explicit valarray(size_t);
105       valarray(const _Tp&, size_t);
106       valarray(const _Tp* __restrict__, size_t);
107       valarray(const valarray&);
108       valarray(const slice_array<_Tp>&);
109       valarray(const gslice_array<_Tp>&);
110       valarray(const mask_array<_Tp>&);
111       valarray(const indirect_array<_Tp>&);
112       template<class _Dom>
113       valarray(const _Expr<_Dom,_Tp>& __e);
114      ~valarray();
116       // _lib.valarray.assign_ assignment:
117       valarray<_Tp>& operator=(const valarray<_Tp>&);
118       valarray<_Tp>& operator=(const _Tp&);
119       valarray<_Tp>& operator=(const slice_array<_Tp>&);
120       valarray<_Tp>& operator=(const gslice_array<_Tp>&);
121       valarray<_Tp>& operator=(const mask_array<_Tp>&);
122       valarray<_Tp>& operator=(const indirect_array<_Tp>&);
124       template<class _Dom> valarray<_Tp>&
125         operator= (const _Expr<_Dom,_Tp>&);
127       // _lib.valarray.access_ element access:
128       // XXX: LWG to be resolved.
129       const _Tp&                 operator[](size_t) const;
130       _Tp&                operator[](size_t);           
131       // _lib.valarray.sub_ subset operations:
132       _Expr<_SClos<_ValArray,_Tp>, _Tp> operator[](slice) const;
133       slice_array<_Tp>    operator[](slice);
134       _Expr<_GClos<_ValArray,_Tp>, _Tp> operator[](const gslice&) const;
135       gslice_array<_Tp>   operator[](const gslice&);
136       valarray<_Tp>              operator[](const valarray<bool>&) const;
137       mask_array<_Tp>     operator[](const valarray<bool>&);
138       _Expr<_IClos<_ValArray, _Tp>, _Tp>
139         operator[](const valarray<size_t>&) const;
140       indirect_array<_Tp> operator[](const valarray<size_t>&);
142       // _lib.valarray.unary_ unary operators:
143       _Expr<_UnClos<_Unary_plus,_ValArray,_Tp>,_Tp>  operator+ () const;
144       _Expr<_UnClos<negate,_ValArray,_Tp>,_Tp> operator- () const;
145       _Expr<_UnClos<_Bitwise_not,_ValArray,_Tp>,_Tp> operator~ () const;
146       _Expr<_UnClos<logical_not,_ValArray,_Tp>,bool> operator! () const;
147       
148       // _lib.valarray.cassign_ computed assignment:
149       valarray<_Tp>& operator*= (const _Tp&);
150       valarray<_Tp>& operator/= (const _Tp&);
151       valarray<_Tp>& operator%= (const _Tp&);
152       valarray<_Tp>& operator+= (const _Tp&);
153       valarray<_Tp>& operator-= (const _Tp&);
154       valarray<_Tp>& operator^= (const _Tp&);
155       valarray<_Tp>& operator&= (const _Tp&);
156       valarray<_Tp>& operator|= (const _Tp&);
157       valarray<_Tp>& operator<<=(const _Tp&);
158       valarray<_Tp>& operator>>=(const _Tp&);
159       valarray<_Tp>& operator*= (const valarray<_Tp>&);
160       valarray<_Tp>& operator/= (const valarray<_Tp>&);
161       valarray<_Tp>& operator%= (const valarray<_Tp>&);
162       valarray<_Tp>& operator+= (const valarray<_Tp>&);
163       valarray<_Tp>& operator-= (const valarray<_Tp>&);
164       valarray<_Tp>& operator^= (const valarray<_Tp>&);
165       valarray<_Tp>& operator|= (const valarray<_Tp>&);
166       valarray<_Tp>& operator&= (const valarray<_Tp>&);
167       valarray<_Tp>& operator<<=(const valarray<_Tp>&);
168       valarray<_Tp>& operator>>=(const valarray<_Tp>&);
170       template<class _Dom>
171         valarray<_Tp>& operator*= (const _Expr<_Dom,_Tp>&);
172       template<class _Dom>
173         valarray<_Tp>& operator/= (const _Expr<_Dom,_Tp>&);
174       template<class _Dom>
175         valarray<_Tp>& operator%= (const _Expr<_Dom,_Tp>&);
176       template<class _Dom>
177         valarray<_Tp>& operator+= (const _Expr<_Dom,_Tp>&);
178       template<class _Dom>
179         valarray<_Tp>& operator-= (const _Expr<_Dom,_Tp>&);
180       template<class _Dom>
181         valarray<_Tp>& operator^= (const _Expr<_Dom,_Tp>&);
182       template<class _Dom>
183         valarray<_Tp>& operator|= (const _Expr<_Dom,_Tp>&);
184       template<class _Dom>
185         valarray<_Tp>& operator&= (const _Expr<_Dom,_Tp>&);
186       template<class _Dom>
187         valarray<_Tp>& operator<<=(const _Expr<_Dom,_Tp>&);
188       template<class _Dom>
189         valarray<_Tp>& operator>>=(const _Expr<_Dom,_Tp>&);
191       
192       // _lib.valarray.members_ member functions:
193       size_t size() const;
194       _Tp    sum() const;       
195       _Tp    min() const;       
196       _Tp    max() const;       
198 //           // FIXME: Extension
199 //       _Tp    product () const;
201       valarray<_Tp> shift (int) const;
202       valarray<_Tp> cshift(int) const;
203       _Expr<_ValFunClos<_ValArray,_Tp>,_Tp> apply(_Tp func(_Tp)) const;
204       _Expr<_RefFunClos<_ValArray,_Tp>,_Tp> apply(_Tp func(const _Tp&)) const;
205       void resize(size_t __size, _Tp __c = _Tp());
207   private:
208       size_t _M_size;
209       _Tp* __restrict__ _M_data;
211       friend class _Array<_Tp>;
212   };
215   template<typename _Tp> struct _Unary_plus : unary_function<_Tp,_Tp> {
216       _Tp operator() (const _Tp& __t) const { return __t; }
217   };
219   template<typename _Tp> struct _Bitwise_and : binary_function<_Tp,_Tp,_Tp> {
220       _Tp operator() (_Tp __x, _Tp __y) const { return __x & __y; }
221   };
223   template<typename _Tp> struct _Bitwise_or : binary_function<_Tp,_Tp,_Tp> {
224       _Tp operator() (_Tp __x, _Tp __y) const { return __x | __y; }
225   };
227   template<typename _Tp> struct _Bitwise_xor : binary_function<_Tp,_Tp,_Tp> {
228       _Tp operator() (_Tp __x, _Tp __y) const { return __x ^ __y; }
229   };
230   
231   template<typename _Tp> struct _Bitwise_not : unary_function<_Tp,_Tp> {
232       _Tp operator() (_Tp __t) const { return ~__t; }
233   };
235   template<typename _Tp> struct _Shift_left : unary_function<_Tp,_Tp> {
236       _Tp operator() (_Tp __x, _Tp __y) const { return __x << __y; }
237   };
239   template<typename _Tp> struct _Shift_right : unary_function<_Tp,_Tp> {
240       _Tp operator() (_Tp __x, _Tp __y) const { return __x >> __y; }
241   };
243   
244   template<typename _Tp>
245   inline const _Tp&
246   valarray<_Tp>::operator[] (size_t __i) const
247   { return _M_data[__i]; }
249   template<typename _Tp>
250   inline _Tp&
251   valarray<_Tp>::operator[] (size_t __i)
252   { return _M_data[__i]; }
254 } // std::
255       
256 #include <bits/slice.h>
257 #include <bits/slice_array.h>
258 #include <bits/gslice.h>
259 #include <bits/gslice_array.h>
260 #include <bits/mask_array.h>
261 #include <bits/indirect_array.h>
263 namespace std
265   template<typename _Tp>
266   inline valarray<_Tp>::valarray () : _M_size (0), _M_data (0) {}
268   template<typename _Tp>
269   inline valarray<_Tp>::valarray (size_t __n) 
270       : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n))
271   { __valarray_default_construct(_M_data, _M_data + __n); }
273   template<typename _Tp>
274   inline valarray<_Tp>::valarray (const _Tp& __t, size_t __n)
275     : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n))
276   { __valarray_fill_construct (_M_data, _M_data + __n, __t); }
278   template<typename _Tp>
279   inline valarray<_Tp>::valarray (const _Tp* __restrict__ __p, size_t __n)
280     : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n))
281   { __valarray_copy_construct (__p, __p + __n, _M_data); }
283   template<typename _Tp>
284   inline valarray<_Tp>::valarray (const valarray<_Tp>& __v)
285     : _M_size(__v._M_size), _M_data(__valarray_get_storage<_Tp>(__v._M_size))
286   { __valarray_copy_construct (__v._M_data, __v._M_data + _M_size, _M_data); }
288   template<typename _Tp>
289   inline valarray<_Tp>::valarray (const slice_array<_Tp>& __sa)
290     : _M_size(__sa._M_sz), _M_data(__valarray_get_storage<_Tp>(__sa._M_sz))
291   {
292     __valarray_copy
293       (__sa._M_array, __sa._M_sz, __sa._M_stride, _Array<_Tp>(_M_data));
294   }
296   template<typename _Tp>
297   inline valarray<_Tp>::valarray (const gslice_array<_Tp>& __ga)
298     : _M_size(__ga._M_index.size()),
299       _M_data(__valarray_get_storage<_Tp>(_M_size))
300   {
301     __valarray_copy
302       (__ga._M_array, _Array<size_t>(__ga._M_index),
303        _Array<_Tp>(_M_data), _M_size);
304   }
306   template<typename _Tp>
307   inline valarray<_Tp>::valarray (const mask_array<_Tp>& __ma)
308     : _M_size(__ma._M_sz), _M_data(__valarray_get_storage<_Tp>(__ma._M_sz))
309   {
310     __valarray_copy
311       (__ma._M_array, __ma._M_mask, _Array<_Tp>(_M_data), _M_size);
312   }
314   template<typename _Tp>
315   inline valarray<_Tp>::valarray (const indirect_array<_Tp>& __ia)
316     : _M_size(__ia._M_sz), _M_data(__valarray_get_storage<_Tp>(__ia._M_sz))
317   {
318     __valarray_copy
319       (__ia._M_array, __ia._M_index, _Array<_Tp>(_M_data), _M_size);
320   }
322   template<typename _Tp> template<class _Dom>
323   inline valarray<_Tp>::valarray (const _Expr<_Dom, _Tp>& __e)
324     : _M_size(__e.size ()), _M_data(__valarray_get_storage<_Tp>(_M_size))
325   { __valarray_copy (__e, _M_size, _Array<_Tp>(_M_data)); }
327   template<typename _Tp>
328   inline valarray<_Tp>::~valarray ()
329   {
330       __valarray_destroy_elements(_M_data, _M_data + _M_size);
331       __valarray_release_memory(_M_data);
332   }
334   template<typename _Tp>
335   inline valarray<_Tp>&
336   valarray<_Tp>::operator= (const valarray<_Tp>& __v)
337   {
338       __valarray_copy(__v._M_data, _M_size, _M_data);
339       return *this;
340   }
342   template<typename _Tp>
343   inline valarray<_Tp>&
344   valarray<_Tp>::operator= (const _Tp& __t)
345   {
346       __valarray_fill (_M_data, _M_size, __t);
347       return *this;
348   }
350   template<typename _Tp>
351   inline valarray<_Tp>&
352   valarray<_Tp>::operator= (const slice_array<_Tp>& __sa)
353   {
354       __valarray_copy (__sa._M_array, __sa._M_sz,
355               __sa._M_stride, _Array<_Tp>(_M_data));
356       return *this;
357   }
359   template<typename _Tp>
360   inline valarray<_Tp>&
361   valarray<_Tp>::operator= (const gslice_array<_Tp>& __ga)
362   {
363       __valarray_copy (__ga._M_array, _Array<size_t>(__ga._M_index),
364               _Array<_Tp>(_M_data), _M_size);
365       return *this;
366   }
368   template<typename _Tp>
369   inline valarray<_Tp>&
370   valarray<_Tp>::operator= (const mask_array<_Tp>& __ma)
371   {
372       __valarray_copy (__ma._M_array, __ma._M_mask,
373               _Array<_Tp>(_M_data), _M_size);
374       return *this;
375   }
377   template<typename _Tp>
378   inline valarray<_Tp>&
379   valarray<_Tp>::operator= (const indirect_array<_Tp>& __ia)
380   {
381       __valarray_copy (__ia._M_array, __ia._M_index,
382                _Array<_Tp>(_M_data), _M_size);
383       return *this;
384   }
386   template<typename _Tp> template<class _Dom>
387   inline valarray<_Tp>&
388   valarray<_Tp>::operator= (const _Expr<_Dom, _Tp>& __e)
389   {
390       __valarray_copy (__e, _M_size, _Array<_Tp>(_M_data));
391       return *this;
392   }
394   template<typename _Tp>
395   inline _Expr<_SClos<_ValArray,_Tp>, _Tp>
396   valarray<_Tp>::operator[] (slice __s) const
397   {
398       typedef _SClos<_ValArray,_Tp> _Closure;
399       return _Expr<_Closure, _Tp> (_Closure (_Array<_Tp>(_M_data), __s));
400   }
402   template<typename _Tp>
403   inline slice_array<_Tp>
404   valarray<_Tp>::operator[] (slice __s)
405   {
406       return slice_array<_Tp> (_Array<_Tp>(_M_data), __s);
407   }
409   template<typename _Tp>
410   inline _Expr<_GClos<_ValArray,_Tp>, _Tp>
411   valarray<_Tp>::operator[] (const gslice& __gs) const
412   {
413       typedef _GClos<_ValArray,_Tp> _Closure;
414       return _Expr<_Closure, _Tp>
415           (_Closure (_Array<_Tp>(_M_data), __gs._M_index->_M_index));
416   }
418   template<typename _Tp>
419   inline gslice_array<_Tp>
420   valarray<_Tp>::operator[] (const gslice& __gs)
421   {
422       return gslice_array<_Tp>
423           (_Array<_Tp>(_M_data), __gs._M_index->_M_index);
424   }
426   template<typename _Tp>
427   inline valarray<_Tp>
428   valarray<_Tp>::operator[] (const valarray<bool>& __m) const
429   {
430       size_t __s (0);
431       size_t __e (__m.size ());
432       for (size_t __i=0; __i<__e; ++__i)
433           if (__m[__i]) ++__s;
434       return valarray<_Tp> (mask_array<_Tp> (_Array<_Tp>(_M_data), __s,
435                                          _Array<bool> (__m)));
436   }
438   template<typename _Tp>
439   inline mask_array<_Tp>
440   valarray<_Tp>::operator[] (const valarray<bool>& __m)
441   {
442       size_t __s (0);
443       size_t __e (__m.size ());
444       for (size_t __i=0; __i<__e; ++__i)
445           if (__m[__i]) ++__s;
446       return mask_array<_Tp> (_Array<_Tp>(_M_data), __s, _Array<bool> (__m));
447   }
449   template<typename _Tp>
450   inline _Expr<_IClos<_ValArray,_Tp>, _Tp>
451   valarray<_Tp>::operator[] (const valarray<size_t>& __i) const
452   {
453       typedef _IClos<_ValArray,_Tp> _Closure;
454       return _Expr<_Closure, _Tp> (_Closure (*this, __i));
455   }
457   template<typename _Tp>
458   inline indirect_array<_Tp>
459   valarray<_Tp>::operator[] (const valarray<size_t>& __i)
460   {
461       return indirect_array<_Tp> (_Array<_Tp>(_M_data), __i.size(),
462                                 _Array<size_t> (__i));
463   }
465   template<class _Tp>
466   inline size_t valarray<_Tp>::size () const { return _M_size; }
468   template<class _Tp>
469   inline _Tp
470   valarray<_Tp>::sum () const
471   {
472       return __valarray_sum(_M_data, _M_data + _M_size);
473   }
475 //   template<typename _Tp>
476 //   inline _Tp
477 //   valarray<_Tp>::product () const
478 //   {
479 //       return __valarray_product(_M_data, _M_data + _M_size);
480 //   }
482   template <class _Tp>
483      inline valarray<_Tp>
484      valarray<_Tp>::shift(int __n) const
485      {
486        _Tp* const __a = static_cast<_Tp*>
487          (__builtin_alloca(sizeof(_Tp) * _M_size));
488        if (__n == 0)                          // no shift
489          __valarray_copy_construct(_M_data, _M_data + _M_size, __a);
490        else if (__n > 0)         // __n > 0: shift left
491          {                 
492            if (size_t(__n) > _M_size)
493              __valarray_default_construct(__a, __a + __n);
494            else
495              {
496                __valarray_copy_construct(_M_data+__n, _M_data + _M_size, __a);
497                __valarray_default_construct(__a+_M_size-__n, __a + _M_size);
498              }
499          }
500        else                        // __n < 0: shift right
501          {                          
502            __valarray_copy_construct (_M_data, _M_data+_M_size+__n, __a-__n);
503            __valarray_default_construct(__a, __a - __n);
504          }
505        return valarray<_Tp> (__a, _M_size);
506      }
508   template <class _Tp>
509      inline valarray<_Tp>
510      valarray<_Tp>::cshift (int __n) const
511      {
512        _Tp* const __a = static_cast<_Tp*>
513          (__builtin_alloca (sizeof(_Tp) * _M_size));
514        if (__n == 0)               // no cshift
515          __valarray_copy_construct(_M_data, _M_data + _M_size, __a);
516        else if (__n > 0)           // cshift left
517          {               
518            __valarray_copy_construct(_M_data, _M_data+__n, __a+_M_size-__n);
519            __valarray_copy_construct(_M_data+__n, _M_data + _M_size, __a);
520          }
521        else                        // cshift right
522          {                       
523            __valarray_copy_construct
524              (_M_data + _M_size+__n, _M_data + _M_size, __a);
525            __valarray_copy_construct
526              (_M_data, _M_data + _M_size+__n, __a - __n);
527          }
528        return valarray<_Tp>(__a, _M_size);
529      }
531   template <class _Tp>
532   inline void
533   valarray<_Tp>::resize (size_t __n, _Tp __c)
534   {
535     // This complication is so to make valarray<valarray<T> > work
536     // even though it is not required by the standard.  Nobody should
537     // be saying valarray<valarray<T> > anyway.  See the specs.
538     __valarray_destroy_elements(_M_data, _M_data + _M_size);
539     if (_M_size != __n)
540       {
541         __valarray_release_memory(_M_data);
542         _M_size = __n;
543         _M_data = __valarray_get_storage<_Tp>(__n);
544       }
545     __valarray_fill_construct(_M_data, _M_data + __n, __c);
546   }
547     
548   template<typename _Tp>
549   inline _Tp
550   valarray<_Tp>::min() const
551   {
552       return *min_element (_M_data, _M_data+_M_size);
553   }
555   template<typename _Tp>
556   inline _Tp
557   valarray<_Tp>::max() const
558   {
559       return *max_element (_M_data, _M_data+_M_size);
560   }
561   
562   template<class _Tp>
563   inline _Expr<_ValFunClos<_ValArray,_Tp>,_Tp>
564   valarray<_Tp>::apply (_Tp func (_Tp)) const
565   {
566       typedef _ValFunClos<_ValArray,_Tp> _Closure;
567       return _Expr<_Closure,_Tp> (_Closure (*this, func));
568   }
570   template<class _Tp>
571   inline _Expr<_RefFunClos<_ValArray,_Tp>,_Tp>
572   valarray<_Tp>::apply (_Tp func (const _Tp &)) const
573   {
574       typedef _RefFunClos<_ValArray,_Tp> _Closure;
575       return _Expr<_Closure,_Tp> (_Closure (*this, func));
576   }
578 #define _DEFINE_VALARRAY_UNARY_OPERATOR(_Op, _Name)                     \
579   template<typename _Tp>                                                \
580   inline _Expr<_UnClos<_Name,_ValArray,_Tp>, _Tp>                       \
581   valarray<_Tp>::operator _Op() const                                   \
582   {                                                                     \
583       typedef _UnClos<_Name,_ValArray,_Tp> _Closure;                    \
584       return _Expr<_Closure, _Tp> (_Closure (*this));                   \
585   }
587     _DEFINE_VALARRAY_UNARY_OPERATOR(+, _Unary_plus)
588     _DEFINE_VALARRAY_UNARY_OPERATOR(-, negate)
589     _DEFINE_VALARRAY_UNARY_OPERATOR(~, _Bitwise_not)
591 #undef _DEFINE_VALARRAY_UNARY_OPERATOR
592   
593   template<typename _Tp>
594   inline _Expr<_UnClos<logical_not,_ValArray,_Tp>, bool>
595   valarray<_Tp>::operator!() const
596   {
597       typedef _UnClos<logical_not,_ValArray,_Tp> _Closure;
598       return _Expr<_Closure, bool> (_Closure (*this));
599   }
601 #define _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(_Op, _Name)               \
602   template<class _Tp>                                                   \
603   inline valarray<_Tp> &                                                \
604   valarray<_Tp>::operator _Op##= (const _Tp &__t)                       \
605   {                                                                     \
606       _Array_augmented_##_Name (_Array<_Tp>(_M_data), _M_size, __t);    \
607       return *this;                                                     \
608   }                                                                     \
609                                                                         \
610   template<class _Tp>                                                   \
611   inline valarray<_Tp> &                                                \
612   valarray<_Tp>::operator _Op##= (const valarray<_Tp> &__v)             \
613   {                                                                     \
614       _Array_augmented_##_Name (_Array<_Tp>(_M_data), _M_size,          \
615                                _Array<_Tp>(__v._M_data));               \
616       return *this;                                                     \
617   }
619 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(+, plus)
620 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(-, minus)
621 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(*, multiplies)
622 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(/, divides)
623 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(%, modulus)
624 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(^, xor)
625 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(&, and)
626 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(|, or)
627 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(<<, shift_left)
628 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(>>, shift_right)
630 #undef _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT
633 } // std::
634   
636 namespace std
639 #define _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(_Op, _Name)          \
640   template<class _Tp> template<class _Dom>                              \
641   inline valarray<_Tp> &                                                \
642   valarray<_Tp>::operator _Op##= (const _Expr<_Dom,_Tp> &__e)           \
643   {                                                                     \
644       _Array_augmented_##_Name (_Array<_Tp>(_M_data), __e, _M_size);    \
645       return *this;                                                     \
646   }
648 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(+, plus)
649 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(-, minus)
650 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(*, multiplies)
651 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(/, divides)
652 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(%, modulus)
653 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(^, xor)
654 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(&, and)
655 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(|, or)
656 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(<<, shift_left)
657 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(>>, shift_right)
659 #undef _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT
660     
662 #define _DEFINE_BINARY_OPERATOR(_Op, _Name)                             \
663   template<typename _Tp>                                                \
664   inline _Expr<_BinClos<_Name,_ValArray,_ValArray,_Tp,_Tp>, _Tp>        \
665   operator _Op (const valarray<_Tp> &__v, const valarray<_Tp> &__w)     \
666   {                                                                     \
667       typedef _BinClos<_Name,_ValArray,_ValArray,_Tp,_Tp> _Closure;     \
668       return _Expr<_Closure, _Tp> (_Closure (__v, __w));                \
669   }                                                                     \
670                                                                         \
671   template<typename _Tp>                                                \
672   inline _Expr<_BinClos<_Name,_ValArray,_Constant,_Tp,_Tp>,_Tp>         \
673   operator _Op (const valarray<_Tp> &__v, const _Tp &__t)               \
674   {                                                                     \
675       typedef _BinClos<_Name,_ValArray,_Constant,_Tp,_Tp> _Closure;     \
676       return _Expr<_Closure, _Tp> (_Closure (__v, __t));                \
677   }                                                                     \
678                                                                         \
679   template<typename _Tp>                                                \
680   inline _Expr<_BinClos<_Name,_Constant,_ValArray,_Tp,_Tp>,_Tp>         \
681   operator _Op (const _Tp &__t, const valarray<_Tp> &__v)               \
682   {                                                                     \
683       typedef _BinClos<_Name,_Constant,_ValArray,_Tp,_Tp> _Closure;     \
684       return _Expr<_Closure, _Tp> (_Closure (__t, __v));                \
685   }
687 _DEFINE_BINARY_OPERATOR(+, plus)
688 _DEFINE_BINARY_OPERATOR(-, minus)
689 _DEFINE_BINARY_OPERATOR(*, multiplies)
690 _DEFINE_BINARY_OPERATOR(/, divides)
691 _DEFINE_BINARY_OPERATOR(%, modulus)
692 _DEFINE_BINARY_OPERATOR(^, _Bitwise_xor)
693 _DEFINE_BINARY_OPERATOR(&, _Bitwise_and)
694 _DEFINE_BINARY_OPERATOR(|, _Bitwise_or)
695 _DEFINE_BINARY_OPERATOR(<<, _Shift_left)
696 _DEFINE_BINARY_OPERATOR(>>, _Shift_right)
698 #undef _DEFINE_BINARY_OPERATOR
700 #define _DEFINE_LOGICAL_OPERATOR(_Op, _Name)                            \
701   template<typename _Tp>                                                \
702   inline _Expr<_BinClos<_Name,_ValArray,_ValArray,_Tp,_Tp>,bool>        \
703   operator _Op (const valarray<_Tp> &__v, const valarray<_Tp> &__w)     \
704   {                                                                     \
705       typedef _BinClos<_Name,_ValArray,_ValArray,_Tp,_Tp> _Closure;     \
706       return _Expr<_Closure, bool> (_Closure (__v, __w));               \
707   }                                                                     \
708                                                                         \
709   template<class _Tp>                                                   \
710   inline _Expr<_BinClos<_Name,_ValArray,_Constant,_Tp,_Tp>,bool>        \
711   operator _Op (const valarray<_Tp> &__v, const _Tp &__t)               \
712   {                                                                     \
713       typedef _BinClos<_Name,_ValArray,_Constant,_Tp,_Tp> _Closure;     \
714       return _Expr<_Closure, bool> (_Closure (__v, __t));               \
715   }                                                                     \
716                                                                         \
717   template<class _Tp>                                                   \
718   inline _Expr<_BinClos<_Name,_Constant,_ValArray,_Tp,_Tp>,bool>        \
719   operator _Op (const _Tp &__t, const valarray<_Tp> &__v)               \
720   {                                                                     \
721       typedef _BinClos<_Name,_Constant,_ValArray,_Tp,_Tp> _Closure;     \
722       return _Expr<_Closure, bool> (_Closure (__t, __v));               \
723   }
725 _DEFINE_LOGICAL_OPERATOR(&&, logical_and)
726 _DEFINE_LOGICAL_OPERATOR(||, logical_or)
727 _DEFINE_LOGICAL_OPERATOR(==, equal_to)
728 _DEFINE_LOGICAL_OPERATOR(!=, not_equal_to)
729 _DEFINE_LOGICAL_OPERATOR(<, less)
730 _DEFINE_LOGICAL_OPERATOR(>, greater)
731 _DEFINE_LOGICAL_OPERATOR(<=, less_equal)
732 _DEFINE_LOGICAL_OPERATOR(>=, greater_equal)
734 #undef _DEFINE_LOGICAL_OPERATOR
736 } // namespace std
738 #endif // _CPP_VALARRAY
740 // Local Variables:
741 // mode:c++
742 // End: