1 // The template and inlines for the -*- C++ -*- valarray class.
3 // Copyright (C) 1997-1999 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr>
33 #define _CPP_VALARRAY 1
35 #include <bits/c++config.h>
36 #include <bits/std_cstddef.h>
37 #include <bits/std_cmath.h>
38 #include <bits/std_cstdlib.h>
39 #include <bits/std_numeric.h>
40 #include <bits/std_functional.h>
41 #include <bits/std_algorithm.h>
45 template<class _Clos, typename _Tp> class _Expr;
47 template<typename _Tp1, typename _Tp2> class _ValArray;
49 template<template<class> class _Oper,
50 template<class, class> class _Meta, class _Dom> struct _UnClos;
52 template<template<class> class _Oper,
53 template<class, class> class _Meta1,
54 template<class, class> class _Meta2,
55 class _Dom1, class _Dom2> class _BinClos;
57 template<template<class, class> class _Meta, class _Dom> class _SClos;
59 template<template<class, class> class _Meta, class _Dom> class _GClos;
61 template<template<class, class> class _Meta, class _Dom> class _IClos;
63 template<template<class, class> class _Meta, class _Dom> class _ValFunClos;
65 template<template<class, class> class _Meta, class _Dom> class _RefFunClos;
67 template<class _Tp> struct _Unary_plus;
68 template<class _Tp> struct _Bitwise_and;
69 template<class _Tp> struct _Bitwise_or;
70 template<class _Tp> struct _Bitwise_xor;
71 template<class _Tp> struct _Bitwise_not;
72 template<class _Tp> struct _Shift_left;
73 template<class _Tp> struct _Shift_right;
75 template<class _Tp> class valarray; // An array of type _Tp
76 class slice; // BLAS-like slice out of an array
77 template<class _Tp> class slice_array;
78 class gslice; // generalized slice out of an array
79 template<class _Tp> class gslice_array;
80 template<class _Tp> class mask_array; // masked array
81 template<class _Tp> class indirect_array; // indirected array
85 #include <bits/valarray_array.h>
86 #include <bits/valarray_meta.h>
90 template<class _Tp> class valarray
93 typedef _Tp value_type;
95 // _lib.valarray.cons_ construct/destroy:
97 explicit valarray(size_t);
98 valarray(const _Tp&, size_t);
99 valarray(const _Tp* __restrict__, size_t);
100 valarray(const valarray&);
101 valarray(const slice_array<_Tp>&);
102 valarray(const gslice_array<_Tp>&);
103 valarray(const mask_array<_Tp>&);
104 valarray(const indirect_array<_Tp>&);
106 valarray(const _Expr<_Dom,_Tp>& __e);
109 // _lib.valarray.assign_ assignment:
110 valarray<_Tp>& operator=(const valarray<_Tp>&);
111 valarray<_Tp>& operator=(const _Tp&);
112 valarray<_Tp>& operator=(const slice_array<_Tp>&);
113 valarray<_Tp>& operator=(const gslice_array<_Tp>&);
114 valarray<_Tp>& operator=(const mask_array<_Tp>&);
115 valarray<_Tp>& operator=(const indirect_array<_Tp>&);
117 template<class _Dom> valarray<_Tp>&
118 operator= (const _Expr<_Dom,_Tp>&);
120 // _lib.valarray.access_ element access:
121 _Tp operator[](size_t) const;
122 _Tp& operator[](size_t);
123 // _lib.valarray.sub_ subset operations:
124 _Expr<_SClos<_ValArray,_Tp>, _Tp> operator[](slice) const;
125 slice_array<_Tp> operator[](slice);
126 _Expr<_GClos<_ValArray,_Tp>, _Tp> operator[](const gslice&) const;
127 gslice_array<_Tp> operator[](const gslice&);
128 valarray<_Tp> operator[](const valarray<bool>&) const;
129 mask_array<_Tp> operator[](const valarray<bool>&);
130 _Expr<_IClos<_ValArray, _Tp>, _Tp>
131 operator[](const valarray<size_t>&) const;
132 indirect_array<_Tp> operator[](const valarray<size_t>&);
134 // _lib.valarray.unary_ unary operators:
135 _Expr<_UnClos<_Unary_plus,_ValArray,_Tp>,_Tp> operator+ () const;
136 _Expr<_UnClos<negate,_ValArray,_Tp>,_Tp> operator- () const;
137 _Expr<_UnClos<_Bitwise_not,_ValArray,_Tp>,_Tp> operator~ () const;
138 _Expr<_UnClos<logical_not,_ValArray,_Tp>,bool> operator! () const;
140 // _lib.valarray.cassign_ computed assignment:
141 valarray<_Tp>& operator*= (const _Tp&);
142 valarray<_Tp>& operator/= (const _Tp&);
143 valarray<_Tp>& operator%= (const _Tp&);
144 valarray<_Tp>& operator+= (const _Tp&);
145 valarray<_Tp>& operator-= (const _Tp&);
146 valarray<_Tp>& operator^= (const _Tp&);
147 valarray<_Tp>& operator&= (const _Tp&);
148 valarray<_Tp>& operator|= (const _Tp&);
149 valarray<_Tp>& operator<<=(const _Tp&);
150 valarray<_Tp>& operator>>=(const _Tp&);
151 valarray<_Tp>& operator*= (const valarray<_Tp>&);
152 valarray<_Tp>& operator/= (const valarray<_Tp>&);
153 valarray<_Tp>& operator%= (const valarray<_Tp>&);
154 valarray<_Tp>& operator+= (const valarray<_Tp>&);
155 valarray<_Tp>& operator-= (const valarray<_Tp>&);
156 valarray<_Tp>& operator^= (const valarray<_Tp>&);
157 valarray<_Tp>& operator|= (const valarray<_Tp>&);
158 valarray<_Tp>& operator&= (const valarray<_Tp>&);
159 valarray<_Tp>& operator<<=(const valarray<_Tp>&);
160 valarray<_Tp>& operator>>=(const valarray<_Tp>&);
163 valarray<_Tp>& operator*= (const _Expr<_Dom,_Tp>&);
165 valarray<_Tp>& operator/= (const _Expr<_Dom,_Tp>&);
167 valarray<_Tp>& operator%= (const _Expr<_Dom,_Tp>&);
169 valarray<_Tp>& operator+= (const _Expr<_Dom,_Tp>&);
171 valarray<_Tp>& operator-= (const _Expr<_Dom,_Tp>&);
173 valarray<_Tp>& operator^= (const _Expr<_Dom,_Tp>&);
175 valarray<_Tp>& operator|= (const _Expr<_Dom,_Tp>&);
177 valarray<_Tp>& operator&= (const _Expr<_Dom,_Tp>&);
179 valarray<_Tp>& operator<<=(const _Expr<_Dom,_Tp>&);
181 valarray<_Tp>& operator>>=(const _Expr<_Dom,_Tp>&);
184 // _lib.valarray.members_ member functions:
190 // // FIXME: Extension
191 // _Tp product () const;
193 valarray<_Tp> shift (int) const;
194 valarray<_Tp> cshift(int) const;
195 _Expr<_ValFunClos<_ValArray,_Tp>,_Tp> apply(_Tp func(_Tp)) const;
196 _Expr<_RefFunClos<_ValArray,_Tp>,_Tp> apply(_Tp func(const _Tp&)) const;
197 void resize(size_t __size, _Tp __c = _Tp());
201 _Tp* __restrict__ _M_data;
203 friend class _Array<_Tp>;
207 template<typename _Tp> struct _Unary_plus : unary_function<_Tp,_Tp> {
208 _Tp operator() (const _Tp& __t) const { return __t; }
211 template<typename _Tp> struct _Bitwise_and : binary_function<_Tp,_Tp,_Tp> {
212 _Tp operator() (_Tp __x, _Tp __y) const { return __x & __y; }
215 template<typename _Tp> struct _Bitwise_or : binary_function<_Tp,_Tp,_Tp> {
216 _Tp operator() (_Tp __x, _Tp __y) const { return __x | __y; }
219 template<typename _Tp> struct _Bitwise_xor : binary_function<_Tp,_Tp,_Tp> {
220 _Tp operator() (_Tp __x, _Tp __y) const { return __x ^ __y; }
223 template<typename _Tp> struct _Bitwise_not : unary_function<_Tp,_Tp> {
224 _Tp operator() (_Tp __t) const { return ~__t; }
227 template<typename _Tp> struct _Shift_left : unary_function<_Tp,_Tp> {
228 _Tp operator() (_Tp __x, _Tp __y) const { return __x << __y; }
231 template<typename _Tp> struct _Shift_right : unary_function<_Tp,_Tp> {
232 _Tp operator() (_Tp __x, _Tp __y) const { return __x >> __y; }
236 template<typename _Tp>
238 valarray<_Tp>::operator[] (size_t __i) const
239 { return _M_data[__i]; }
241 template<typename _Tp>
243 valarray<_Tp>::operator[] (size_t __i)
244 { return _M_data[__i]; }
248 #include <bits/slice.h>
249 #include <bits/slice_array.h>
250 #include <bits/gslice.h>
251 #include <bits/gslice_array.h>
252 #include <bits/mask_array.h>
253 #include <bits/indirect_array.h>
257 template<typename _Tp>
258 inline valarray<_Tp>::valarray () : _M_size (0), _M_data (0) {}
260 template<typename _Tp>
261 inline valarray<_Tp>::valarray (size_t __n)
263 _M_data(static_cast<_Tp*__restrict__>
264 (__valarray_get_memory(__n * sizeof (_Tp))))
265 { __valarray_default_construct(_M_data, _M_data + __n); }
267 template<typename _Tp>
268 inline valarray<_Tp>::valarray (const _Tp& __t, size_t __n)
270 _M_data(static_cast<_Tp*__restrict__>
271 (__valarray_get_memory(__n * sizeof (_Tp))))
272 { __valarray_fill_construct (_M_data, _M_data + __n, __t); }
274 template<typename _Tp>
275 inline valarray<_Tp>::valarray (const _Tp* __restrict__ __p, size_t __n)
277 _M_data(static_cast<_Tp*__restrict__>
278 (__valarray_get_memory(__n * sizeof (_Tp))))
279 { __valarray_copy_construct (__p, __p + __n, _M_data); }
281 template<typename _Tp>
282 inline valarray<_Tp>::valarray (const valarray<_Tp>& __v)
283 : _M_size(__v._M_size),
284 _M_data(static_cast<_Tp*__restrict__>
285 (__valarray_get_memory(__v._M_size * sizeof (_Tp))))
286 { __valarray_copy_construct (__v._M_data, __v._M_data + _M_size, _M_data); }
288 template<typename _Tp>
289 inline valarray<_Tp>::valarray (const slice_array<_Tp>& __sa)
290 : _M_size(__sa._M_sz),
291 _M_data(static_cast<_Tp*__restrict__>
292 (__valarray_get_memory(__sa._M_sz * sizeof (_Tp))))
294 __valarray_copy_construct
295 (__sa._M_array, __sa._M_sz, __sa._M_stride, _Array<_Tp>(_M_data));
298 template<typename _Tp>
299 inline valarray<_Tp>::valarray (const gslice_array<_Tp>& __ga)
300 : _M_size(__ga._M_index.size()),
301 _M_data(static_cast<_Tp*__restrict__>
302 (__valarray_get_memory(_M_size * sizeof (_Tp))))
304 __valarray_copy_construct
305 (__ga._M_array, _Array<size_t>(__ga._M_index),
306 _Array<_Tp>(_M_data), _M_size);
309 template<typename _Tp>
310 inline valarray<_Tp>::valarray (const mask_array<_Tp>& __ma)
311 : _M_size(__ma._M_sz),
312 _M_data(static_cast<_Tp*__restrict__>
313 (__valarray_get_memory(__ma._M_sz * sizeof (_Tp))))
315 __valarray_copy_construct
316 (__ma._M_array, __ma._M_mask, _Array<_Tp>(_M_data), _M_size);
319 template<typename _Tp>
320 inline valarray<_Tp>::valarray (const indirect_array<_Tp>& __ia)
321 : _M_size(__ia._M_sz),
322 _M_data(static_cast<_Tp*__restrict__>
323 (__valarray_get_memory(__ia._M_sz * sizeof (_Tp))))
325 __valarray_copy_construct
326 (__ia._M_array, __ia._M_index, _Array<_Tp>(_M_data), _M_size);
329 template<typename _Tp> template<class _Dom>
330 inline valarray<_Tp>::valarray (const _Expr<_Dom, _Tp>& __e)
331 : _M_size(__e.size ()),
332 _M_data(static_cast<_Tp*__restrict__>
333 (__valarray_copy_construct(_M_size * sizeof (_Tp))))
334 { __valarray_copy_construct (__e, _M_size, _Array<_Tp>(_M_data)); }
336 template<typename _Tp>
337 inline valarray<_Tp>::~valarray ()
339 __valarray_destroy_elements(_M_data, _M_data + _M_size);
340 __valarray_release_memory(_M_data);
343 template<typename _Tp>
344 inline valarray<_Tp>&
345 valarray<_Tp>::operator= (const valarray<_Tp>& __v)
347 __valarray_copy(__v._M_data, _M_size, _M_data);
351 template<typename _Tp>
352 inline valarray<_Tp>&
353 valarray<_Tp>::operator= (const _Tp& __t)
355 __valarray_fill (_M_data, _M_size, __t);
359 template<typename _Tp>
360 inline valarray<_Tp>&
361 valarray<_Tp>::operator= (const slice_array<_Tp>& __sa)
363 __valarray_copy (__sa._M_array, __sa._M_sz,
364 __sa._M_stride, _Array<_Tp>(_M_data));
368 template<typename _Tp>
369 inline valarray<_Tp>&
370 valarray<_Tp>::operator= (const gslice_array<_Tp>& __ga)
372 __valarray_copy (__ga._M_array, _Array<size_t>(__ga._M_index),
373 _Array<_Tp>(_M_data), _M_size);
377 template<typename _Tp>
378 inline valarray<_Tp>&
379 valarray<_Tp>::operator= (const mask_array<_Tp>& __ma)
381 __valarray_copy (__ma._M_array, __ma._M_mask,
382 _Array<_Tp>(_M_data), _M_size);
386 template<typename _Tp>
387 inline valarray<_Tp>&
388 valarray<_Tp>::operator= (const indirect_array<_Tp>& __ia)
390 __valarray_copy (__ia._M_array, __ia._M_index,
391 _Array<_Tp>(_M_data), _M_size);
395 template<typename _Tp> template<class _Dom>
396 inline valarray<_Tp>&
397 valarray<_Tp>::operator= (const _Expr<_Dom, _Tp>& __e)
399 __valarray_copy (__e, _M_size, _Array<_Tp>(_M_data));
403 template<typename _Tp>
404 inline _Expr<_SClos<_ValArray,_Tp>, _Tp>
405 valarray<_Tp>::operator[] (slice __s) const
407 typedef _SClos<_ValArray,_Tp> _Closure;
408 return _Expr<_Closure, _Tp> (_Closure (_Array<_Tp>(_M_data), __s));
411 template<typename _Tp>
412 inline slice_array<_Tp>
413 valarray<_Tp>::operator[] (slice __s)
415 return slice_array<_Tp> (_Array<_Tp>(_M_data), __s);
418 template<typename _Tp>
419 inline _Expr<_GClos<_ValArray,_Tp>, _Tp>
420 valarray<_Tp>::operator[] (const gslice& __gs) const
422 typedef _GClos<_ValArray,_Tp> _Closure;
423 return _Expr<_Closure, _Tp>
424 (_Closure (_Array<_Tp>(_M_data), __gs._M_index->_M_index));
427 template<typename _Tp>
428 inline gslice_array<_Tp>
429 valarray<_Tp>::operator[] (const gslice& __gs)
431 return gslice_array<_Tp>
432 (_Array<_Tp>(_M_data), __gs._M_index->_M_index);
435 template<typename _Tp>
437 valarray<_Tp>::operator[] (const valarray<bool>& __m) const
440 size_t __e (__m.size ());
441 for (size_t __i=0; __i<__e; ++__i)
443 return valarray<_Tp> (mask_array<_Tp> (_Array<_Tp>(_M_data), __s,
444 _Array<bool> (__m)));
447 template<typename _Tp>
448 inline mask_array<_Tp>
449 valarray<_Tp>::operator[] (const valarray<bool>& __m)
452 size_t __e (__m.size ());
453 for (size_t __i=0; __i<__e; ++__i)
455 return mask_array<_Tp> (_Array<_Tp>(_M_data), __s, _Array<bool> (__m));
458 template<typename _Tp>
459 inline _Expr<_IClos<_ValArray,_Tp>, _Tp>
460 valarray<_Tp>::operator[] (const valarray<size_t>& __i) const
462 typedef _IClos<_ValArray,_Tp> _Closure;
463 return _Expr<_Closure, _Tp> (_Closure (*this, __i));
466 template<typename _Tp>
467 inline indirect_array<_Tp>
468 valarray<_Tp>::operator[] (const valarray<size_t>& __i)
470 return indirect_array<_Tp> (_Array<_Tp>(_M_data), __i.size(),
471 _Array<size_t> (__i));
475 inline size_t valarray<_Tp>::size () const { return _M_size; }
479 valarray<_Tp>::sum () const
481 return __valarray_sum(_M_data, _M_data + _M_size);
484 // template<typename _Tp>
486 // valarray<_Tp>::product () const
488 // return __valarray_product(_M_data, _M_data + _M_size);
493 valarray<_Tp>::shift (int __n) const
495 _Tp* const __a = static_cast<_Tp*>
496 (__builtin_alloca (sizeof(_Tp) * _M_size));
497 if (! __n) // __n == 0: no shift
498 __valarray_copy_construct(_M_data, _M_data + _M_size, __a);
499 else if (__n > 0) { // __n > 0: shift left
501 __valarray_default_construct(__a, __a + __n);
503 __valarray_copy_construct(_M_data+__n, _M_data + _M_size, __a);
504 __valarray_default_construct(__a+_M_size-__n, __a + _M_size);
507 else { // __n < 0: shift right
508 __valarray_copy_construct (_M_data, _M_data+_M_size+__n, __a-__n);
509 __valarray_default_construct(__a, __a - __n);
511 return valarray<_Tp> (__a, _M_size);
516 valarray<_Tp>::cshift (int __n) const
518 _Tp* const __a = static_cast<_Tp*>
519 (__builtin_alloca (sizeof(_Tp) * _M_size));
520 if (! __n) // __n == 0: no cshift
521 __valarray_copy_construct(_M_data, _M_data + _M_size, __a);
522 else if (__n > 0) { // __n > 0: cshift left
523 __valarray_copy_construct(_M_data, _M_data+__n, __a+_M_size-__n);
524 __valarray_copy_construct(_M_data+__n, _M_data + _M_size, __a);
526 else { // __n < 0: cshift right
527 __valarray_copy_construct
528 (_M_data + _M_size+__n, _M_data + _M_size, __a);
529 __valarray_copy_construct
530 (_M_data, _M_data + _M_size+__n, __a - __n);
532 return valarray<_Tp> (__a, _M_size);
537 valarray<_Tp>::resize (size_t __n, _Tp __c)
539 if (_M_size != __n) {
540 __valarray_destroy_elements(_M_data, _M_data + _M_size);
541 __valarray_release_memory(_M_data);
543 _M_data = static_cast<_Tp*__restrict__>
544 (__valarray_get_memory(__n * sizeof (_Tp)));
545 __valarray_fill_construct(_M_data, _M_data + __n, __c);
548 // this is so to make valarray<valarray<T> > work
549 // even though it is not required by the standard.
550 __valarray_destroy_elements(_M_data, _M_data + _M_size);
551 __valarray_fill_construct(_M_data, _M_data + _M_size, __c);
555 template<typename _Tp>
557 valarray<_Tp>::min() const
559 return *min_element (_M_data, _M_data+_M_size);
562 template<typename _Tp>
564 valarray<_Tp>::max() const
566 return *max_element (_M_data, _M_data+_M_size);
570 inline _Expr<_ValFunClos<_ValArray,_Tp>,_Tp>
571 valarray<_Tp>::apply (_Tp func (_Tp)) const
573 typedef _ValFunClos<_ValArray,_Tp> _Closure;
574 return _Expr<_Closure,_Tp> (_Closure (*this, func));
578 inline _Expr<_RefFunClos<_ValArray,_Tp>,_Tp>
579 valarray<_Tp>::apply (_Tp func (const _Tp &)) const
581 typedef _RefFunClos<_ValArray,_Tp> _Closure;
582 return _Expr<_Closure,_Tp> (_Closure (*this, func));
585 #define _DEFINE_VALARRAY_UNARY_OPERATOR(_Op, _Name) \
586 template<typename _Tp> \
587 inline _Expr<_UnClos<_Name,_ValArray,_Tp>, _Tp> \
588 valarray<_Tp>::operator##_Op() const \
590 typedef _UnClos<_Name,_ValArray,_Tp> _Closure; \
591 return _Expr<_Closure, _Tp> (_Closure (*this)); \
594 _DEFINE_VALARRAY_UNARY_OPERATOR(+, _Unary_plus)
595 _DEFINE_VALARRAY_UNARY_OPERATOR(-, negate)
596 _DEFINE_VALARRAY_UNARY_OPERATOR(~, _Bitwise_not)
598 #undef _DEFINE_VALARRAY_UNARY_OPERATOR
600 template<typename _Tp>
601 inline _Expr<_UnClos<logical_not,_ValArray,_Tp>, bool>
602 valarray<_Tp>::operator!() const
604 typedef _UnClos<logical_not,_ValArray,_Tp> _Closure;
605 return _Expr<_Closure, bool> (_Closure (*this));
608 #define _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(_Op, _Name) \
609 template<class _Tp> \
610 inline valarray<_Tp> & \
611 valarray<_Tp>::operator##_Op##= (const _Tp &__t) \
613 _Array_augmented_##_Name (_Array<_Tp>(_M_data), _M_size, __t); \
617 template<class _Tp> \
618 inline valarray<_Tp> & \
619 valarray<_Tp>::operator##_Op##= (const valarray<_Tp> &__v) \
621 _Array_augmented_##_Name (_Array<_Tp>(_M_data), _M_size, \
622 _Array<_Tp>(__v._M_data)); \
626 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(+, plus)
627 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(-, minus)
628 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(*, multiplies)
629 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(/, divides)
630 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(%, modulus)
631 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(^, xor)
632 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(&, and)
633 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(|, or)
634 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(<<, shift_left)
635 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(>>, shift_right)
637 #undef _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT
645 #define _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(_Op, _Name) \
646 template<class _Tp> template<class _Dom> \
647 inline valarray<_Tp> & \
648 valarray<_Tp>::operator##_Op##= (const _Expr<_Dom,_Tp> &__e) \
650 _Array_augmented_##_Name (_Array<_Tp>(_M_data), __e, _M_size); \
654 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(+, plus)
655 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(-, minus)
656 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(*, multiplies)
657 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(/, divides)
658 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(%, modulus)
659 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(^, xor)
660 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(&, and)
661 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(|, or)
662 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(<<, shift_left)
663 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(>>, shift_right)
665 #undef _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT
668 #define _DEFINE_BINARY_OPERATOR(_Op, _Name) \
669 template<typename _Tp> \
670 inline _Expr<_BinClos<_Name,_ValArray,_ValArray,_Tp,_Tp>, _Tp> \
671 operator##_Op (const valarray<_Tp> &__v, const valarray<_Tp> &__w) \
673 typedef _BinClos<_Name,_ValArray,_ValArray,_Tp,_Tp> _Closure; \
674 return _Expr<_Closure, _Tp> (_Closure (__v, __w)); \
677 template<typename _Tp> \
678 inline _Expr<_BinClos<_Name,_ValArray,_Constant,_Tp,_Tp>,_Tp> \
679 operator##_Op (const valarray<_Tp> &__v, const _Tp &__t) \
681 typedef _BinClos<_Name,_ValArray,_Constant,_Tp,_Tp> _Closure; \
682 return _Expr<_Closure, _Tp> (_Closure (__v, __t)); \
685 template<typename _Tp> \
686 inline _Expr<_BinClos<_Name,_Constant,_ValArray,_Tp,_Tp>,_Tp> \
687 operator##_Op (const _Tp &__t, const valarray<_Tp> &__v) \
689 typedef _BinClos<_Name,_Constant,_ValArray,_Tp,_Tp> _Closure; \
690 return _Expr<_Closure, _Tp> (_Closure (__t, __v)); \
693 _DEFINE_BINARY_OPERATOR(+, plus)
694 _DEFINE_BINARY_OPERATOR(-, minus)
695 _DEFINE_BINARY_OPERATOR(*, multiplies)
696 _DEFINE_BINARY_OPERATOR(/, divides)
697 _DEFINE_BINARY_OPERATOR(%, modulus)
698 _DEFINE_BINARY_OPERATOR(^, _Bitwise_xor)
699 _DEFINE_BINARY_OPERATOR(&, _Bitwise_and)
700 _DEFINE_BINARY_OPERATOR(|, _Bitwise_or)
701 _DEFINE_BINARY_OPERATOR(<<, _Shift_left)
702 _DEFINE_BINARY_OPERATOR(>>, _Shift_right)
704 #undef _DEFINE_BINARY_OPERATOR
706 #define _DEFINE_LOGICAL_OPERATOR(_Op, _Name) \
707 template<typename _Tp> \
708 inline _Expr<_BinClos<_Name,_ValArray,_ValArray,_Tp,_Tp>,bool> \
709 operator##_Op (const valarray<_Tp> &__v, const valarray<_Tp> &__w) \
711 typedef _BinClos<_Name,_ValArray,_ValArray,_Tp,_Tp> _Closure; \
712 return _Expr<_Closure, bool> (_Closure (__v, __w)); \
715 template<class _Tp> \
716 inline _Expr<_BinClos<_Name,_ValArray,_Constant,_Tp,_Tp>,bool> \
717 operator##_Op (const valarray<_Tp> &__v, const _Tp &__t) \
719 typedef _BinClos<_Name,_ValArray,_Constant,_Tp,_Tp> _Closure; \
720 return _Expr<_Closure, bool> (_Closure (__v, __t)); \
723 template<class _Tp> \
724 inline _Expr<_BinClos<_Name,_Constant,_ValArray,_Tp,_Tp>,bool> \
725 operator##_Op (const _Tp &__t, const valarray<_Tp> &__v) \
727 typedef _BinClos<_Name,_Constant,_ValArray,_Tp,_Tp> _Closure; \
728 return _Expr<_Closure, bool> (_Closure (__t, __v)); \
731 _DEFINE_LOGICAL_OPERATOR(&&, logical_and)
732 _DEFINE_LOGICAL_OPERATOR(||, logical_or)
733 _DEFINE_LOGICAL_OPERATOR(==, equal_to)
734 _DEFINE_LOGICAL_OPERATOR(!=, not_equal_to)
735 _DEFINE_LOGICAL_OPERATOR(<, less)
736 _DEFINE_LOGICAL_OPERATOR(>, greater)
737 _DEFINE_LOGICAL_OPERATOR(<=, less_equal)
738 _DEFINE_LOGICAL_OPERATOR(>=, greater_equal)
740 #undef _DEFINE_VALARRAY_OPERATOR
744 #endif // _CPP_VALARRAY