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