OSDN Git Service

2005-08-17 Kelley Cook <kcook@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / std / std_valarray.h
1 // The template and inlines for the -*- C++ -*- valarray class.
2
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2004
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library.  This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
10 // any later version.
11
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING.  If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 // USA.
21
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction.  Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License.  This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
30
31 // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr>
32
33 /** @file valarray
34  *  This is a Standard C++ Library header. 
35  */
36
37 #ifndef _GLIBCXX_VALARRAY
38 #define _GLIBCXX_VALARRAY 1
39
40 #pragma GCC system_header
41
42 #include <bits/c++config.h>
43 #include <cstddef>
44 #include <cmath>
45 #include <cstdlib>
46 #include <numeric>
47 #include <algorithm>
48 #include <debug/debug.h>
49
50 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 } // namespace std
91
92 #include <bits/valarray_array.h>
93 #include <bits/valarray_before.h>
94   
95 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       template<class _Dom>
149         valarray(const _Expr<_Dom, _Tp>& __e);
150
151       ~valarray();
152
153       // _lib.valarray.assign_ assignment:
154       /**
155        *  @brief  Assign elements to an array.
156        *
157        *  Assign elements of array to values in @a v.  Results are undefined
158        *  if @a v does not have the same size as this array.
159        *
160        *  @param  v  Valarray to get values from.
161        */
162       valarray<_Tp>& operator=(const valarray<_Tp>&);
163
164       /**
165        *  @brief  Assign elements to a value.
166        *
167        *  Assign all elements of array to @a t.
168        *
169        *  @param  t  Value for elements.
170        */
171       valarray<_Tp>& operator=(const _Tp&);
172
173       /**
174        *  @brief  Assign elements to an array subset.
175        *
176        *  Assign elements of array to values in @a sa.  Results are undefined
177        *  if @a sa does not have the same size as this array.
178        *
179        *  @param  sa  Array slice to get values from.
180        */
181       valarray<_Tp>& operator=(const slice_array<_Tp>&);
182
183       /**
184        *  @brief  Assign elements to an array subset.
185        *
186        *  Assign elements of array to values in @a ga.  Results are undefined
187        *  if @a ga does not have the same size as this array.
188        *
189        *  @param  ga  Array slice to get values from.
190        */
191       valarray<_Tp>& operator=(const gslice_array<_Tp>&);
192
193       /**
194        *  @brief  Assign elements to an array subset.
195        *
196        *  Assign elements of array to values in @a ma.  Results are undefined
197        *  if @a ma does not have the same size as this array.
198        *
199        *  @param  ma  Array slice to get values from.
200        */
201       valarray<_Tp>& operator=(const mask_array<_Tp>&);
202
203       /**
204        *  @brief  Assign elements to an array subset.
205        *
206        *  Assign elements of array to values in @a ia.  Results are undefined
207        *  if @a ia does not have the same size as this array.
208        *
209        *  @param  ia  Array slice to get values from.
210        */
211       valarray<_Tp>& operator=(const indirect_array<_Tp>&);
212
213       template<class _Dom> valarray<_Tp>&
214         operator= (const _Expr<_Dom, _Tp>&);
215
216       // _lib.valarray.access_ element access:
217       /**
218        *  Return a reference to the i'th array element.  
219        *
220        *  @param  i  Index of element to return.
221        *  @return  Reference to the i'th element.
222        */
223       _Tp&                operator[](size_t);
224
225       // _GLIBCXX_RESOLVE_LIB_DEFECTS
226       // 389. Const overload of valarray::operator[] returns by value.
227       const _Tp&          operator[](size_t) const;
228
229       // _lib.valarray.sub_ subset operations:
230       /**
231        *  @brief  Return an array subset.
232        *
233        *  Returns a new valarray containing the elements of the array
234        *  indicated by the slice argument.  The new valarray has the same size
235        *  as the input slice.  @see slice.
236        *
237        *  @param  s  The source slice.
238        *  @return  New valarray containing elements in @a s.
239        */
240       _Expr<_SClos<_ValArray, _Tp>, _Tp> operator[](slice) const;
241
242       /**
243        *  @brief  Return a reference to an array subset.
244        *
245        *  Returns a new valarray containing the elements of the array
246        *  indicated by the slice argument.  The new valarray has the same size
247        *  as the input slice.  @see slice.
248        *
249        *  @param  s  The source slice.
250        *  @return  New valarray containing elements in @a s.
251        */
252       slice_array<_Tp>    operator[](slice);
253
254       /**
255        *  @brief  Return an array subset.
256        *
257        *  Returns a slice_array referencing the elements of the array
258        *  indicated by the slice argument.  @see gslice.
259        *
260        *  @param  s  The source slice.
261        *  @return  Slice_array referencing elements indicated by @a s.
262        */
263       _Expr<_GClos<_ValArray, _Tp>, _Tp> operator[](const gslice&) const;
264
265       /**
266        *  @brief  Return a reference to an array subset.
267        *
268        *  Returns a new valarray containing the elements of the array
269        *  indicated by the gslice argument.  The new valarray has
270        *  the same size as the input gslice.  @see gslice.
271        *
272        *  @param  s  The source gslice.
273        *  @return  New valarray containing elements in @a s.
274        */
275       gslice_array<_Tp>   operator[](const gslice&);
276
277       /**
278        *  @brief  Return an array subset.
279        *
280        *  Returns a new valarray containing the elements of the array
281        *  indicated by the argument.  The input is a valarray of bool which
282        *  represents a bitmask indicating which elements should be copied into
283        *  the new valarray.  Each element of the array is added to the return
284        *  valarray if the corresponding element of the argument is true.
285        *
286        *  @param  m  The valarray bitmask.
287        *  @return  New valarray containing elements indicated by @a m.
288        */
289       valarray<_Tp>       operator[](const valarray<bool>&) const;
290
291       /**
292        *  @brief  Return a reference to an array subset.
293        *
294        *  Returns a new mask_array referencing the elements of the array
295        *  indicated by the argument.  The input is a valarray of bool which
296        *  represents a bitmask indicating which elements are part of the
297        *  subset.  Elements of the array are part of the subset if the
298        *  corresponding element of the argument is true.
299        *
300        *  @param  m  The valarray bitmask.
301        *  @return  New valarray containing elements indicated by @a m.
302        */
303       mask_array<_Tp>     operator[](const valarray<bool>&);
304
305       /**
306        *  @brief  Return an array subset.
307        *
308        *  Returns a new valarray containing the elements of the array
309        *  indicated by the argument.  The elements in the argument are
310        *  interpreted as the indices of elements of this valarray to copy to
311        *  the return valarray.
312        *
313        *  @param  i  The valarray element index list.
314        *  @return  New valarray containing elements in @a s.
315        */
316       _Expr<_IClos<_ValArray, _Tp>, _Tp>
317         operator[](const valarray<size_t>&) const;
318
319       /**
320        *  @brief  Return a reference to an array subset.
321        *
322        *  Returns an indirect_array referencing the elements of the array
323        *  indicated by the argument.  The elements in the argument are
324        *  interpreted as the indices of elements of this valarray to include
325        *  in the subset.  The returned indirect_array refers to these
326        *  elements.
327        *
328        *  @param  i  The valarray element index list.
329        *  @return  Indirect_array referencing elements in @a i.
330        */
331       indirect_array<_Tp> operator[](const valarray<size_t>&);
332
333       // _lib.valarray.unary_ unary operators:
334       ///  Return a new valarray by applying unary + to each element.
335       typename _UnaryOp<__unary_plus>::_Rt  operator+() const;
336
337       ///  Return a new valarray by applying unary - to each element.
338       typename _UnaryOp<__negate>::_Rt      operator-() const;
339
340       ///  Return a new valarray by applying unary ~ to each element.
341       typename _UnaryOp<__bitwise_not>::_Rt operator~() const;
342
343       ///  Return a new valarray by applying unary ! to each element.
344       typename _UnaryOp<__logical_not>::_Rt operator!() const;
345
346       // _lib.valarray.cassign_ computed assignment:
347       ///  Multiply each element of array by @a t.
348       valarray<_Tp>& operator*=(const _Tp&);
349
350       ///  Divide each element of array by @a t.
351       valarray<_Tp>& operator/=(const _Tp&);
352
353       ///  Set each element e of array to e % @a t.
354       valarray<_Tp>& operator%=(const _Tp&);
355
356       ///  Add @a t to each element of array.
357       valarray<_Tp>& operator+=(const _Tp&);
358
359       ///  Subtract @a t to each element of array.
360       valarray<_Tp>& operator-=(const _Tp&);
361
362       ///  Set each element e of array to e ^ @a t.
363       valarray<_Tp>& operator^=(const _Tp&);
364
365       ///  Set each element e of array to e & @a t.
366       valarray<_Tp>& operator&=(const _Tp&);
367
368       ///  Set each element e of array to e | @a t.
369       valarray<_Tp>& operator|=(const _Tp&);
370
371       ///  Left shift each element e of array by @a t bits.
372       valarray<_Tp>& operator<<=(const _Tp&);
373
374       ///  Right shift each element e of array by @a t bits.
375       valarray<_Tp>& operator>>=(const _Tp&);
376
377       ///  Multiply elements of array by corresponding elements of @a v.
378       valarray<_Tp>& operator*=(const valarray<_Tp>&);
379
380       ///  Divide elements of array by corresponding elements of @a v.
381       valarray<_Tp>& operator/=(const valarray<_Tp>&);
382
383       ///  Modulo elements of array by corresponding elements of @a v.
384       valarray<_Tp>& operator%=(const valarray<_Tp>&);
385
386       ///  Add corresponding elements of @a v to elements of array.
387       valarray<_Tp>& operator+=(const valarray<_Tp>&);
388
389       ///  Subtract corresponding elements of @a v from elements of array.
390       valarray<_Tp>& operator-=(const valarray<_Tp>&);
391
392       ///  Logical xor corresponding elements of @a v with elements of array.
393       valarray<_Tp>& operator^=(const valarray<_Tp>&);
394
395       ///  Logical or corresponding elements of @a v with elements of array.
396       valarray<_Tp>& operator|=(const valarray<_Tp>&);
397
398       ///  Logical and corresponding elements of @a v with elements of array.
399       valarray<_Tp>& operator&=(const valarray<_Tp>&);
400
401       ///  Left shift elements of array by corresponding elements of @a v.
402       valarray<_Tp>& operator<<=(const valarray<_Tp>&);
403
404       ///  Right shift elements of array by corresponding elements of @a v.
405       valarray<_Tp>& operator>>=(const valarray<_Tp>&);
406
407       template<class _Dom>
408         valarray<_Tp>& operator*=(const _Expr<_Dom, _Tp>&);
409       template<class _Dom>
410         valarray<_Tp>& operator/=(const _Expr<_Dom, _Tp>&);
411       template<class _Dom>
412         valarray<_Tp>& operator%=(const _Expr<_Dom, _Tp>&);
413       template<class _Dom>
414         valarray<_Tp>& operator+=(const _Expr<_Dom, _Tp>&);
415       template<class _Dom>
416         valarray<_Tp>& operator-=(const _Expr<_Dom, _Tp>&);
417       template<class _Dom>
418         valarray<_Tp>& operator^=(const _Expr<_Dom, _Tp>&);
419       template<class _Dom>
420         valarray<_Tp>& operator|=(const _Expr<_Dom, _Tp>&);
421       template<class _Dom>
422         valarray<_Tp>& operator&=(const _Expr<_Dom, _Tp>&);
423       template<class _Dom>
424         valarray<_Tp>& operator<<=(const _Expr<_Dom, _Tp>&);
425       template<class _Dom>
426         valarray<_Tp>& operator>>=(const _Expr<_Dom, _Tp>&);
427
428       // _lib.valarray.members_ member functions:
429       ///  Return the number of elements in array.
430       size_t size() const;
431
432       /**
433        *  @brief  Return the sum of all elements in the array.
434        *
435        *  Accumulates the sum of all elements into a Tp using +=.  The order
436        *  of adding the elements is unspecified.
437        */
438       _Tp    sum() const;
439
440       ///  Return the minimum element using operator<().
441       _Tp    min() const;       
442
443       ///  Return the maximum element using operator<().
444       _Tp    max() const;       
445
446       /**
447        *  @brief  Return a shifted array.
448        *
449        *  A new valarray is constructed as a copy of this array with elements
450        *  in shifted positions.  For an element with index i, the new position
451        *  is i - n.  The new valarray has the same size as the current one.
452        *  New elements without a value are set to 0.  Elements whose new
453        *  position is outside the bounds of the array are discarded.
454        *
455        *  Positive arguments shift toward index 0, discarding elements [0, n).
456        *  Negative arguments discard elements from the top of the array.
457        *
458        *  @param  n  Number of element positions to shift.
459        *  @return  New valarray with elements in shifted positions.
460        */
461       valarray<_Tp> shift (int) const;
462
463       /**
464        *  @brief  Return a rotated 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) % size().  The new valarray has the same size as the
469        *  current one.  Elements that are shifted beyond the array bounds are
470        *  shifted into the other end of the array.  No elements are lost.
471        *
472        *  Positive arguments shift toward index 0, wrapping around the top.
473        *  Negative arguments shift towards the top, wrapping around to 0.
474        *
475        *  @param  n  Number of element positions to rotate.
476        *  @return  New valarray with elements in shifted positions.
477        */
478       valarray<_Tp> cshift(int) const;
479
480       /**
481        *  @brief  Apply a function to the array.
482        *
483        *  Returns a new valarray with elements assigned to the result of
484        *  applying func to the corresponding element of this array.  The new
485        *  array has the same size as this one.
486        *
487        *  @param  func  Function of Tp returning Tp to apply.
488        *  @return  New valarray with transformed elements.
489        */
490       _Expr<_ValFunClos<_ValArray, _Tp>, _Tp> apply(_Tp func(_Tp)) const;
491
492       /**
493        *  @brief  Apply a function to the array.
494        *
495        *  Returns a new valarray with elements assigned to the result of
496        *  applying func to the corresponding element of this array.  The new
497        *  array has the same size as this one.
498        *
499        *  @param  func  Function of const Tp& returning Tp to apply.
500        *  @return  New valarray with transformed elements.
501        */
502       _Expr<_RefFunClos<_ValArray, _Tp>, _Tp> apply(_Tp func(const _Tp&)) const;
503
504       /**
505        *  @brief  Resize array.
506        *
507        *  Resize this array to @a size and set all elements to @a c.  All
508        *  references and iterators are invalidated.
509        *
510        *  @param  size  New array size.
511        *  @param  c  New value for all elements.
512        */
513       void resize(size_t __size, _Tp __c = _Tp());
514
515     private:
516       size_t _M_size;
517       _Tp* __restrict__ _M_data;
518       
519       friend class _Array<_Tp>;
520     };
521   
522   template<typename _Tp>
523     inline const _Tp&
524     valarray<_Tp>::operator[](size_t __i) const
525     { 
526       __glibcxx_requires_subscript(__i);
527       return _M_data[__i];
528     }
529
530   template<typename _Tp>
531     inline _Tp&
532     valarray<_Tp>::operator[](size_t __i)
533     { 
534       __glibcxx_requires_subscript(__i);
535       return _M_data[__i];
536     }
537
538 } // std::
539
540 #include <bits/valarray_after.h>
541
542 #include <bits/slice_array.h>
543 #include <bits/gslice.h>
544 #include <bits/gslice_array.h>
545 #include <bits/mask_array.h>
546 #include <bits/indirect_array.h>
547
548 namespace std
549 {
550   template<typename _Tp>
551     inline
552     valarray<_Tp>::valarray() : _M_size(0), _M_data(0) {}
553
554   template<typename _Tp>
555     inline 
556     valarray<_Tp>::valarray(size_t __n) 
557     : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n))
558     { std::__valarray_default_construct(_M_data, _M_data + __n); }
559
560   template<typename _Tp>
561     inline
562     valarray<_Tp>::valarray(const _Tp& __t, size_t __n)
563     : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n))
564     { std::__valarray_fill_construct(_M_data, _M_data + __n, __t); }
565
566   template<typename _Tp>
567     inline
568     valarray<_Tp>::valarray(const _Tp* __restrict__ __p, size_t __n)
569     : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n))
570     { 
571       _GLIBCXX_DEBUG_ASSERT(__p != 0 || __n == 0);
572       std::__valarray_copy_construct(__p, __p + __n, _M_data); 
573     }
574
575   template<typename _Tp>
576     inline
577     valarray<_Tp>::valarray(const valarray<_Tp>& __v)
578     : _M_size(__v._M_size), _M_data(__valarray_get_storage<_Tp>(__v._M_size))
579     { std::__valarray_copy_construct(__v._M_data, __v._M_data + _M_size,
580                                      _M_data); }
581
582   template<typename _Tp>
583     inline
584     valarray<_Tp>::valarray(const slice_array<_Tp>& __sa)
585     : _M_size(__sa._M_sz), _M_data(__valarray_get_storage<_Tp>(__sa._M_sz))
586     {
587       std::__valarray_copy
588         (__sa._M_array, __sa._M_sz, __sa._M_stride, _Array<_Tp>(_M_data));
589     }
590
591   template<typename _Tp>
592     inline
593     valarray<_Tp>::valarray(const gslice_array<_Tp>& __ga)
594     : _M_size(__ga._M_index.size()),
595       _M_data(__valarray_get_storage<_Tp>(_M_size))
596     {
597       std::__valarray_copy
598         (__ga._M_array, _Array<size_t>(__ga._M_index),
599          _Array<_Tp>(_M_data), _M_size);
600     }
601
602   template<typename _Tp>
603     inline
604     valarray<_Tp>::valarray(const mask_array<_Tp>& __ma)
605     : _M_size(__ma._M_sz), _M_data(__valarray_get_storage<_Tp>(__ma._M_sz))
606     {
607       std::__valarray_copy
608         (__ma._M_array, __ma._M_mask, _Array<_Tp>(_M_data), _M_size);
609     }
610
611   template<typename _Tp>
612     inline
613     valarray<_Tp>::valarray(const indirect_array<_Tp>& __ia)
614     : _M_size(__ia._M_sz), _M_data(__valarray_get_storage<_Tp>(__ia._M_sz))
615     {
616       std::__valarray_copy
617         (__ia._M_array, __ia._M_index, _Array<_Tp>(_M_data), _M_size);
618     }
619
620   template<typename _Tp> template<class _Dom>
621     inline
622     valarray<_Tp>::valarray(const _Expr<_Dom, _Tp>& __e)
623     : _M_size(__e.size()), _M_data(__valarray_get_storage<_Tp>(_M_size))
624     { std::__valarray_copy(__e, _M_size, _Array<_Tp>(_M_data)); }
625
626   template<typename _Tp>
627     inline
628     valarray<_Tp>::~valarray()
629     {
630       std::__valarray_destroy_elements(_M_data, _M_data + _M_size);
631       std::__valarray_release_memory(_M_data);
632     }
633
634   template<typename _Tp>
635     inline valarray<_Tp>&
636     valarray<_Tp>::operator=(const valarray<_Tp>& __v)
637     {
638       _GLIBCXX_DEBUG_ASSERT(_M_size == __v._M_size);
639       std::__valarray_copy(__v._M_data, _M_size, _M_data);
640       return *this;
641     }
642
643   template<typename _Tp>
644     inline valarray<_Tp>&
645     valarray<_Tp>::operator=(const _Tp& __t)
646     {
647       std::__valarray_fill(_M_data, _M_size, __t);
648       return *this;
649     }
650
651   template<typename _Tp>
652     inline valarray<_Tp>&
653     valarray<_Tp>::operator=(const slice_array<_Tp>& __sa)
654     {
655       _GLIBCXX_DEBUG_ASSERT(_M_size == __sa._M_sz);
656       std::__valarray_copy(__sa._M_array, __sa._M_sz,
657                            __sa._M_stride, _Array<_Tp>(_M_data));
658       return *this;
659     }
660
661   template<typename _Tp>
662     inline valarray<_Tp>&
663     valarray<_Tp>::operator=(const gslice_array<_Tp>& __ga)
664     {
665       _GLIBCXX_DEBUG_ASSERT(_M_size == __ga._M_index.size());
666       std::__valarray_copy(__ga._M_array, _Array<size_t>(__ga._M_index),
667                            _Array<_Tp>(_M_data), _M_size);
668       return *this;
669     }
670
671   template<typename _Tp>
672     inline valarray<_Tp>&
673     valarray<_Tp>::operator=(const mask_array<_Tp>& __ma)
674     {
675       _GLIBCXX_DEBUG_ASSERT(_M_size == __ma._M_sz);
676       std::__valarray_copy(__ma._M_array, __ma._M_mask,
677                            _Array<_Tp>(_M_data), _M_size);
678       return *this;
679     }
680
681   template<typename _Tp>
682     inline valarray<_Tp>&
683     valarray<_Tp>::operator=(const indirect_array<_Tp>& __ia)
684     {
685       _GLIBCXX_DEBUG_ASSERT(_M_size == __ia._M_sz);
686       std::__valarray_copy(__ia._M_array, __ia._M_index,
687                            _Array<_Tp>(_M_data), _M_size);
688       return *this;
689     }
690
691   template<typename _Tp> template<class _Dom>
692     inline valarray<_Tp>&
693     valarray<_Tp>::operator=(const _Expr<_Dom, _Tp>& __e)
694     {
695       _GLIBCXX_DEBUG_ASSERT(_M_size == __e.size());
696       std::__valarray_copy(__e, _M_size, _Array<_Tp>(_M_data));
697       return *this;
698     }
699
700   template<typename _Tp>
701     inline _Expr<_SClos<_ValArray,_Tp>, _Tp>
702     valarray<_Tp>::operator[](slice __s) const
703     {
704       typedef _SClos<_ValArray,_Tp> _Closure;
705       return _Expr<_Closure, _Tp>(_Closure (_Array<_Tp>(_M_data), __s));
706     }
707
708   template<typename _Tp>
709     inline slice_array<_Tp>
710     valarray<_Tp>::operator[](slice __s)
711     { return slice_array<_Tp>(_Array<_Tp>(_M_data), __s); }
712
713   template<typename _Tp>
714     inline _Expr<_GClos<_ValArray,_Tp>, _Tp>
715     valarray<_Tp>::operator[](const gslice& __gs) const
716     {
717       typedef _GClos<_ValArray,_Tp> _Closure;
718       return _Expr<_Closure, _Tp>
719         (_Closure(_Array<_Tp>(_M_data), __gs._M_index->_M_index));
720     }
721
722   template<typename _Tp>
723     inline gslice_array<_Tp>
724     valarray<_Tp>::operator[](const gslice& __gs)
725     {
726       return gslice_array<_Tp>
727         (_Array<_Tp>(_M_data), __gs._M_index->_M_index);
728     }
729
730   template<typename _Tp>
731     inline valarray<_Tp>
732     valarray<_Tp>::operator[](const valarray<bool>& __m) const
733     {
734       size_t __s = 0;
735       size_t __e = __m.size();
736       for (size_t __i=0; __i<__e; ++__i)
737         if (__m[__i]) ++__s;
738       return valarray<_Tp>(mask_array<_Tp>(_Array<_Tp>(_M_data), __s,
739                                            _Array<bool> (__m)));
740     }
741
742   template<typename _Tp>
743     inline mask_array<_Tp>
744     valarray<_Tp>::operator[](const valarray<bool>& __m)
745     {
746       size_t __s = 0;
747       size_t __e = __m.size();
748       for (size_t __i=0; __i<__e; ++__i)
749         if (__m[__i]) ++__s;
750       return mask_array<_Tp>(_Array<_Tp>(_M_data), __s, _Array<bool>(__m));
751     }
752
753   template<typename _Tp>
754     inline _Expr<_IClos<_ValArray,_Tp>, _Tp>
755     valarray<_Tp>::operator[](const valarray<size_t>& __i) const
756     {
757       typedef _IClos<_ValArray,_Tp> _Closure;
758       return _Expr<_Closure, _Tp>(_Closure(*this, __i));
759     }
760
761   template<typename _Tp>
762     inline indirect_array<_Tp>
763     valarray<_Tp>::operator[](const valarray<size_t>& __i)
764     {
765       return indirect_array<_Tp>(_Array<_Tp>(_M_data), __i.size(),
766                                  _Array<size_t>(__i));
767     }
768
769   template<class _Tp>
770     inline size_t 
771     valarray<_Tp>::size() const
772     { return _M_size; }
773
774   template<class _Tp>
775     inline _Tp
776     valarray<_Tp>::sum() const
777     {
778       _GLIBCXX_DEBUG_ASSERT(_M_size > 0);
779       return std::__valarray_sum(_M_data, _M_data + _M_size);
780     }
781
782   template <class _Tp>
783      inline valarray<_Tp>
784      valarray<_Tp>::shift(int __n) const
785      {
786        _Tp* const __a = static_cast<_Tp*>
787          (__builtin_alloca(sizeof(_Tp) * _M_size));
788        if (__n == 0)                          // no shift
789          std::__valarray_copy_construct(_M_data, _M_data + _M_size, __a);
790        else if (__n > 0)         // __n > 0: shift left
791          {                 
792            if (size_t(__n) > _M_size)
793              std::__valarray_default_construct(__a, __a + __n);
794            else
795              {
796                std::__valarray_copy_construct(_M_data + __n,
797                                               _M_data + _M_size, __a);
798                std::__valarray_default_construct(__a + _M_size -__n,
799                                                  __a + _M_size);
800              }
801          }
802        else                        // __n < 0: shift right
803          {                          
804            std::__valarray_copy_construct (_M_data, _M_data + _M_size + __n,
805                                            __a - __n);
806            std::__valarray_default_construct(__a, __a - __n);
807          }
808        return valarray<_Tp>(__a, _M_size);
809      }
810
811   template <class _Tp>
812      inline valarray<_Tp>
813      valarray<_Tp>::cshift (int __n) const
814      {
815        _Tp* const __a = static_cast<_Tp*>
816          (__builtin_alloca (sizeof(_Tp) * _M_size));
817        if (__n == 0)               // no cshift
818          std::__valarray_copy_construct(_M_data, _M_data + _M_size, __a);
819        else if (__n > 0)           // cshift left
820          {               
821            std::__valarray_copy_construct(_M_data, _M_data + __n,
822                                           __a + _M_size - __n);
823            std::__valarray_copy_construct(_M_data + __n, _M_data + _M_size,
824                                           __a);
825          }
826        else                        // cshift right
827          {                       
828            std::__valarray_copy_construct
829              (_M_data + _M_size + __n, _M_data + _M_size, __a);
830            std::__valarray_copy_construct
831              (_M_data, _M_data + _M_size+__n, __a - __n);
832          }
833        return valarray<_Tp>(__a, _M_size);
834      }
835
836   template <class _Tp>
837     inline void
838     valarray<_Tp>::resize (size_t __n, _Tp __c)
839     {
840       // This complication is so to make valarray<valarray<T> > work
841       // even though it is not required by the standard.  Nobody should
842       // be saying valarray<valarray<T> > anyway.  See the specs.
843       std::__valarray_destroy_elements(_M_data, _M_data + _M_size);
844       if (_M_size != __n)
845         {
846           std::__valarray_release_memory(_M_data);
847           _M_size = __n;
848           _M_data = __valarray_get_storage<_Tp>(__n);
849         }
850       std::__valarray_fill_construct(_M_data, _M_data + __n, __c);
851     }
852     
853   template<typename _Tp>
854     inline _Tp
855     valarray<_Tp>::min() const
856     {
857       _GLIBCXX_DEBUG_ASSERT(_M_size > 0);
858       return *std::min_element(_M_data, _M_data+_M_size);
859     }
860
861   template<typename _Tp>
862     inline _Tp
863     valarray<_Tp>::max() const
864     {
865       _GLIBCXX_DEBUG_ASSERT(_M_size > 0);
866       return *std::max_element(_M_data, _M_data+_M_size);
867     }
868   
869   template<class _Tp>
870     inline _Expr<_ValFunClos<_ValArray, _Tp>, _Tp>
871     valarray<_Tp>::apply(_Tp func(_Tp)) const
872     {
873       typedef _ValFunClos<_ValArray, _Tp> _Closure;
874       return _Expr<_Closure, _Tp>(_Closure(*this, func));
875     }
876
877   template<class _Tp>
878     inline _Expr<_RefFunClos<_ValArray, _Tp>, _Tp>
879     valarray<_Tp>::apply(_Tp func(const _Tp &)) const
880     {
881       typedef _RefFunClos<_ValArray, _Tp> _Closure;
882       return _Expr<_Closure, _Tp>(_Closure(*this, func));
883     }
884
885 #define _DEFINE_VALARRAY_UNARY_OPERATOR(_Op, _Name)                     \
886   template<typename _Tp>                                                \
887     inline typename valarray<_Tp>::template _UnaryOp<_Name>::_Rt        \
888     valarray<_Tp>::operator _Op() const                                 \
889     {                                                                   \
890       typedef _UnClos<_Name, _ValArray, _Tp> _Closure;                  \
891       typedef typename __fun<_Name, _Tp>::result_type _Rt;              \
892       return _Expr<_Closure, _Rt>(_Closure(*this));                     \
893     }
894
895     _DEFINE_VALARRAY_UNARY_OPERATOR(+, __unary_plus)
896     _DEFINE_VALARRAY_UNARY_OPERATOR(-, __negate)
897     _DEFINE_VALARRAY_UNARY_OPERATOR(~, __bitwise_not)
898     _DEFINE_VALARRAY_UNARY_OPERATOR (!, __logical_not)
899
900 #undef _DEFINE_VALARRAY_UNARY_OPERATOR
901
902 #define _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(_Op, _Name)               \
903   template<class _Tp>                                                   \
904     inline valarray<_Tp>&                                               \
905     valarray<_Tp>::operator _Op##=(const _Tp &__t)                      \
906     {                                                                   \
907       _Array_augmented_##_Name(_Array<_Tp>(_M_data), _M_size, __t);     \
908       return *this;                                                     \
909     }                                                                   \
910                                                                         \
911   template<class _Tp>                                                   \
912     inline valarray<_Tp>&                                               \
913     valarray<_Tp>::operator _Op##=(const valarray<_Tp> &__v)            \
914     {                                                                   \
915       _GLIBCXX_DEBUG_ASSERT(_M_size == __v._M_size);                    \
916       _Array_augmented_##_Name(_Array<_Tp>(_M_data), _M_size,           \
917                                _Array<_Tp>(__v._M_data));               \
918       return *this;                                                     \
919     }
920
921 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(+, __plus)
922 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(-, __minus)
923 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(*, __multiplies)
924 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(/, __divides)
925 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(%, __modulus)
926 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(^, __bitwise_xor)
927 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(&, __bitwise_and)
928 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(|, __bitwise_or)
929 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(<<, __shift_left)
930 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(>>, __shift_right)
931
932 #undef _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT
933
934 #define _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(_Op, _Name)          \
935   template<class _Tp> template<class _Dom>                              \
936     inline valarray<_Tp>&                                               \
937     valarray<_Tp>::operator _Op##=(const _Expr<_Dom, _Tp>& __e)         \
938     {                                                                   \
939       _Array_augmented_##_Name(_Array<_Tp>(_M_data), __e, _M_size);     \
940       return *this;                                                     \
941     }
942
943 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(+, __plus)
944 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(-, __minus)
945 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(*, __multiplies)
946 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(/, __divides)
947 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(%, __modulus)
948 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(^, __bitwise_xor)
949 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(&, __bitwise_and)
950 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(|, __bitwise_or)
951 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(<<, __shift_left)
952 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(>>, __shift_right)
953
954 #undef _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT
955     
956
957 #define _DEFINE_BINARY_OPERATOR(_Op, _Name)                             \
958   template<typename _Tp>                                                \
959     inline _Expr<_BinClos<_Name, _ValArray, _ValArray, _Tp, _Tp>,       \
960                  typename __fun<_Name, _Tp>::result_type>               \
961     operator _Op(const valarray<_Tp>& __v, const valarray<_Tp>& __w)    \
962     {                                                                   \
963       _GLIBCXX_DEBUG_ASSERT(__v.size() == __w.size());                  \
964       typedef _BinClos<_Name, _ValArray, _ValArray, _Tp, _Tp> _Closure; \
965       typedef typename __fun<_Name, _Tp>::result_type _Rt;              \
966       return _Expr<_Closure, _Rt>(_Closure(__v, __w));                  \
967     }                                                                   \
968                                                                         \
969   template<typename _Tp>                                                \
970     inline _Expr<_BinClos<_Name, _ValArray,_Constant, _Tp, _Tp>,        \
971                  typename __fun<_Name, _Tp>::result_type>               \
972     operator _Op(const valarray<_Tp>& __v, const _Tp& __t)              \
973     {                                                                   \
974       typedef _BinClos<_Name, _ValArray, _Constant, _Tp, _Tp> _Closure; \
975       typedef typename __fun<_Name, _Tp>::result_type _Rt;              \
976       return _Expr<_Closure, _Rt>(_Closure(__v, __t));                  \
977     }                                                                   \
978                                                                         \
979   template<typename _Tp>                                                \
980     inline _Expr<_BinClos<_Name, _Constant, _ValArray, _Tp, _Tp>,       \
981                  typename __fun<_Name, _Tp>::result_type>               \
982     operator _Op(const _Tp& __t, const valarray<_Tp>& __v)              \
983     {                                                                   \
984       typedef _BinClos<_Name, _Constant, _ValArray, _Tp, _Tp> _Closure; \
985       typedef typename __fun<_Name, _Tp>::result_type _Rt;              \
986       return _Expr<_Closure, _Tp>(_Closure(__t, __v));                  \
987     }
988
989 _DEFINE_BINARY_OPERATOR(+, __plus)
990 _DEFINE_BINARY_OPERATOR(-, __minus)
991 _DEFINE_BINARY_OPERATOR(*, __multiplies)
992 _DEFINE_BINARY_OPERATOR(/, __divides)
993 _DEFINE_BINARY_OPERATOR(%, __modulus)
994 _DEFINE_BINARY_OPERATOR(^, __bitwise_xor)
995 _DEFINE_BINARY_OPERATOR(&, __bitwise_and)
996 _DEFINE_BINARY_OPERATOR(|, __bitwise_or)
997 _DEFINE_BINARY_OPERATOR(<<, __shift_left)
998 _DEFINE_BINARY_OPERATOR(>>, __shift_right)
999 _DEFINE_BINARY_OPERATOR(&&, __logical_and)
1000 _DEFINE_BINARY_OPERATOR(||, __logical_or)
1001 _DEFINE_BINARY_OPERATOR(==, __equal_to)
1002 _DEFINE_BINARY_OPERATOR(!=, __not_equal_to)
1003 _DEFINE_BINARY_OPERATOR(<, __less)
1004 _DEFINE_BINARY_OPERATOR(>, __greater)
1005 _DEFINE_BINARY_OPERATOR(<=, __less_equal)
1006 _DEFINE_BINARY_OPERATOR(>=, __greater_equal)
1007
1008 } // namespace std
1009
1010 #endif /* _GLIBCXX_VALARRAY */