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, 2008, 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_RESOLVE_LIB_DEFECTS
00673       // 630. arrays of valarray.
00674       if (_M_size == __v._M_size)
00675     std::__valarray_copy(__v._M_data, _M_size, _M_data);
00676       else
00677     {
00678       if (_M_data)
00679         {
00680           std::__valarray_destroy_elements(_M_data, _M_data + _M_size);
00681           std::__valarray_release_memory(_M_data);
00682         }
00683       _M_size = __v._M_size;
00684       _M_data = __valarray_get_storage<_Tp>(_M_size);
00685       std::__valarray_copy_construct(__v._M_data, __v._M_data + _M_size,
00686                      _M_data);
00687     }
00688       return *this;
00689     }
00690 
00691 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00692   template<typename _Tp>
00693     inline valarray<_Tp>&
00694     valarray<_Tp>::operator=(initializer_list<_Tp> __l)
00695     {
00696       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00697       // 630. arrays of valarray.
00698       if (_M_size == __l.size())
00699     std::__valarray_copy(__l.begin(), __l.size(), _M_data);
00700       else
00701     {
00702       if (_M_data)
00703         {
00704           std::__valarray_destroy_elements(_M_data, _M_data + _M_size);
00705           std::__valarray_release_memory(_M_data);
00706         }
00707       _M_size = __l.size();
00708       _M_data = __valarray_get_storage<_Tp>(_M_size);
00709       std::__valarray_copy_construct(__l.begin(), __l.begin() + _M_size,
00710                      _M_data);
00711     }
00712       return *this;
00713     }
00714 #endif
00715 
00716   template<typename _Tp>
00717     inline valarray<_Tp>&
00718     valarray<_Tp>::operator=(const _Tp& __t)
00719     {
00720       std::__valarray_fill(_M_data, _M_size, __t);
00721       return *this;
00722     }
00723 
00724   template<typename _Tp>
00725     inline valarray<_Tp>&
00726     valarray<_Tp>::operator=(const slice_array<_Tp>& __sa)
00727     {
00728       _GLIBCXX_DEBUG_ASSERT(_M_size == __sa._M_sz);
00729       std::__valarray_copy(__sa._M_array, __sa._M_sz,
00730                __sa._M_stride, _Array<_Tp>(_M_data));
00731       return *this;
00732     }
00733 
00734   template<typename _Tp>
00735     inline valarray<_Tp>&
00736     valarray<_Tp>::operator=(const gslice_array<_Tp>& __ga)
00737     {
00738       _GLIBCXX_DEBUG_ASSERT(_M_size == __ga._M_index.size());
00739       std::__valarray_copy(__ga._M_array, _Array<size_t>(__ga._M_index),
00740                _Array<_Tp>(_M_data), _M_size);
00741       return *this;
00742     }
00743 
00744   template<typename _Tp>
00745     inline valarray<_Tp>&
00746     valarray<_Tp>::operator=(const mask_array<_Tp>& __ma)
00747     {
00748       _GLIBCXX_DEBUG_ASSERT(_M_size == __ma._M_sz);
00749       std::__valarray_copy(__ma._M_array, __ma._M_mask,
00750                _Array<_Tp>(_M_data), _M_size);
00751       return *this;
00752     }
00753 
00754   template<typename _Tp>
00755     inline valarray<_Tp>&
00756     valarray<_Tp>::operator=(const indirect_array<_Tp>& __ia)
00757     {
00758       _GLIBCXX_DEBUG_ASSERT(_M_size == __ia._M_sz);
00759       std::__valarray_copy(__ia._M_array, __ia._M_index,
00760                _Array<_Tp>(_M_data), _M_size);
00761       return *this;
00762     }
00763 
00764   template<typename _Tp> template<class _Dom>
00765     inline valarray<_Tp>&
00766     valarray<_Tp>::operator=(const _Expr<_Dom, _Tp>& __e)
00767     {
00768       _GLIBCXX_DEBUG_ASSERT(_M_size == __e.size());
00769       std::__valarray_copy(__e, _M_size, _Array<_Tp>(_M_data));
00770       return *this;
00771     }
00772 
00773   template<typename _Tp>
00774     inline _Expr<_SClos<_ValArray,_Tp>, _Tp>
00775     valarray<_Tp>::operator[](slice __s) const
00776     {
00777       typedef _SClos<_ValArray,_Tp> _Closure;
00778       return _Expr<_Closure, _Tp>(_Closure (_Array<_Tp>(_M_data), __s));
00779     }
00780 
00781   template<typename _Tp>
00782     inline slice_array<_Tp>
00783     valarray<_Tp>::operator[](slice __s)
00784     { return slice_array<_Tp>(_Array<_Tp>(_M_data), __s); }
00785 
00786   template<typename _Tp>
00787     inline _Expr<_GClos<_ValArray,_Tp>, _Tp>
00788     valarray<_Tp>::operator[](const gslice& __gs) const
00789     {
00790       typedef _GClos<_ValArray,_Tp> _Closure;
00791       return _Expr<_Closure, _Tp>
00792     (_Closure(_Array<_Tp>(_M_data), __gs._M_index->_M_index));
00793     }
00794 
00795   template<typename _Tp>
00796     inline gslice_array<_Tp>
00797     valarray<_Tp>::operator[](const gslice& __gs)
00798     {
00799       return gslice_array<_Tp>
00800     (_Array<_Tp>(_M_data), __gs._M_index->_M_index);
00801     }
00802 
00803   template<typename _Tp>
00804     inline valarray<_Tp>
00805     valarray<_Tp>::operator[](const valarray<bool>& __m) const
00806     {
00807       size_t __s = 0;
00808       size_t __e = __m.size();
00809       for (size_t __i=0; __i<__e; ++__i)
00810     if (__m[__i]) ++__s;
00811       return valarray<_Tp>(mask_array<_Tp>(_Array<_Tp>(_M_data), __s,
00812                        _Array<bool> (__m)));
00813     }
00814 
00815   template<typename _Tp>
00816     inline mask_array<_Tp>
00817     valarray<_Tp>::operator[](const valarray<bool>& __m)
00818     {
00819       size_t __s = 0;
00820       size_t __e = __m.size();
00821       for (size_t __i=0; __i<__e; ++__i)
00822     if (__m[__i]) ++__s;
00823       return mask_array<_Tp>(_Array<_Tp>(_M_data), __s, _Array<bool>(__m));
00824     }
00825 
00826   template<typename _Tp>
00827     inline _Expr<_IClos<_ValArray,_Tp>, _Tp>
00828     valarray<_Tp>::operator[](const valarray<size_t>& __i) const
00829     {
00830       typedef _IClos<_ValArray,_Tp> _Closure;
00831       return _Expr<_Closure, _Tp>(_Closure(*this, __i));
00832     }
00833 
00834   template<typename _Tp>
00835     inline indirect_array<_Tp>
00836     valarray<_Tp>::operator[](const valarray<size_t>& __i)
00837     {
00838       return indirect_array<_Tp>(_Array<_Tp>(_M_data), __i.size(),
00839                  _Array<size_t>(__i));
00840     }
00841 
00842   template<class _Tp>
00843     inline size_t 
00844     valarray<_Tp>::size() const
00845     { return _M_size; }
00846 
00847   template<class _Tp>
00848     inline _Tp
00849     valarray<_Tp>::sum() const
00850     {
00851       _GLIBCXX_DEBUG_ASSERT(_M_size > 0);
00852       return std::__valarray_sum(_M_data, _M_data + _M_size);
00853     }
00854 
00855   template<class _Tp>
00856      inline valarray<_Tp>
00857      valarray<_Tp>::shift(int __n) const
00858      {
00859        valarray<_Tp> __ret;
00860 
00861        if (_M_size == 0)
00862      return __ret;
00863 
00864        _Tp* __restrict__ __tmp_M_data =
00865      std::__valarray_get_storage<_Tp>(_M_size);
00866 
00867        if (__n == 0)
00868      std::__valarray_copy_construct(_M_data,
00869                     _M_data + _M_size, __tmp_M_data);
00870        else if (__n > 0)      // shift left
00871      {
00872        if (size_t(__n) > _M_size)
00873          __n = int(_M_size);
00874 
00875        std::__valarray_copy_construct(_M_data + __n,
00876                       _M_data + _M_size, __tmp_M_data);
00877        std::__valarray_default_construct(__tmp_M_data + _M_size - __n,
00878                          __tmp_M_data + _M_size);
00879      }
00880        else                   // shift right
00881      {
00882        if (-size_t(__n) > _M_size)
00883          __n = -int(_M_size);
00884 
00885        std::__valarray_copy_construct(_M_data, _M_data + _M_size + __n,
00886                       __tmp_M_data - __n);
00887        std::__valarray_default_construct(__tmp_M_data,
00888                          __tmp_M_data - __n);
00889      }
00890 
00891        __ret._M_size = _M_size;
00892        __ret._M_data = __tmp_M_data;
00893        return __ret;
00894      }
00895 
00896   template<class _Tp>
00897      inline valarray<_Tp>
00898      valarray<_Tp>::cshift(int __n) const
00899      {
00900        valarray<_Tp> __ret;
00901 
00902        if (_M_size == 0)
00903      return __ret;
00904 
00905        _Tp* __restrict__ __tmp_M_data =
00906      std::__valarray_get_storage<_Tp>(_M_size);
00907 
00908        if (__n == 0)
00909      std::__valarray_copy_construct(_M_data,
00910                     _M_data + _M_size, __tmp_M_data);
00911        else if (__n > 0)      // cshift left
00912      {
00913        if (size_t(__n) > _M_size)
00914          __n = int(__n % _M_size);
00915 
00916        std::__valarray_copy_construct(_M_data, _M_data + __n,
00917                       __tmp_M_data + _M_size - __n);
00918        std::__valarray_copy_construct(_M_data + __n, _M_data + _M_size,
00919                       __tmp_M_data);
00920      }
00921        else                   // cshift right
00922      {
00923        if (-size_t(__n) > _M_size)
00924          __n = -int(-size_t(__n) % _M_size);
00925 
00926        std::__valarray_copy_construct(_M_data + _M_size + __n,
00927                       _M_data + _M_size, __tmp_M_data);
00928        std::__valarray_copy_construct(_M_data, _M_data + _M_size + __n,
00929                       __tmp_M_data - __n);
00930      }
00931 
00932        __ret._M_size = _M_size;
00933        __ret._M_data = __tmp_M_data;
00934        return __ret;
00935      }
00936 
00937   template<class _Tp>
00938     inline void
00939     valarray<_Tp>::resize(size_t __n, _Tp __c)
00940     {
00941       // This complication is so to make valarray<valarray<T> > work
00942       // even though it is not required by the standard.  Nobody should
00943       // be saying valarray<valarray<T> > anyway.  See the specs.
00944       std::__valarray_destroy_elements(_M_data, _M_data + _M_size);
00945       if (_M_size != __n)
00946     {
00947       std::__valarray_release_memory(_M_data);
00948       _M_size = __n;
00949       _M_data = __valarray_get_storage<_Tp>(__n);
00950     }
00951       std::__valarray_fill_construct(_M_data, _M_data + __n, __c);
00952     }
00953     
00954   template<typename _Tp>
00955     inline _Tp
00956     valarray<_Tp>::min() const
00957     {
00958       _GLIBCXX_DEBUG_ASSERT(_M_size > 0);
00959       return *std::min_element(_M_data, _M_data + _M_size);
00960     }
00961 
00962   template<typename _Tp>
00963     inline _Tp
00964     valarray<_Tp>::max() const
00965     {
00966       _GLIBCXX_DEBUG_ASSERT(_M_size > 0);
00967       return *std::max_element(_M_data, _M_data + _M_size);
00968     }
00969   
00970   template<class _Tp>
00971     inline _Expr<_ValFunClos<_ValArray, _Tp>, _Tp>
00972     valarray<_Tp>::apply(_Tp func(_Tp)) const
00973     {
00974       typedef _ValFunClos<_ValArray, _Tp> _Closure;
00975       return _Expr<_Closure, _Tp>(_Closure(*this, func));
00976     }
00977 
00978   template<class _Tp>
00979     inline _Expr<_RefFunClos<_ValArray, _Tp>, _Tp>
00980     valarray<_Tp>::apply(_Tp func(const _Tp &)) const
00981     {
00982       typedef _RefFunClos<_ValArray, _Tp> _Closure;
00983       return _Expr<_Closure, _Tp>(_Closure(*this, func));
00984     }
00985 
00986 #define _DEFINE_VALARRAY_UNARY_OPERATOR(_Op, _Name)                     \
00987   template<typename _Tp>                        \
00988     inline typename valarray<_Tp>::template _UnaryOp<_Name>::_Rt        \
00989     valarray<_Tp>::operator _Op() const                 \
00990     {                                   \
00991       typedef _UnClos<_Name, _ValArray, _Tp> _Closure;                  \
00992       typedef typename __fun<_Name, _Tp>::result_type _Rt;              \
00993       return _Expr<_Closure, _Rt>(_Closure(*this));         \
00994     }
00995 
00996     _DEFINE_VALARRAY_UNARY_OPERATOR(+, __unary_plus)
00997     _DEFINE_VALARRAY_UNARY_OPERATOR(-, __negate)
00998     _DEFINE_VALARRAY_UNARY_OPERATOR(~, __bitwise_not)
00999     _DEFINE_VALARRAY_UNARY_OPERATOR (!, __logical_not)
01000 
01001 #undef _DEFINE_VALARRAY_UNARY_OPERATOR
01002 
01003 #define _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(_Op, _Name)               \
01004   template<class _Tp>                           \
01005     inline valarray<_Tp>&                       \
01006     valarray<_Tp>::operator _Op##=(const _Tp &__t)          \
01007     {                                   \
01008       _Array_augmented_##_Name(_Array<_Tp>(_M_data), _M_size, __t); \
01009       return *this;                         \
01010     }                                   \
01011                                     \
01012   template<class _Tp>                           \
01013     inline valarray<_Tp>&                       \
01014     valarray<_Tp>::operator _Op##=(const valarray<_Tp> &__v)        \
01015     {                                   \
01016       _GLIBCXX_DEBUG_ASSERT(_M_size == __v._M_size);                    \
01017       _Array_augmented_##_Name(_Array<_Tp>(_M_data), _M_size,       \
01018                    _Array<_Tp>(__v._M_data));       \
01019       return *this;                         \
01020     }
01021 
01022 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(+, __plus)
01023 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(-, __minus)
01024 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(*, __multiplies)
01025 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(/, __divides)
01026 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(%, __modulus)
01027 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(^, __bitwise_xor)
01028 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(&, __bitwise_and)
01029 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(|, __bitwise_or)
01030 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(<<, __shift_left)
01031 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(>>, __shift_right)
01032 
01033 #undef _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT
01034 
01035 #define _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(_Op, _Name)          \
01036   template<class _Tp> template<class _Dom>              \
01037     inline valarray<_Tp>&                       \
01038     valarray<_Tp>::operator _Op##=(const _Expr<_Dom, _Tp>& __e)     \
01039     {                                   \
01040       _Array_augmented_##_Name(_Array<_Tp>(_M_data), __e, _M_size); \
01041       return *this;                         \
01042     }
01043 
01044 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(+, __plus)
01045 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(-, __minus)
01046 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(*, __multiplies)
01047 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(/, __divides)
01048 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(%, __modulus)
01049 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(^, __bitwise_xor)
01050 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(&, __bitwise_and)
01051 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(|, __bitwise_or)
01052 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(<<, __shift_left)
01053 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(>>, __shift_right)
01054 
01055 #undef _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT
01056     
01057 
01058 #define _DEFINE_BINARY_OPERATOR(_Op, _Name)             \
01059   template<typename _Tp>                        \
01060     inline _Expr<_BinClos<_Name, _ValArray, _ValArray, _Tp, _Tp>,       \
01061                  typename __fun<_Name, _Tp>::result_type>               \
01062     operator _Op(const valarray<_Tp>& __v, const valarray<_Tp>& __w)    \
01063     {                                   \
01064       _GLIBCXX_DEBUG_ASSERT(__v.size() == __w.size());                  \
01065       typedef _BinClos<_Name, _ValArray, _ValArray, _Tp, _Tp> _Closure; \
01066       typedef typename __fun<_Name, _Tp>::result_type _Rt;              \
01067       return _Expr<_Closure, _Rt>(_Closure(__v, __w));                  \
01068     }                                   \
01069                                     \
01070   template<typename _Tp>                        \
01071     inline _Expr<_BinClos<_Name, _ValArray,_Constant, _Tp, _Tp>,        \
01072                  typename __fun<_Name, _Tp>::result_type>               \
01073     operator _Op(const valarray<_Tp>& __v, const _Tp& __t)      \
01074     {                                   \
01075       typedef _BinClos<_Name, _ValArray, _Constant, _Tp, _Tp> _Closure; \
01076       typedef typename __fun<_Name, _Tp>::result_type _Rt;              \
01077       return _Expr<_Closure, _Rt>(_Closure(__v, __t));                  \
01078     }                                   \
01079                                     \
01080   template<typename _Tp>                        \
01081     inline _Expr<_BinClos<_Name, _Constant, _ValArray, _Tp, _Tp>,       \
01082                  typename __fun<_Name, _Tp>::result_type>               \
01083     operator _Op(const _Tp& __t, const valarray<_Tp>& __v)      \
01084     {                                   \
01085       typedef _BinClos<_Name, _Constant, _ValArray, _Tp, _Tp> _Closure; \
01086       typedef typename __fun<_Name, _Tp>::result_type _Rt;              \
01087       return _Expr<_Closure, _Rt>(_Closure(__t, __v));                  \
01088     }
01089 
01090 _DEFINE_BINARY_OPERATOR(+, __plus)
01091 _DEFINE_BINARY_OPERATOR(-, __minus)
01092 _DEFINE_BINARY_OPERATOR(*, __multiplies)
01093 _DEFINE_BINARY_OPERATOR(/, __divides)
01094 _DEFINE_BINARY_OPERATOR(%, __modulus)
01095 _DEFINE_BINARY_OPERATOR(^, __bitwise_xor)
01096 _DEFINE_BINARY_OPERATOR(&, __bitwise_and)
01097 _DEFINE_BINARY_OPERATOR(|, __bitwise_or)
01098 _DEFINE_BINARY_OPERATOR(<<, __shift_left)
01099 _DEFINE_BINARY_OPERATOR(>>, __shift_right)
01100 _DEFINE_BINARY_OPERATOR(&&, __logical_and)
01101 _DEFINE_BINARY_OPERATOR(||, __logical_or)
01102 _DEFINE_BINARY_OPERATOR(==, __equal_to)
01103 _DEFINE_BINARY_OPERATOR(!=, __not_equal_to)
01104 _DEFINE_BINARY_OPERATOR(<, __less)
01105 _DEFINE_BINARY_OPERATOR(>, __greater)
01106 _DEFINE_BINARY_OPERATOR(<=, __less_equal)
01107 _DEFINE_BINARY_OPERATOR(>=, __greater_equal)
01108 
01109 #undef _DEFINE_BINARY_OPERATOR
01110 
01111   // @} group numeric_arrays
01112 
01113 _GLIBCXX_END_NAMESPACE
01114 
01115 #endif /* _GLIBCXX_VALARRAY */