Import gcc-4.4.2
[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, 2008, 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       return *this;
685     }
686 #endif
687
688   template<typename _Tp>
689     inline valarray<_Tp>&
690     valarray<_Tp>::operator=(const _Tp& __t)
691     {
692       std::__valarray_fill(_M_data, _M_size, __t);
693       return *this;
694     }
695
696   template<typename _Tp>
697     inline valarray<_Tp>&
698     valarray<_Tp>::operator=(const slice_array<_Tp>& __sa)
699     {
700       _GLIBCXX_DEBUG_ASSERT(_M_size == __sa._M_sz);
701       std::__valarray_copy(__sa._M_array, __sa._M_sz,
702                            __sa._M_stride, _Array<_Tp>(_M_data));
703       return *this;
704     }
705
706   template<typename _Tp>
707     inline valarray<_Tp>&
708     valarray<_Tp>::operator=(const gslice_array<_Tp>& __ga)
709     {
710       _GLIBCXX_DEBUG_ASSERT(_M_size == __ga._M_index.size());
711       std::__valarray_copy(__ga._M_array, _Array<size_t>(__ga._M_index),
712                            _Array<_Tp>(_M_data), _M_size);
713       return *this;
714     }
715
716   template<typename _Tp>
717     inline valarray<_Tp>&
718     valarray<_Tp>::operator=(const mask_array<_Tp>& __ma)
719     {
720       _GLIBCXX_DEBUG_ASSERT(_M_size == __ma._M_sz);
721       std::__valarray_copy(__ma._M_array, __ma._M_mask,
722                            _Array<_Tp>(_M_data), _M_size);
723       return *this;
724     }
725
726   template<typename _Tp>
727     inline valarray<_Tp>&
728     valarray<_Tp>::operator=(const indirect_array<_Tp>& __ia)
729     {
730       _GLIBCXX_DEBUG_ASSERT(_M_size == __ia._M_sz);
731       std::__valarray_copy(__ia._M_array, __ia._M_index,
732                            _Array<_Tp>(_M_data), _M_size);
733       return *this;
734     }
735
736   template<typename _Tp> template<class _Dom>
737     inline valarray<_Tp>&
738     valarray<_Tp>::operator=(const _Expr<_Dom, _Tp>& __e)
739     {
740       _GLIBCXX_DEBUG_ASSERT(_M_size == __e.size());
741       std::__valarray_copy(__e, _M_size, _Array<_Tp>(_M_data));
742       return *this;
743     }
744
745   template<typename _Tp>
746     inline _Expr<_SClos<_ValArray,_Tp>, _Tp>
747     valarray<_Tp>::operator[](slice __s) const
748     {
749       typedef _SClos<_ValArray,_Tp> _Closure;
750       return _Expr<_Closure, _Tp>(_Closure (_Array<_Tp>(_M_data), __s));
751     }
752
753   template<typename _Tp>
754     inline slice_array<_Tp>
755     valarray<_Tp>::operator[](slice __s)
756     { return slice_array<_Tp>(_Array<_Tp>(_M_data), __s); }
757
758   template<typename _Tp>
759     inline _Expr<_GClos<_ValArray,_Tp>, _Tp>
760     valarray<_Tp>::operator[](const gslice& __gs) const
761     {
762       typedef _GClos<_ValArray,_Tp> _Closure;
763       return _Expr<_Closure, _Tp>
764         (_Closure(_Array<_Tp>(_M_data), __gs._M_index->_M_index));
765     }
766
767   template<typename _Tp>
768     inline gslice_array<_Tp>
769     valarray<_Tp>::operator[](const gslice& __gs)
770     {
771       return gslice_array<_Tp>
772         (_Array<_Tp>(_M_data), __gs._M_index->_M_index);
773     }
774
775   template<typename _Tp>
776     inline valarray<_Tp>
777     valarray<_Tp>::operator[](const valarray<bool>& __m) const
778     {
779       size_t __s = 0;
780       size_t __e = __m.size();
781       for (size_t __i=0; __i<__e; ++__i)
782         if (__m[__i]) ++__s;
783       return valarray<_Tp>(mask_array<_Tp>(_Array<_Tp>(_M_data), __s,
784                                            _Array<bool> (__m)));
785     }
786
787   template<typename _Tp>
788     inline mask_array<_Tp>
789     valarray<_Tp>::operator[](const valarray<bool>& __m)
790     {
791       size_t __s = 0;
792       size_t __e = __m.size();
793       for (size_t __i=0; __i<__e; ++__i)
794         if (__m[__i]) ++__s;
795       return mask_array<_Tp>(_Array<_Tp>(_M_data), __s, _Array<bool>(__m));
796     }
797
798   template<typename _Tp>
799     inline _Expr<_IClos<_ValArray,_Tp>, _Tp>
800     valarray<_Tp>::operator[](const valarray<size_t>& __i) const
801     {
802       typedef _IClos<_ValArray,_Tp> _Closure;
803       return _Expr<_Closure, _Tp>(_Closure(*this, __i));
804     }
805
806   template<typename _Tp>
807     inline indirect_array<_Tp>
808     valarray<_Tp>::operator[](const valarray<size_t>& __i)
809     {
810       return indirect_array<_Tp>(_Array<_Tp>(_M_data), __i.size(),
811                                  _Array<size_t>(__i));
812     }
813
814   template<class _Tp>
815     inline size_t 
816     valarray<_Tp>::size() const
817     { return _M_size; }
818
819   template<class _Tp>
820     inline _Tp
821     valarray<_Tp>::sum() const
822     {
823       _GLIBCXX_DEBUG_ASSERT(_M_size > 0);
824       return std::__valarray_sum(_M_data, _M_data + _M_size);
825     }
826
827   template<class _Tp>
828      inline valarray<_Tp>
829      valarray<_Tp>::shift(int __n) const
830      {
831        valarray<_Tp> __ret;
832
833        if (_M_size == 0)
834          return __ret;
835
836        _Tp* __restrict__ __tmp_M_data =
837          std::__valarray_get_storage<_Tp>(_M_size);
838
839        if (__n == 0)
840          std::__valarray_copy_construct(_M_data,
841                                         _M_data + _M_size, __tmp_M_data);
842        else if (__n > 0)      // shift left
843          {
844            if (size_t(__n) > _M_size)
845              __n = int(_M_size);
846
847            std::__valarray_copy_construct(_M_data + __n,
848                                           _M_data + _M_size, __tmp_M_data);
849            std::__valarray_default_construct(__tmp_M_data + _M_size - __n,
850                                              __tmp_M_data + _M_size);
851          }
852        else                   // shift right
853          {
854            if (-size_t(__n) > _M_size)
855              __n = -int(_M_size);
856
857            std::__valarray_copy_construct(_M_data, _M_data + _M_size + __n,
858                                           __tmp_M_data - __n);
859            std::__valarray_default_construct(__tmp_M_data,
860                                              __tmp_M_data - __n);
861          }
862
863        __ret._M_size = _M_size;
864        __ret._M_data = __tmp_M_data;
865        return __ret;
866      }
867
868   template<class _Tp>
869      inline valarray<_Tp>
870      valarray<_Tp>::cshift(int __n) const
871      {
872        valarray<_Tp> __ret;
873
874        if (_M_size == 0)
875          return __ret;
876
877        _Tp* __restrict__ __tmp_M_data =
878          std::__valarray_get_storage<_Tp>(_M_size);
879
880        if (__n == 0)
881          std::__valarray_copy_construct(_M_data,
882                                         _M_data + _M_size, __tmp_M_data);
883        else if (__n > 0)      // cshift left
884          {
885            if (size_t(__n) > _M_size)
886              __n = int(__n % _M_size);
887
888            std::__valarray_copy_construct(_M_data, _M_data + __n,
889                                           __tmp_M_data + _M_size - __n);
890            std::__valarray_copy_construct(_M_data + __n, _M_data + _M_size,
891                                           __tmp_M_data);
892          }
893        else                   // cshift right
894          {
895            if (-size_t(__n) > _M_size)
896              __n = -int(-size_t(__n) % _M_size);
897
898            std::__valarray_copy_construct(_M_data + _M_size + __n,
899                                           _M_data + _M_size, __tmp_M_data);
900            std::__valarray_copy_construct(_M_data, _M_data + _M_size + __n,
901                                           __tmp_M_data - __n);
902          }
903
904        __ret._M_size = _M_size;
905        __ret._M_data = __tmp_M_data;
906        return __ret;
907      }
908
909   template<class _Tp>
910     inline void
911     valarray<_Tp>::resize(size_t __n, _Tp __c)
912     {
913       // This complication is so to make valarray<valarray<T> > work
914       // even though it is not required by the standard.  Nobody should
915       // be saying valarray<valarray<T> > anyway.  See the specs.
916       std::__valarray_destroy_elements(_M_data, _M_data + _M_size);
917       if (_M_size != __n)
918         {
919           std::__valarray_release_memory(_M_data);
920           _M_size = __n;
921           _M_data = __valarray_get_storage<_Tp>(__n);
922         }
923       std::__valarray_fill_construct(_M_data, _M_data + __n, __c);
924     }
925     
926   template<typename _Tp>
927     inline _Tp
928     valarray<_Tp>::min() const
929     {
930       _GLIBCXX_DEBUG_ASSERT(_M_size > 0);
931       return *std::min_element(_M_data, _M_data + _M_size);
932     }
933
934   template<typename _Tp>
935     inline _Tp
936     valarray<_Tp>::max() const
937     {
938       _GLIBCXX_DEBUG_ASSERT(_M_size > 0);
939       return *std::max_element(_M_data, _M_data + _M_size);
940     }
941   
942   template<class _Tp>
943     inline _Expr<_ValFunClos<_ValArray, _Tp>, _Tp>
944     valarray<_Tp>::apply(_Tp func(_Tp)) const
945     {
946       typedef _ValFunClos<_ValArray, _Tp> _Closure;
947       return _Expr<_Closure, _Tp>(_Closure(*this, func));
948     }
949
950   template<class _Tp>
951     inline _Expr<_RefFunClos<_ValArray, _Tp>, _Tp>
952     valarray<_Tp>::apply(_Tp func(const _Tp &)) const
953     {
954       typedef _RefFunClos<_ValArray, _Tp> _Closure;
955       return _Expr<_Closure, _Tp>(_Closure(*this, func));
956     }
957
958 #define _DEFINE_VALARRAY_UNARY_OPERATOR(_Op, _Name)                     \
959   template<typename _Tp>                                                \
960     inline typename valarray<_Tp>::template _UnaryOp<_Name>::_Rt        \
961     valarray<_Tp>::operator _Op() const                                 \
962     {                                                                   \
963       typedef _UnClos<_Name, _ValArray, _Tp> _Closure;                  \
964       typedef typename __fun<_Name, _Tp>::result_type _Rt;              \
965       return _Expr<_Closure, _Rt>(_Closure(*this));                     \
966     }
967
968     _DEFINE_VALARRAY_UNARY_OPERATOR(+, __unary_plus)
969     _DEFINE_VALARRAY_UNARY_OPERATOR(-, __negate)
970     _DEFINE_VALARRAY_UNARY_OPERATOR(~, __bitwise_not)
971     _DEFINE_VALARRAY_UNARY_OPERATOR (!, __logical_not)
972
973 #undef _DEFINE_VALARRAY_UNARY_OPERATOR
974
975 #define _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(_Op, _Name)               \
976   template<class _Tp>                                                   \
977     inline valarray<_Tp>&                                               \
978     valarray<_Tp>::operator _Op##=(const _Tp &__t)                      \
979     {                                                                   \
980       _Array_augmented_##_Name(_Array<_Tp>(_M_data), _M_size, __t);     \
981       return *this;                                                     \
982     }                                                                   \
983                                                                         \
984   template<class _Tp>                                                   \
985     inline valarray<_Tp>&                                               \
986     valarray<_Tp>::operator _Op##=(const valarray<_Tp> &__v)            \
987     {                                                                   \
988       _GLIBCXX_DEBUG_ASSERT(_M_size == __v._M_size);                    \
989       _Array_augmented_##_Name(_Array<_Tp>(_M_data), _M_size,           \
990                                _Array<_Tp>(__v._M_data));               \
991       return *this;                                                     \
992     }
993
994 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(+, __plus)
995 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(-, __minus)
996 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(*, __multiplies)
997 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(/, __divides)
998 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(%, __modulus)
999 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(^, __bitwise_xor)
1000 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(&, __bitwise_and)
1001 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(|, __bitwise_or)
1002 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(<<, __shift_left)
1003 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(>>, __shift_right)
1004
1005 #undef _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT
1006
1007 #define _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(_Op, _Name)          \
1008   template<class _Tp> template<class _Dom>                              \
1009     inline valarray<_Tp>&                                               \
1010     valarray<_Tp>::operator _Op##=(const _Expr<_Dom, _Tp>& __e)         \
1011     {                                                                   \
1012       _Array_augmented_##_Name(_Array<_Tp>(_M_data), __e, _M_size);     \
1013       return *this;                                                     \
1014     }
1015
1016 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(+, __plus)
1017 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(-, __minus)
1018 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(*, __multiplies)
1019 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(/, __divides)
1020 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(%, __modulus)
1021 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(^, __bitwise_xor)
1022 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(&, __bitwise_and)
1023 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(|, __bitwise_or)
1024 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(<<, __shift_left)
1025 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(>>, __shift_right)
1026
1027 #undef _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT
1028     
1029
1030 #define _DEFINE_BINARY_OPERATOR(_Op, _Name)                             \
1031   template<typename _Tp>                                                \
1032     inline _Expr<_BinClos<_Name, _ValArray, _ValArray, _Tp, _Tp>,       \
1033                  typename __fun<_Name, _Tp>::result_type>               \
1034     operator _Op(const valarray<_Tp>& __v, const valarray<_Tp>& __w)    \
1035     {                                                                   \
1036       _GLIBCXX_DEBUG_ASSERT(__v.size() == __w.size());                  \
1037       typedef _BinClos<_Name, _ValArray, _ValArray, _Tp, _Tp> _Closure; \
1038       typedef typename __fun<_Name, _Tp>::result_type _Rt;              \
1039       return _Expr<_Closure, _Rt>(_Closure(__v, __w));                  \
1040     }                                                                   \
1041                                                                         \
1042   template<typename _Tp>                                                \
1043     inline _Expr<_BinClos<_Name, _ValArray,_Constant, _Tp, _Tp>,        \
1044                  typename __fun<_Name, _Tp>::result_type>               \
1045     operator _Op(const valarray<_Tp>& __v, const _Tp& __t)              \
1046     {                                                                   \
1047       typedef _BinClos<_Name, _ValArray, _Constant, _Tp, _Tp> _Closure; \
1048       typedef typename __fun<_Name, _Tp>::result_type _Rt;              \
1049       return _Expr<_Closure, _Rt>(_Closure(__v, __t));                  \
1050     }                                                                   \
1051                                                                         \
1052   template<typename _Tp>                                                \
1053     inline _Expr<_BinClos<_Name, _Constant, _ValArray, _Tp, _Tp>,       \
1054                  typename __fun<_Name, _Tp>::result_type>               \
1055     operator _Op(const _Tp& __t, const valarray<_Tp>& __v)              \
1056     {                                                                   \
1057       typedef _BinClos<_Name, _Constant, _ValArray, _Tp, _Tp> _Closure; \
1058       typedef typename __fun<_Name, _Tp>::result_type _Rt;              \
1059       return _Expr<_Closure, _Rt>(_Closure(__t, __v));                  \
1060     }
1061
1062 _DEFINE_BINARY_OPERATOR(+, __plus)
1063 _DEFINE_BINARY_OPERATOR(-, __minus)
1064 _DEFINE_BINARY_OPERATOR(*, __multiplies)
1065 _DEFINE_BINARY_OPERATOR(/, __divides)
1066 _DEFINE_BINARY_OPERATOR(%, __modulus)
1067 _DEFINE_BINARY_OPERATOR(^, __bitwise_xor)
1068 _DEFINE_BINARY_OPERATOR(&, __bitwise_and)
1069 _DEFINE_BINARY_OPERATOR(|, __bitwise_or)
1070 _DEFINE_BINARY_OPERATOR(<<, __shift_left)
1071 _DEFINE_BINARY_OPERATOR(>>, __shift_right)
1072 _DEFINE_BINARY_OPERATOR(&&, __logical_and)
1073 _DEFINE_BINARY_OPERATOR(||, __logical_or)
1074 _DEFINE_BINARY_OPERATOR(==, __equal_to)
1075 _DEFINE_BINARY_OPERATOR(!=, __not_equal_to)
1076 _DEFINE_BINARY_OPERATOR(<, __less)
1077 _DEFINE_BINARY_OPERATOR(>, __greater)
1078 _DEFINE_BINARY_OPERATOR(<=, __less_equal)
1079 _DEFINE_BINARY_OPERATOR(>=, __greater_equal)
1080
1081 #undef _DEFINE_BINARY_OPERATOR
1082
1083   // @} group numeric_arrays
1084
1085 _GLIBCXX_END_NAMESPACE
1086
1087 #endif /* _GLIBCXX_VALARRAY */