OSDN Git Service

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