Merged revisions 143552,143554,143557,143560,143562,143564-143567,143570-143573,14357...
[official-gcc.git] / libstdc++-v3 / include / std / valarray
blobbfeae8e78096c9c2501db45e887c52db27d9fb6e
1 // The template and inlines for the -*- C++ -*- valarray class.
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007, 2009
5 // Free Software Foundation, Inc.
6 //
7 // This file is part of the GNU ISO C++ Library.  This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 2, or (at your option)
11 // any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this library; see the file COPYING.  If not, write to
20 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
21 // Boston, MA 02110-1301, USA.
23 // As a special exception, you may use this file as part of a free software
24 // library without restriction.  Specifically, if other files instantiate
25 // templates or use macros or inline functions from this file, or you compile
26 // this file and link it with other files to produce an executable, this
27 // file does not by itself cause the resulting executable to be covered by
28 // the GNU General Public License.  This exception does not however
29 // invalidate any other reasons why the executable file might be covered by
30 // the GNU General Public License.
32 /** @file valarray
33  *  This is a Standard C++ Library header. 
34  */
36 // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr>
38 #ifndef _GLIBCXX_VALARRAY
39 #define _GLIBCXX_VALARRAY 1
41 #pragma GCC system_header
43 #include <bits/c++config.h>
44 #include <cstddef>
45 #include <cmath>
46 #include <algorithm>
47 #include <debug/debug.h>
48 #include <initializer_list>
50 _GLIBCXX_BEGIN_NAMESPACE(std)
52   template<class _Clos, typename _Tp> 
53     class _Expr;
55   template<typename _Tp1, typename _Tp2> 
56     class _ValArray;    
58   template<class _Oper, template<class, class> class _Meta, class _Dom>
59     struct _UnClos;
61   template<class _Oper,
62         template<class, class> class _Meta1,
63         template<class, class> class _Meta2,
64         class _Dom1, class _Dom2> 
65     class _BinClos;
67   template<template<class, class> class _Meta, class _Dom> 
68     class _SClos;
70   template<template<class, class> class _Meta, class _Dom> 
71     class _GClos;
72     
73   template<template<class, class> class _Meta, class _Dom> 
74     class _IClos;
75     
76   template<template<class, class> class _Meta, class _Dom> 
77     class _ValFunClos;
78   
79   template<template<class, class> class _Meta, class _Dom> 
80     class _RefFunClos;
82   template<class _Tp> class valarray;   // An array of type _Tp
83   class slice;                          // BLAS-like slice out of an array
84   template<class _Tp> class slice_array;
85   class gslice;                         // generalized slice out of an array
86   template<class _Tp> class gslice_array;
87   template<class _Tp> class mask_array;     // masked array
88   template<class _Tp> class indirect_array; // indirected array
90 _GLIBCXX_END_NAMESPACE
92 #include <bits/valarray_array.h>
93 #include <bits/valarray_before.h>
94   
95 _GLIBCXX_BEGIN_NAMESPACE(std)
97   /**
98    * @defgroup numeric_arrays Numeric Arrays
99    * @ingroup numerics
100    *
101    * Classes and functions for representing and manipulating arrays of elements.
102    * @{
103    */
105   /**
106    *  @brief  Smart array designed to support numeric processing.
107    *
108    *  A valarray is an array that provides constraints intended to allow for
109    *  effective optimization of numeric array processing by reducing the
110    *  aliasing that can result from pointer representations.  It represents a
111    *  one-dimensional array from which different multidimensional subsets can
112    *  be accessed and modified.
113    *  
114    *  @param  Tp  Type of object in the array.
115    */
116   template<class _Tp> 
117     class valarray
118     {
119       template<class _Op>
120         struct _UnaryOp 
121         {
122           typedef typename __fun<_Op, _Tp>::result_type __rt;
123           typedef _Expr<_UnClos<_Op, _ValArray, _Tp>, __rt> _Rt;
124         };
125     public:
126       typedef _Tp value_type;
127       
128         // _lib.valarray.cons_ construct/destroy:
129       ///  Construct an empty array.
130       valarray();
132       ///  Construct an array with @a n elements.
133       explicit valarray(size_t);
135       ///  Construct an array with @a n elements initialized to @a t.
136       valarray(const _Tp&, size_t);
138       ///  Construct an array initialized to the first @a n elements of @a t.
139       valarray(const _Tp* __restrict__, size_t);
141       ///  Copy constructor.
142       valarray(const valarray&);
144       ///  Construct an array with the same size and values in @a sa.
145       valarray(const slice_array<_Tp>&);
147       ///  Construct an array with the same size and values in @a ga.
148       valarray(const gslice_array<_Tp>&);
150       ///  Construct an array with the same size and values in @a ma.
151       valarray(const mask_array<_Tp>&);
153       ///  Construct an array with the same size and values in @a ia.
154       valarray(const indirect_array<_Tp>&);
156 #ifdef __GXX_EXPERIMENTAL_CXX0X__
157       ///  Construct an array with an initializer_list of values.
158       valarray(initializer_list<_Tp>);
159 #endif
161       template<class _Dom>
162         valarray(const _Expr<_Dom, _Tp>& __e);
164       ~valarray();
166       // _lib.valarray.assign_ assignment:
167       /**
168        *  @brief  Assign elements to an array.
169        *
170        *  Assign elements of array to values in @a v.  Results are undefined
171        *  if @a v does not have the same size as this array.
172        *
173        *  @param  v  Valarray to get values from.
174        */
175       valarray<_Tp>& operator=(const valarray<_Tp>&);
177       /**
178        *  @brief  Assign elements to a value.
179        *
180        *  Assign all elements of array to @a t.
181        *
182        *  @param  t  Value for elements.
183        */
184       valarray<_Tp>& operator=(const _Tp&);
186       /**
187        *  @brief  Assign elements to an array subset.
188        *
189        *  Assign elements of array to values in @a sa.  Results are undefined
190        *  if @a sa does not have the same size as this array.
191        *
192        *  @param  sa  Array slice to get values from.
193        */
194       valarray<_Tp>& operator=(const slice_array<_Tp>&);
196       /**
197        *  @brief  Assign elements to an array subset.
198        *
199        *  Assign elements of array to values in @a ga.  Results are undefined
200        *  if @a ga does not have the same size as this array.
201        *
202        *  @param  ga  Array slice to get values from.
203        */
204       valarray<_Tp>& operator=(const gslice_array<_Tp>&);
206       /**
207        *  @brief  Assign elements to an array subset.
208        *
209        *  Assign elements of array to values in @a ma.  Results are undefined
210        *  if @a ma does not have the same size as this array.
211        *
212        *  @param  ma  Array slice to get values from.
213        */
214       valarray<_Tp>& operator=(const mask_array<_Tp>&);
216       /**
217        *  @brief  Assign elements to an array subset.
218        *
219        *  Assign elements of array to values in @a ia.  Results are undefined
220        *  if @a ia does not have the same size as this array.
221        *
222        *  @param  ia  Array slice to get values from.
223        */
224       valarray<_Tp>& operator=(const indirect_array<_Tp>&);
226 #ifdef __GXX_EXPERIMENTAL_CXX0X__
227       /**
228        *  @brief  Assign elements to an initializer_list.
229        *
230        *  Assign elements of array to values in @a l.  Results are undefined
231        *  if @a l does not have the same size as this array.
232        *
233        *  @param  l  initializer_list to get values from.
234        */
235       valarray& operator=(initializer_list<_Tp>);
236 #endif
238       template<class _Dom> valarray<_Tp>&
239         operator= (const _Expr<_Dom, _Tp>&);
241       // _lib.valarray.access_ element access:
242       /**
243        *  Return a reference to the i'th array element.  
244        *
245        *  @param  i  Index of element to return.
246        *  @return  Reference to the i'th element.
247        */
248       _Tp&                operator[](size_t);
250       // _GLIBCXX_RESOLVE_LIB_DEFECTS
251       // 389. Const overload of valarray::operator[] returns by value.
252       const _Tp&          operator[](size_t) const;
254       // _lib.valarray.sub_ subset operations:
255       /**
256        *  @brief  Return an array subset.
257        *
258        *  Returns a new valarray containing the elements of the array
259        *  indicated by the slice argument.  The new valarray has the same size
260        *  as the input slice.  @see slice.
261        *
262        *  @param  s  The source slice.
263        *  @return  New valarray containing elements in @a s.
264        */
265       _Expr<_SClos<_ValArray, _Tp>, _Tp> operator[](slice) const;
267       /**
268        *  @brief  Return a reference to an array subset.
269        *
270        *  Returns a new valarray containing the elements of the array
271        *  indicated by the slice argument.  The new valarray has the same size
272        *  as the input slice.  @see slice.
273        *
274        *  @param  s  The source slice.
275        *  @return  New valarray containing elements in @a s.
276        */
277       slice_array<_Tp>    operator[](slice);
279       /**
280        *  @brief  Return an array subset.
281        *
282        *  Returns a slice_array referencing the elements of the array
283        *  indicated by the slice argument.  @see gslice.
284        *
285        *  @param  s  The source slice.
286        *  @return  Slice_array referencing elements indicated by @a s.
287        */
288       _Expr<_GClos<_ValArray, _Tp>, _Tp> operator[](const gslice&) const;
290       /**
291        *  @brief  Return a reference to an array subset.
292        *
293        *  Returns a new valarray containing the elements of the array
294        *  indicated by the gslice argument.  The new valarray has
295        *  the same size as the input gslice.  @see gslice.
296        *
297        *  @param  s  The source gslice.
298        *  @return  New valarray containing elements in @a s.
299        */
300       gslice_array<_Tp>   operator[](const gslice&);
302       /**
303        *  @brief  Return an array subset.
304        *
305        *  Returns a new valarray containing the elements of the array
306        *  indicated by the argument.  The input is a valarray of bool which
307        *  represents a bitmask indicating which elements should be copied into
308        *  the new valarray.  Each element of the array is added to the return
309        *  valarray if the corresponding element of the argument is true.
310        *
311        *  @param  m  The valarray bitmask.
312        *  @return  New valarray containing elements indicated by @a m.
313        */
314       valarray<_Tp>       operator[](const valarray<bool>&) const;
316       /**
317        *  @brief  Return a reference to an array subset.
318        *
319        *  Returns a new mask_array referencing the elements of the array
320        *  indicated by the argument.  The input is a valarray of bool which
321        *  represents a bitmask indicating which elements are part of the
322        *  subset.  Elements of the array are part of the subset if the
323        *  corresponding element of the argument is true.
324        *
325        *  @param  m  The valarray bitmask.
326        *  @return  New valarray containing elements indicated by @a m.
327        */
328       mask_array<_Tp>     operator[](const valarray<bool>&);
330       /**
331        *  @brief  Return an array subset.
332        *
333        *  Returns a new valarray containing the elements of the array
334        *  indicated by the argument.  The elements in the argument are
335        *  interpreted as the indices of elements of this valarray to copy to
336        *  the return valarray.
337        *
338        *  @param  i  The valarray element index list.
339        *  @return  New valarray containing elements in @a s.
340        */
341       _Expr<_IClos<_ValArray, _Tp>, _Tp>
342         operator[](const valarray<size_t>&) const;
344       /**
345        *  @brief  Return a reference to an array subset.
346        *
347        *  Returns an indirect_array referencing the elements of the array
348        *  indicated by the argument.  The elements in the argument are
349        *  interpreted as the indices of elements of this valarray to include
350        *  in the subset.  The returned indirect_array refers to these
351        *  elements.
352        *
353        *  @param  i  The valarray element index list.
354        *  @return  Indirect_array referencing elements in @a i.
355        */
356       indirect_array<_Tp> operator[](const valarray<size_t>&);
358       // _lib.valarray.unary_ unary operators:
359       ///  Return a new valarray by applying unary + to each element.
360       typename _UnaryOp<__unary_plus>::_Rt  operator+() const;
362       ///  Return a new valarray by applying unary - to each element.
363       typename _UnaryOp<__negate>::_Rt      operator-() const;
365       ///  Return a new valarray by applying unary ~ to each element.
366       typename _UnaryOp<__bitwise_not>::_Rt operator~() const;
368       ///  Return a new valarray by applying unary ! to each element.
369       typename _UnaryOp<__logical_not>::_Rt operator!() const;
371       // _lib.valarray.cassign_ computed assignment:
372       ///  Multiply each element of array by @a t.
373       valarray<_Tp>& operator*=(const _Tp&);
375       ///  Divide each element of array by @a t.
376       valarray<_Tp>& operator/=(const _Tp&);
378       ///  Set each element e of array to e % @a t.
379       valarray<_Tp>& operator%=(const _Tp&);
381       ///  Add @a t to each element of array.
382       valarray<_Tp>& operator+=(const _Tp&);
384       ///  Subtract @a t to each element of array.
385       valarray<_Tp>& operator-=(const _Tp&);
387       ///  Set each element e of array to e ^ @a t.
388       valarray<_Tp>& operator^=(const _Tp&);
390       ///  Set each element e of array to e & @a t.
391       valarray<_Tp>& operator&=(const _Tp&);
393       ///  Set each element e of array to e | @a t.
394       valarray<_Tp>& operator|=(const _Tp&);
396       ///  Left shift each element e of array by @a t bits.
397       valarray<_Tp>& operator<<=(const _Tp&);
399       ///  Right shift each element e of array by @a t bits.
400       valarray<_Tp>& operator>>=(const _Tp&);
402       ///  Multiply elements of array by corresponding elements of @a v.
403       valarray<_Tp>& operator*=(const valarray<_Tp>&);
405       ///  Divide elements of array by corresponding elements of @a v.
406       valarray<_Tp>& operator/=(const valarray<_Tp>&);
408       ///  Modulo elements of array by corresponding elements of @a v.
409       valarray<_Tp>& operator%=(const valarray<_Tp>&);
411       ///  Add corresponding elements of @a v to elements of array.
412       valarray<_Tp>& operator+=(const valarray<_Tp>&);
414       ///  Subtract corresponding elements of @a v from elements of array.
415       valarray<_Tp>& operator-=(const valarray<_Tp>&);
417       ///  Logical xor corresponding elements of @a v with elements of array.
418       valarray<_Tp>& operator^=(const valarray<_Tp>&);
420       ///  Logical or corresponding elements of @a v with elements of array.
421       valarray<_Tp>& operator|=(const valarray<_Tp>&);
423       ///  Logical and corresponding elements of @a v with elements of array.
424       valarray<_Tp>& operator&=(const valarray<_Tp>&);
426       ///  Left shift elements of array by corresponding elements of @a v.
427       valarray<_Tp>& operator<<=(const valarray<_Tp>&);
429       ///  Right shift elements of array by corresponding elements of @a v.
430       valarray<_Tp>& operator>>=(const valarray<_Tp>&);
432       template<class _Dom>
433         valarray<_Tp>& operator*=(const _Expr<_Dom, _Tp>&);
434       template<class _Dom>
435         valarray<_Tp>& operator/=(const _Expr<_Dom, _Tp>&);
436       template<class _Dom>
437         valarray<_Tp>& operator%=(const _Expr<_Dom, _Tp>&);
438       template<class _Dom>
439         valarray<_Tp>& operator+=(const _Expr<_Dom, _Tp>&);
440       template<class _Dom>
441         valarray<_Tp>& operator-=(const _Expr<_Dom, _Tp>&);
442       template<class _Dom>
443         valarray<_Tp>& operator^=(const _Expr<_Dom, _Tp>&);
444       template<class _Dom>
445         valarray<_Tp>& operator|=(const _Expr<_Dom, _Tp>&);
446       template<class _Dom>
447         valarray<_Tp>& operator&=(const _Expr<_Dom, _Tp>&);
448       template<class _Dom>
449         valarray<_Tp>& operator<<=(const _Expr<_Dom, _Tp>&);
450       template<class _Dom>
451         valarray<_Tp>& operator>>=(const _Expr<_Dom, _Tp>&);
453       // _lib.valarray.members_ member functions:
454       ///  Return the number of elements in array.
455       size_t size() const;
457       /**
458        *  @brief  Return the sum of all elements in the array.
459        *
460        *  Accumulates the sum of all elements into a Tp using +=.  The order
461        *  of adding the elements is unspecified.
462        */
463       _Tp    sum() const;
465       ///  Return the minimum element using operator<().
466       _Tp    min() const;       
468       ///  Return the maximum element using operator<().
469       _Tp    max() const;       
471       /**
472        *  @brief  Return a shifted array.
473        *
474        *  A new valarray is constructed as a copy of this array with elements
475        *  in shifted positions.  For an element with index i, the new position
476        *  is i - n.  The new valarray has the same size as the current one.
477        *  New elements without a value are set to 0.  Elements whose new
478        *  position is outside the bounds of the array are discarded.
479        *
480        *  Positive arguments shift toward index 0, discarding elements [0, n).
481        *  Negative arguments discard elements from the top of the array.
482        *
483        *  @param  n  Number of element positions to shift.
484        *  @return  New valarray with elements in shifted positions.
485        */
486       valarray<_Tp> shift (int) const;
488       /**
489        *  @brief  Return a rotated array.
490        *
491        *  A new valarray is constructed as a copy of this array with elements
492        *  in shifted positions.  For an element with index i, the new position
493        *  is (i - n) % size().  The new valarray has the same size as the
494        *  current one.  Elements that are shifted beyond the array bounds are
495        *  shifted into the other end of the array.  No elements are lost.
496        *
497        *  Positive arguments shift toward index 0, wrapping around the top.
498        *  Negative arguments shift towards the top, wrapping around to 0.
499        *
500        *  @param  n  Number of element positions to rotate.
501        *  @return  New valarray with elements in shifted positions.
502        */
503       valarray<_Tp> cshift(int) const;
505       /**
506        *  @brief  Apply a function to the array.
507        *
508        *  Returns a new valarray with elements assigned to the result of
509        *  applying func to the corresponding element of this array.  The new
510        *  array has the same size as this one.
511        *
512        *  @param  func  Function of Tp returning Tp to apply.
513        *  @return  New valarray with transformed elements.
514        */
515       _Expr<_ValFunClos<_ValArray, _Tp>, _Tp> apply(_Tp func(_Tp)) const;
517       /**
518        *  @brief  Apply a function to the array.
519        *
520        *  Returns a new valarray with elements assigned to the result of
521        *  applying func to the corresponding element of this array.  The new
522        *  array has the same size as this one.
523        *
524        *  @param  func  Function of const Tp& returning Tp to apply.
525        *  @return  New valarray with transformed elements.
526        */
527       _Expr<_RefFunClos<_ValArray, _Tp>, _Tp> apply(_Tp func(const _Tp&)) const;
529       /**
530        *  @brief  Resize array.
531        *
532        *  Resize this array to @a size and set all elements to @a c.  All
533        *  references and iterators are invalidated.
534        *
535        *  @param  size  New array size.
536        *  @param  c  New value for all elements.
537        */
538       void resize(size_t __size, _Tp __c = _Tp());
540     private:
541       size_t _M_size;
542       _Tp* __restrict__ _M_data;
543       
544       friend class _Array<_Tp>;
545     };
546   
547   template<typename _Tp>
548     inline const _Tp&
549     valarray<_Tp>::operator[](size_t __i) const
550     { 
551       __glibcxx_requires_subscript(__i);
552       return _M_data[__i];
553     }
555   template<typename _Tp>
556     inline _Tp&
557     valarray<_Tp>::operator[](size_t __i)
558     { 
559       __glibcxx_requires_subscript(__i);
560       return _M_data[__i];
561     }
563   // @} group numeric_arrays
565 _GLIBCXX_END_NAMESPACE
567 #include <bits/valarray_after.h>
568 #include <bits/slice_array.h>
569 #include <bits/gslice.h>
570 #include <bits/gslice_array.h>
571 #include <bits/mask_array.h>
572 #include <bits/indirect_array.h>
574 _GLIBCXX_BEGIN_NAMESPACE(std)
576   /**
577    * @addtogroup numeric_arrays
578    * @{
579    */
581   template<typename _Tp>
582     inline
583     valarray<_Tp>::valarray() : _M_size(0), _M_data(0) {}
585   template<typename _Tp>
586     inline 
587     valarray<_Tp>::valarray(size_t __n) 
588     : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n))
589     { std::__valarray_default_construct(_M_data, _M_data + __n); }
591   template<typename _Tp>
592     inline
593     valarray<_Tp>::valarray(const _Tp& __t, size_t __n)
594     : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n))
595     { std::__valarray_fill_construct(_M_data, _M_data + __n, __t); }
597   template<typename _Tp>
598     inline
599     valarray<_Tp>::valarray(const _Tp* __restrict__ __p, size_t __n)
600     : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n))
601     { 
602       _GLIBCXX_DEBUG_ASSERT(__p != 0 || __n == 0);
603       std::__valarray_copy_construct(__p, __p + __n, _M_data); 
604     }
606   template<typename _Tp>
607     inline
608     valarray<_Tp>::valarray(const valarray<_Tp>& __v)
609     : _M_size(__v._M_size), _M_data(__valarray_get_storage<_Tp>(__v._M_size))
610     { std::__valarray_copy_construct(__v._M_data, __v._M_data + _M_size,
611                                      _M_data); }
613   template<typename _Tp>
614     inline
615     valarray<_Tp>::valarray(const slice_array<_Tp>& __sa)
616     : _M_size(__sa._M_sz), _M_data(__valarray_get_storage<_Tp>(__sa._M_sz))
617     {
618       std::__valarray_copy_construct
619         (__sa._M_array, __sa._M_sz, __sa._M_stride, _Array<_Tp>(_M_data));
620     }
622   template<typename _Tp>
623     inline
624     valarray<_Tp>::valarray(const gslice_array<_Tp>& __ga)
625     : _M_size(__ga._M_index.size()),
626       _M_data(__valarray_get_storage<_Tp>(_M_size))
627     {
628       std::__valarray_copy_construct
629         (__ga._M_array, _Array<size_t>(__ga._M_index),
630          _Array<_Tp>(_M_data), _M_size);
631     }
633   template<typename _Tp>
634     inline
635     valarray<_Tp>::valarray(const mask_array<_Tp>& __ma)
636     : _M_size(__ma._M_sz), _M_data(__valarray_get_storage<_Tp>(__ma._M_sz))
637     {
638       std::__valarray_copy_construct
639         (__ma._M_array, __ma._M_mask, _Array<_Tp>(_M_data), _M_size);
640     }
642   template<typename _Tp>
643     inline
644     valarray<_Tp>::valarray(const indirect_array<_Tp>& __ia)
645     : _M_size(__ia._M_sz), _M_data(__valarray_get_storage<_Tp>(__ia._M_sz))
646     {
647       std::__valarray_copy_construct
648         (__ia._M_array, __ia._M_index, _Array<_Tp>(_M_data), _M_size);
649     }
651 #ifdef __GXX_EXPERIMENTAL_CXX0X__
652   template<typename _Tp>
653     inline
654     valarray<_Tp>::valarray(initializer_list<_Tp> __l)
655       : _M_size(__l.size()), _M_data(__valarray_get_storage<_Tp>(__l.size()))
656     { std::__valarray_copy_construct (__l.begin(), __l.end(), _M_data); }
657 #endif
659   template<typename _Tp> template<class _Dom>
660     inline
661     valarray<_Tp>::valarray(const _Expr<_Dom, _Tp>& __e)
662     : _M_size(__e.size()), _M_data(__valarray_get_storage<_Tp>(_M_size))
663     { std::__valarray_copy_construct(__e, _M_size, _Array<_Tp>(_M_data)); }
665   template<typename _Tp>
666     inline
667     valarray<_Tp>::~valarray()
668     {
669       std::__valarray_destroy_elements(_M_data, _M_data + _M_size);
670       std::__valarray_release_memory(_M_data);
671     }
673   template<typename _Tp>
674     inline valarray<_Tp>&
675     valarray<_Tp>::operator=(const valarray<_Tp>& __v)
676     {
677       _GLIBCXX_DEBUG_ASSERT(_M_size == __v._M_size);
678       std::__valarray_copy(__v._M_data, _M_size, _M_data);
679       return *this;
680     }
682 #ifdef __GXX_EXPERIMENTAL_CXX0X__
683   template<typename _Tp>
684     inline valarray<_Tp>&
685     valarray<_Tp>::operator=(initializer_list<_Tp> __l)
686     {
687       _GLIBCXX_DEBUG_ASSERT(_M_size == __l.size());
688       std::__valarray_copy(__l.begin(), __l.size(), _M_data);
689     }
690 #endif
692   template<typename _Tp>
693     inline valarray<_Tp>&
694     valarray<_Tp>::operator=(const _Tp& __t)
695     {
696       std::__valarray_fill(_M_data, _M_size, __t);
697       return *this;
698     }
700   template<typename _Tp>
701     inline valarray<_Tp>&
702     valarray<_Tp>::operator=(const slice_array<_Tp>& __sa)
703     {
704       _GLIBCXX_DEBUG_ASSERT(_M_size == __sa._M_sz);
705       std::__valarray_copy(__sa._M_array, __sa._M_sz,
706                            __sa._M_stride, _Array<_Tp>(_M_data));
707       return *this;
708     }
710   template<typename _Tp>
711     inline valarray<_Tp>&
712     valarray<_Tp>::operator=(const gslice_array<_Tp>& __ga)
713     {
714       _GLIBCXX_DEBUG_ASSERT(_M_size == __ga._M_index.size());
715       std::__valarray_copy(__ga._M_array, _Array<size_t>(__ga._M_index),
716                            _Array<_Tp>(_M_data), _M_size);
717       return *this;
718     }
720   template<typename _Tp>
721     inline valarray<_Tp>&
722     valarray<_Tp>::operator=(const mask_array<_Tp>& __ma)
723     {
724       _GLIBCXX_DEBUG_ASSERT(_M_size == __ma._M_sz);
725       std::__valarray_copy(__ma._M_array, __ma._M_mask,
726                            _Array<_Tp>(_M_data), _M_size);
727       return *this;
728     }
730   template<typename _Tp>
731     inline valarray<_Tp>&
732     valarray<_Tp>::operator=(const indirect_array<_Tp>& __ia)
733     {
734       _GLIBCXX_DEBUG_ASSERT(_M_size == __ia._M_sz);
735       std::__valarray_copy(__ia._M_array, __ia._M_index,
736                            _Array<_Tp>(_M_data), _M_size);
737       return *this;
738     }
740   template<typename _Tp> template<class _Dom>
741     inline valarray<_Tp>&
742     valarray<_Tp>::operator=(const _Expr<_Dom, _Tp>& __e)
743     {
744       _GLIBCXX_DEBUG_ASSERT(_M_size == __e.size());
745       std::__valarray_copy(__e, _M_size, _Array<_Tp>(_M_data));
746       return *this;
747     }
749   template<typename _Tp>
750     inline _Expr<_SClos<_ValArray,_Tp>, _Tp>
751     valarray<_Tp>::operator[](slice __s) const
752     {
753       typedef _SClos<_ValArray,_Tp> _Closure;
754       return _Expr<_Closure, _Tp>(_Closure (_Array<_Tp>(_M_data), __s));
755     }
757   template<typename _Tp>
758     inline slice_array<_Tp>
759     valarray<_Tp>::operator[](slice __s)
760     { return slice_array<_Tp>(_Array<_Tp>(_M_data), __s); }
762   template<typename _Tp>
763     inline _Expr<_GClos<_ValArray,_Tp>, _Tp>
764     valarray<_Tp>::operator[](const gslice& __gs) const
765     {
766       typedef _GClos<_ValArray,_Tp> _Closure;
767       return _Expr<_Closure, _Tp>
768         (_Closure(_Array<_Tp>(_M_data), __gs._M_index->_M_index));
769     }
771   template<typename _Tp>
772     inline gslice_array<_Tp>
773     valarray<_Tp>::operator[](const gslice& __gs)
774     {
775       return gslice_array<_Tp>
776         (_Array<_Tp>(_M_data), __gs._M_index->_M_index);
777     }
779   template<typename _Tp>
780     inline valarray<_Tp>
781     valarray<_Tp>::operator[](const valarray<bool>& __m) const
782     {
783       size_t __s = 0;
784       size_t __e = __m.size();
785       for (size_t __i=0; __i<__e; ++__i)
786         if (__m[__i]) ++__s;
787       return valarray<_Tp>(mask_array<_Tp>(_Array<_Tp>(_M_data), __s,
788                                            _Array<bool> (__m)));
789     }
791   template<typename _Tp>
792     inline mask_array<_Tp>
793     valarray<_Tp>::operator[](const valarray<bool>& __m)
794     {
795       size_t __s = 0;
796       size_t __e = __m.size();
797       for (size_t __i=0; __i<__e; ++__i)
798         if (__m[__i]) ++__s;
799       return mask_array<_Tp>(_Array<_Tp>(_M_data), __s, _Array<bool>(__m));
800     }
802   template<typename _Tp>
803     inline _Expr<_IClos<_ValArray,_Tp>, _Tp>
804     valarray<_Tp>::operator[](const valarray<size_t>& __i) const
805     {
806       typedef _IClos<_ValArray,_Tp> _Closure;
807       return _Expr<_Closure, _Tp>(_Closure(*this, __i));
808     }
810   template<typename _Tp>
811     inline indirect_array<_Tp>
812     valarray<_Tp>::operator[](const valarray<size_t>& __i)
813     {
814       return indirect_array<_Tp>(_Array<_Tp>(_M_data), __i.size(),
815                                  _Array<size_t>(__i));
816     }
818   template<class _Tp>
819     inline size_t 
820     valarray<_Tp>::size() const
821     { return _M_size; }
823   template<class _Tp>
824     inline _Tp
825     valarray<_Tp>::sum() const
826     {
827       _GLIBCXX_DEBUG_ASSERT(_M_size > 0);
828       return std::__valarray_sum(_M_data, _M_data + _M_size);
829     }
831   template<class _Tp>
832      inline valarray<_Tp>
833      valarray<_Tp>::shift(int __n) const
834      {
835        valarray<_Tp> __ret;
837        if (_M_size == 0)
838          return __ret;
840        _Tp* __restrict__ __tmp_M_data =
841          std::__valarray_get_storage<_Tp>(_M_size);
843        if (__n == 0)
844          std::__valarray_copy_construct(_M_data,
845                                         _M_data + _M_size, __tmp_M_data);
846        else if (__n > 0)      // shift left
847          {
848            if (size_t(__n) > _M_size)
849              __n = int(_M_size);
851            std::__valarray_copy_construct(_M_data + __n,
852                                           _M_data + _M_size, __tmp_M_data);
853            std::__valarray_default_construct(__tmp_M_data + _M_size - __n,
854                                              __tmp_M_data + _M_size);
855          }
856        else                   // shift right
857          {
858            if (-size_t(__n) > _M_size)
859              __n = -int(_M_size);
861            std::__valarray_copy_construct(_M_data, _M_data + _M_size + __n,
862                                           __tmp_M_data - __n);
863            std::__valarray_default_construct(__tmp_M_data,
864                                              __tmp_M_data - __n);
865          }
867        __ret._M_size = _M_size;
868        __ret._M_data = __tmp_M_data;
869        return __ret;
870      }
872   template<class _Tp>
873      inline valarray<_Tp>
874      valarray<_Tp>::cshift(int __n) const
875      {
876        valarray<_Tp> __ret;
878        if (_M_size == 0)
879          return __ret;
881        _Tp* __restrict__ __tmp_M_data =
882          std::__valarray_get_storage<_Tp>(_M_size);
884        if (__n == 0)
885          std::__valarray_copy_construct(_M_data,
886                                         _M_data + _M_size, __tmp_M_data);
887        else if (__n > 0)      // cshift left
888          {
889            if (size_t(__n) > _M_size)
890              __n = int(__n % _M_size);
892            std::__valarray_copy_construct(_M_data, _M_data + __n,
893                                           __tmp_M_data + _M_size - __n);
894            std::__valarray_copy_construct(_M_data + __n, _M_data + _M_size,
895                                           __tmp_M_data);
896          }
897        else                   // cshift right
898          {
899            if (-size_t(__n) > _M_size)
900              __n = -int(-size_t(__n) % _M_size);
902            std::__valarray_copy_construct(_M_data + _M_size + __n,
903                                           _M_data + _M_size, __tmp_M_data);
904            std::__valarray_copy_construct(_M_data, _M_data + _M_size + __n,
905                                           __tmp_M_data - __n);
906          }
908        __ret._M_size = _M_size;
909        __ret._M_data = __tmp_M_data;
910        return __ret;
911      }
913   template<class _Tp>
914     inline void
915     valarray<_Tp>::resize(size_t __n, _Tp __c)
916     {
917       // This complication is so to make valarray<valarray<T> > work
918       // even though it is not required by the standard.  Nobody should
919       // be saying valarray<valarray<T> > anyway.  See the specs.
920       std::__valarray_destroy_elements(_M_data, _M_data + _M_size);
921       if (_M_size != __n)
922         {
923           std::__valarray_release_memory(_M_data);
924           _M_size = __n;
925           _M_data = __valarray_get_storage<_Tp>(__n);
926         }
927       std::__valarray_fill_construct(_M_data, _M_data + __n, __c);
928     }
929     
930   template<typename _Tp>
931     inline _Tp
932     valarray<_Tp>::min() const
933     {
934       _GLIBCXX_DEBUG_ASSERT(_M_size > 0);
935       return *std::min_element(_M_data, _M_data + _M_size);
936     }
938   template<typename _Tp>
939     inline _Tp
940     valarray<_Tp>::max() const
941     {
942       _GLIBCXX_DEBUG_ASSERT(_M_size > 0);
943       return *std::max_element(_M_data, _M_data + _M_size);
944     }
945   
946   template<class _Tp>
947     inline _Expr<_ValFunClos<_ValArray, _Tp>, _Tp>
948     valarray<_Tp>::apply(_Tp func(_Tp)) const
949     {
950       typedef _ValFunClos<_ValArray, _Tp> _Closure;
951       return _Expr<_Closure, _Tp>(_Closure(*this, func));
952     }
954   template<class _Tp>
955     inline _Expr<_RefFunClos<_ValArray, _Tp>, _Tp>
956     valarray<_Tp>::apply(_Tp func(const _Tp &)) const
957     {
958       typedef _RefFunClos<_ValArray, _Tp> _Closure;
959       return _Expr<_Closure, _Tp>(_Closure(*this, func));
960     }
962 #define _DEFINE_VALARRAY_UNARY_OPERATOR(_Op, _Name)                     \
963   template<typename _Tp>                                                \
964     inline typename valarray<_Tp>::template _UnaryOp<_Name>::_Rt        \
965     valarray<_Tp>::operator _Op() const                                 \
966     {                                                                   \
967       typedef _UnClos<_Name, _ValArray, _Tp> _Closure;                  \
968       typedef typename __fun<_Name, _Tp>::result_type _Rt;              \
969       return _Expr<_Closure, _Rt>(_Closure(*this));                     \
970     }
972     _DEFINE_VALARRAY_UNARY_OPERATOR(+, __unary_plus)
973     _DEFINE_VALARRAY_UNARY_OPERATOR(-, __negate)
974     _DEFINE_VALARRAY_UNARY_OPERATOR(~, __bitwise_not)
975     _DEFINE_VALARRAY_UNARY_OPERATOR (!, __logical_not)
977 #undef _DEFINE_VALARRAY_UNARY_OPERATOR
979 #define _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(_Op, _Name)               \
980   template<class _Tp>                                                   \
981     inline valarray<_Tp>&                                               \
982     valarray<_Tp>::operator _Op##=(const _Tp &__t)                      \
983     {                                                                   \
984       _Array_augmented_##_Name(_Array<_Tp>(_M_data), _M_size, __t);     \
985       return *this;                                                     \
986     }                                                                   \
987                                                                         \
988   template<class _Tp>                                                   \
989     inline valarray<_Tp>&                                               \
990     valarray<_Tp>::operator _Op##=(const valarray<_Tp> &__v)            \
991     {                                                                   \
992       _GLIBCXX_DEBUG_ASSERT(_M_size == __v._M_size);                    \
993       _Array_augmented_##_Name(_Array<_Tp>(_M_data), _M_size,           \
994                                _Array<_Tp>(__v._M_data));               \
995       return *this;                                                     \
996     }
998 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(+, __plus)
999 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(-, __minus)
1000 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(*, __multiplies)
1001 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(/, __divides)
1002 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(%, __modulus)
1003 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(^, __bitwise_xor)
1004 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(&, __bitwise_and)
1005 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(|, __bitwise_or)
1006 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(<<, __shift_left)
1007 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(>>, __shift_right)
1009 #undef _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT
1011 #define _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(_Op, _Name)          \
1012   template<class _Tp> template<class _Dom>                              \
1013     inline valarray<_Tp>&                                               \
1014     valarray<_Tp>::operator _Op##=(const _Expr<_Dom, _Tp>& __e)         \
1015     {                                                                   \
1016       _Array_augmented_##_Name(_Array<_Tp>(_M_data), __e, _M_size);     \
1017       return *this;                                                     \
1018     }
1020 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(+, __plus)
1021 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(-, __minus)
1022 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(*, __multiplies)
1023 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(/, __divides)
1024 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(%, __modulus)
1025 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(^, __bitwise_xor)
1026 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(&, __bitwise_and)
1027 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(|, __bitwise_or)
1028 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(<<, __shift_left)
1029 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(>>, __shift_right)
1031 #undef _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT
1032     
1034 #define _DEFINE_BINARY_OPERATOR(_Op, _Name)                             \
1035   template<typename _Tp>                                                \
1036     inline _Expr<_BinClos<_Name, _ValArray, _ValArray, _Tp, _Tp>,       \
1037                  typename __fun<_Name, _Tp>::result_type>               \
1038     operator _Op(const valarray<_Tp>& __v, const valarray<_Tp>& __w)    \
1039     {                                                                   \
1040       _GLIBCXX_DEBUG_ASSERT(__v.size() == __w.size());                  \
1041       typedef _BinClos<_Name, _ValArray, _ValArray, _Tp, _Tp> _Closure; \
1042       typedef typename __fun<_Name, _Tp>::result_type _Rt;              \
1043       return _Expr<_Closure, _Rt>(_Closure(__v, __w));                  \
1044     }                                                                   \
1045                                                                         \
1046   template<typename _Tp>                                                \
1047     inline _Expr<_BinClos<_Name, _ValArray,_Constant, _Tp, _Tp>,        \
1048                  typename __fun<_Name, _Tp>::result_type>               \
1049     operator _Op(const valarray<_Tp>& __v, const _Tp& __t)              \
1050     {                                                                   \
1051       typedef _BinClos<_Name, _ValArray, _Constant, _Tp, _Tp> _Closure; \
1052       typedef typename __fun<_Name, _Tp>::result_type _Rt;              \
1053       return _Expr<_Closure, _Rt>(_Closure(__v, __t));                  \
1054     }                                                                   \
1055                                                                         \
1056   template<typename _Tp>                                                \
1057     inline _Expr<_BinClos<_Name, _Constant, _ValArray, _Tp, _Tp>,       \
1058                  typename __fun<_Name, _Tp>::result_type>               \
1059     operator _Op(const _Tp& __t, const valarray<_Tp>& __v)              \
1060     {                                                                   \
1061       typedef _BinClos<_Name, _Constant, _ValArray, _Tp, _Tp> _Closure; \
1062       typedef typename __fun<_Name, _Tp>::result_type _Rt;              \
1063       return _Expr<_Closure, _Rt>(_Closure(__t, __v));                  \
1064     }
1066 _DEFINE_BINARY_OPERATOR(+, __plus)
1067 _DEFINE_BINARY_OPERATOR(-, __minus)
1068 _DEFINE_BINARY_OPERATOR(*, __multiplies)
1069 _DEFINE_BINARY_OPERATOR(/, __divides)
1070 _DEFINE_BINARY_OPERATOR(%, __modulus)
1071 _DEFINE_BINARY_OPERATOR(^, __bitwise_xor)
1072 _DEFINE_BINARY_OPERATOR(&, __bitwise_and)
1073 _DEFINE_BINARY_OPERATOR(|, __bitwise_or)
1074 _DEFINE_BINARY_OPERATOR(<<, __shift_left)
1075 _DEFINE_BINARY_OPERATOR(>>, __shift_right)
1076 _DEFINE_BINARY_OPERATOR(&&, __logical_and)
1077 _DEFINE_BINARY_OPERATOR(||, __logical_or)
1078 _DEFINE_BINARY_OPERATOR(==, __equal_to)
1079 _DEFINE_BINARY_OPERATOR(!=, __not_equal_to)
1080 _DEFINE_BINARY_OPERATOR(<, __less)
1081 _DEFINE_BINARY_OPERATOR(>, __greater)
1082 _DEFINE_BINARY_OPERATOR(<=, __less_equal)
1083 _DEFINE_BINARY_OPERATOR(>=, __greater_equal)
1085 #undef _DEFINE_BINARY_OPERATOR
1087   // @} group numeric_arrays
1089 _GLIBCXX_END_NAMESPACE
1091 #endif /* _GLIBCXX_VALARRAY */