OSDN Git Service

d7bd7a44959772f64566025a0a4fd97a237e974f
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / std / std_valarray.h
1 // The template and inlines for the -*- C++ -*- valarray class.
2
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002
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@DPTMaths.ENS-Cachan.Fr>
32
33 /** @file valarray
34  *  This is a Standard C++ Library header.  You should @c #include this header
35  *  in your programs, rather than any of the "st[dl]_*.h" implementation files.
36  */
37
38 #ifndef _CPP_VALARRAY
39 #define _CPP_VALARRAY 1
40
41 #pragma GCC system_header
42
43 #include <bits/c++config.h>
44 #include <cstddef>
45 #include <cmath>
46 #include <cstdlib>
47 #include <numeric>
48 #include <functional>
49 #include <algorithm>
50
51 namespace std
52 {
53     template<class _Clos, typename _Tp> class _Expr;
54
55     template<typename _Tp1, typename _Tp2> class _ValArray;    
56
57     template<template<class> class _Oper,
58         template<class, class> class _Meta, class _Dom> struct _UnClos;
59
60     template<template<class> class _Oper,
61         template<class, class> class _Meta1,
62         template<class, class> class _Meta2,
63         class _Dom1, class _Dom2> class _BinClos;
64
65     template<template<class, class> class _Meta, class _Dom> class _SClos;
66
67     template<template<class, class> class _Meta, class _Dom> class _GClos;
68     
69     template<template<class, class> class _Meta, class _Dom> class _IClos;
70     
71     template<template<class, class> class _Meta, class _Dom> class _ValFunClos;
72
73     template<template<class, class> class _Meta, class _Dom> class _RefFunClos;
74
75     template<class _Tp> struct _Unary_plus;
76     template<class _Tp> struct _Bitwise_and;
77     template<class _Tp> struct _Bitwise_or;
78     template<class _Tp> struct _Bitwise_xor;  
79     template<class _Tp> struct _Bitwise_not;
80     template<class _Tp> struct _Shift_left;
81     template<class _Tp> struct _Shift_right;
82   
83     template<class _Tp> class valarray;   // An array of type _Tp
84     class slice;                          // BLAS-like slice out of an array
85     template<class _Tp> class slice_array;
86     class gslice;                         // generalized slice out of an array
87     template<class _Tp> class gslice_array;
88     template<class _Tp> class mask_array;     // masked array
89     template<class _Tp> class indirect_array; // indirected array
90
91 } // namespace std
92
93 #include <bits/valarray_array.h>
94 #include <bits/valarray_meta.h>
95   
96 namespace std
97 {
98   template<class _Tp> class valarray
99   {
100   public:
101       typedef _Tp value_type;
102
103       // _lib.valarray.cons_ construct/destroy:
104       valarray();
105       explicit valarray(size_t);
106       valarray(const _Tp&, size_t);
107       valarray(const _Tp* __restrict__, size_t);
108       valarray(const valarray&);
109       valarray(const slice_array<_Tp>&);
110       valarray(const gslice_array<_Tp>&);
111       valarray(const mask_array<_Tp>&);
112       valarray(const indirect_array<_Tp>&);
113       template<class _Dom>
114       valarray(const _Expr<_Dom,_Tp>& __e);
115      ~valarray();
116
117       // _lib.valarray.assign_ assignment:
118       valarray<_Tp>& operator=(const valarray<_Tp>&);
119       valarray<_Tp>& operator=(const _Tp&);
120       valarray<_Tp>& operator=(const slice_array<_Tp>&);
121       valarray<_Tp>& operator=(const gslice_array<_Tp>&);
122       valarray<_Tp>& operator=(const mask_array<_Tp>&);
123       valarray<_Tp>& operator=(const indirect_array<_Tp>&);
124
125       template<class _Dom> valarray<_Tp>&
126         operator= (const _Expr<_Dom,_Tp>&);
127
128       // _lib.valarray.access_ element access:
129       // XXX: LWG to be resolved.
130       const _Tp&                 operator[](size_t) const;
131       _Tp&                operator[](size_t);           
132       // _lib.valarray.sub_ subset operations:
133       _Expr<_SClos<_ValArray,_Tp>, _Tp> operator[](slice) const;
134       slice_array<_Tp>    operator[](slice);
135       _Expr<_GClos<_ValArray,_Tp>, _Tp> operator[](const gslice&) const;
136       gslice_array<_Tp>   operator[](const gslice&);
137       valarray<_Tp>              operator[](const valarray<bool>&) const;
138       mask_array<_Tp>     operator[](const valarray<bool>&);
139       _Expr<_IClos<_ValArray, _Tp>, _Tp>
140         operator[](const valarray<size_t>&) const;
141       indirect_array<_Tp> operator[](const valarray<size_t>&);
142
143       // _lib.valarray.unary_ unary operators:
144       _Expr<_UnClos<_Unary_plus,_ValArray,_Tp>,_Tp>  operator+ () const;
145       _Expr<_UnClos<negate,_ValArray,_Tp>,_Tp> operator- () const;
146       _Expr<_UnClos<_Bitwise_not,_ValArray,_Tp>,_Tp> operator~ () const;
147       _Expr<_UnClos<logical_not,_ValArray,_Tp>,bool> operator! () const;
148       
149       // _lib.valarray.cassign_ computed assignment:
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 _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>&);
169       valarray<_Tp>& operator>>=(const valarray<_Tp>&);
170
171       template<class _Dom>
172         valarray<_Tp>& operator*= (const _Expr<_Dom,_Tp>&);
173       template<class _Dom>
174         valarray<_Tp>& operator/= (const _Expr<_Dom,_Tp>&);
175       template<class _Dom>
176         valarray<_Tp>& operator%= (const _Expr<_Dom,_Tp>&);
177       template<class _Dom>
178         valarray<_Tp>& operator+= (const _Expr<_Dom,_Tp>&);
179       template<class _Dom>
180         valarray<_Tp>& operator-= (const _Expr<_Dom,_Tp>&);
181       template<class _Dom>
182         valarray<_Tp>& operator^= (const _Expr<_Dom,_Tp>&);
183       template<class _Dom>
184         valarray<_Tp>& operator|= (const _Expr<_Dom,_Tp>&);
185       template<class _Dom>
186         valarray<_Tp>& operator&= (const _Expr<_Dom,_Tp>&);
187       template<class _Dom>
188         valarray<_Tp>& operator<<=(const _Expr<_Dom,_Tp>&);
189       template<class _Dom>
190         valarray<_Tp>& operator>>=(const _Expr<_Dom,_Tp>&);
191
192       
193       // _lib.valarray.members_ member functions:
194       size_t size() const;
195       _Tp    sum() const;       
196       _Tp    min() const;       
197       _Tp    max() const;       
198
199 //           // FIXME: Extension
200 //       _Tp    product () const;
201
202       valarray<_Tp> shift (int) const;
203       valarray<_Tp> cshift(int) const;
204       _Expr<_ValFunClos<_ValArray,_Tp>,_Tp> apply(_Tp func(_Tp)) const;
205       _Expr<_RefFunClos<_ValArray,_Tp>,_Tp> apply(_Tp func(const _Tp&)) const;
206       void resize(size_t __size, _Tp __c = _Tp());
207
208   private:
209       size_t _M_size;
210       _Tp* __restrict__ _M_data;
211
212       friend class _Array<_Tp>;
213   };
214
215
216   template<typename _Tp> struct _Unary_plus : unary_function<_Tp,_Tp> {
217       _Tp operator() (const _Tp& __t) const { return __t; }
218   };
219
220   template<typename _Tp> struct _Bitwise_and : binary_function<_Tp,_Tp,_Tp> {
221       _Tp operator() (_Tp __x, _Tp __y) const { return __x & __y; }
222   };
223
224   template<typename _Tp> struct _Bitwise_or : binary_function<_Tp,_Tp,_Tp> {
225       _Tp operator() (_Tp __x, _Tp __y) const { return __x | __y; }
226   };
227
228   template<typename _Tp> struct _Bitwise_xor : binary_function<_Tp,_Tp,_Tp> {
229       _Tp operator() (_Tp __x, _Tp __y) const { return __x ^ __y; }
230   };
231   
232   template<typename _Tp> struct _Bitwise_not : unary_function<_Tp,_Tp> {
233       _Tp operator() (_Tp __t) const { return ~__t; }
234   };
235
236   template<typename _Tp> struct _Shift_left : unary_function<_Tp,_Tp> {
237       _Tp operator() (_Tp __x, _Tp __y) const { return __x << __y; }
238   };
239
240   template<typename _Tp> struct _Shift_right : unary_function<_Tp,_Tp> {
241       _Tp operator() (_Tp __x, _Tp __y) const { return __x >> __y; }
242   };
243
244   
245   template<typename _Tp>
246   inline const _Tp&
247   valarray<_Tp>::operator[] (size_t __i) const
248   { return _M_data[__i]; }
249
250   template<typename _Tp>
251   inline _Tp&
252   valarray<_Tp>::operator[] (size_t __i)
253   { return _M_data[__i]; }
254
255 } // std::
256       
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>
262
263 namespace std
264 {
265   template<typename _Tp>
266   inline valarray<_Tp>::valarray () : _M_size (0), _M_data (0) {}
267
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); }
272
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); }
277
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); }
282
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); }
287
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   }
295
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   }
305
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   }
313
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   }
321
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)); }
326
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   }
333
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   }
341
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   }
349
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   }
358
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   }
367
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   }
376
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   }
385
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   }
393
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   }
401
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   }
408
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   }
417
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   }
425
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   }
437
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   }
448
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   }
456
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   }
464
465   template<class _Tp>
466   inline size_t valarray<_Tp>::size () const { return _M_size; }
467
468   template<class _Tp>
469   inline _Tp
470   valarray<_Tp>::sum () const
471   {
472       return __valarray_sum(_M_data, _M_data + _M_size);
473   }
474
475 //   template<typename _Tp>
476 //   inline _Tp
477 //   valarray<_Tp>::product () const
478 //   {
479 //       return __valarray_product(_M_data, _M_data + _M_size);
480 //   }
481
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      }
507
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      }
530
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   }
554
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   }
569
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   }
577
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   }
586
587     _DEFINE_VALARRAY_UNARY_OPERATOR(+, _Unary_plus)
588     _DEFINE_VALARRAY_UNARY_OPERATOR(-, negate)
589     _DEFINE_VALARRAY_UNARY_OPERATOR(~, _Bitwise_not)
590
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   }
600
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   }
618
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)
629
630 #undef _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT
631
632
633 } // std::
634   
635
636 namespace std
637 {
638
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   }
647
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)
658
659 #undef _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT
660     
661
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   }
686
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)
697
698 #undef _DEFINE_BINARY_OPERATOR
699
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   }
724
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)
733
734 #undef _DEFINE_LOGICAL_OPERATOR
735
736 } // namespace std
737
738 #endif // _CPP_VALARRAY
739
740 // Local Variables:
741 // mode:c++
742 // End: