valarray

Go to the documentation of this file.
00001 // The template and inlines for the -*- C++ -*- valarray class.
00002 
00003 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2004
00004 // Free Software Foundation, Inc.
00005 //
00006 // This file is part of the GNU ISO C++ Library.  This library is free
00007 // software; you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the
00009 // Free Software Foundation; either version 2, or (at your option)
00010 // any later version.
00011 
00012 // This library is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 
00017 // You should have received a copy of the GNU General Public License along
00018 // with this library; see the file COPYING.  If not, write to the Free
00019 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
00020 // USA.
00021 
00022 // As a special exception, you may use this file as part of a free software
00023 // library without restriction.  Specifically, if other files instantiate
00024 // templates or use macros or inline functions from this file, or you compile
00025 // this file and link it with other files to produce an executable, this
00026 // file does not by itself cause the resulting executable to be covered by
00027 // the GNU General Public License.  This exception does not however
00028 // invalidate any other reasons why the executable file might be covered by
00029 // the GNU General Public License.
00030 
00031 // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr>
00032 
00037 #ifndef _GLIBCXX_VALARRAY
00038 #define _GLIBCXX_VALARRAY 1
00039 
00040 #pragma GCC system_header
00041 
00042 #include <bits/c++config.h>
00043 #include <cstddef>
00044 #include <cmath>
00045 #include <cstdlib>
00046 #include <numeric>
00047 #include <algorithm>
00048 #include <debug/debug.h>
00049 
00050 namespace std
00051 {
00052   template<class _Clos, typename _Tp> 
00053     class _Expr;
00054 
00055   template<typename _Tp1, typename _Tp2> 
00056     class _ValArray;    
00057 
00058   template<class _Oper, template<class, class> class _Meta, class _Dom>
00059     struct _UnClos;
00060 
00061   template<class _Oper,
00062         template<class, class> class _Meta1,
00063         template<class, class> class _Meta2,
00064         class _Dom1, class _Dom2> 
00065     class _BinClos;
00066 
00067   template<template<class, class> class _Meta, class _Dom> 
00068     class _SClos;
00069 
00070   template<template<class, class> class _Meta, class _Dom> 
00071     class _GClos;
00072     
00073   template<template<class, class> class _Meta, class _Dom> 
00074     class _IClos;
00075     
00076   template<template<class, class> class _Meta, class _Dom> 
00077     class _ValFunClos;
00078   
00079   template<template<class, class> class _Meta, class _Dom> 
00080     class _RefFunClos;
00081 
00082   template<class _Tp> class valarray;   // An array of type _Tp
00083   class slice;                          // BLAS-like slice out of an array
00084   template<class _Tp> class slice_array;
00085   class gslice;                         // generalized slice out of an array
00086   template<class _Tp> class gslice_array;
00087   template<class _Tp> class mask_array;     // masked array
00088   template<class _Tp> class indirect_array; // indirected array
00089 
00090 } // namespace std
00091 
00092 #include <bits/valarray_array.h>
00093 #include <bits/valarray_before.h>
00094   
00095 namespace std
00096 {
00108   template<class _Tp> 
00109     class valarray
00110     {
00111       template<class _Op>
00112     struct _UnaryOp 
00113     {
00114       typedef typename __fun<_Op, _Tp>::result_type __rt;
00115       typedef _Expr<_UnClos<_Op, _ValArray, _Tp>, __rt> _Rt;
00116     };
00117     public:
00118       typedef _Tp value_type;
00119       
00120     // _lib.valarray.cons_ construct/destroy:
00122       valarray();
00123 
00125       explicit valarray(size_t);
00126 
00128       valarray(const _Tp&, size_t);
00129 
00131       valarray(const _Tp* __restrict__, size_t);
00132 
00134       valarray(const valarray&);
00135 
00137       valarray(const slice_array<_Tp>&);
00138 
00140       valarray(const gslice_array<_Tp>&);
00141 
00143       valarray(const mask_array<_Tp>&);
00144 
00146       valarray(const indirect_array<_Tp>&);
00147 
00148       template<class _Dom>
00149     valarray(const _Expr<_Dom, _Tp>& __e);
00150 
00151       ~valarray();
00152 
00153       // _lib.valarray.assign_ assignment:
00162       valarray<_Tp>& operator=(const valarray<_Tp>&);
00163 
00171       valarray<_Tp>& operator=(const _Tp&);
00172 
00181       valarray<_Tp>& operator=(const slice_array<_Tp>&);
00182 
00191       valarray<_Tp>& operator=(const gslice_array<_Tp>&);
00192 
00201       valarray<_Tp>& operator=(const mask_array<_Tp>&);
00202 
00211       valarray<_Tp>& operator=(const indirect_array<_Tp>&);
00212 
00213       template<class _Dom> valarray<_Tp>&
00214     operator= (const _Expr<_Dom, _Tp>&);
00215 
00216       // _lib.valarray.access_ element access:
00223       _Tp&                operator[](size_t);
00224 
00225       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00226       // 389. Const overload of valarray::operator[] returns by value.
00227       const _Tp&          operator[](size_t) const;
00228 
00229       // _lib.valarray.sub_ subset operations:
00240       _Expr<_SClos<_ValArray, _Tp>, _Tp> operator[](slice) const;
00241 
00252       slice_array<_Tp>    operator[](slice);
00253 
00263       _Expr<_GClos<_ValArray, _Tp>, _Tp> operator[](const gslice&) const;
00264 
00275       gslice_array<_Tp>   operator[](const gslice&);
00276 
00289       valarray<_Tp>       operator[](const valarray<bool>&) const;
00290 
00303       mask_array<_Tp>     operator[](const valarray<bool>&);
00304 
00316       _Expr<_IClos<_ValArray, _Tp>, _Tp>
00317         operator[](const valarray<size_t>&) const;
00318 
00331       indirect_array<_Tp> operator[](const valarray<size_t>&);
00332 
00333       // _lib.valarray.unary_ unary operators:
00335       typename _UnaryOp<__unary_plus>::_Rt  operator+() const;
00336 
00338       typename _UnaryOp<__negate>::_Rt      operator-() const;
00339 
00341       typename _UnaryOp<__bitwise_not>::_Rt operator~() const;
00342 
00344       typename _UnaryOp<__logical_not>::_Rt operator!() const;
00345 
00346       // _lib.valarray.cassign_ computed assignment:
00348       valarray<_Tp>& operator*=(const _Tp&);
00349 
00351       valarray<_Tp>& operator/=(const _Tp&);
00352 
00354       valarray<_Tp>& operator%=(const _Tp&);
00355 
00357       valarray<_Tp>& operator+=(const _Tp&);
00358 
00360       valarray<_Tp>& operator-=(const _Tp&);
00361 
00363       valarray<_Tp>& operator^=(const _Tp&);
00364 
00366       valarray<_Tp>& operator&=(const _Tp&);
00367 
00369       valarray<_Tp>& operator|=(const _Tp&);
00370 
00372       valarray<_Tp>& operator<<=(const _Tp&);
00373 
00375       valarray<_Tp>& operator>>=(const _Tp&);
00376 
00378       valarray<_Tp>& operator*=(const valarray<_Tp>&);
00379 
00381       valarray<_Tp>& operator/=(const valarray<_Tp>&);
00382 
00384       valarray<_Tp>& operator%=(const valarray<_Tp>&);
00385 
00387       valarray<_Tp>& operator+=(const valarray<_Tp>&);
00388 
00390       valarray<_Tp>& operator-=(const valarray<_Tp>&);
00391 
00393       valarray<_Tp>& operator^=(const valarray<_Tp>&);
00394 
00396       valarray<_Tp>& operator|=(const valarray<_Tp>&);
00397 
00399       valarray<_Tp>& operator&=(const valarray<_Tp>&);
00400 
00402       valarray<_Tp>& operator<<=(const valarray<_Tp>&);
00403 
00405       valarray<_Tp>& operator>>=(const valarray<_Tp>&);
00406 
00407       template<class _Dom>
00408     valarray<_Tp>& operator*=(const _Expr<_Dom, _Tp>&);
00409       template<class _Dom>
00410     valarray<_Tp>& operator/=(const _Expr<_Dom, _Tp>&);
00411       template<class _Dom>
00412     valarray<_Tp>& operator%=(const _Expr<_Dom, _Tp>&);
00413       template<class _Dom>
00414     valarray<_Tp>& operator+=(const _Expr<_Dom, _Tp>&);
00415       template<class _Dom>
00416     valarray<_Tp>& operator-=(const _Expr<_Dom, _Tp>&);
00417       template<class _Dom>
00418     valarray<_Tp>& operator^=(const _Expr<_Dom, _Tp>&);
00419       template<class _Dom>
00420     valarray<_Tp>& operator|=(const _Expr<_Dom, _Tp>&);
00421       template<class _Dom>
00422     valarray<_Tp>& operator&=(const _Expr<_Dom, _Tp>&);
00423       template<class _Dom>
00424         valarray<_Tp>& operator<<=(const _Expr<_Dom, _Tp>&);
00425       template<class _Dom>
00426     valarray<_Tp>& operator>>=(const _Expr<_Dom, _Tp>&);
00427 
00428       // _lib.valarray.members_ member functions:
00430       size_t size() const;
00431 
00438       _Tp    sum() const;
00439 
00441       _Tp    min() const;   
00442 
00444       _Tp    max() const;   
00445 
00461       valarray<_Tp> shift (int) const;
00462 
00478       valarray<_Tp> cshift(int) const;
00479 
00490       _Expr<_ValFunClos<_ValArray, _Tp>, _Tp> apply(_Tp func(_Tp)) const;
00491 
00502       _Expr<_RefFunClos<_ValArray, _Tp>, _Tp> apply(_Tp func(const _Tp&)) const;
00503 
00513       void resize(size_t __size, _Tp __c = _Tp());
00514 
00515     private:
00516       size_t _M_size;
00517       _Tp* __restrict__ _M_data;
00518       
00519       friend class _Array<_Tp>;
00520     };
00521   
00522   template<typename _Tp>
00523     inline const _Tp&
00524     valarray<_Tp>::operator[](size_t __i) const
00525     { 
00526       __glibcxx_requires_subscript(__i);
00527       return _M_data[__i];
00528     }
00529 
00530   template<typename _Tp>
00531     inline _Tp&
00532     valarray<_Tp>::operator[](size_t __i)
00533     { 
00534       __glibcxx_requires_subscript(__i);
00535       return _M_data[__i];
00536     }
00537 
00538 } // std::
00539 
00540 #include <bits/valarray_after.h>
00541 
00542 #include <bits/slice_array.h>
00543 #include <bits/gslice.h>
00544 #include <bits/gslice_array.h>
00545 #include <bits/mask_array.h>
00546 #include <bits/indirect_array.h>
00547 
00548 namespace std
00549 {
00550   template<typename _Tp>
00551     inline
00552     valarray<_Tp>::valarray() : _M_size(0), _M_data(0) {}
00553 
00554   template<typename _Tp>
00555     inline 
00556     valarray<_Tp>::valarray(size_t __n) 
00557     : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n))
00558     { std::__valarray_default_construct(_M_data, _M_data + __n); }
00559 
00560   template<typename _Tp>
00561     inline
00562     valarray<_Tp>::valarray(const _Tp& __t, size_t __n)
00563     : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n))
00564     { std::__valarray_fill_construct(_M_data, _M_data + __n, __t); }
00565 
00566   template<typename _Tp>
00567     inline
00568     valarray<_Tp>::valarray(const _Tp* __restrict__ __p, size_t __n)
00569     : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n))
00570     { 
00571       _GLIBCXX_DEBUG_ASSERT(__p != 0 || __n == 0);
00572       std::__valarray_copy_construct(__p, __p + __n, _M_data); 
00573     }
00574 
00575   template<typename _Tp>
00576     inline
00577     valarray<_Tp>::valarray(const valarray<_Tp>& __v)
00578     : _M_size(__v._M_size), _M_data(__valarray_get_storage<_Tp>(__v._M_size))
00579     { std::__valarray_copy_construct(__v._M_data, __v._M_data + _M_size,
00580                      _M_data); }
00581 
00582   template<typename _Tp>
00583     inline
00584     valarray<_Tp>::valarray(const slice_array<_Tp>& __sa)
00585     : _M_size(__sa._M_sz), _M_data(__valarray_get_storage<_Tp>(__sa._M_sz))
00586     {
00587       std::__valarray_copy_construct
00588     (__sa._M_array, __sa._M_sz, __sa._M_stride, _Array<_Tp>(_M_data));
00589     }
00590 
00591   template<typename _Tp>
00592     inline
00593     valarray<_Tp>::valarray(const gslice_array<_Tp>& __ga)
00594     : _M_size(__ga._M_index.size()),
00595       _M_data(__valarray_get_storage<_Tp>(_M_size))
00596     {
00597       std::__valarray_copy_construct
00598     (__ga._M_array, _Array<size_t>(__ga._M_index),
00599      _Array<_Tp>(_M_data), _M_size);
00600     }
00601 
00602   template<typename _Tp>
00603     inline
00604     valarray<_Tp>::valarray(const mask_array<_Tp>& __ma)
00605     : _M_size(__ma._M_sz), _M_data(__valarray_get_storage<_Tp>(__ma._M_sz))
00606     {
00607       std::__valarray_copy_construct
00608     (__ma._M_array, __ma._M_mask, _Array<_Tp>(_M_data), _M_size);
00609     }
00610 
00611   template<typename _Tp>
00612     inline
00613     valarray<_Tp>::valarray(const indirect_array<_Tp>& __ia)
00614     : _M_size(__ia._M_sz), _M_data(__valarray_get_storage<_Tp>(__ia._M_sz))
00615     {
00616       std::__valarray_copy_construct
00617     (__ia._M_array, __ia._M_index, _Array<_Tp>(_M_data), _M_size);
00618     }
00619 
00620   template<typename _Tp> template<class _Dom>
00621     inline
00622     valarray<_Tp>::valarray(const _Expr<_Dom, _Tp>& __e)
00623     : _M_size(__e.size()), _M_data(__valarray_get_storage<_Tp>(_M_size))
00624     { std::__valarray_copy_construct(__e, _M_size, _Array<_Tp>(_M_data)); }
00625 
00626   template<typename _Tp>
00627     inline
00628     valarray<_Tp>::~valarray()
00629     {
00630       std::__valarray_destroy_elements(_M_data, _M_data + _M_size);
00631       std::__valarray_release_memory(_M_data);
00632     }
00633 
00634   template<typename _Tp>
00635     inline valarray<_Tp>&
00636     valarray<_Tp>::operator=(const valarray<_Tp>& __v)
00637     {
00638       _GLIBCXX_DEBUG_ASSERT(_M_size == __v._M_size);
00639       std::__valarray_copy(__v._M_data, _M_size, _M_data);
00640       return *this;
00641     }
00642 
00643   template<typename _Tp>
00644     inline valarray<_Tp>&
00645     valarray<_Tp>::operator=(const _Tp& __t)
00646     {
00647       std::__valarray_fill(_M_data, _M_size, __t);
00648       return *this;
00649     }
00650 
00651   template<typename _Tp>
00652     inline valarray<_Tp>&
00653     valarray<_Tp>::operator=(const slice_array<_Tp>& __sa)
00654     {
00655       _GLIBCXX_DEBUG_ASSERT(_M_size == __sa._M_sz);
00656       std::__valarray_copy(__sa._M_array, __sa._M_sz,
00657                __sa._M_stride, _Array<_Tp>(_M_data));
00658       return *this;
00659     }
00660 
00661   template<typename _Tp>
00662     inline valarray<_Tp>&
00663     valarray<_Tp>::operator=(const gslice_array<_Tp>& __ga)
00664     {
00665       _GLIBCXX_DEBUG_ASSERT(_M_size == __ga._M_index.size());
00666       std::__valarray_copy(__ga._M_array, _Array<size_t>(__ga._M_index),
00667                _Array<_Tp>(_M_data), _M_size);
00668       return *this;
00669     }
00670 
00671   template<typename _Tp>
00672     inline valarray<_Tp>&
00673     valarray<_Tp>::operator=(const mask_array<_Tp>& __ma)
00674     {
00675       _GLIBCXX_DEBUG_ASSERT(_M_size == __ma._M_sz);
00676       std::__valarray_copy(__ma._M_array, __ma._M_mask,
00677                _Array<_Tp>(_M_data), _M_size);
00678       return *this;
00679     }
00680 
00681   template<typename _Tp>
00682     inline valarray<_Tp>&
00683     valarray<_Tp>::operator=(const indirect_array<_Tp>& __ia)
00684     {
00685       _GLIBCXX_DEBUG_ASSERT(_M_size == __ia._M_sz);
00686       std::__valarray_copy(__ia._M_array, __ia._M_index,
00687                _Array<_Tp>(_M_data), _M_size);
00688       return *this;
00689     }
00690 
00691   template<typename _Tp> template<class _Dom>
00692     inline valarray<_Tp>&
00693     valarray<_Tp>::operator=(const _Expr<_Dom, _Tp>& __e)
00694     {
00695       _GLIBCXX_DEBUG_ASSERT(_M_size == __e.size());
00696       std::__valarray_copy(__e, _M_size, _Array<_Tp>(_M_data));
00697       return *this;
00698     }
00699 
00700   template<typename _Tp>
00701     inline _Expr<_SClos<_ValArray,_Tp>, _Tp>
00702     valarray<_Tp>::operator[](slice __s) const
00703     {
00704       typedef _SClos<_ValArray,_Tp> _Closure;
00705       return _Expr<_Closure, _Tp>(_Closure (_Array<_Tp>(_M_data), __s));
00706     }
00707 
00708   template<typename _Tp>
00709     inline slice_array<_Tp>
00710     valarray<_Tp>::operator[](slice __s)
00711     { return slice_array<_Tp>(_Array<_Tp>(_M_data), __s); }
00712 
00713   template<typename _Tp>
00714     inline _Expr<_GClos<_ValArray,_Tp>, _Tp>
00715     valarray<_Tp>::operator[](const gslice& __gs) const
00716     {
00717       typedef _GClos<_ValArray,_Tp> _Closure;
00718       return _Expr<_Closure, _Tp>
00719     (_Closure(_Array<_Tp>(_M_data), __gs._M_index->_M_index));
00720     }
00721 
00722   template<typename _Tp>
00723     inline gslice_array<_Tp>
00724     valarray<_Tp>::operator[](const gslice& __gs)
00725     {
00726       return gslice_array<_Tp>
00727     (_Array<_Tp>(_M_data), __gs._M_index->_M_index);
00728     }
00729 
00730   template<typename _Tp>
00731     inline valarray<_Tp>
00732     valarray<_Tp>::operator[](const valarray<bool>& __m) const
00733     {
00734       size_t __s = 0;
00735       size_t __e = __m.size();
00736       for (size_t __i=0; __i<__e; ++__i)
00737     if (__m[__i]) ++__s;
00738       return valarray<_Tp>(mask_array<_Tp>(_Array<_Tp>(_M_data), __s,
00739                        _Array<bool> (__m)));
00740     }
00741 
00742   template<typename _Tp>
00743     inline mask_array<_Tp>
00744     valarray<_Tp>::operator[](const valarray<bool>& __m)
00745     {
00746       size_t __s = 0;
00747       size_t __e = __m.size();
00748       for (size_t __i=0; __i<__e; ++__i)
00749     if (__m[__i]) ++__s;
00750       return mask_array<_Tp>(_Array<_Tp>(_M_data), __s, _Array<bool>(__m));
00751     }
00752 
00753   template<typename _Tp>
00754     inline _Expr<_IClos<_ValArray,_Tp>, _Tp>
00755     valarray<_Tp>::operator[](const valarray<size_t>& __i) const
00756     {
00757       typedef _IClos<_ValArray,_Tp> _Closure;
00758       return _Expr<_Closure, _Tp>(_Closure(*this, __i));
00759     }
00760 
00761   template<typename _Tp>
00762     inline indirect_array<_Tp>
00763     valarray<_Tp>::operator[](const valarray<size_t>& __i)
00764     {
00765       return indirect_array<_Tp>(_Array<_Tp>(_M_data), __i.size(),
00766                  _Array<size_t>(__i));
00767     }
00768 
00769   template<class _Tp>
00770     inline size_t 
00771     valarray<_Tp>::size() const
00772     { return _M_size; }
00773 
00774   template<class _Tp>
00775     inline _Tp
00776     valarray<_Tp>::sum() const
00777     {
00778       _GLIBCXX_DEBUG_ASSERT(_M_size > 0);
00779       return std::__valarray_sum(_M_data, _M_data + _M_size);
00780     }
00781 
00782   template <class _Tp>
00783      inline valarray<_Tp>
00784      valarray<_Tp>::shift(int __n) const
00785      {
00786        _Tp* const __a = static_cast<_Tp*>
00787          (__builtin_alloca(sizeof(_Tp) * _M_size));
00788        if (__n == 0)                          // no shift
00789          std::__valarray_copy_construct(_M_data, _M_data + _M_size, __a);
00790        else if (__n > 0)         // __n > 0: shift left
00791          {                 
00792            if (size_t(__n) > _M_size)
00793              std::__valarray_default_construct(__a, __a + __n);
00794            else
00795              {
00796                std::__valarray_copy_construct(_M_data + __n,
00797                           _M_data + _M_size, __a);
00798                std::__valarray_default_construct(__a + _M_size -__n,
00799                          __a + _M_size);
00800              }
00801          }
00802        else                        // __n < 0: shift right
00803          {                          
00804            std::__valarray_copy_construct (_M_data, _M_data + _M_size + __n,
00805                        __a - __n);
00806            std::__valarray_default_construct(__a, __a - __n);
00807          }
00808        return valarray<_Tp>(__a, _M_size);
00809      }
00810 
00811   template <class _Tp>
00812      inline valarray<_Tp>
00813      valarray<_Tp>::cshift (int __n) const
00814      {
00815        _Tp* const __a = static_cast<_Tp*>
00816          (__builtin_alloca (sizeof(_Tp) * _M_size));
00817        if (__n == 0)               // no cshift
00818          std::__valarray_copy_construct(_M_data, _M_data + _M_size, __a);
00819        else if (__n > 0)           // cshift left
00820          {               
00821            std::__valarray_copy_construct(_M_data, _M_data + __n,
00822                       __a + _M_size - __n);
00823            std::__valarray_copy_construct(_M_data + __n, _M_data + _M_size,
00824                       __a);
00825          }
00826        else                        // cshift right
00827          {                       
00828            std::__valarray_copy_construct
00829              (_M_data + _M_size + __n, _M_data + _M_size, __a);
00830            std::__valarray_copy_construct
00831              (_M_data, _M_data + _M_size+__n, __a - __n);
00832          }
00833        return valarray<_Tp>(__a, _M_size);
00834      }
00835 
00836   template <class _Tp>
00837     inline void
00838     valarray<_Tp>::resize (size_t __n, _Tp __c)
00839     {
00840       // This complication is so to make valarray<valarray<T> > work
00841       // even though it is not required by the standard.  Nobody should
00842       // be saying valarray<valarray<T> > anyway.  See the specs.
00843       std::__valarray_destroy_elements(_M_data, _M_data + _M_size);
00844       if (_M_size != __n)
00845     {
00846       std::__valarray_release_memory(_M_data);
00847       _M_size = __n;
00848       _M_data = __valarray_get_storage<_Tp>(__n);
00849     }
00850       std::__valarray_fill_construct(_M_data, _M_data + __n, __c);
00851     }
00852     
00853   template<typename _Tp>
00854     inline _Tp
00855     valarray<_Tp>::min() const
00856     {
00857       _GLIBCXX_DEBUG_ASSERT(_M_size > 0);
00858       return *std::min_element(_M_data, _M_data+_M_size);
00859     }
00860 
00861   template<typename _Tp>
00862     inline _Tp
00863     valarray<_Tp>::max() const
00864     {
00865       _GLIBCXX_DEBUG_ASSERT(_M_size > 0);
00866       return *std::max_element(_M_data, _M_data+_M_size);
00867     }
00868   
00869   template<class _Tp>
00870     inline _Expr<_ValFunClos<_ValArray, _Tp>, _Tp>
00871     valarray<_Tp>::apply(_Tp func(_Tp)) const
00872     {
00873       typedef _ValFunClos<_ValArray, _Tp> _Closure;
00874       return _Expr<_Closure, _Tp>(_Closure(*this, func));
00875     }
00876 
00877   template<class _Tp>
00878     inline _Expr<_RefFunClos<_ValArray, _Tp>, _Tp>
00879     valarray<_Tp>::apply(_Tp func(const _Tp &)) const
00880     {
00881       typedef _RefFunClos<_ValArray, _Tp> _Closure;
00882       return _Expr<_Closure, _Tp>(_Closure(*this, func));
00883     }
00884 
00885 #define _DEFINE_VALARRAY_UNARY_OPERATOR(_Op, _Name)                     \
00886   template<typename _Tp>                        \
00887     inline typename valarray<_Tp>::template _UnaryOp<_Name>::_Rt        \
00888     valarray<_Tp>::operator _Op() const                 \
00889     {                                   \
00890       typedef _UnClos<_Name, _ValArray, _Tp> _Closure;                  \
00891       typedef typename __fun<_Name, _Tp>::result_type _Rt;              \
00892       return _Expr<_Closure, _Rt>(_Closure(*this));         \
00893     }
00894 
00895     _DEFINE_VALARRAY_UNARY_OPERATOR(+, __unary_plus)
00896     _DEFINE_VALARRAY_UNARY_OPERATOR(-, __negate)
00897     _DEFINE_VALARRAY_UNARY_OPERATOR(~, __bitwise_not)
00898     _DEFINE_VALARRAY_UNARY_OPERATOR (!, __logical_not)
00899 
00900 #undef _DEFINE_VALARRAY_UNARY_OPERATOR
00901 
00902 #define _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(_Op, _Name)               \
00903   template<class _Tp>                           \
00904     inline valarray<_Tp>&                       \
00905     valarray<_Tp>::operator _Op##=(const _Tp &__t)          \
00906     {                                   \
00907       _Array_augmented_##_Name(_Array<_Tp>(_M_data), _M_size, __t); \
00908       return *this;                         \
00909     }                                   \
00910                                     \
00911   template<class _Tp>                           \
00912     inline valarray<_Tp>&                       \
00913     valarray<_Tp>::operator _Op##=(const valarray<_Tp> &__v)        \
00914     {                                   \
00915       _GLIBCXX_DEBUG_ASSERT(_M_size == __v._M_size);                    \
00916       _Array_augmented_##_Name(_Array<_Tp>(_M_data), _M_size,       \
00917                    _Array<_Tp>(__v._M_data));       \
00918       return *this;                         \
00919     }
00920 
00921 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(+, __plus)
00922 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(-, __minus)
00923 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(*, __multiplies)
00924 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(/, __divides)
00925 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(%, __modulus)
00926 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(^, __bitwise_xor)
00927 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(&, __bitwise_and)
00928 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(|, __bitwise_or)
00929 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(<<, __shift_left)
00930 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(>>, __shift_right)
00931 
00932 #undef _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT
00933 
00934 #define _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(_Op, _Name)          \
00935   template<class _Tp> template<class _Dom>              \
00936     inline valarray<_Tp>&                       \
00937     valarray<_Tp>::operator _Op##=(const _Expr<_Dom, _Tp>& __e)     \
00938     {                                   \
00939       _Array_augmented_##_Name(_Array<_Tp>(_M_data), __e, _M_size); \
00940       return *this;                         \
00941     }
00942 
00943 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(+, __plus)
00944 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(-, __minus)
00945 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(*, __multiplies)
00946 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(/, __divides)
00947 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(%, __modulus)
00948 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(^, __bitwise_xor)
00949 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(&, __bitwise_and)
00950 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(|, __bitwise_or)
00951 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(<<, __shift_left)
00952 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(>>, __shift_right)
00953 
00954 #undef _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT
00955     
00956 
00957 #define _DEFINE_BINARY_OPERATOR(_Op, _Name)             \
00958   template<typename _Tp>                        \
00959     inline _Expr<_BinClos<_Name, _ValArray, _ValArray, _Tp, _Tp>,       \
00960                  typename __fun<_Name, _Tp>::result_type>               \
00961     operator _Op(const valarray<_Tp>& __v, const valarray<_Tp>& __w)    \
00962     {                                   \
00963       _GLIBCXX_DEBUG_ASSERT(__v.size() == __w.size());                  \
00964       typedef _BinClos<_Name, _ValArray, _ValArray, _Tp, _Tp> _Closure; \
00965       typedef typename __fun<_Name, _Tp>::result_type _Rt;              \
00966       return _Expr<_Closure, _Rt>(_Closure(__v, __w));                  \
00967     }                                   \
00968                                     \
00969   template<typename _Tp>                        \
00970     inline _Expr<_BinClos<_Name, _ValArray,_Constant, _Tp, _Tp>,        \
00971                  typename __fun<_Name, _Tp>::result_type>               \
00972     operator _Op(const valarray<_Tp>& __v, const _Tp& __t)      \
00973     {                                   \
00974       typedef _BinClos<_Name, _ValArray, _Constant, _Tp, _Tp> _Closure; \
00975       typedef typename __fun<_Name, _Tp>::result_type _Rt;              \
00976       return _Expr<_Closure, _Rt>(_Closure(__v, __t));                  \
00977     }                                   \
00978                                     \
00979   template<typename _Tp>                        \
00980     inline _Expr<_BinClos<_Name, _Constant, _ValArray, _Tp, _Tp>,       \
00981                  typename __fun<_Name, _Tp>::result_type>               \
00982     operator _Op(const _Tp& __t, const valarray<_Tp>& __v)      \
00983     {                                   \
00984       typedef _BinClos<_Name, _Constant, _ValArray, _Tp, _Tp> _Closure; \
00985       typedef typename __fun<_Name, _Tp>::result_type _Rt;              \
00986       return _Expr<_Closure, _Tp>(_Closure(__t, __v));                  \
00987     }
00988 
00989 _DEFINE_BINARY_OPERATOR(+, __plus)
00990 _DEFINE_BINARY_OPERATOR(-, __minus)
00991 _DEFINE_BINARY_OPERATOR(*, __multiplies)
00992 _DEFINE_BINARY_OPERATOR(/, __divides)
00993 _DEFINE_BINARY_OPERATOR(%, __modulus)
00994 _DEFINE_BINARY_OPERATOR(^, __bitwise_xor)
00995 _DEFINE_BINARY_OPERATOR(&, __bitwise_and)
00996 _DEFINE_BINARY_OPERATOR(|, __bitwise_or)
00997 _DEFINE_BINARY_OPERATOR(<<, __shift_left)
00998 _DEFINE_BINARY_OPERATOR(>>, __shift_right)
00999 _DEFINE_BINARY_OPERATOR(&&, __logical_and)
01000 _DEFINE_BINARY_OPERATOR(||, __logical_or)
01001 _DEFINE_BINARY_OPERATOR(==, __equal_to)
01002 _DEFINE_BINARY_OPERATOR(!=, __not_equal_to)
01003 _DEFINE_BINARY_OPERATOR(<, __less)
01004 _DEFINE_BINARY_OPERATOR(>, __greater)
01005 _DEFINE_BINARY_OPERATOR(<=, __less_equal)
01006 _DEFINE_BINARY_OPERATOR(>=, __greater_equal)
01007 
01008 } // namespace std
01009 
01010 #endif /* _GLIBCXX_VALARRAY */

Generated on Tue Feb 2 16:56:53 2010 for GNU C++ STL by  doxygen 1.4.7