Whole document tree
    

Whole document tree

std_valarray.h Source File
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       // XXX: LWG to be resolved.
00124       const _Tp&                 operator[](size_t) const;
00125       _Tp&                operator[](size_t);       
00126       // _lib.valarray.sub_ subset operations:
00127       _Expr<_SClos<_ValArray,_Tp>, _Tp> operator[](slice) const;
00128       slice_array<_Tp>    operator[](slice);
00129       _Expr<_GClos<_ValArray,_Tp>, _Tp> operator[](const gslice&) const;
00130       gslice_array<_Tp>   operator[](const gslice&);
00131       valarray<_Tp>          operator[](const valarray<bool>&) const;
00132       mask_array<_Tp>     operator[](const valarray<bool>&);
00133       _Expr<_IClos<_ValArray, _Tp>, _Tp>
00134         operator[](const valarray<size_t>&) const;
00135       indirect_array<_Tp> operator[](const valarray<size_t>&);
00136 
00137       // _lib.valarray.unary_ unary operators:
00138       _Expr<_UnClos<_Unary_plus,_ValArray,_Tp>,_Tp>  operator+ () const;
00139       _Expr<_UnClos<negate,_ValArray,_Tp>,_Tp> operator- () const;
00140       _Expr<_UnClos<_Bitwise_not,_ValArray,_Tp>,_Tp> operator~ () const;
00141       _Expr<_UnClos<logical_not,_ValArray,_Tp>,bool> operator! () const;
00142       
00143       // _lib.valarray.cassign_ computed assignment:
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 _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       valarray<_Tp>& operator>>=(const valarray<_Tp>&);
00164 
00165       template<class _Dom>
00166         valarray<_Tp>& operator*= (const _Expr<_Dom,_Tp>&);
00167       template<class _Dom>
00168         valarray<_Tp>& operator/= (const _Expr<_Dom,_Tp>&);
00169       template<class _Dom>
00170         valarray<_Tp>& operator%= (const _Expr<_Dom,_Tp>&);
00171       template<class _Dom>
00172         valarray<_Tp>& operator+= (const _Expr<_Dom,_Tp>&);
00173       template<class _Dom>
00174         valarray<_Tp>& operator-= (const _Expr<_Dom,_Tp>&);
00175       template<class _Dom>
00176         valarray<_Tp>& operator^= (const _Expr<_Dom,_Tp>&);
00177       template<class _Dom>
00178         valarray<_Tp>& operator|= (const _Expr<_Dom,_Tp>&);
00179       template<class _Dom>
00180         valarray<_Tp>& operator&= (const _Expr<_Dom,_Tp>&);
00181       template<class _Dom>
00182         valarray<_Tp>& operator<<=(const _Expr<_Dom,_Tp>&);
00183       template<class _Dom>
00184         valarray<_Tp>& operator>>=(const _Expr<_Dom,_Tp>&);
00185 
00186       
00187       // _lib.valarray.members_ member functions:
00188       size_t size() const;
00189       _Tp    sum() const;   
00190       _Tp    min() const;   
00191       _Tp    max() const;   
00192 
00193 //           // FIXME: Extension
00194 //       _Tp    product () const;
00195 
00196       valarray<_Tp> shift (int) const;
00197       valarray<_Tp> cshift(int) const;
00198       _Expr<_ValFunClos<_ValArray,_Tp>,_Tp> apply(_Tp func(_Tp)) const;
00199       _Expr<_RefFunClos<_ValArray,_Tp>,_Tp> apply(_Tp func(const _Tp&)) const;
00200       void resize(size_t __size, _Tp __c = _Tp());
00201 
00202   private:
00203       size_t _M_size;
00204       _Tp* __restrict__ _M_data;
00205 
00206       friend class _Array<_Tp>;
00207   };
00208 
00209 
00210   template<typename _Tp> struct _Unary_plus : unary_function<_Tp,_Tp> {
00211       _Tp operator() (const _Tp& __t) const { return __t; }
00212   };
00213 
00214   template<typename _Tp> struct _Bitwise_and : binary_function<_Tp,_Tp,_Tp> {
00215       _Tp operator() (_Tp __x, _Tp __y) const { return __x & __y; }
00216   };
00217 
00218   template<typename _Tp> struct _Bitwise_or : binary_function<_Tp,_Tp,_Tp> {
00219       _Tp operator() (_Tp __x, _Tp __y) const { return __x | __y; }
00220   };
00221 
00222   template<typename _Tp> struct _Bitwise_xor : binary_function<_Tp,_Tp,_Tp> {
00223       _Tp operator() (_Tp __x, _Tp __y) const { return __x ^ __y; }
00224   };
00225   
00226   template<typename _Tp> struct _Bitwise_not : unary_function<_Tp,_Tp> {
00227       _Tp operator() (_Tp __t) const { return ~__t; }
00228   };
00229 
00230   template<typename _Tp> struct _Shift_left : unary_function<_Tp,_Tp> {
00231       _Tp operator() (_Tp __x, _Tp __y) const { return __x << __y; }
00232   };
00233 
00234   template<typename _Tp> struct _Shift_right : unary_function<_Tp,_Tp> {
00235       _Tp operator() (_Tp __x, _Tp __y) const { return __x >> __y; }
00236   };
00237 
00238   
00239   template<typename _Tp>
00240   inline const _Tp&
00241   valarray<_Tp>::operator[] (size_t __i) const
00242   { return _M_data[__i]; }
00243 
00244   template<typename _Tp>
00245   inline _Tp&
00246   valarray<_Tp>::operator[] (size_t __i)
00247   { return _M_data[__i]; }
00248 
00249 } // std::
00250       
00251 #include <bits/slice.h>
00252 #include <bits/slice_array.h>
00253 #include <bits/gslice.h>
00254 #include <bits/gslice_array.h>
00255 #include <bits/mask_array.h>
00256 #include <bits/indirect_array.h>
00257 
00258 namespace std
00259 {
00260   template<typename _Tp>
00261   inline valarray<_Tp>::valarray () : _M_size (0), _M_data (0) {}
00262 
00263   template<typename _Tp>
00264   inline valarray<_Tp>::valarray (size_t __n) 
00265       : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n))
00266   { __valarray_default_construct(_M_data, _M_data + __n); }
00267 
00268   template<typename _Tp>
00269   inline valarray<_Tp>::valarray (const _Tp& __t, size_t __n)
00270     : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n))
00271   { __valarray_fill_construct (_M_data, _M_data + __n, __t); }
00272 
00273   template<typename _Tp>
00274   inline valarray<_Tp>::valarray (const _Tp* __restrict__ __p, size_t __n)
00275     : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n))
00276   { __valarray_copy_construct (__p, __p + __n, _M_data); }
00277 
00278   template<typename _Tp>
00279   inline valarray<_Tp>::valarray (const valarray<_Tp>& __v)
00280     : _M_size(__v._M_size), _M_data(__valarray_get_storage<_Tp>(__v._M_size))
00281   { __valarray_copy_construct (__v._M_data, __v._M_data + _M_size, _M_data); }
00282 
00283   template<typename _Tp>
00284   inline valarray<_Tp>::valarray (const slice_array<_Tp>& __sa)
00285     : _M_size(__sa._M_sz), _M_data(__valarray_get_storage<_Tp>(__sa._M_sz))
00286   {
00287     __valarray_copy
00288       (__sa._M_array, __sa._M_sz, __sa._M_stride, _Array<_Tp>(_M_data));
00289   }
00290 
00291   template<typename _Tp>
00292   inline valarray<_Tp>::valarray (const gslice_array<_Tp>& __ga)
00293     : _M_size(__ga._M_index.size()),
00294       _M_data(__valarray_get_storage<_Tp>(_M_size))
00295   {
00296     __valarray_copy
00297       (__ga._M_array, _Array<size_t>(__ga._M_index),
00298        _Array<_Tp>(_M_data), _M_size);
00299   }
00300 
00301   template<typename _Tp>
00302   inline valarray<_Tp>::valarray (const mask_array<_Tp>& __ma)
00303     : _M_size(__ma._M_sz), _M_data(__valarray_get_storage<_Tp>(__ma._M_sz))
00304   {
00305     __valarray_copy
00306       (__ma._M_array, __ma._M_mask, _Array<_Tp>(_M_data), _M_size);
00307   }
00308 
00309   template<typename _Tp>
00310   inline valarray<_Tp>::valarray (const indirect_array<_Tp>& __ia)
00311     : _M_size(__ia._M_sz), _M_data(__valarray_get_storage<_Tp>(__ia._M_sz))
00312   {
00313     __valarray_copy
00314       (__ia._M_array, __ia._M_index, _Array<_Tp>(_M_data), _M_size);
00315   }
00316 
00317   template<typename _Tp> template<class _Dom>
00318   inline valarray<_Tp>::valarray (const _Expr<_Dom, _Tp>& __e)
00319     : _M_size(__e.size ()), _M_data(__valarray_get_storage<_Tp>(_M_size))
00320   { __valarray_copy (__e, _M_size, _Array<_Tp>(_M_data)); }
00321 
00322   template<typename _Tp>
00323   inline valarray<_Tp>::~valarray ()
00324   {
00325       __valarray_destroy_elements(_M_data, _M_data + _M_size);
00326       __valarray_release_memory(_M_data);
00327   }
00328 
00329   template<typename _Tp>
00330   inline valarray<_Tp>&
00331   valarray<_Tp>::operator= (const valarray<_Tp>& __v)
00332   {
00333       __valarray_copy(__v._M_data, _M_size, _M_data);
00334       return *this;
00335   }
00336 
00337   template<typename _Tp>
00338   inline valarray<_Tp>&
00339   valarray<_Tp>::operator= (const _Tp& __t)
00340   {
00341       __valarray_fill (_M_data, _M_size, __t);
00342       return *this;
00343   }
00344 
00345   template<typename _Tp>
00346   inline valarray<_Tp>&
00347   valarray<_Tp>::operator= (const slice_array<_Tp>& __sa)
00348   {
00349       __valarray_copy (__sa._M_array, __sa._M_sz,
00350               __sa._M_stride, _Array<_Tp>(_M_data));
00351       return *this;
00352   }
00353 
00354   template<typename _Tp>
00355   inline valarray<_Tp>&
00356   valarray<_Tp>::operator= (const gslice_array<_Tp>& __ga)
00357   {
00358       __valarray_copy (__ga._M_array, _Array<size_t>(__ga._M_index),
00359               _Array<_Tp>(_M_data), _M_size);
00360       return *this;
00361   }
00362 
00363   template<typename _Tp>
00364   inline valarray<_Tp>&
00365   valarray<_Tp>::operator= (const mask_array<_Tp>& __ma)
00366   {
00367       __valarray_copy (__ma._M_array, __ma._M_mask,
00368               _Array<_Tp>(_M_data), _M_size);
00369       return *this;
00370   }
00371 
00372   template<typename _Tp>
00373   inline valarray<_Tp>&
00374   valarray<_Tp>::operator= (const indirect_array<_Tp>& __ia)
00375   {
00376       __valarray_copy (__ia._M_array, __ia._M_index,
00377                _Array<_Tp>(_M_data), _M_size);
00378       return *this;
00379   }
00380 
00381   template<typename _Tp> template<class _Dom>
00382   inline valarray<_Tp>&
00383   valarray<_Tp>::operator= (const _Expr<_Dom, _Tp>& __e)
00384   {
00385       __valarray_copy (__e, _M_size, _Array<_Tp>(_M_data));
00386       return *this;
00387   }
00388 
00389   template<typename _Tp>
00390   inline _Expr<_SClos<_ValArray,_Tp>, _Tp>
00391   valarray<_Tp>::operator[] (slice __s) const
00392   {
00393       typedef _SClos<_ValArray,_Tp> _Closure;
00394       return _Expr<_Closure, _Tp> (_Closure (_Array<_Tp>(_M_data), __s));
00395   }
00396 
00397   template<typename _Tp>
00398   inline slice_array<_Tp>
00399   valarray<_Tp>::operator[] (slice __s)
00400   {
00401       return slice_array<_Tp> (_Array<_Tp>(_M_data), __s);
00402   }
00403 
00404   template<typename _Tp>
00405   inline _Expr<_GClos<_ValArray,_Tp>, _Tp>
00406   valarray<_Tp>::operator[] (const gslice& __gs) const
00407   {
00408       typedef _GClos<_ValArray,_Tp> _Closure;
00409       return _Expr<_Closure, _Tp>
00410           (_Closure (_Array<_Tp>(_M_data), __gs._M_index->_M_index));
00411   }
00412 
00413   template<typename _Tp>
00414   inline gslice_array<_Tp>
00415   valarray<_Tp>::operator[] (const gslice& __gs)
00416   {
00417       return gslice_array<_Tp>
00418           (_Array<_Tp>(_M_data), __gs._M_index->_M_index);
00419   }
00420 
00421   template<typename _Tp>
00422   inline valarray<_Tp>
00423   valarray<_Tp>::operator[] (const valarray<bool>& __m) const
00424   {
00425       size_t __s (0);
00426       size_t __e (__m.size ());
00427       for (size_t __i=0; __i<__e; ++__i)
00428           if (__m[__i]) ++__s;
00429       return valarray<_Tp> (mask_array<_Tp> (_Array<_Tp>(_M_data), __s,
00430                                          _Array<bool> (__m)));
00431   }
00432 
00433   template<typename _Tp>
00434   inline mask_array<_Tp>
00435   valarray<_Tp>::operator[] (const valarray<bool>& __m)
00436   {
00437       size_t __s (0);
00438       size_t __e (__m.size ());
00439       for (size_t __i=0; __i<__e; ++__i)
00440           if (__m[__i]) ++__s;
00441       return mask_array<_Tp> (_Array<_Tp>(_M_data), __s, _Array<bool> (__m));
00442   }
00443 
00444   template<typename _Tp>
00445   inline _Expr<_IClos<_ValArray,_Tp>, _Tp>
00446   valarray<_Tp>::operator[] (const valarray<size_t>& __i) const
00447   {
00448       typedef _IClos<_ValArray,_Tp> _Closure;
00449       return _Expr<_Closure, _Tp> (_Closure (*this, __i));
00450   }
00451 
00452   template<typename _Tp>
00453   inline indirect_array<_Tp>
00454   valarray<_Tp>::operator[] (const valarray<size_t>& __i)
00455   {
00456       return indirect_array<_Tp> (_Array<_Tp>(_M_data), __i.size(),
00457                                 _Array<size_t> (__i));
00458   }
00459 
00460   template<class _Tp>
00461   inline size_t valarray<_Tp>::size () const { return _M_size; }
00462 
00463   template<class _Tp>
00464   inline _Tp
00465   valarray<_Tp>::sum () const
00466   {
00467       return __valarray_sum(_M_data, _M_data + _M_size);
00468   }
00469 
00470 //   template<typename _Tp>
00471 //   inline _Tp
00472 //   valarray<_Tp>::product () const
00473 //   {
00474 //       return __valarray_product(_M_data, _M_data + _M_size);
00475 //   }
00476 
00477   template <class _Tp>
00478      inline valarray<_Tp>
00479      valarray<_Tp>::shift(int __n) const
00480      {
00481        _Tp* const __a = static_cast<_Tp*>
00482          (__builtin_alloca(sizeof(_Tp) * _M_size));
00483        if (__n == 0)                          // no shift
00484          __valarray_copy_construct(_M_data, _M_data + _M_size, __a);
00485        else if (__n > 0)         // __n > 0: shift left
00486          {                 
00487            if (size_t(__n) > _M_size)
00488              __valarray_default_construct(__a, __a + __n);
00489            else
00490              {
00491                __valarray_copy_construct(_M_data+__n, _M_data + _M_size, __a);
00492                __valarray_default_construct(__a+_M_size-__n, __a + _M_size);
00493              }
00494          }
00495        else                        // __n < 0: shift right
00496          {                          
00497            __valarray_copy_construct (_M_data, _M_data+_M_size+__n, __a-__n);
00498            __valarray_default_construct(__a, __a - __n);
00499          }
00500        return valarray<_Tp> (__a, _M_size);
00501      }
00502 
00503   template <class _Tp>
00504      inline valarray<_Tp>
00505      valarray<_Tp>::cshift (int __n) const
00506      {
00507        _Tp* const __a = static_cast<_Tp*>
00508          (__builtin_alloca (sizeof(_Tp) * _M_size));
00509        if (__n == 0)               // no cshift
00510          __valarray_copy_construct(_M_data, _M_data + _M_size, __a);
00511        else if (__n > 0)           // cshift left
00512          {               
00513            __valarray_copy_construct(_M_data, _M_data+__n, __a+_M_size-__n);
00514            __valarray_copy_construct(_M_data+__n, _M_data + _M_size, __a);
00515          }
00516        else                        // cshift right
00517          {                       
00518            __valarray_copy_construct
00519              (_M_data + _M_size+__n, _M_data + _M_size, __a);
00520            __valarray_copy_construct
00521              (_M_data, _M_data + _M_size+__n, __a - __n);
00522          }
00523        return valarray<_Tp>(__a, _M_size);
00524      }
00525 
00526   template <class _Tp>
00527   inline void
00528   valarray<_Tp>::resize (size_t __n, _Tp __c)
00529   {
00530     // This complication is so to make valarray<valarray<T> > work
00531     // even though it is not required by the standard.  Nobody should
00532     // be saying valarray<valarray<T> > anyway.  See the specs.
00533     __valarray_destroy_elements(_M_data, _M_data + _M_size);
00534     if (_M_size != __n)
00535       {
00536         __valarray_release_memory(_M_data);
00537         _M_size = __n;
00538         _M_data = __valarray_get_storage<_Tp>(__n);
00539       }
00540     __valarray_fill_construct(_M_data, _M_data + __n, __c);
00541   }
00542     
00543   template<typename _Tp>
00544   inline _Tp
00545   valarray<_Tp>::min() const
00546   {
00547       return *min_element (_M_data, _M_data+_M_size);
00548   }
00549 
00550   template<typename _Tp>
00551   inline _Tp
00552   valarray<_Tp>::max() const
00553   {
00554       return *max_element (_M_data, _M_data+_M_size);
00555   }
00556   
00557   template<class _Tp>
00558   inline _Expr<_ValFunClos<_ValArray,_Tp>,_Tp>
00559   valarray<_Tp>::apply (_Tp func (_Tp)) const
00560   {
00561       typedef _ValFunClos<_ValArray,_Tp> _Closure;
00562       return _Expr<_Closure,_Tp> (_Closure (*this, func));
00563   }
00564 
00565   template<class _Tp>
00566   inline _Expr<_RefFunClos<_ValArray,_Tp>,_Tp>
00567   valarray<_Tp>::apply (_Tp func (const _Tp &)) const
00568   {
00569       typedef _RefFunClos<_ValArray,_Tp> _Closure;
00570       return _Expr<_Closure,_Tp> (_Closure (*this, func));
00571   }
00572 
00573 #define _DEFINE_VALARRAY_UNARY_OPERATOR(_Op, _Name)                     \
00574   template<typename _Tp>                        \
00575   inline _Expr<_UnClos<_Name,_ValArray,_Tp>, _Tp>                   \
00576   valarray<_Tp>::operator _Op() const                   \
00577   {                                 \
00578       typedef _UnClos<_Name,_ValArray,_Tp> _Closure;                    \
00579       return _Expr<_Closure, _Tp> (_Closure (*this));           \
00580   }
00581 
00582     _DEFINE_VALARRAY_UNARY_OPERATOR(+, _Unary_plus)
00583     _DEFINE_VALARRAY_UNARY_OPERATOR(-, negate)
00584     _DEFINE_VALARRAY_UNARY_OPERATOR(~, _Bitwise_not)
00585 
00586 #undef _DEFINE_VALARRAY_UNARY_OPERATOR
00587   
00588   template<typename _Tp>
00589   inline _Expr<_UnClos<logical_not,_ValArray,_Tp>, bool>
00590   valarray<_Tp>::operator!() const
00591   {
00592       typedef _UnClos<logical_not,_ValArray,_Tp> _Closure;
00593       return _Expr<_Closure, bool> (_Closure (*this));
00594   }
00595 
00596 #define _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(_Op, _Name)               \
00597   template<class _Tp>                           \
00598   inline valarray<_Tp> &                        \
00599   valarray<_Tp>::operator _Op##= (const _Tp &__t)           \
00600   {                                 \
00601       _Array_augmented_##_Name (_Array<_Tp>(_M_data), _M_size, __t);    \
00602       return *this;                         \
00603   }                                 \
00604                                     \
00605   template<class _Tp>                           \
00606   inline valarray<_Tp> &                        \
00607   valarray<_Tp>::operator _Op##= (const valarray<_Tp> &__v)     \
00608   {                                 \
00609       _Array_augmented_##_Name (_Array<_Tp>(_M_data), _M_size,      \
00610                                _Array<_Tp>(__v._M_data));       \
00611       return *this;                         \
00612   }
00613 
00614 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(+, plus)
00615 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(-, minus)
00616 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(*, multiplies)
00617 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(/, divides)
00618 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(%, modulus)
00619 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(^, xor)
00620 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(&, and)
00621 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(|, or)
00622 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(<<, shift_left)
00623 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(>>, shift_right)
00624 
00625 #undef _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT
00626 
00627 
00628 } // std::
00629   
00630 
00631 namespace std
00632 {
00633 
00634 #define _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(_Op, _Name)          \
00635   template<class _Tp> template<class _Dom>              \
00636   inline valarray<_Tp> &                        \
00637   valarray<_Tp>::operator _Op##= (const _Expr<_Dom,_Tp> &__e)       \
00638   {                                 \
00639       _Array_augmented_##_Name (_Array<_Tp>(_M_data), __e, _M_size);    \
00640       return *this;                         \
00641   }
00642 
00643 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(+, plus)
00644 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(-, minus)
00645 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(*, multiplies)
00646 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(/, divides)
00647 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(%, modulus)
00648 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(^, xor)
00649 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(&, and)
00650 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(|, or)
00651 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(<<, shift_left)
00652 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(>>, shift_right)
00653 
00654 #undef _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT
00655     
00656 
00657 #define _DEFINE_BINARY_OPERATOR(_Op, _Name)             \
00658   template<typename _Tp>                        \
00659   inline _Expr<_BinClos<_Name,_ValArray,_ValArray,_Tp,_Tp>, _Tp>        \
00660   operator _Op (const valarray<_Tp> &__v, const valarray<_Tp> &__w) \
00661   {                                 \
00662       typedef _BinClos<_Name,_ValArray,_ValArray,_Tp,_Tp> _Closure;     \
00663       return _Expr<_Closure, _Tp> (_Closure (__v, __w));        \
00664   }                                 \
00665                                     \
00666   template<typename _Tp>                        \
00667   inline _Expr<_BinClos<_Name,_ValArray,_Constant,_Tp,_Tp>,_Tp>         \
00668   operator _Op (const valarray<_Tp> &__v, const _Tp &__t)       \
00669   {                                 \
00670       typedef _BinClos<_Name,_ValArray,_Constant,_Tp,_Tp> _Closure; \
00671       return _Expr<_Closure, _Tp> (_Closure (__v, __t));            \
00672   }                                 \
00673                                     \
00674   template<typename _Tp>                        \
00675   inline _Expr<_BinClos<_Name,_Constant,_ValArray,_Tp,_Tp>,_Tp>         \
00676   operator _Op (const _Tp &__t, const valarray<_Tp> &__v)       \
00677   {                                 \
00678       typedef _BinClos<_Name,_Constant,_ValArray,_Tp,_Tp> _Closure;     \
00679       return _Expr<_Closure, _Tp> (_Closure (__t, __v));            \
00680   }
00681 
00682 _DEFINE_BINARY_OPERATOR(+, plus)
00683 _DEFINE_BINARY_OPERATOR(-, minus)
00684 _DEFINE_BINARY_OPERATOR(*, multiplies)
00685 _DEFINE_BINARY_OPERATOR(/, divides)
00686 _DEFINE_BINARY_OPERATOR(%, modulus)
00687 _DEFINE_BINARY_OPERATOR(^, _Bitwise_xor)
00688 _DEFINE_BINARY_OPERATOR(&, _Bitwise_and)
00689 _DEFINE_BINARY_OPERATOR(|, _Bitwise_or)
00690 _DEFINE_BINARY_OPERATOR(<<, _Shift_left)
00691 _DEFINE_BINARY_OPERATOR(>>, _Shift_right)
00692 
00693 #undef _DEFINE_BINARY_OPERATOR
00694 
00695 #define _DEFINE_LOGICAL_OPERATOR(_Op, _Name)                \
00696   template<typename _Tp>                        \
00697   inline _Expr<_BinClos<_Name,_ValArray,_ValArray,_Tp,_Tp>,bool>        \
00698   operator _Op (const valarray<_Tp> &__v, const valarray<_Tp> &__w) \
00699   {                                 \
00700       typedef _BinClos<_Name,_ValArray,_ValArray,_Tp,_Tp> _Closure;     \
00701       return _Expr<_Closure, bool> (_Closure (__v, __w));               \
00702   }                                 \
00703                                     \
00704   template<class _Tp>                           \
00705   inline _Expr<_BinClos<_Name,_ValArray,_Constant,_Tp,_Tp>,bool>        \
00706   operator _Op (const valarray<_Tp> &__v, const _Tp &__t)       \
00707   {                                 \
00708       typedef _BinClos<_Name,_ValArray,_Constant,_Tp,_Tp> _Closure;     \
00709       return _Expr<_Closure, bool> (_Closure (__v, __t));           \
00710   }                                 \
00711                                     \
00712   template<class _Tp>                           \
00713   inline _Expr<_BinClos<_Name,_Constant,_ValArray,_Tp,_Tp>,bool>        \
00714   operator _Op (const _Tp &__t, const valarray<_Tp> &__v)       \
00715   {                                 \
00716       typedef _BinClos<_Name,_Constant,_ValArray,_Tp,_Tp> _Closure;     \
00717       return _Expr<_Closure, bool> (_Closure (__t, __v));           \
00718   }
00719 
00720 _DEFINE_LOGICAL_OPERATOR(&&, logical_and)
00721 _DEFINE_LOGICAL_OPERATOR(||, logical_or)
00722 _DEFINE_LOGICAL_OPERATOR(==, equal_to)
00723 _DEFINE_LOGICAL_OPERATOR(!=, not_equal_to)
00724 _DEFINE_LOGICAL_OPERATOR(<, less)
00725 _DEFINE_LOGICAL_OPERATOR(>, greater)
00726 _DEFINE_LOGICAL_OPERATOR(<=, less_equal)
00727 _DEFINE_LOGICAL_OPERATOR(>=, greater_equal)
00728 
00729 #undef _DEFINE_LOGICAL_OPERATOR
00730 
00731 } // namespace std
00732 
00733 #endif // _CPP_VALARRAY
00734 
00735 // Local Variables:
00736 // mode:c++
00737 // End:

Generated on Mon Apr 8 03:11:35 2002 for libstdc++-v3 Source by doxygen1.2.15