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, 2003, 2004, 2005,
00004 // 2006, 2007, 2009
00005 // Free Software Foundation, Inc.
00006 //
00007 // This file is part of the GNU ISO C++ Library.  This library is free
00008 // software; you can redistribute it and/or modify it under the
00009 // terms of the GNU General Public License as published by the
00010 // Free Software Foundation; either version 3, or (at your option)
00011 // any later version.
00012 
00013 // This library is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 
00018 // Under Section 7 of GPL version 3, you are granted additional
00019 // permissions described in the GCC Runtime Library Exception, version
00020 // 3.1, as published by the Free Software Foundation.
00021 
00022 // You should have received a copy of the GNU General Public License and
00023 // a copy of the GCC Runtime Library Exception along with this program;
00024 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00025 // <http://www.gnu.org/licenses/>.
00026 
00027 /** @file valarray
00028  *  This is a Standard C++ Library header. 
00029  */
00030 
00031 // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr>
00032 
00033 #ifndef _GLIBCXX_VALARRAY
00034 #define _GLIBCXX_VALARRAY 1
00035 
00036 #pragma GCC system_header
00037 
00038 #include <bits/c++config.h>
00039 #include <cstddef>
00040 #include <cmath>
00041 #include <algorithm>
00042 #include <debug/debug.h>
00043 #include <initializer_list>
00044 
00045 _GLIBCXX_BEGIN_NAMESPACE(std)
00046 
00047   template<class _Clos, typename _Tp> 
00048     class _Expr;
00049 
00050   template<typename _Tp1, typename _Tp2> 
00051     class _ValArray;    
00052 
00053   template<class _Oper, template<class, class> class _Meta, class _Dom>
00054     struct _UnClos;
00055 
00056   template<class _Oper,
00057         template<class, class> class _Meta1,
00058         template<class, class> class _Meta2,
00059         class _Dom1, class _Dom2> 
00060     class _BinClos;
00061 
00062   template<template<class, class> class _Meta, class _Dom> 
00063     class _SClos;
00064 
00065   template<template<class, class> class _Meta, class _Dom> 
00066     class _GClos;
00067     
00068   template<template<class, class> class _Meta, class _Dom> 
00069     class _IClos;
00070     
00071   template<template<class, class> class _Meta, class _Dom> 
00072     class _ValFunClos;
00073   
00074   template<template<class, class> class _Meta, class _Dom> 
00075     class _RefFunClos;
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 _GLIBCXX_END_NAMESPACE
00086 
00087 #include <bits/valarray_array.h>
00088 #include <bits/valarray_before.h>
00089   
00090 _GLIBCXX_BEGIN_NAMESPACE(std)
00091 
00092   /**
00093    * @defgroup numeric_arrays Numeric Arrays
00094    * @ingroup numerics
00095    *
00096    * Classes and functions for representing and manipulating arrays of elements.
00097    * @{
00098    */
00099 
00100   /**
00101    *  @brief  Smart array designed to support numeric processing.
00102    *
00103    *  A valarray is an array that provides constraints intended to allow for
00104    *  effective optimization of numeric array processing by reducing the
00105    *  aliasing that can result from pointer representations.  It represents a
00106    *  one-dimensional array from which different multidimensional subsets can
00107    *  be accessed and modified.
00108    *  
00109    *  @param  Tp  Type of object in the array.
00110    */
00111   template<class _Tp> 
00112     class valarray
00113     {
00114       template<class _Op>
00115     struct _UnaryOp 
00116     {
00117       typedef typename __fun<_Op, _Tp>::result_type __rt;
00118       typedef _Expr<_UnClos<_Op, _ValArray, _Tp>, __rt> _Rt;
00119     };
00120     public:
00121       typedef _Tp value_type;
00122       
00123     // _lib.valarray.cons_ construct/destroy:
00124       ///  Construct an empty array.
00125       valarray();
00126 
00127       ///  Construct an array with @a n elements.
00128       explicit valarray(size_t);
00129 
00130       ///  Construct an array with @a n elements initialized to @a t.
00131       valarray(const _Tp&, size_t);
00132 
00133       ///  Construct an array initialized to the first @a n elements of @a t.
00134       valarray(const _Tp* __restrict__, size_t);
00135 
00136       ///  Copy constructor.
00137       valarray(const valarray&);
00138 
00139       ///  Construct an array with the same size and values in @a sa.
00140       valarray(const slice_array<_Tp>&);
00141 
00142       ///  Construct an array with the same size and values in @a ga.
00143       valarray(const gslice_array<_Tp>&);
00144 
00145       ///  Construct an array with the same size and values in @a ma.
00146       valarray(const mask_array<_Tp>&);
00147 
00148       ///  Construct an array with the same size and values in @a ia.
00149       valarray(const indirect_array<_Tp>&);
00150 
00151 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00152       ///  Construct an array with an initializer_list of values.
00153       valarray(initializer_list<_Tp>);
00154 #endif
00155 
00156       template<class _Dom>
00157     valarray(const _Expr<_Dom, _Tp>& __e);
00158 
00159       ~valarray();
00160 
00161       // _lib.valarray.assign_ assignment:
00162       /**
00163        *  @brief  Assign elements to an array.
00164        *
00165        *  Assign elements of array to values in @a v.  Results are undefined
00166        *  if @a v does not have the same size as this array.
00167        *
00168        *  @param  v  Valarray to get values from.
00169        */
00170       valarray<_Tp>& operator=(const valarray<_Tp>&);
00171 
00172       /**
00173        *  @brief  Assign elements to a value.
00174        *
00175        *  Assign all elements of array to @a t.
00176        *
00177        *  @param  t  Value for elements.
00178        */
00179       valarray<_Tp>& operator=(const _Tp&);
00180 
00181       /**
00182        *  @brief  Assign elements to an array subset.
00183        *
00184        *  Assign elements of array to values in @a sa.  Results are undefined
00185        *  if @a sa does not have the same size as this array.
00186        *
00187        *  @param  sa  Array slice to get values from.
00188        */
00189       valarray<_Tp>& operator=(const slice_array<_Tp>&);
00190 
00191       /**
00192        *  @brief  Assign elements to an array subset.
00193        *
00194        *  Assign elements of array to values in @a ga.  Results are undefined
00195        *  if @a ga does not have the same size as this array.
00196        *
00197        *  @param  ga  Array slice to get values from.
00198        */
00199       valarray<_Tp>& operator=(const gslice_array<_Tp>&);
00200 
00201       /**
00202        *  @brief  Assign elements to an array subset.
00203        *
00204        *  Assign elements of array to values in @a ma.  Results are undefined
00205        *  if @a ma does not have the same size as this array.
00206        *
00207        *  @param  ma  Array slice to get values from.
00208        */
00209       valarray<_Tp>& operator=(const mask_array<_Tp>&);
00210 
00211       /**
00212        *  @brief  Assign elements to an array subset.
00213        *
00214        *  Assign elements of array to values in @a ia.  Results are undefined
00215        *  if @a ia does not have the same size as this array.
00216        *
00217        *  @param  ia  Array slice to get values from.
00218        */
00219       valarray<_Tp>& operator=(const indirect_array<_Tp>&);
00220 
00221 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00222       /**
00223        *  @brief  Assign elements to an initializer_list.
00224        *
00225        *  Assign elements of array to values in @a l.  Results are undefined
00226        *  if @a l does not have the same size as this array.
00227        *
00228        *  @param  l  initializer_list to get values from.
00229        */
00230       valarray& operator=(initializer_list<_Tp>);
00231 #endif
00232 
00233       template<class _Dom> valarray<_Tp>&
00234     operator= (const _Expr<_Dom, _Tp>&);
00235 
00236       // _lib.valarray.access_ element access:
00237       /**
00238        *  Return a reference to the i'th array element.  
00239        *
00240        *  @param  i  Index of element to return.
00241        *  @return  Reference to the i'th element.
00242        */
00243       _Tp&                operator[](size_t);
00244 
00245       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00246       // 389. Const overload of valarray::operator[] returns by value.
00247       const _Tp&          operator[](size_t) const;
00248 
00249       // _lib.valarray.sub_ subset operations:
00250       /**
00251        *  @brief  Return an array subset.
00252        *
00253        *  Returns a new valarray containing the elements of the array
00254        *  indicated by the slice argument.  The new valarray has the same size
00255        *  as the input slice.  @see slice.
00256        *
00257        *  @param  s  The source slice.
00258        *  @return  New valarray containing elements in @a s.
00259        */
00260       _Expr<_SClos<_ValArray, _Tp>, _Tp> operator[](slice) const;
00261 
00262       /**
00263        *  @brief  Return a reference to an array subset.
00264        *
00265        *  Returns a new valarray containing the elements of the array
00266        *  indicated by the slice argument.  The new valarray has the same size
00267        *  as the input slice.  @see slice.
00268        *
00269        *  @param  s  The source slice.
00270        *  @return  New valarray containing elements in @a s.
00271        */
00272       slice_array<_Tp>    operator[](slice);
00273 
00274       /**
00275        *  @brief  Return an array subset.
00276        *
00277        *  Returns a slice_array referencing the elements of the array
00278        *  indicated by the slice argument.  @see gslice.
00279        *
00280        *  @param  s  The source slice.
00281        *  @return  Slice_array referencing elements indicated by @a s.
00282        */
00283       _Expr<_GClos<_ValArray, _Tp>, _Tp> operator[](const gslice&) const;
00284 
00285       /**
00286        *  @brief  Return a reference to an array subset.
00287        *
00288        *  Returns a new valarray containing the elements of the array
00289        *  indicated by the gslice argument.  The new valarray has
00290        *  the same size as the input gslice.  @see gslice.
00291        *
00292        *  @param  s  The source gslice.
00293        *  @return  New valarray containing elements in @a s.
00294        */
00295       gslice_array<_Tp>   operator[](const gslice&);
00296 
00297       /**
00298        *  @brief  Return an array subset.
00299        *
00300        *  Returns a new valarray containing the elements of the array
00301        *  indicated by the argument.  The input is a valarray of bool which
00302        *  represents a bitmask indicating which elements should be copied into
00303        *  the new valarray.  Each element of the array is added to the return
00304        *  valarray if the corresponding element of the argument is true.
00305        *
00306        *  @param  m  The valarray bitmask.
00307        *  @return  New valarray containing elements indicated by @a m.
00308        */
00309       valarray<_Tp>       operator[](const valarray<bool>&) const;
00310 
00311       /**
00312        *  @brief  Return a reference to an array subset.
00313        *
00314        *  Returns a new mask_array referencing the elements of the array
00315        *  indicated by the argument.  The input is a valarray of bool which
00316        *  represents a bitmask indicating which elements are part of the
00317        *  subset.  Elements of the array are part of the subset if the
00318        *  corresponding element of the argument is true.
00319        *
00320        *  @param  m  The valarray bitmask.
00321        *  @return  New valarray containing elements indicated by @a m.
00322        */
00323       mask_array<_Tp>     operator[](const valarray<bool>&);
00324 
00325       /**
00326        *  @brief  Return an array subset.
00327        *
00328        *  Returns a new valarray containing the elements of the array
00329        *  indicated by the argument.  The elements in the argument are
00330        *  interpreted as the indices of elements of this valarray to copy to
00331        *  the return valarray.
00332        *
00333        *  @param  i  The valarray element index list.
00334        *  @return  New valarray containing elements in @a s.
00335        */
00336       _Expr<_IClos<_ValArray, _Tp>, _Tp>
00337         operator[](const valarray<size_t>&) const;
00338 
00339       /**
00340        *  @brief  Return a reference to an array subset.
00341        *
00342        *  Returns an indirect_array referencing the elements of the array
00343        *  indicated by the argument.  The elements in the argument are
00344        *  interpreted as the indices of elements of this valarray to include
00345        *  in the subset.  The returned indirect_array refers to these
00346        *  elements.
00347        *
00348        *  @param  i  The valarray element index list.
00349        *  @return  Indirect_array referencing elements in @a i.
00350        */
00351       indirect_array<_Tp> operator[](const valarray<size_t>&);
00352 
00353       // _lib.valarray.unary_ unary operators:
00354       ///  Return a new valarray by applying unary + to each element.
00355       typename _UnaryOp<__unary_plus>::_Rt  operator+() const;
00356 
00357       ///  Return a new valarray by applying unary - to each element.
00358       typename _UnaryOp<__negate>::_Rt      operator-() const;
00359 
00360       ///  Return a new valarray by applying unary ~ to each element.
00361       typename _UnaryOp<__bitwise_not>::_Rt operator~() const;
00362 
00363       ///  Return a new valarray by applying unary ! to each element.
00364       typename _UnaryOp<__logical_not>::_Rt operator!() const;
00365 
00366       // _lib.valarray.cassign_ computed assignment:
00367       ///  Multiply each element of array by @a t.
00368       valarray<_Tp>& operator*=(const _Tp&);
00369 
00370       ///  Divide each element of array by @a t.
00371       valarray<_Tp>& operator/=(const _Tp&);
00372 
00373       ///  Set each element e of array to e % @a t.
00374       valarray<_Tp>& operator%=(const _Tp&);
00375 
00376       ///  Add @a t to each element of array.
00377       valarray<_Tp>& operator+=(const _Tp&);
00378 
00379       ///  Subtract @a t to each element of array.
00380       valarray<_Tp>& operator-=(const _Tp&);
00381 
00382       ///  Set each element e of array to e ^ @a t.
00383       valarray<_Tp>& operator^=(const _Tp&);
00384 
00385       ///  Set each element e of array to e & @a t.
00386       valarray<_Tp>& operator&=(const _Tp&);
00387 
00388       ///  Set each element e of array to e | @a t.
00389       valarray<_Tp>& operator|=(const _Tp&);
00390 
00391       ///  Left shift each element e of array by @a t bits.
00392       valarray<_Tp>& operator<<=(const _Tp&);
00393 
00394       ///  Right shift each element e of array by @a t bits.
00395       valarray<_Tp>& operator>>=(const _Tp&);
00396 
00397       ///  Multiply elements of array by corresponding elements of @a v.
00398       valarray<_Tp>& operator*=(const valarray<_Tp>&);
00399 
00400       ///  Divide elements of array by corresponding elements of @a v.
00401       valarray<_Tp>& operator/=(const valarray<_Tp>&);
00402 
00403       ///  Modulo elements of array by corresponding elements of @a v.
00404       valarray<_Tp>& operator%=(const valarray<_Tp>&);
00405 
00406       ///  Add corresponding elements of @a v to elements of array.
00407       valarray<_Tp>& operator+=(const valarray<_Tp>&);
00408 
00409       ///  Subtract corresponding elements of @a v from elements of array.
00410       valarray<_Tp>& operator-=(const valarray<_Tp>&);
00411 
00412       ///  Logical xor corresponding elements of @a v with elements of array.
00413       valarray<_Tp>& operator^=(const valarray<_Tp>&);
00414 
00415       ///  Logical or corresponding elements of @a v with elements of array.
00416       valarray<_Tp>& operator|=(const valarray<_Tp>&);
00417 
00418       ///  Logical and corresponding elements of @a v with elements of array.
00419       valarray<_Tp>& operator&=(const valarray<_Tp>&);
00420 
00421       ///  Left shift elements of array by corresponding elements of @a v.
00422       valarray<_Tp>& operator<<=(const valarray<_Tp>&);
00423 
00424       ///  Right shift elements of array by corresponding elements of @a v.
00425       valarray<_Tp>& operator>>=(const valarray<_Tp>&);
00426 
00427       template<class _Dom>
00428     valarray<_Tp>& operator*=(const _Expr<_Dom, _Tp>&);
00429       template<class _Dom>
00430     valarray<_Tp>& operator/=(const _Expr<_Dom, _Tp>&);
00431       template<class _Dom>
00432     valarray<_Tp>& operator%=(const _Expr<_Dom, _Tp>&);
00433       template<class _Dom>
00434     valarray<_Tp>& operator+=(const _Expr<_Dom, _Tp>&);
00435       template<class _Dom>
00436     valarray<_Tp>& operator-=(const _Expr<_Dom, _Tp>&);
00437       template<class _Dom>
00438     valarray<_Tp>& operator^=(const _Expr<_Dom, _Tp>&);
00439       template<class _Dom>
00440     valarray<_Tp>& operator|=(const _Expr<_Dom, _Tp>&);
00441       template<class _Dom>
00442     valarray<_Tp>& operator&=(const _Expr<_Dom, _Tp>&);
00443       template<class _Dom>
00444         valarray<_Tp>& operator<<=(const _Expr<_Dom, _Tp>&);
00445       template<class _Dom>
00446     valarray<_Tp>& operator>>=(const _Expr<_Dom, _Tp>&);
00447 
00448       // _lib.valarray.members_ member functions:
00449       ///  Return the number of elements in array.
00450       size_t size() const;
00451 
00452       /**
00453        *  @brief  Return the sum of all elements in the array.
00454        *
00455        *  Accumulates the sum of all elements into a Tp using +=.  The order
00456        *  of adding the elements is unspecified.
00457        */
00458       _Tp    sum() const;
00459 
00460       ///  Return the minimum element using operator<().
00461       _Tp    min() const;   
00462 
00463       ///  Return the maximum element using operator<().
00464       _Tp    max() const;   
00465 
00466       /**
00467        *  @brief  Return a shifted array.
00468        *
00469        *  A new valarray is constructed as a copy of this array with elements
00470        *  in shifted positions.  For an element with index i, the new position
00471        *  is i - n.  The new valarray has the same size as the current one.
00472        *  New elements without a value are set to 0.  Elements whose new
00473        *  position is outside the bounds of the array are discarded.
00474        *
00475        *  Positive arguments shift toward index 0, discarding elements [0, n).
00476        *  Negative arguments discard elements from the top of the array.
00477        *
00478        *  @param  n  Number of element positions to shift.
00479        *  @return  New valarray with elements in shifted positions.
00480        */
00481       valarray<_Tp> shift (int) const;
00482 
00483       /**
00484        *  @brief  Return a rotated array.
00485        *
00486        *  A new valarray is constructed as a copy of this array with elements
00487        *  in shifted positions.  For an element with index i, the new position
00488        *  is (i - n) % size().  The new valarray has the same size as the
00489        *  current one.  Elements that are shifted beyond the array bounds are
00490        *  shifted into the other end of the array.  No elements are lost.
00491        *
00492        *  Positive arguments shift toward index 0, wrapping around the top.
00493        *  Negative arguments shift towards the top, wrapping around to 0.
00494        *
00495        *  @param  n  Number of element positions to rotate.
00496        *  @return  New valarray with elements in shifted positions.
00497        */
00498       valarray<_Tp> cshift(int) const;
00499 
00500       /**
00501        *  @brief  Apply a function to the array.
00502        *
00503        *  Returns a new valarray with elements assigned to the result of
00504        *  applying func to the corresponding element of this array.  The new
00505        *  array has the same size as this one.
00506        *
00507        *  @param  func  Function of Tp returning Tp to apply.
00508        *  @return  New valarray with transformed elements.
00509        */
00510       _Expr<_ValFunClos<_ValArray, _Tp>, _Tp> apply(_Tp func(_Tp)) const;
00511 
00512       /**
00513        *  @brief  Apply a function to the array.
00514        *
00515        *  Returns a new valarray with elements assigned to the result of
00516        *  applying func to the corresponding element of this array.  The new
00517        *  array has the same size as this one.
00518        *
00519        *  @param  func  Function of const Tp& returning Tp to apply.
00520        *  @return  New valarray with transformed elements.
00521        */
00522       _Expr<_RefFunClos<_ValArray, _Tp>, _Tp> apply(_Tp func(const _Tp&)) const;
00523 
00524       /**
00525        *  @brief  Resize array.
00526        *
00527        *  Resize this array to @a size and set all elements to @a c.  All
00528        *  references and iterators are invalidated.
00529        *
00530        *  @param  size  New array size.
00531        *  @param  c  New value for all elements.
00532        */
00533       void resize(size_t __size, _Tp __c = _Tp());
00534 
00535     private:
00536       size_t _M_size;
00537       _Tp* __restrict__ _M_data;
00538       
00539       friend class _Array<_Tp>;
00540     };
00541   
00542   template<typename _Tp>
00543     inline const _Tp&
00544     valarray<_Tp>::operator[](size_t __i) const
00545     { 
00546       __glibcxx_requires_subscript(__i);
00547       return _M_data[__i];
00548     }
00549 
00550   template<typename _Tp>
00551     inline _Tp&
00552     valarray<_Tp>::operator[](size_t __i)
00553     { 
00554       __glibcxx_requires_subscript(__i);
00555       return _M_data[__i];
00556     }
00557 
00558   // @} group numeric_arrays
00559 
00560 _GLIBCXX_END_NAMESPACE
00561 
00562 #include <bits/valarray_after.h>
00563 #include <bits/slice_array.h>
00564 #include <bits/gslice.h>
00565 #include <bits/gslice_array.h>
00566 #include <bits/mask_array.h>
00567 #include <bits/indirect_array.h>
00568 
00569 _GLIBCXX_BEGIN_NAMESPACE(std)
00570 
00571   /**
00572    * @addtogroup numeric_arrays
00573    * @{
00574    */
00575 
00576   template<typename _Tp>
00577     inline
00578     valarray<_Tp>::valarray() : _M_size(0), _M_data(0) {}
00579 
00580   template<typename _Tp>
00581     inline 
00582     valarray<_Tp>::valarray(size_t __n) 
00583     : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n))
00584     { std::__valarray_default_construct(_M_data, _M_data + __n); }
00585 
00586   template<typename _Tp>
00587     inline
00588     valarray<_Tp>::valarray(const _Tp& __t, size_t __n)
00589     : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n))
00590     { std::__valarray_fill_construct(_M_data, _M_data + __n, __t); }
00591 
00592   template<typename _Tp>
00593     inline
00594     valarray<_Tp>::valarray(const _Tp* __restrict__ __p, size_t __n)
00595     : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n))
00596     { 
00597       _GLIBCXX_DEBUG_ASSERT(__p != 0 || __n == 0);
00598       std::__valarray_copy_construct(__p, __p + __n, _M_data); 
00599     }
00600 
00601   template<typename _Tp>
00602     inline
00603     valarray<_Tp>::valarray(const valarray<_Tp>& __v)
00604     : _M_size(__v._M_size), _M_data(__valarray_get_storage<_Tp>(__v._M_size))
00605     { std::__valarray_copy_construct(__v._M_data, __v._M_data + _M_size,
00606                      _M_data); }
00607 
00608   template<typename _Tp>
00609     inline
00610     valarray<_Tp>::valarray(const slice_array<_Tp>& __sa)
00611     : _M_size(__sa._M_sz), _M_data(__valarray_get_storage<_Tp>(__sa._M_sz))
00612     {
00613       std::__valarray_copy_construct
00614     (__sa._M_array, __sa._M_sz, __sa._M_stride, _Array<_Tp>(_M_data));
00615     }
00616 
00617   template<typename _Tp>
00618     inline
00619     valarray<_Tp>::valarray(const gslice_array<_Tp>& __ga)
00620     : _M_size(__ga._M_index.size()),
00621       _M_data(__valarray_get_storage<_Tp>(_M_size))
00622     {
00623       std::__valarray_copy_construct
00624     (__ga._M_array, _Array<size_t>(__ga._M_index),
00625      _Array<_Tp>(_M_data), _M_size);
00626     }
00627 
00628   template<typename _Tp>
00629     inline
00630     valarray<_Tp>::valarray(const mask_array<_Tp>& __ma)
00631     : _M_size(__ma._M_sz), _M_data(__valarray_get_storage<_Tp>(__ma._M_sz))
00632     {
00633       std::__valarray_copy_construct
00634     (__ma._M_array, __ma._M_mask, _Array<_Tp>(_M_data), _M_size);
00635     }
00636 
00637   template<typename _Tp>
00638     inline
00639     valarray<_Tp>::valarray(const indirect_array<_Tp>& __ia)
00640     : _M_size(__ia._M_sz), _M_data(__valarray_get_storage<_Tp>(__ia._M_sz))
00641     {
00642       std::__valarray_copy_construct
00643     (__ia._M_array, __ia._M_index, _Array<_Tp>(_M_data), _M_size);
00644     }
00645 
00646 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00647   template<typename _Tp>
00648     inline
00649     valarray<_Tp>::valarray(initializer_list<_Tp> __l)
00650       : _M_size(__l.size()), _M_data(__valarray_get_storage<_Tp>(__l.size()))
00651     { std::__valarray_copy_construct (__l.begin(), __l.end(), _M_data); }
00652 #endif
00653 
00654   template<typename _Tp> template<class _Dom>
00655     inline
00656     valarray<_Tp>::valarray(const _Expr<_Dom, _Tp>& __e)
00657     : _M_size(__e.size()), _M_data(__valarray_get_storage<_Tp>(_M_size))
00658     { std::__valarray_copy_construct(__e, _M_size, _Array<_Tp>(_M_data)); }
00659 
00660   template<typename _Tp>
00661     inline
00662     valarray<_Tp>::~valarray()
00663     {
00664       std::__valarray_destroy_elements(_M_data, _M_data + _M_size);
00665       std::__valarray_release_memory(_M_data);
00666     }
00667 
00668   template<typename _Tp>
00669     inline valarray<_Tp>&
00670     valarray<_Tp>::operator=(const valarray<_Tp>& __v)
00671     {
00672       _GLIBCXX_DEBUG_ASSERT(_M_size == __v._M_size);
00673       std::__valarray_copy(__v._M_data, _M_size, _M_data);
00674       return *this;
00675     }
00676 
00677 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00678   template<typename _Tp>
00679     inline valarray<_Tp>&
00680     valarray<_Tp>::operator=(initializer_list<_Tp> __l)
00681     {
00682       _GLIBCXX_DEBUG_ASSERT(_M_size == __l.size());
00683       std::__valarray_copy(__l.begin(), __l.size(), _M_data);
00684     }
00685 #endif
00686 
00687   template<typename _Tp>
00688     inline valarray<_Tp>&
00689     valarray<_Tp>::operator=(const _Tp& __t)
00690     {
00691       std::__valarray_fill(_M_data, _M_size, __t);
00692       return *this;
00693     }
00694 
00695   template<typename _Tp>
00696     inline valarray<_Tp>&
00697     valarray<_Tp>::operator=(const slice_array<_Tp>& __sa)
00698     {
00699       _GLIBCXX_DEBUG_ASSERT(_M_size == __sa._M_sz);
00700       std::__valarray_copy(__sa._M_array, __sa._M_sz,
00701                __sa._M_stride, _Array<_Tp>(_M_data));
00702       return *this;
00703     }
00704 
00705   template<typename _Tp>
00706     inline valarray<_Tp>&
00707     valarray<_Tp>::operator=(const gslice_array<_Tp>& __ga)
00708     {
00709       _GLIBCXX_DEBUG_ASSERT(_M_size == __ga._M_index.size());
00710       std::__valarray_copy(__ga._M_array, _Array<size_t>(__ga._M_index),
00711                _Array<_Tp>(_M_data), _M_size);
00712       return *this;
00713     }
00714 
00715   template<typename _Tp>
00716     inline valarray<_Tp>&
00717     valarray<_Tp>::operator=(const mask_array<_Tp>& __ma)
00718     {
00719       _GLIBCXX_DEBUG_ASSERT(_M_size == __ma._M_sz);
00720       std::__valarray_copy(__ma._M_array, __ma._M_mask,
00721                _Array<_Tp>(_M_data), _M_size);
00722       return *this;
00723     }
00724 
00725   template<typename _Tp>
00726     inline valarray<_Tp>&
00727     valarray<_Tp>::operator=(const indirect_array<_Tp>& __ia)
00728     {
00729       _GLIBCXX_DEBUG_ASSERT(_M_size == __ia._M_sz);
00730       std::__valarray_copy(__ia._M_array, __ia._M_index,
00731                _Array<_Tp>(_M_data), _M_size);
00732       return *this;
00733     }
00734 
00735   template<typename _Tp> template<class _Dom>
00736     inline valarray<_Tp>&
00737     valarray<_Tp>::operator=(const _Expr<_Dom, _Tp>& __e)
00738     {
00739       _GLIBCXX_DEBUG_ASSERT(_M_size == __e.size());
00740       std::__valarray_copy(__e, _M_size, _Array<_Tp>(_M_data));
00741       return *this;
00742     }
00743 
00744   template<typename _Tp>
00745     inline _Expr<_SClos<_ValArray,_Tp>, _Tp>
00746     valarray<_Tp>::operator[](slice __s) const
00747     {
00748       typedef _SClos<_ValArray,_Tp> _Closure;
00749       return _Expr<_Closure, _Tp>(_Closure (_Array<_Tp>(_M_data), __s));
00750     }
00751 
00752   template<typename _Tp>
00753     inline slice_array<_Tp>
00754     valarray<_Tp>::operator[](slice __s)
00755     { return slice_array<_Tp>(_Array<_Tp>(_M_data), __s); }
00756 
00757   template<typename _Tp>
00758     inline _Expr<_GClos<_ValArray,_Tp>, _Tp>
00759     valarray<_Tp>::operator[](const gslice& __gs) const
00760     {
00761       typedef _GClos<_ValArray,_Tp> _Closure;
00762       return _Expr<_Closure, _Tp>
00763     (_Closure(_Array<_Tp>(_M_data), __gs._M_index->_M_index));
00764     }
00765 
00766   template<typename _Tp>
00767     inline gslice_array<_Tp>
00768     valarray<_Tp>::operator[](const gslice& __gs)
00769     {
00770       return gslice_array<_Tp>
00771     (_Array<_Tp>(_M_data), __gs._M_index->_M_index);
00772     }
00773 
00774   template<typename _Tp>
00775     inline valarray<_Tp>
00776     valarray<_Tp>::operator[](const valarray<bool>& __m) const
00777     {
00778       size_t __s = 0;
00779       size_t __e = __m.size();
00780       for (size_t __i=0; __i<__e; ++__i)
00781     if (__m[__i]) ++__s;
00782       return valarray<_Tp>(mask_array<_Tp>(_Array<_Tp>(_M_data), __s,
00783                        _Array<bool> (__m)));
00784     }
00785 
00786   template<typename _Tp>
00787     inline mask_array<_Tp>
00788     valarray<_Tp>::operator[](const valarray<bool>& __m)
00789     {
00790       size_t __s = 0;
00791       size_t __e = __m.size();
00792       for (size_t __i=0; __i<__e; ++__i)
00793     if (__m[__i]) ++__s;
00794       return mask_array<_Tp>(_Array<_Tp>(_M_data), __s, _Array<bool>(__m));
00795     }
00796 
00797   template<typename _Tp>
00798     inline _Expr<_IClos<_ValArray,_Tp>, _Tp>
00799     valarray<_Tp>::operator[](const valarray<size_t>& __i) const
00800     {
00801       typedef _IClos<_ValArray,_Tp> _Closure;
00802       return _Expr<_Closure, _Tp>(_Closure(*this, __i));
00803     }
00804 
00805   template<typename _Tp>
00806     inline indirect_array<_Tp>
00807     valarray<_Tp>::operator[](const valarray<size_t>& __i)
00808     {
00809       return indirect_array<_Tp>(_Array<_Tp>(_M_data), __i.size(),
00810                  _Array<size_t>(__i));
00811     }
00812 
00813   template<class _Tp>
00814     inline size_t 
00815     valarray<_Tp>::size() const
00816     { return _M_size; }
00817 
00818   template<class _Tp>
00819     inline _Tp
00820     valarray<_Tp>::sum() const
00821     {
00822       _GLIBCXX_DEBUG_ASSERT(_M_size > 0);
00823       return std::__valarray_sum(_M_data, _M_data + _M_size);
00824     }
00825 
00826   template<class _Tp>
00827      inline valarray<_Tp>
00828      valarray<_Tp>::shift(int __n) const
00829      {
00830        valarray<_Tp> __ret;
00831 
00832        if (_M_size == 0)
00833      return __ret;
00834 
00835        _Tp* __restrict__ __tmp_M_data =
00836      std::__valarray_get_storage<_Tp>(_M_size);
00837 
00838        if (__n == 0)
00839      std::__valarray_copy_construct(_M_data,
00840                     _M_data + _M_size, __tmp_M_data);
00841        else if (__n > 0)      // shift left
00842      {
00843        if (size_t(__n) > _M_size)
00844          __n = int(_M_size);
00845 
00846        std::__valarray_copy_construct(_M_data + __n,
00847                       _M_data + _M_size, __tmp_M_data);
00848        std::__valarray_default_construct(__tmp_M_data + _M_size - __n,
00849                          __tmp_M_data + _M_size);
00850      }
00851        else                   // shift right
00852      {
00853        if (-size_t(__n) > _M_size)
00854          __n = -int(_M_size);
00855 
00856        std::__valarray_copy_construct(_M_data, _M_data + _M_size + __n,
00857                       __tmp_M_data - __n);
00858        std::__valarray_default_construct(__tmp_M_data,
00859                          __tmp_M_data - __n);
00860      }
00861 
00862        __ret._M_size = _M_size;
00863        __ret._M_data = __tmp_M_data;
00864        return __ret;
00865      }
00866 
00867   template<class _Tp>
00868      inline valarray<_Tp>
00869      valarray<_Tp>::cshift(int __n) const
00870      {
00871        valarray<_Tp> __ret;
00872 
00873        if (_M_size == 0)
00874      return __ret;
00875 
00876        _Tp* __restrict__ __tmp_M_data =
00877      std::__valarray_get_storage<_Tp>(_M_size);
00878 
00879        if (__n == 0)
00880      std::__valarray_copy_construct(_M_data,
00881                     _M_data + _M_size, __tmp_M_data);
00882        else if (__n > 0)      // cshift left
00883      {
00884        if (size_t(__n) > _M_size)
00885          __n = int(__n % _M_size);
00886 
00887        std::__valarray_copy_construct(_M_data, _M_data + __n,
00888                       __tmp_M_data + _M_size - __n);
00889        std::__valarray_copy_construct(_M_data + __n, _M_data + _M_size,
00890                       __tmp_M_data);
00891      }
00892        else                   // cshift right
00893      {
00894        if (-size_t(__n) > _M_size)
00895          __n = -int(-size_t(__n) % _M_size);
00896 
00897        std::__valarray_copy_construct(_M_data + _M_size + __n,
00898                       _M_data + _M_size, __tmp_M_data);
00899        std::__valarray_copy_construct(_M_data, _M_data + _M_size + __n,
00900                       __tmp_M_data - __n);
00901      }
00902 
00903        __ret._M_size = _M_size;
00904        __ret._M_data = __tmp_M_data;
00905        return __ret;
00906      }
00907 
00908   template<class _Tp>
00909     inline void
00910     valarray<_Tp>::resize(size_t __n, _Tp __c)
00911     {
00912       // This complication is so to make valarray<valarray<T> > work
00913       // even though it is not required by the standard.  Nobody should
00914       // be saying valarray<valarray<T> > anyway.  See the specs.
00915       std::__valarray_destroy_elements(_M_data, _M_data + _M_size);
00916       if (_M_size != __n)
00917     {
00918       std::__valarray_release_memory(_M_data);
00919       _M_size = __n;
00920       _M_data = __valarray_get_storage<_Tp>(__n);
00921     }
00922       std::__valarray_fill_construct(_M_data, _M_data + __n, __c);
00923     }
00924     
00925   template<typename _Tp>
00926     inline _Tp
00927     valarray<_Tp>::min() const
00928     {
00929       _GLIBCXX_DEBUG_ASSERT(_M_size > 0);
00930       return *std::min_element(_M_data, _M_data + _M_size);
00931     }
00932 
00933   template<typename _Tp>
00934     inline _Tp
00935     valarray<_Tp>::max() const
00936     {
00937       _GLIBCXX_DEBUG_ASSERT(_M_size > 0);
00938       return *std::max_element(_M_data, _M_data + _M_size);
00939     }
00940   
00941   template<class _Tp>
00942     inline _Expr<_ValFunClos<_ValArray, _Tp>, _Tp>
00943     valarray<_Tp>::apply(_Tp func(_Tp)) const
00944     {
00945       typedef _ValFunClos<_ValArray, _Tp> _Closure;
00946       return _Expr<_Closure, _Tp>(_Closure(*this, func));
00947     }
00948 
00949   template<class _Tp>
00950     inline _Expr<_RefFunClos<_ValArray, _Tp>, _Tp>
00951     valarray<_Tp>::apply(_Tp func(const _Tp &)) const
00952     {
00953       typedef _RefFunClos<_ValArray, _Tp> _Closure;
00954       return _Expr<_Closure, _Tp>(_Closure(*this, func));
00955     }
00956 
00957 #define _DEFINE_VALARRAY_UNARY_OPERATOR(_Op, _Name)                     \
00958   template<typename _Tp>                        \
00959     inline typename valarray<_Tp>::template _UnaryOp<_Name>::_Rt        \
00960     valarray<_Tp>::operator _Op() const                 \
00961     {                                   \
00962       typedef _UnClos<_Name, _ValArray, _Tp> _Closure;                  \
00963       typedef typename __fun<_Name, _Tp>::result_type _Rt;              \
00964       return _Expr<_Closure, _Rt>(_Closure(*this));         \
00965     }
00966 
00967     _DEFINE_VALARRAY_UNARY_OPERATOR(+, __unary_plus)
00968     _DEFINE_VALARRAY_UNARY_OPERATOR(-, __negate)
00969     _DEFINE_VALARRAY_UNARY_OPERATOR(~, __bitwise_not)
00970     _DEFINE_VALARRAY_UNARY_OPERATOR (!, __logical_not)
00971 
00972 #undef _DEFINE_VALARRAY_UNARY_OPERATOR
00973 
00974 #define _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(_Op, _Name)               \
00975   template<class _Tp>                           \
00976     inline valarray<_Tp>&                       \
00977     valarray<_Tp>::operator _Op##=(const _Tp &__t)          \
00978     {                                   \
00979       _Array_augmented_##_Name(_Array<_Tp>(_M_data), _M_size, __t); \
00980       return *this;                         \
00981     }                                   \
00982                                     \
00983   template<class _Tp>                           \
00984     inline valarray<_Tp>&                       \
00985     valarray<_Tp>::operator _Op##=(const valarray<_Tp> &__v)        \
00986     {                                   \
00987       _GLIBCXX_DEBUG_ASSERT(_M_size == __v._M_size);                    \
00988       _Array_augmented_##_Name(_Array<_Tp>(_M_data), _M_size,       \
00989                    _Array<_Tp>(__v._M_data));       \
00990       return *this;                         \
00991     }
00992 
00993 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(+, __plus)
00994 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(-, __minus)
00995 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(*, __multiplies)
00996 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(/, __divides)
00997 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(%, __modulus)
00998 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(^, __bitwise_xor)
00999 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(&, __bitwise_and)
01000 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(|, __bitwise_or)
01001 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(<<, __shift_left)
01002 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(>>, __shift_right)
01003 
01004 #undef _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT
01005 
01006 #define _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(_Op, _Name)          \
01007   template<class _Tp> template<class _Dom>              \
01008     inline valarray<_Tp>&                       \
01009     valarray<_Tp>::operator _Op##=(const _Expr<_Dom, _Tp>& __e)     \
01010     {                                   \
01011       _Array_augmented_##_Name(_Array<_Tp>(_M_data), __e, _M_size); \
01012       return *this;                         \
01013     }
01014 
01015 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(+, __plus)
01016 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(-, __minus)
01017 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(*, __multiplies)
01018 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(/, __divides)
01019 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(%, __modulus)
01020 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(^, __bitwise_xor)
01021 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(&, __bitwise_and)
01022 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(|, __bitwise_or)
01023 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(<<, __shift_left)
01024 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(>>, __shift_right)
01025 
01026 #undef _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT
01027     
01028 
01029 #define _DEFINE_BINARY_OPERATOR(_Op, _Name)             \
01030   template<typename _Tp>                        \
01031     inline _Expr<_BinClos<_Name, _ValArray, _ValArray, _Tp, _Tp>,       \
01032                  typename __fun<_Name, _Tp>::result_type>               \
01033     operator _Op(const valarray<_Tp>& __v, const valarray<_Tp>& __w)    \
01034     {                                   \
01035       _GLIBCXX_DEBUG_ASSERT(__v.size() == __w.size());                  \
01036       typedef _BinClos<_Name, _ValArray, _ValArray, _Tp, _Tp> _Closure; \
01037       typedef typename __fun<_Name, _Tp>::result_type _Rt;              \
01038       return _Expr<_Closure, _Rt>(_Closure(__v, __w));                  \
01039     }                                   \
01040                                     \
01041   template<typename _Tp>                        \
01042     inline _Expr<_BinClos<_Name, _ValArray,_Constant, _Tp, _Tp>,        \
01043                  typename __fun<_Name, _Tp>::result_type>               \
01044     operator _Op(const valarray<_Tp>& __v, const _Tp& __t)      \
01045     {                                   \
01046       typedef _BinClos<_Name, _ValArray, _Constant, _Tp, _Tp> _Closure; \
01047       typedef typename __fun<_Name, _Tp>::result_type _Rt;              \
01048       return _Expr<_Closure, _Rt>(_Closure(__v, __t));                  \
01049     }                                   \
01050                                     \
01051   template<typename _Tp>                        \
01052     inline _Expr<_BinClos<_Name, _Constant, _ValArray, _Tp, _Tp>,       \
01053                  typename __fun<_Name, _Tp>::result_type>               \
01054     operator _Op(const _Tp& __t, const valarray<_Tp>& __v)      \
01055     {                                   \
01056       typedef _BinClos<_Name, _Constant, _ValArray, _Tp, _Tp> _Closure; \
01057       typedef typename __fun<_Name, _Tp>::result_type _Rt;              \
01058       return _Expr<_Closure, _Rt>(_Closure(__t, __v));                  \
01059     }
01060 
01061 _DEFINE_BINARY_OPERATOR(+, __plus)
01062 _DEFINE_BINARY_OPERATOR(-, __minus)
01063 _DEFINE_BINARY_OPERATOR(*, __multiplies)
01064 _DEFINE_BINARY_OPERATOR(/, __divides)
01065 _DEFINE_BINARY_OPERATOR(%, __modulus)
01066 _DEFINE_BINARY_OPERATOR(^, __bitwise_xor)
01067 _DEFINE_BINARY_OPERATOR(&, __bitwise_and)
01068 _DEFINE_BINARY_OPERATOR(|, __bitwise_or)
01069 _DEFINE_BINARY_OPERATOR(<<, __shift_left)
01070 _DEFINE_BINARY_OPERATOR(>>, __shift_right)
01071 _DEFINE_BINARY_OPERATOR(&&, __logical_and)
01072 _DEFINE_BINARY_OPERATOR(||, __logical_or)
01073 _DEFINE_BINARY_OPERATOR(==, __equal_to)
01074 _DEFINE_BINARY_OPERATOR(!=, __not_equal_to)
01075 _DEFINE_BINARY_OPERATOR(<, __less)
01076 _DEFINE_BINARY_OPERATOR(>, __greater)
01077 _DEFINE_BINARY_OPERATOR(<=, __less_equal)
01078 _DEFINE_BINARY_OPERATOR(>=, __greater_equal)
01079 
01080 #undef _DEFINE_BINARY_OPERATOR
01081 
01082   // @} group numeric_arrays
01083 
01084 _GLIBCXX_END_NAMESPACE
01085 
01086 #endif /* _GLIBCXX_VALARRAY */

Generated on Tue Apr 21 13:13:37 2009 for libstdc++ by  doxygen 1.5.8