# valarray

Go to the documentation of this file.

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