Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

std_valarray.h

Go to the documentation of this file.
00001 // The template and inlines for the -*- C++ -*- valarray class.
00002 
00003 // Copyright (C) 1997-1999, 2000, 2001 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 2, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // You should have received a copy of the GNU General Public License along
00017 // with this library; see the file COPYING.  If not, write to the Free
00018 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
00019 // USA.
00020 
00021 // As a special exception, you may use this file as part of a free software
00022 // library without restriction.  Specifically, if other files instantiate
00023 // templates or use macros or inline functions from this file, or you compile
00024 // this file and link it with other files to produce an executable, this
00025 // file does not by itself cause the resulting executable to be covered by
00026 // the GNU General Public License.  This exception does not however
00027 // invalidate any other reasons why the executable file might be covered by
00028 // the GNU General Public License.
00029 
00030 // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr>
00031 
00032 #ifndef _CPP_VALARRAY
00033 #define _CPP_VALARRAY 1
00034 
00035 #pragma GCC system_header
00036 
00037 #include <bits/c++config.h>
00038 #include <bits/std_cstddef.h>
00039 #include <bits/std_cmath.h>
00040 #include <bits/std_cstdlib.h>
00041 #include <bits/std_numeric.h>
00042 #include <bits/std_functional.h>
00043 #include <bits/std_algorithm.h>
00044 
00045 namespace std
00046 {
00047     template<class _Clos, typename _Tp> class _Expr;
00048 
00049     template<typename _Tp1, typename _Tp2> class _ValArray;    
00050 
00051     template<template<class> class _Oper,
00052         template<class, class> class _Meta, class _Dom> struct _UnClos;
00053 
00054     template<template<class> class _Oper,
00055         template<class, class> class _Meta1,
00056         template<class, class> class _Meta2,
00057         class _Dom1, class _Dom2> class _BinClos;
00058 
00059     template<template<class, class> class _Meta, class _Dom> class _SClos;
00060 
00061     template<template<class, class> class _Meta, class _Dom> class _GClos;
00062     
00063     template<template<class, class> class _Meta, class _Dom> class _IClos;
00064     
00065     template<template<class, class> class _Meta, class _Dom> class _ValFunClos;
00066 
00067     template<template<class, class> class _Meta, class _Dom> class _RefFunClos;
00068 
00069     template<class _Tp> struct _Unary_plus;
00070     template<class _Tp> struct _Bitwise_and;
00071     template<class _Tp> struct _Bitwise_or;
00072     template<class _Tp> struct _Bitwise_xor;  
00073     template<class _Tp> struct _Bitwise_not;
00074     template<class _Tp> struct _Shift_left;
00075     template<class _Tp> struct _Shift_right;
00076   
00077     template<class _Tp> class valarray;   // An array of type _Tp
00078     class slice;                          // BLAS-like slice out of an array
00079     template<class _Tp> class slice_array;
00080     class gslice;                         // generalized slice out of an array
00081     template<class _Tp> class gslice_array;
00082     template<class _Tp> class mask_array;     // masked array
00083     template<class _Tp> class indirect_array; // indirected array
00084 
00085 } // namespace std
00086 
00087 #include <bits/valarray_array.h>
00088 #include <bits/valarray_meta.h>
00089   
00090 namespace std
00091 {
00092   template<class _Tp> class valarray
00093   {
00094   public:
00095       typedef _Tp value_type;
00096 
00097       // _lib.valarray.cons_ construct/destroy:
00098       valarray();
00099       explicit valarray(size_t);
00100       valarray(const _Tp&, size_t);
00101       valarray(const _Tp* __restrict__, size_t);
00102       valarray(const valarray&);
00103       valarray(const slice_array<_Tp>&);
00104       valarray(const gslice_array<_Tp>&);
00105       valarray(const mask_array<_Tp>&);
00106       valarray(const indirect_array<_Tp>&);
00107       template<class _Dom>
00108       valarray(const _Expr<_Dom,_Tp>& __e);
00109      ~valarray();
00110 
00111       // _lib.valarray.assign_ assignment:
00112       valarray<_Tp>& operator=(const valarray<_Tp>&);
00113       valarray<_Tp>& operator=(const _Tp&);
00114       valarray<_Tp>& operator=(const slice_array<_Tp>&);
00115       valarray<_Tp>& operator=(const gslice_array<_Tp>&);
00116       valarray<_Tp>& operator=(const mask_array<_Tp>&);
00117       valarray<_Tp>& operator=(const indirect_array<_Tp>&);
00118 
00119       template<class _Dom> valarray<_Tp>&
00120         operator= (const _Expr<_Dom,_Tp>&);
00121 
00122       // _lib.valarray.access_ element access:
00123       _Tp                 operator[](size_t) const;
00124       _Tp&                operator[](size_t);       
00125       // _lib.valarray.sub_ subset operations:
00126       _Expr<_SClos<_ValArray,_Tp>, _Tp> operator[](slice) const;
00127       slice_array<_Tp>    operator[](slice);
00128       _Expr<_GClos<_ValArray,_Tp>, _Tp> operator[](const gslice&) const;
00129       gslice_array<_Tp>   operator[](const gslice&);
00130       valarray<_Tp>          operator[](const valarray<bool>&) const;
00131       mask_array<_Tp>     operator[](const valarray<bool>&);
00132       _Expr<_IClos<_ValArray, _Tp>, _Tp>
00133         operator[](const valarray<size_t>&) const;
00134       indirect_array<_Tp> operator[](const valarray<size_t>&);
00135 
00136       // _lib.valarray.unary_ unary operators:
00137       _Expr<_UnClos<_Unary_plus,_ValArray,_Tp>,_Tp>  operator+ () const;
00138       _Expr<_UnClos<negate,_ValArray,_Tp>,_Tp> operator- () const;
00139       _Expr<_UnClos<_Bitwise_not,_ValArray,_Tp>,_Tp> operator~ () const;
00140       _Expr<_UnClos<logical_not,_ValArray,_Tp>,bool> operator! () const;
00141       
00142       // _lib.valarray.cassign_ computed assignment:
00143       valarray<_Tp>& operator*= (const _Tp&);
00144       valarray<_Tp>& operator/= (const _Tp&);
00145       valarray<_Tp>& operator%= (const _Tp&);
00146       valarray<_Tp>& operator+= (const _Tp&);
00147       valarray<_Tp>& operator-= (const _Tp&);
00148       valarray<_Tp>& operator^= (const _Tp&);
00149       valarray<_Tp>& operator&= (const _Tp&);
00150       valarray<_Tp>& operator|= (const _Tp&);
00151       valarray<_Tp>& operator<<=(const _Tp&);
00152       valarray<_Tp>& operator>>=(const _Tp&);
00153       valarray<_Tp>& operator*= (const valarray<_Tp>&);
00154       valarray<_Tp>& operator/= (const valarray<_Tp>&);
00155       valarray<_Tp>& operator%= (const valarray<_Tp>&);
00156       valarray<_Tp>& operator+= (const valarray<_Tp>&);
00157       valarray<_Tp>& operator-= (const valarray<_Tp>&);
00158       valarray<_Tp>& operator^= (const valarray<_Tp>&);
00159       valarray<_Tp>& operator|= (const valarray<_Tp>&);
00160       valarray<_Tp>& operator&= (const valarray<_Tp>&);
00161       valarray<_Tp>& operator<<=(const valarray<_Tp>&);
00162       valarray<_Tp>& operator>>=(const valarray<_Tp>&);
00163 
00164       template<class _Dom>
00165         valarray<_Tp>& operator*= (const _Expr<_Dom,_Tp>&);
00166       template<class _Dom>
00167         valarray<_Tp>& operator/= (const _Expr<_Dom,_Tp>&);
00168       template<class _Dom>
00169         valarray<_Tp>& operator%= (const _Expr<_Dom,_Tp>&);
00170       template<class _Dom>
00171         valarray<_Tp>& operator+= (const _Expr<_Dom,_Tp>&);
00172       template<class _Dom>
00173         valarray<_Tp>& operator-= (const _Expr<_Dom,_Tp>&);
00174       template<class _Dom>
00175         valarray<_Tp>& operator^= (const _Expr<_Dom,_Tp>&);
00176       template<class _Dom>
00177         valarray<_Tp>& operator|= (const _Expr<_Dom,_Tp>&);
00178       template<class _Dom>
00179         valarray<_Tp>& operator&= (const _Expr<_Dom,_Tp>&);
00180       template<class _Dom>
00181         valarray<_Tp>& operator<<=(const _Expr<_Dom,_Tp>&);
00182       template<class _Dom>
00183         valarray<_Tp>& operator>>=(const _Expr<_Dom,_Tp>&);
00184 
00185       
00186       // _lib.valarray.members_ member functions:
00187       size_t size() const;
00188       _Tp    sum() const;   
00189       _Tp    min() const;   
00190       _Tp    max() const;   
00191 
00192 //           // FIXME: Extension
00193 //       _Tp    product () const;
00194 
00195       valarray<_Tp> shift (int) const;
00196       valarray<_Tp> cshift(int) const;
00197       _Expr<_ValFunClos<_ValArray,_Tp>,_Tp> apply(_Tp func(_Tp)) const;
00198       _Expr<_RefFunClos<_ValArray,_Tp>,_Tp> apply(_Tp func(const _Tp&)) const;
00199       void resize(size_t __size, _Tp __c = _Tp());
00200 
00201   private:
00202       size_t _M_size;
00203       _Tp* __restrict__ _M_data;
00204 
00205       friend class _Array<_Tp>;
00206   };
00207 
00208 
00209   template<typename _Tp> struct _Unary_plus : unary_function<_Tp,_Tp> {
00210       _Tp operator() (const _Tp& __t) const { return __t; }
00211   };
00212 
00213   template<typename _Tp> struct _Bitwise_and : binary_function<_Tp,_Tp,_Tp> {
00214       _Tp operator() (_Tp __x, _Tp __y) const { return __x & __y; }
00215   };
00216 
00217   template<typename _Tp> struct _Bitwise_or : binary_function<_Tp,_Tp,_Tp> {
00218       _Tp operator() (_Tp __x, _Tp __y) const { return __x | __y; }
00219   };
00220 
00221   template<typename _Tp> struct _Bitwise_xor : binary_function<_Tp,_Tp,_Tp> {
00222       _Tp operator() (_Tp __x, _Tp __y) const { return __x ^ __y; }
00223   };
00224   
00225   template<typename _Tp> struct _Bitwise_not : unary_function<_Tp,_Tp> {
00226       _Tp operator() (_Tp __t) const { return ~__t; }
00227   };
00228 
00229   template<typename _Tp> struct _Shift_left : unary_function<_Tp,_Tp> {
00230       _Tp operator() (_Tp __x, _Tp __y) const { return __x << __y; }
00231   };
00232 
00233   template<typename _Tp> struct _Shift_right : unary_function<_Tp,_Tp> {
00234       _Tp operator() (_Tp __x, _Tp __y) const { return __x >> __y; }
00235   };
00236 
00237   
00238   template<typename _Tp>
00239   inline _Tp
00240   valarray<_Tp>::operator[] (size_t __i) const
00241   { return _M_data[__i]; }
00242 
00243   template<typename _Tp>
00244   inline _Tp&
00245   valarray<_Tp>::operator[] (size_t __i)
00246   { return _M_data[__i]; }
00247 
00248 } // std::
00249       
00250 #include <bits/slice.h>
00251 #include <bits/slice_array.h>
00252 #include <bits/gslice.h>
00253 #include <bits/gslice_array.h>
00254 #include <bits/mask_array.h>
00255 #include <bits/indirect_array.h>
00256 
00257 namespace std
00258 {
00259   template<typename _Tp>
00260   inline valarray<_Tp>::valarray () : _M_size (0), _M_data (0) {}
00261 
00262   template<typename _Tp>
00263   inline valarray<_Tp>::valarray (size_t __n) 
00264       : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n))
00265   { __valarray_default_construct(_M_data, _M_data + __n); }
00266 
00267   template<typename _Tp>
00268   inline valarray<_Tp>::valarray (const _Tp& __t, size_t __n)
00269     : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n))
00270   { __valarray_fill_construct (_M_data, _M_data + __n, __t); }
00271 
00272   template<typename _Tp>
00273   inline valarray<_Tp>::valarray (const _Tp* __restrict__ __p, size_t __n)
00274     : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n))
00275   { __valarray_copy_construct (__p, __p + __n, _M_data); }
00276 
00277   template<typename _Tp>
00278   inline valarray<_Tp>::valarray (const valarray<_Tp>& __v)
00279     : _M_size(__v._M_size), _M_data(__valarray_get_storage<_Tp>(__v._M_size))
00280   { __valarray_copy_construct (__v._M_data, __v._M_data + _M_size, _M_data); }
00281 
00282   template<typename _Tp>
00283   inline valarray<_Tp>::valarray (const slice_array<_Tp>& __sa)
00284     : _M_size(__sa._M_sz), _M_data(__valarray_get_storage<_Tp>(__sa._M_sz))
00285   {
00286     __valarray_copy
00287       (__sa._M_array, __sa._M_sz, __sa._M_stride, _Array<_Tp>(_M_data));
00288   }
00289 
00290   template<typename _Tp>
00291   inline valarray<_Tp>::valarray (const gslice_array<_Tp>& __ga)
00292     : _M_size(__ga._M_index.size()),
00293       _M_data(__valarray_get_storage<_Tp>(_M_size))
00294   {
00295     __valarray_copy
00296       (__ga._M_array, _Array<size_t>(__ga._M_index),
00297        _Array<_Tp>(_M_data), _M_size);
00298   }
00299 
00300   template<typename _Tp>
00301   inline valarray<_Tp>::valarray (const mask_array<_Tp>& __ma)
00302     : _M_size(__ma._M_sz), _M_data(__valarray_get_storage<_Tp>(__ma._M_sz))
00303   {
00304     __valarray_copy
00305       (__ma._M_array, __ma._M_mask, _Array<_Tp>(_M_data), _M_size);
00306   }
00307 
00308   template<typename _Tp>
00309   inline valarray<_Tp>::valarray (const indirect_array<_Tp>& __ia)
00310     : _M_size(__ia._M_sz), _M_data(__valarray_get_storage<_Tp>(__ia._M_sz))
00311   {
00312     __valarray_copy
00313       (__ia._M_array, __ia._M_index, _Array<_Tp>(_M_data), _M_size);
00314   }
00315 
00316   template<typename _Tp> template<class _Dom>
00317   inline valarray<_Tp>::valarray (const _Expr<_Dom, _Tp>& __e)
00318     : _M_size(__e.size ()), _M_data(__valarray_get_storage<_Tp>(_M_size))
00319   { __valarray_copy (__e, _M_size, _Array<_Tp>(_M_data)); }
00320 
00321   template<typename _Tp>
00322   inline valarray<_Tp>::~valarray ()
00323   {
00324       __valarray_destroy_elements(_M_data, _M_data + _M_size);
00325       __valarray_release_memory(_M_data);
00326   }
00327 
00328   template<typename _Tp>
00329   inline valarray<_Tp>&
00330   valarray<_Tp>::operator= (const valarray<_Tp>& __v)
00331   {
00332       __valarray_copy(__v._M_data, _M_size, _M_data);
00333       return *this;
00334   }
00335 
00336   template<typename _Tp>
00337   inline valarray<_Tp>&
00338   valarray<_Tp>::operator= (const _Tp& __t)
00339   {
00340       __valarray_fill (_M_data, _M_size, __t);
00341       return *this;
00342   }
00343 
00344   template<typename _Tp>
00345   inline valarray<_Tp>&
00346   valarray<_Tp>::operator= (const slice_array<_Tp>& __sa)
00347   {
00348       __valarray_copy (__sa._M_array, __sa._M_sz,
00349               __sa._M_stride, _Array<_Tp>(_M_data));
00350       return *this;
00351   }
00352 
00353   template<typename _Tp>
00354   inline valarray<_Tp>&
00355   valarray<_Tp>::operator= (const gslice_array<_Tp>& __ga)
00356   {
00357       __valarray_copy (__ga._M_array, _Array<size_t>(__ga._M_index),
00358               _Array<_Tp>(_M_data), _M_size);
00359       return *this;
00360   }
00361 
00362   template<typename _Tp>
00363   inline valarray<_Tp>&
00364   valarray<_Tp>::operator= (const mask_array<_Tp>& __ma)
00365   {
00366       __valarray_copy (__ma._M_array, __ma._M_mask,
00367               _Array<_Tp>(_M_data), _M_size);
00368       return *this;
00369   }
00370 
00371   template<typename _Tp>
00372   inline valarray<_Tp>&
00373   valarray<_Tp>::operator= (const indirect_array<_Tp>& __ia)
00374   {
00375       __valarray_copy (__ia._M_array, __ia._M_index,
00376                _Array<_Tp>(_M_data), _M_size);
00377       return *this;
00378   }
00379 
00380   template<typename _Tp> template<class _Dom>
00381   inline valarray<_Tp>&
00382   valarray<_Tp>::operator= (const _Expr<_Dom, _Tp>& __e)
00383   {
00384       __valarray_copy (__e, _M_size, _Array<_Tp>(_M_data));
00385       return *this;
00386   }
00387 
00388   template<typename _Tp>
00389   inline _Expr<_SClos<_ValArray,_Tp>, _Tp>
00390   valarray<_Tp>::operator[] (slice __s) const
00391   {
00392       typedef _SClos<_ValArray,_Tp> _Closure;
00393       return _Expr<_Closure, _Tp> (_Closure (_Array<_Tp>(_M_data), __s));
00394   }
00395 
00396   template<typename _Tp>
00397   inline slice_array<_Tp>
00398   valarray<_Tp>::operator[] (slice __s)
00399   {
00400       return slice_array<_Tp> (_Array<_Tp>(_M_data), __s);
00401   }
00402 
00403   template<typename _Tp>
00404   inline _Expr<_GClos<_ValArray,_Tp>, _Tp>
00405   valarray<_Tp>::operator[] (const gslice& __gs) const
00406   {
00407       typedef _GClos<_ValArray,_Tp> _Closure;
00408       return _Expr<_Closure, _Tp>
00409           (_Closure (_Array<_Tp>(_M_data), __gs._M_index->_M_index));
00410   }
00411 
00412   template<typename _Tp>
00413   inline gslice_array<_Tp>
00414   valarray<_Tp>::operator[] (const gslice& __gs)
00415   {
00416       return gslice_array<_Tp>
00417           (_Array<_Tp>(_M_data), __gs._M_index->_M_index);
00418   }
00419 
00420   template<typename _Tp>
00421   inline valarray<_Tp>
00422   valarray<_Tp>::operator[] (const valarray<bool>& __m) const
00423   {
00424       size_t __s (0);
00425       size_t __e (__m.size ());
00426       for (size_t __i=0; __i<__e; ++__i)
00427           if (__m[__i]) ++__s;
00428       return valarray<_Tp> (mask_array<_Tp> (_Array<_Tp>(_M_data), __s,
00429                                          _Array<bool> (__m)));
00430   }
00431 
00432   template<typename _Tp>
00433   inline mask_array<_Tp>
00434   valarray<_Tp>::operator[] (const valarray<bool>& __m)
00435   {
00436       size_t __s (0);
00437       size_t __e (__m.size ());
00438       for (size_t __i=0; __i<__e; ++__i)
00439           if (__m[__i]) ++__s;
00440       return mask_array<_Tp> (_Array<_Tp>(_M_data), __s, _Array<bool> (__m));
00441   }
00442 
00443   template<typename _Tp>
00444   inline _Expr<_IClos<_ValArray,_Tp>, _Tp>
00445   valarray<_Tp>::operator[] (const valarray<size_t>& __i) const
00446   {
00447       typedef _IClos<_ValArray,_Tp> _Closure;
00448       return _Expr<_Closure, _Tp> (_Closure (*this, __i));
00449   }
00450 
00451   template<typename _Tp>
00452   inline indirect_array<_Tp>
00453   valarray<_Tp>::operator[] (const valarray<size_t>& __i)
00454   {
00455       return indirect_array<_Tp> (_Array<_Tp>(_M_data), __i.size(),
00456                                 _Array<size_t> (__i));
00457   }
00458 
00459   template<class _Tp>
00460   inline size_t valarray<_Tp>::size () const { return _M_size; }
00461 
00462   template<class _Tp>
00463   inline _Tp
00464   valarray<_Tp>::sum () const
00465   {
00466       return __valarray_sum(_M_data, _M_data + _M_size);
00467   }
00468 
00469 //   template<typename _Tp>
00470 //   inline _Tp
00471 //   valarray<_Tp>::product () const
00472 //   {
00473 //       return __valarray_product(_M_data, _M_data + _M_size);
00474 //   }
00475 
00476   template <class _Tp>
00477      inline valarray<_Tp>
00478      valarray<_Tp>::shift(int __n) const
00479      {
00480        _Tp* const __a = static_cast<_Tp*>
00481          (__builtin_alloca(sizeof(_Tp) * _M_size));
00482        if (__n == 0)                          // no shift
00483          __valarray_copy_construct(_M_data, _M_data + _M_size, __a);
00484        else if (__n > 0)         // __n > 0: shift left
00485          {                 
00486            if (size_t(__n) > _M_size)
00487              __valarray_default_construct(__a, __a + __n);
00488            else
00489              {
00490                __valarray_copy_construct(_M_data+__n, _M_data + _M_size, __a);
00491                __valarray_default_construct(__a+_M_size-__n, __a + _M_size);
00492              }
00493          }
00494        else                        // __n < 0: shift right
00495          {                          
00496            __valarray_copy_construct (_M_data, _M_data+_M_size+__n, __a-__n);
00497            __valarray_default_construct(__a, __a - __n);
00498          }
00499        return valarray<_Tp> (__a, _M_size);
00500      }
00501 
00502   template <class _Tp>
00503      inline valarray<_Tp>
00504      valarray<_Tp>::cshift (int __n) const
00505      {
00506        _Tp* const __a = static_cast<_Tp*>
00507          (__builtin_alloca (sizeof(_Tp) * _M_size));
00508        if (__n == 0)               // no cshift
00509          __valarray_copy_construct(_M_data, _M_data + _M_size, __a);
00510        else if (__n > 0)           // cshift left
00511          {               
00512            __valarray_copy_construct(_M_data, _M_data+__n, __a+_M_size-__n);
00513            __valarray_copy_construct(_M_data+__n, _M_data + _M_size, __a);
00514          }
00515        else                        // cshift right
00516          {                       
00517            __valarray_copy_construct
00518              (_M_data + _M_size+__n, _M_data + _M_size, __a);
00519            __valarray_copy_construct
00520              (_M_data, _M_data + _M_size+__n, __a - __n);
00521          }
00522        return valarray<_Tp>(__a, _M_size);
00523      }
00524 
00525   template <class _Tp>
00526   inline void
00527   valarray<_Tp>::resize (size_t __n, _Tp __c)
00528   {
00529     // This complication is so to make valarray<valarray<T> > work
00530     // even though it is not required by the standard.  Nobody should
00531     // be saying valarray<valarray<T> > anyway.  See the specs.
00532     __valarray_destroy_elements(_M_data, _M_data + _M_size);
00533     if (_M_size != __n)
00534       {
00535         __valarray_release_memory(_M_data);
00536         _M_size = __n;
00537         _M_data = __valarray_get_storage<_Tp>(__n);
00538       }
00539     __valarray_fill_construct(_M_data, _M_data + __n, __c);
00540   }
00541     
00542   template<typename _Tp>
00543   inline _Tp
00544   valarray<_Tp>::min() const
00545   {
00546       return *min_element (_M_data, _M_data+_M_size);
00547   }
00548 
00549   template<typename _Tp>
00550   inline _Tp
00551   valarray<_Tp>::max() const
00552   {
00553       return *max_element (_M_data, _M_data+_M_size);
00554   }
00555   
00556   template<class _Tp>
00557   inline _Expr<_ValFunClos<_ValArray,_Tp>,_Tp>
00558   valarray<_Tp>::apply (_Tp func (_Tp)) const
00559   {
00560       typedef _ValFunClos<_ValArray,_Tp> _Closure;
00561       return _Expr<_Closure,_Tp> (_Closure (*this, func));
00562   }
00563 
00564   template<class _Tp>
00565   inline _Expr<_RefFunClos<_ValArray,_Tp>,_Tp>
00566   valarray<_Tp>::apply (_Tp func (const _Tp &)) const
00567   {
00568       typedef _RefFunClos<_ValArray,_Tp> _Closure;
00569       return _Expr<_Closure,_Tp> (_Closure (*this, func));
00570   }
00571 
00572 #define _DEFINE_VALARRAY_UNARY_OPERATOR(_Op, _Name)                     \
00573   template<typename _Tp>                        \
00574   inline _Expr<_UnClos<_Name,_ValArray,_Tp>, _Tp>                   \
00575   valarray<_Tp>::operator _Op() const                   \
00576   {                                 \
00577       typedef _UnClos<_Name,_ValArray,_Tp> _Closure;                    \
00578       return _Expr<_Closure, _Tp> (_Closure (*this));           \
00579   }
00580 
00581     _DEFINE_VALARRAY_UNARY_OPERATOR(+, _Unary_plus)
00582     _DEFINE_VALARRAY_UNARY_OPERATOR(-, negate)
00583     _DEFINE_VALARRAY_UNARY_OPERATOR(~, _Bitwise_not)
00584 
00585 #undef _DEFINE_VALARRAY_UNARY_OPERATOR
00586   
00587   template<typename _Tp>
00588   inline _Expr<_UnClos<logical_not,_ValArray,_Tp>, bool>
00589   valarray<_Tp>::operator!() const
00590   {
00591       typedef _UnClos<logical_not,_ValArray,_Tp> _Closure;
00592       return _Expr<_Closure, bool> (_Closure (*this));
00593   }
00594 
00595 #define _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(_Op, _Name)               \
00596   template<class _Tp>                           \
00597   inline valarray<_Tp> &                        \
00598   valarray<_Tp>::operator _Op##= (const _Tp &__t)           \
00599   {                                 \
00600       _Array_augmented_##_Name (_Array<_Tp>(_M_data), _M_size, __t);    \
00601       return *this;                         \
00602   }                                 \
00603                                     \
00604   template<class _Tp>                           \
00605   inline valarray<_Tp> &                        \
00606   valarray<_Tp>::operator _Op##= (const valarray<_Tp> &__v)     \
00607   {                                 \
00608       _Array_augmented_##_Name (_Array<_Tp>(_M_data), _M_size,      \
00609                                _Array<_Tp>(__v._M_data));       \
00610       return *this;                         \
00611   }
00612 
00613 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(+, plus)
00614 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(-, minus)
00615 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(*, multiplies)
00616 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(/, divides)
00617 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(%, modulus)
00618 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(^, xor)
00619 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(&, and)
00620 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(|, or)
00621 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(<<, shift_left)
00622 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(>>, shift_right)
00623 
00624 #undef _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT
00625 
00626 
00627 } // std::
00628   
00629 
00630 namespace std
00631 {
00632 
00633 #define _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(_Op, _Name)          \
00634   template<class _Tp> template<class _Dom>              \
00635   inline valarray<_Tp> &                        \
00636   valarray<_Tp>::operator _Op##= (const _Expr<_Dom,_Tp> &__e)       \
00637   {                                 \
00638       _Array_augmented_##_Name (_Array<_Tp>(_M_data), __e, _M_size);    \
00639       return *this;                         \
00640   }
00641 
00642 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(+, plus)
00643 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(-, minus)
00644 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(*, multiplies)
00645 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(/, divides)
00646 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(%, modulus)
00647 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(^, xor)
00648 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(&, and)
00649 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(|, or)
00650 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(<<, shift_left)
00651 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(>>, shift_right)
00652 
00653 #undef _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT
00654     
00655 
00656 #define _DEFINE_BINARY_OPERATOR(_Op, _Name)             \
00657   template<typename _Tp>                        \
00658   inline _Expr<_BinClos<_Name,_ValArray,_ValArray,_Tp,_Tp>, _Tp>        \
00659   operator _Op (const valarray<_Tp> &__v, const valarray<_Tp> &__w) \
00660   {                                 \
00661       typedef _BinClos<_Name,_ValArray,_ValArray,_Tp,_Tp> _Closure;     \
00662       return _Expr<_Closure, _Tp> (_Closure (__v, __w));        \
00663   }                                 \
00664                                     \
00665   template<typename _Tp>                        \
00666   inline _Expr<_BinClos<_Name,_ValArray,_Constant,_Tp,_Tp>,_Tp>         \
00667   operator _Op (const valarray<_Tp> &__v, const _Tp &__t)       \
00668   {                                 \
00669       typedef _BinClos<_Name,_ValArray,_Constant,_Tp,_Tp> _Closure; \
00670       return _Expr<_Closure, _Tp> (_Closure (__v, __t));            \
00671   }                                 \
00672                                     \
00673   template<typename _Tp>                        \
00674   inline _Expr<_BinClos<_Name,_Constant,_ValArray,_Tp,_Tp>,_Tp>         \
00675   operator _Op (const _Tp &__t, const valarray<_Tp> &__v)       \
00676   {                                 \
00677       typedef _BinClos<_Name,_Constant,_ValArray,_Tp,_Tp> _Closure;     \
00678       return _Expr<_Closure, _Tp> (_Closure (__t, __v));            \
00679   }
00680 
00681 _DEFINE_BINARY_OPERATOR(+, plus)
00682 _DEFINE_BINARY_OPERATOR(-, minus)
00683 _DEFINE_BINARY_OPERATOR(*, multiplies)
00684 _DEFINE_BINARY_OPERATOR(/, divides)
00685 _DEFINE_BINARY_OPERATOR(%, modulus)
00686 _DEFINE_BINARY_OPERATOR(^, _Bitwise_xor)
00687 _DEFINE_BINARY_OPERATOR(&, _Bitwise_and)
00688 _DEFINE_BINARY_OPERATOR(|, _Bitwise_or)
00689 _DEFINE_BINARY_OPERATOR(<<, _Shift_left)
00690 _DEFINE_BINARY_OPERATOR(>>, _Shift_right)
00691 
00692 #undef _DEFINE_BINARY_OPERATOR
00693 
00694 #define _DEFINE_LOGICAL_OPERATOR(_Op, _Name)                \
00695   template<typename _Tp>                        \
00696   inline _Expr<_BinClos<_Name,_ValArray,_ValArray,_Tp,_Tp>,bool>        \
00697   operator _Op (const valarray<_Tp> &__v, const valarray<_Tp> &__w) \
00698   {                                 \
00699       typedef _BinClos<_Name,_ValArray,_ValArray,_Tp,_Tp> _Closure;     \
00700       return _Expr<_Closure, bool> (_Closure (__v, __w));               \
00701   }                                 \
00702                                     \
00703   template<class _Tp>                           \
00704   inline _Expr<_BinClos<_Name,_ValArray,_Constant,_Tp,_Tp>,bool>        \
00705   operator _Op (const valarray<_Tp> &__v, const _Tp &__t)       \
00706   {                                 \
00707       typedef _BinClos<_Name,_ValArray,_Constant,_Tp,_Tp> _Closure;     \
00708       return _Expr<_Closure, bool> (_Closure (__v, __t));           \
00709   }                                 \
00710                                     \
00711   template<class _Tp>                           \
00712   inline _Expr<_BinClos<_Name,_Constant,_ValArray,_Tp,_Tp>,bool>        \
00713   operator _Op (const _Tp &__t, const valarray<_Tp> &__v)       \
00714   {                                 \
00715       typedef _BinClos<_Name,_Constant,_ValArray,_Tp,_Tp> _Closure;     \
00716       return _Expr<_Closure, bool> (_Closure (__t, __v));           \
00717   }
00718 
00719 _DEFINE_LOGICAL_OPERATOR(&&, logical_and)
00720 _DEFINE_LOGICAL_OPERATOR(||, logical_or)
00721 _DEFINE_LOGICAL_OPERATOR(==, equal_to)
00722 _DEFINE_LOGICAL_OPERATOR(!=, not_equal_to)
00723 _DEFINE_LOGICAL_OPERATOR(<, less)
00724 _DEFINE_LOGICAL_OPERATOR(>, greater)
00725 _DEFINE_LOGICAL_OPERATOR(<=, less_equal)
00726 _DEFINE_LOGICAL_OPERATOR(>=, greater_equal)
00727 
00728 #undef _DEFINE_LOGICAL_OPERATOR
00729 
00730 } // namespace std
00731 
00732 #endif // _CPP_VALARRAY
00733 
00734 // Local Variables:
00735 // mode:c++
00736 // End:

Generated at Tue May 1 16:28:38 2001 for libstdc++-v3 by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001