]> gcc.gnu.org Git - gcc.git/blob - libstdc++-v3/bits/std_valarray.h
valarray_array.h (__valarray_get_storage): New function.
[gcc.git] / libstdc++-v3 / bits / std_valarray.h
1 // The template and inlines for the -*- C++ -*- valarray class.
2
3 // Copyright (C) 1997-1999, 2000 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
10
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.
15
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,
19 // USA.
20
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.
29
30 // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr>
31
32 #ifndef _CPP_VALARRAY
33 #define _CPP_VALARRAY 1
34
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>
42
43 namespace std {
44
45 template<class _Clos, typename _Tp> class _Expr;
46
47 template<typename _Tp1, typename _Tp2> class _ValArray;
48
49 template<template<class> class _Oper,
50 template<class, class> class _Meta, class _Dom> struct _UnClos;
51
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;
56
57 template<template<class, class> class _Meta, class _Dom> class _SClos;
58
59 template<template<class, class> class _Meta, class _Dom> class _GClos;
60
61 template<template<class, class> class _Meta, class _Dom> class _IClos;
62
63 template<template<class, class> class _Meta, class _Dom> class _ValFunClos;
64
65 template<template<class, class> class _Meta, class _Dom> class _RefFunClos;
66
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;
74
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
82
83 }
84
85 #include <bits/valarray_array.h>
86 #include <bits/valarray_meta.h>
87
88 namespace std {
89
90 template<class _Tp> class valarray
91 {
92 public:
93 typedef _Tp value_type;
94
95 // _lib.valarray.cons_ construct/destroy:
96 valarray();
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>&);
105 template<class _Dom>
106 valarray(const _Expr<_Dom,_Tp>& __e);
107 ~valarray();
108
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>&);
116
117 template<class _Dom> valarray<_Tp>&
118 operator= (const _Expr<_Dom,_Tp>&);
119
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>&);
133
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;
139
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>&);
161
162 template<class _Dom>
163 valarray<_Tp>& operator*= (const _Expr<_Dom,_Tp>&);
164 template<class _Dom>
165 valarray<_Tp>& operator/= (const _Expr<_Dom,_Tp>&);
166 template<class _Dom>
167 valarray<_Tp>& operator%= (const _Expr<_Dom,_Tp>&);
168 template<class _Dom>
169 valarray<_Tp>& operator+= (const _Expr<_Dom,_Tp>&);
170 template<class _Dom>
171 valarray<_Tp>& operator-= (const _Expr<_Dom,_Tp>&);
172 template<class _Dom>
173 valarray<_Tp>& operator^= (const _Expr<_Dom,_Tp>&);
174 template<class _Dom>
175 valarray<_Tp>& operator|= (const _Expr<_Dom,_Tp>&);
176 template<class _Dom>
177 valarray<_Tp>& operator&= (const _Expr<_Dom,_Tp>&);
178 template<class _Dom>
179 valarray<_Tp>& operator<<=(const _Expr<_Dom,_Tp>&);
180 template<class _Dom>
181 valarray<_Tp>& operator>>=(const _Expr<_Dom,_Tp>&);
182
183
184 // _lib.valarray.members_ member functions:
185 size_t size() const;
186 _Tp sum() const;
187 _Tp min() const;
188 _Tp max() const;
189
190 // // FIXME: Extension
191 // _Tp product () const;
192
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());
198
199 private:
200 size_t _M_size;
201 _Tp* __restrict__ _M_data;
202
203 friend class _Array<_Tp>;
204 };
205
206
207 template<typename _Tp> struct _Unary_plus : unary_function<_Tp,_Tp> {
208 _Tp operator() (const _Tp& __t) const { return __t; }
209 };
210
211 template<typename _Tp> struct _Bitwise_and : binary_function<_Tp,_Tp,_Tp> {
212 _Tp operator() (_Tp __x, _Tp __y) const { return __x & __y; }
213 };
214
215 template<typename _Tp> struct _Bitwise_or : binary_function<_Tp,_Tp,_Tp> {
216 _Tp operator() (_Tp __x, _Tp __y) const { return __x | __y; }
217 };
218
219 template<typename _Tp> struct _Bitwise_xor : binary_function<_Tp,_Tp,_Tp> {
220 _Tp operator() (_Tp __x, _Tp __y) const { return __x ^ __y; }
221 };
222
223 template<typename _Tp> struct _Bitwise_not : unary_function<_Tp,_Tp> {
224 _Tp operator() (_Tp __t) const { return ~__t; }
225 };
226
227 template<typename _Tp> struct _Shift_left : unary_function<_Tp,_Tp> {
228 _Tp operator() (_Tp __x, _Tp __y) const { return __x << __y; }
229 };
230
231 template<typename _Tp> struct _Shift_right : unary_function<_Tp,_Tp> {
232 _Tp operator() (_Tp __x, _Tp __y) const { return __x >> __y; }
233 };
234
235
236 template<typename _Tp>
237 inline _Tp
238 valarray<_Tp>::operator[] (size_t __i) const
239 { return _M_data[__i]; }
240
241 template<typename _Tp>
242 _Tp&
243 valarray<_Tp>::operator[] (size_t __i)
244 { return _M_data[__i]; }
245
246 } // std::
247
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>
254
255 namespace std {
256
257 template<typename _Tp>
258 inline valarray<_Tp>::valarray () : _M_size (0), _M_data (0) {}
259
260 template<typename _Tp>
261 inline valarray<_Tp>::valarray (size_t __n)
262 : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n))
263 { __valarray_default_construct(_M_data, _M_data + __n); }
264
265 template<typename _Tp>
266 inline valarray<_Tp>::valarray (const _Tp& __t, size_t __n)
267 : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n))
268 { __valarray_fill_construct (_M_data, _M_data + __n, __t); }
269
270 template<typename _Tp>
271 inline valarray<_Tp>::valarray (const _Tp* __restrict__ __p, size_t __n)
272 : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n))
273 { __valarray_copy_construct (__p, __p + __n, _M_data); }
274
275 template<typename _Tp>
276 inline valarray<_Tp>::valarray (const valarray<_Tp>& __v)
277 : _M_size(__v._M_size), _M_data(__valarray_get_storage<_Tp>(__v._M_size))
278 { __valarray_copy_construct (__v._M_data, __v._M_data + _M_size, _M_data); }
279
280 template<typename _Tp>
281 inline valarray<_Tp>::valarray (const slice_array<_Tp>& __sa)
282 : _M_size(__sa._M_sz), _M_data(__valarray_get_storage<_Tp>(__sa._M_sz))
283 {
284 __valarray_copy_construct
285 (__sa._M_array, __sa._M_sz, __sa._M_stride, _Array<_Tp>(_M_data));
286 }
287
288 template<typename _Tp>
289 inline valarray<_Tp>::valarray (const gslice_array<_Tp>& __ga)
290 : _M_size(__ga._M_index.size()),
291 _M_data(__valarray_get_storage<_Tp>(_M_size))
292 {
293 __valarray_copy_construct
294 (__ga._M_array, _Array<size_t>(__ga._M_index),
295 _Array<_Tp>(_M_data), _M_size);
296 }
297
298 template<typename _Tp>
299 inline valarray<_Tp>::valarray (const mask_array<_Tp>& __ma)
300 : _M_size(__ma._M_sz), _M_data(__valarray_get_storage<_Tp>(__ma._M_sz))
301 {
302 __valarray_copy_construct
303 (__ma._M_array, __ma._M_mask, _Array<_Tp>(_M_data), _M_size);
304 }
305
306 template<typename _Tp>
307 inline valarray<_Tp>::valarray (const indirect_array<_Tp>& __ia)
308 : _M_size(__ia._M_sz), _M_data(__valarray_get_storage<_Tp>(__ia._M_sz))
309 {
310 __valarray_copy_construct
311 (__ia._M_array, __ia._M_index, _Array<_Tp>(_M_data), _M_size);
312 }
313
314 template<typename _Tp> template<class _Dom>
315 inline valarray<_Tp>::valarray (const _Expr<_Dom, _Tp>& __e)
316 : _M_size(__e.size ()), _M_data(__valarray_get_storage<_Tp>(_M_size))
317 { __valarray_copy_construct (__e, _M_size, _Array<_Tp>(_M_data)); }
318
319 template<typename _Tp>
320 inline valarray<_Tp>::~valarray ()
321 {
322 __valarray_destroy_elements(_M_data, _M_data + _M_size);
323 __valarray_release_memory(_M_data);
324 }
325
326 template<typename _Tp>
327 inline valarray<_Tp>&
328 valarray<_Tp>::operator= (const valarray<_Tp>& __v)
329 {
330 __valarray_copy(__v._M_data, _M_size, _M_data);
331 return *this;
332 }
333
334 template<typename _Tp>
335 inline valarray<_Tp>&
336 valarray<_Tp>::operator= (const _Tp& __t)
337 {
338 __valarray_fill (_M_data, _M_size, __t);
339 return *this;
340 }
341
342 template<typename _Tp>
343 inline valarray<_Tp>&
344 valarray<_Tp>::operator= (const slice_array<_Tp>& __sa)
345 {
346 __valarray_copy (__sa._M_array, __sa._M_sz,
347 __sa._M_stride, _Array<_Tp>(_M_data));
348 return *this;
349 }
350
351 template<typename _Tp>
352 inline valarray<_Tp>&
353 valarray<_Tp>::operator= (const gslice_array<_Tp>& __ga)
354 {
355 __valarray_copy (__ga._M_array, _Array<size_t>(__ga._M_index),
356 _Array<_Tp>(_M_data), _M_size);
357 return *this;
358 }
359
360 template<typename _Tp>
361 inline valarray<_Tp>&
362 valarray<_Tp>::operator= (const mask_array<_Tp>& __ma)
363 {
364 __valarray_copy (__ma._M_array, __ma._M_mask,
365 _Array<_Tp>(_M_data), _M_size);
366 return *this;
367 }
368
369 template<typename _Tp>
370 inline valarray<_Tp>&
371 valarray<_Tp>::operator= (const indirect_array<_Tp>& __ia)
372 {
373 __valarray_copy (__ia._M_array, __ia._M_index,
374 _Array<_Tp>(_M_data), _M_size);
375 return *this;
376 }
377
378 template<typename _Tp> template<class _Dom>
379 inline valarray<_Tp>&
380 valarray<_Tp>::operator= (const _Expr<_Dom, _Tp>& __e)
381 {
382 __valarray_copy (__e, _M_size, _Array<_Tp>(_M_data));
383 return *this;
384 }
385
386 template<typename _Tp>
387 inline _Expr<_SClos<_ValArray,_Tp>, _Tp>
388 valarray<_Tp>::operator[] (slice __s) const
389 {
390 typedef _SClos<_ValArray,_Tp> _Closure;
391 return _Expr<_Closure, _Tp> (_Closure (_Array<_Tp>(_M_data), __s));
392 }
393
394 template<typename _Tp>
395 inline slice_array<_Tp>
396 valarray<_Tp>::operator[] (slice __s)
397 {
398 return slice_array<_Tp> (_Array<_Tp>(_M_data), __s);
399 }
400
401 template<typename _Tp>
402 inline _Expr<_GClos<_ValArray,_Tp>, _Tp>
403 valarray<_Tp>::operator[] (const gslice& __gs) const
404 {
405 typedef _GClos<_ValArray,_Tp> _Closure;
406 return _Expr<_Closure, _Tp>
407 (_Closure (_Array<_Tp>(_M_data), __gs._M_index->_M_index));
408 }
409
410 template<typename _Tp>
411 inline gslice_array<_Tp>
412 valarray<_Tp>::operator[] (const gslice& __gs)
413 {
414 return gslice_array<_Tp>
415 (_Array<_Tp>(_M_data), __gs._M_index->_M_index);
416 }
417
418 template<typename _Tp>
419 inline valarray<_Tp>
420 valarray<_Tp>::operator[] (const valarray<bool>& __m) const
421 {
422 size_t __s (0);
423 size_t __e (__m.size ());
424 for (size_t __i=0; __i<__e; ++__i)
425 if (__m[__i]) ++__s;
426 return valarray<_Tp> (mask_array<_Tp> (_Array<_Tp>(_M_data), __s,
427 _Array<bool> (__m)));
428 }
429
430 template<typename _Tp>
431 inline mask_array<_Tp>
432 valarray<_Tp>::operator[] (const valarray<bool>& __m)
433 {
434 size_t __s (0);
435 size_t __e (__m.size ());
436 for (size_t __i=0; __i<__e; ++__i)
437 if (__m[__i]) ++__s;
438 return mask_array<_Tp> (_Array<_Tp>(_M_data), __s, _Array<bool> (__m));
439 }
440
441 template<typename _Tp>
442 inline _Expr<_IClos<_ValArray,_Tp>, _Tp>
443 valarray<_Tp>::operator[] (const valarray<size_t>& __i) const
444 {
445 typedef _IClos<_ValArray,_Tp> _Closure;
446 return _Expr<_Closure, _Tp> (_Closure (*this, __i));
447 }
448
449 template<typename _Tp>
450 inline indirect_array<_Tp>
451 valarray<_Tp>::operator[] (const valarray<size_t>& __i)
452 {
453 return indirect_array<_Tp> (_Array<_Tp>(_M_data), __i.size(),
454 _Array<size_t> (__i));
455 }
456
457 template<class _Tp>
458 inline size_t valarray<_Tp>::size () const { return _M_size; }
459
460 template<class _Tp>
461 inline _Tp
462 valarray<_Tp>::sum () const
463 {
464 return __valarray_sum(_M_data, _M_data + _M_size);
465 }
466
467 // template<typename _Tp>
468 // inline _Tp
469 // valarray<_Tp>::product () const
470 // {
471 // return __valarray_product(_M_data, _M_data + _M_size);
472 // }
473
474 template <class _Tp>
475 inline valarray<_Tp>
476 valarray<_Tp>::shift (int __n) const
477 {
478 _Tp* const __a = static_cast<_Tp*>
479 (__builtin_alloca (sizeof(_Tp) * _M_size));
480 if (! __n) // __n == 0: no shift
481 __valarray_copy_construct(_M_data, _M_data + _M_size, __a);
482 else if (__n > 0) { // __n > 0: shift left
483 if (__n > _M_size)
484 __valarray_default_construct(__a, __a + __n);
485 else {
486 __valarray_copy_construct(_M_data+__n, _M_data + _M_size, __a);
487 __valarray_default_construct(__a+_M_size-__n, __a + _M_size);
488 }
489 }
490 else { // __n < 0: shift right
491 __valarray_copy_construct (_M_data, _M_data+_M_size+__n, __a-__n);
492 __valarray_default_construct(__a, __a - __n);
493 }
494 return valarray<_Tp> (__a, _M_size);
495 }
496
497 template <class _Tp>
498 inline valarray<_Tp>
499 valarray<_Tp>::cshift (int __n) const
500 {
501 _Tp* const __a = static_cast<_Tp*>
502 (__builtin_alloca (sizeof(_Tp) * _M_size));
503 if (! __n) // __n == 0: no cshift
504 __valarray_copy_construct(_M_data, _M_data + _M_size, __a);
505 else if (__n > 0) { // __n > 0: cshift left
506 __valarray_copy_construct(_M_data, _M_data+__n, __a+_M_size-__n);
507 __valarray_copy_construct(_M_data+__n, _M_data + _M_size, __a);
508 }
509 else { // __n < 0: cshift right
510 __valarray_copy_construct
511 (_M_data + _M_size+__n, _M_data + _M_size, __a);
512 __valarray_copy_construct
513 (_M_data, _M_data + _M_size+__n, __a - __n);
514 }
515 return valarray<_Tp> (__a, _M_size);
516 }
517
518 template <class _Tp>
519 inline void
520 valarray<_Tp>::resize (size_t __n, _Tp __c)
521 {
522 // This complication is so to make valarray<valarray<T> > work
523 // even though it is not required by the standard. Nobody should
524 // be saying valarray<valarray<T> > anyway. See the specs.
525 __valarray_destroy_elements(_M_data, _M_data + _M_size);
526 if (_M_size != __n)
527 {
528 __valarray_release_memory(_M_data);
529 _M_size = __n;
530 _M_data = __valarray_get_storage<_Tp>(__n);
531 }
532 __valarray_fill_construct(_M_data, _M_data + __n, __c);
533 }
534
535 template<typename _Tp>
536 inline _Tp
537 valarray<_Tp>::min() const
538 {
539 return *min_element (_M_data, _M_data+_M_size);
540 }
541
542 template<typename _Tp>
543 inline _Tp
544 valarray<_Tp>::max() const
545 {
546 return *max_element (_M_data, _M_data+_M_size);
547 }
548
549 template<class _Tp>
550 inline _Expr<_ValFunClos<_ValArray,_Tp>,_Tp>
551 valarray<_Tp>::apply (_Tp func (_Tp)) const
552 {
553 typedef _ValFunClos<_ValArray,_Tp> _Closure;
554 return _Expr<_Closure,_Tp> (_Closure (*this, func));
555 }
556
557 template<class _Tp>
558 inline _Expr<_RefFunClos<_ValArray,_Tp>,_Tp>
559 valarray<_Tp>::apply (_Tp func (const _Tp &)) const
560 {
561 typedef _RefFunClos<_ValArray,_Tp> _Closure;
562 return _Expr<_Closure,_Tp> (_Closure (*this, func));
563 }
564
565 #define _DEFINE_VALARRAY_UNARY_OPERATOR(_Op, _Name) \
566 template<typename _Tp> \
567 inline _Expr<_UnClos<_Name,_ValArray,_Tp>, _Tp> \
568 valarray<_Tp>::operator _Op() const \
569 { \
570 typedef _UnClos<_Name,_ValArray,_Tp> _Closure; \
571 return _Expr<_Closure, _Tp> (_Closure (*this)); \
572 }
573
574 _DEFINE_VALARRAY_UNARY_OPERATOR(+, _Unary_plus)
575 _DEFINE_VALARRAY_UNARY_OPERATOR(-, negate)
576 _DEFINE_VALARRAY_UNARY_OPERATOR(~, _Bitwise_not)
577
578 #undef _DEFINE_VALARRAY_UNARY_OPERATOR
579
580 template<typename _Tp>
581 inline _Expr<_UnClos<logical_not,_ValArray,_Tp>, bool>
582 valarray<_Tp>::operator!() const
583 {
584 typedef _UnClos<logical_not,_ValArray,_Tp> _Closure;
585 return _Expr<_Closure, bool> (_Closure (*this));
586 }
587
588 #define _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(_Op, _Name) \
589 template<class _Tp> \
590 inline valarray<_Tp> & \
591 valarray<_Tp>::operator _Op##= (const _Tp &__t) \
592 { \
593 _Array_augmented_##_Name (_Array<_Tp>(_M_data), _M_size, __t); \
594 return *this; \
595 } \
596 \
597 template<class _Tp> \
598 inline valarray<_Tp> & \
599 valarray<_Tp>::operator _Op##= (const valarray<_Tp> &__v) \
600 { \
601 _Array_augmented_##_Name (_Array<_Tp>(_M_data), _M_size, \
602 _Array<_Tp>(__v._M_data)); \
603 return *this; \
604 }
605
606 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(+, plus)
607 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(-, minus)
608 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(*, multiplies)
609 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(/, divides)
610 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(%, modulus)
611 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(^, xor)
612 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(&, and)
613 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(|, or)
614 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(<<, shift_left)
615 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(>>, shift_right)
616
617 #undef _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT
618
619
620 } // std::
621
622
623 namespace std {
624
625 #define _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(_Op, _Name) \
626 template<class _Tp> template<class _Dom> \
627 inline valarray<_Tp> & \
628 valarray<_Tp>::operator _Op##= (const _Expr<_Dom,_Tp> &__e) \
629 { \
630 _Array_augmented_##_Name (_Array<_Tp>(_M_data), __e, _M_size); \
631 return *this; \
632 }
633
634 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(+, plus)
635 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(-, minus)
636 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(*, multiplies)
637 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(/, divides)
638 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(%, modulus)
639 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(^, xor)
640 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(&, and)
641 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(|, or)
642 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(<<, shift_left)
643 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(>>, shift_right)
644
645 #undef _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT
646
647
648 #define _DEFINE_BINARY_OPERATOR(_Op, _Name) \
649 template<typename _Tp> \
650 inline _Expr<_BinClos<_Name,_ValArray,_ValArray,_Tp,_Tp>, _Tp> \
651 operator _Op (const valarray<_Tp> &__v, const valarray<_Tp> &__w) \
652 { \
653 typedef _BinClos<_Name,_ValArray,_ValArray,_Tp,_Tp> _Closure; \
654 return _Expr<_Closure, _Tp> (_Closure (__v, __w)); \
655 } \
656 \
657 template<typename _Tp> \
658 inline _Expr<_BinClos<_Name,_ValArray,_Constant,_Tp,_Tp>,_Tp> \
659 operator _Op (const valarray<_Tp> &__v, const _Tp &__t) \
660 { \
661 typedef _BinClos<_Name,_ValArray,_Constant,_Tp,_Tp> _Closure; \
662 return _Expr<_Closure, _Tp> (_Closure (__v, __t)); \
663 } \
664 \
665 template<typename _Tp> \
666 inline _Expr<_BinClos<_Name,_Constant,_ValArray,_Tp,_Tp>,_Tp> \
667 operator _Op (const _Tp &__t, const valarray<_Tp> &__v) \
668 { \
669 typedef _BinClos<_Name,_Constant,_ValArray,_Tp,_Tp> _Closure; \
670 return _Expr<_Closure, _Tp> (_Closure (__t, __v)); \
671 }
672
673 _DEFINE_BINARY_OPERATOR(+, plus)
674 _DEFINE_BINARY_OPERATOR(-, minus)
675 _DEFINE_BINARY_OPERATOR(*, multiplies)
676 _DEFINE_BINARY_OPERATOR(/, divides)
677 _DEFINE_BINARY_OPERATOR(%, modulus)
678 _DEFINE_BINARY_OPERATOR(^, _Bitwise_xor)
679 _DEFINE_BINARY_OPERATOR(&, _Bitwise_and)
680 _DEFINE_BINARY_OPERATOR(|, _Bitwise_or)
681 _DEFINE_BINARY_OPERATOR(<<, _Shift_left)
682 _DEFINE_BINARY_OPERATOR(>>, _Shift_right)
683
684 #undef _DEFINE_BINARY_OPERATOR
685
686 #define _DEFINE_LOGICAL_OPERATOR(_Op, _Name) \
687 template<typename _Tp> \
688 inline _Expr<_BinClos<_Name,_ValArray,_ValArray,_Tp,_Tp>,bool> \
689 operator _Op (const valarray<_Tp> &__v, const valarray<_Tp> &__w) \
690 { \
691 typedef _BinClos<_Name,_ValArray,_ValArray,_Tp,_Tp> _Closure; \
692 return _Expr<_Closure, bool> (_Closure (__v, __w)); \
693 } \
694 \
695 template<class _Tp> \
696 inline _Expr<_BinClos<_Name,_ValArray,_Constant,_Tp,_Tp>,bool> \
697 operator _Op (const valarray<_Tp> &__v, const _Tp &__t) \
698 { \
699 typedef _BinClos<_Name,_ValArray,_Constant,_Tp,_Tp> _Closure; \
700 return _Expr<_Closure, bool> (_Closure (__v, __t)); \
701 } \
702 \
703 template<class _Tp> \
704 inline _Expr<_BinClos<_Name,_Constant,_ValArray,_Tp,_Tp>,bool> \
705 operator _Op (const _Tp &__t, const valarray<_Tp> &__v) \
706 { \
707 typedef _BinClos<_Name,_Constant,_ValArray,_Tp,_Tp> _Closure; \
708 return _Expr<_Closure, bool> (_Closure (__t, __v)); \
709 }
710
711 _DEFINE_LOGICAL_OPERATOR(&&, logical_and)
712 _DEFINE_LOGICAL_OPERATOR(||, logical_or)
713 _DEFINE_LOGICAL_OPERATOR(==, equal_to)
714 _DEFINE_LOGICAL_OPERATOR(!=, not_equal_to)
715 _DEFINE_LOGICAL_OPERATOR(<, less)
716 _DEFINE_LOGICAL_OPERATOR(>, greater)
717 _DEFINE_LOGICAL_OPERATOR(<=, less_equal)
718 _DEFINE_LOGICAL_OPERATOR(>=, greater_equal)
719
720 #undef _DEFINE_VALARRAY_OPERATOR
721
722 } // namespace std
723
724 #endif // _CPP_VALARRAY
725
726 // Local Variables:
727 // mode:c++
728 // End:
This page took 0.074559 seconds and 5 git commands to generate.