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

Generated on 19 Jun 2018 for libstdc++ by  doxygen 1.6.1