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