3 * Silicon Graphics Computer Systems, Inc.
8 * This material is provided "as is", with absolutely no warranty expressed
9 * or implied. Any use is at your own risk.
11 * Permission to use or copy this software for any purpose is hereby granted
12 * without fee, provided the above notices are retained on all copies.
13 * Permission to modify the code and to distribute modified code is granted,
14 * provided the above notices are retained, and a notice that the code was
15 * modified is included with the above copyright notice.
19 #ifndef _STLP_VALARRAY_H
20 #define _STLP_VALARRAY_H
22 #ifndef _STLP_INTERNAL_CMATH
23 # include <stl/_cmath.h>
26 #ifndef _STLP_INTERNAL_NEW
27 # include <stl/_new.h>
30 #ifndef _STLP_INTERNAL_ALGO_H
31 # include <stl/_algo.h>
34 #ifndef _STLP_INTERNAL_NUMERIC_H
35 # include <stl/_numeric.h>
38 #ifndef _STLP_INTERNAL_LIMITS
39 # include <stl/_limits.h>
42 /* As we only need the _STLP_ASSERT macro from _debug.h we test it to include _debug.h */
44 # include <stl/debug/_debug.h>
52 template <class _Tp> class valarray;
53 typedef valarray<bool> _Valarray_bool;
54 typedef valarray<size_t> _Valarray_size_t;
56 template <class _Tp> class slice_array;
57 template <class _Tp> class gslice_array;
58 template <class _Tp> class mask_array;
59 template <class _Tp> class indirect_array;
61 //----------------------------------------------------------------------
64 // Base class to handle memory allocation and deallocation. We can't just
65 // use vector<>, because vector<bool> would be unsuitable as an internal
66 // representation for valarray<bool>.
74 _Valarray_base() : _M_first(0), _M_size(0) {}
75 _Valarray_base(size_t __n) : _M_first(0), _M_size(0) { _M_allocate(__n); }
76 ~_Valarray_base() { _M_deallocate(); }
78 void _M_allocate(size_t __n) {
80 _M_first = __STATIC_CAST(_Tp*, (malloc(__n * sizeof(_Tp))));
82 #if !defined(_STLP_NO_BAD_ALLOC) && defined(_STLP_USE_EXCEPTIONS)
85 throw _STLP_STD::bad_alloc();
95 void _M_deallocate() {
103 class valarray : private _Valarray_base<_Tp>
108 typedef _Tp value_type;
110 // Basic constructors
111 valarray() : _Valarray_base<_Tp>() {}
112 explicit valarray(size_t __n) : _Valarray_base<_Tp>(__n)
113 { uninitialized_fill_n(this->_M_first, this->_M_size, _STLP_DEFAULT_CONSTRUCTED(value_type)); }
114 valarray(const value_type& __x, size_t __n) : _Valarray_base<_Tp>(__n)
115 { uninitialized_fill_n(this->_M_first, this->_M_size, __x); }
116 valarray(const value_type* __p, size_t __n) : _Valarray_base<_Tp>(__n)
117 { uninitialized_copy(__p, __p + __n, this->_M_first); }
118 valarray(const valarray<_Tp>& __x) : _Valarray_base<_Tp>(__x._M_size) {
119 uninitialized_copy(__x._M_first, __x._M_first + __x._M_size,
123 // Constructors from auxiliary array types
124 valarray(const slice_array<_Tp>&);
125 valarray(const gslice_array<_Tp>&);
126 valarray(const mask_array<_Tp>&);
127 valarray(const indirect_array<_Tp>&);
130 ~valarray() { _STLP_STD::_Destroy_Range(this->_M_first, this->_M_first + this->_M_size); }
132 // Extension: constructor that doesn't initialize valarray elements to a
133 // specific value. This is faster for types such as int and double.
135 void _M_initialize(const __true_type&) {}
136 void _M_initialize(const __false_type&)
137 { uninitialized_fill_n(this->_M_first, this->_M_size, _STLP_DEFAULT_CONSTRUCTED(_Tp)); }
141 valarray(size_t __n, _NoInit) : _Valarray_base<_Tp>(__n) {
142 typedef typename __type_traits<_Tp>::has_trivial_default_constructor _Is_Trivial;
143 _M_initialize(_Is_Trivial());
146 public: // Assignment
147 // Basic assignment. Note that 'x = y' is undefined if x.size() != y.size()
148 valarray<_Tp>& operator=(const valarray<_Tp>& __x) {
149 _STLP_ASSERT(__x.size() == this->size())
153 /* Eventhough the behavior is undefined when the sizes are different,
154 copying the data correspoding to the minimum of both the lengths
155 will prevent a crash */
156 if( this->size() < __x._M_size )
157 copy(__x._M_first, __x._M_first + this->size(), this->_M_first);
159 copy(__x._M_first, __x._M_first + __x._M_size, this->_M_first);
161 #else /* __SYMBIAN32__ */
162 copy(__x._M_first, __x._M_first + __x._M_size, this->_M_first);
164 #endif /* __SYMBIAN32__ */
171 valarray<_Tp>& operator=(const value_type& __x) {
172 fill_n(this->_M_first, this->_M_size, __x);
176 // Assignment of auxiliary array types
177 valarray<_Tp>& operator=(const slice_array<_Tp>&);
178 valarray<_Tp>& operator=(const gslice_array<_Tp>&);
179 valarray<_Tp>& operator=(const mask_array<_Tp>&);
180 valarray<_Tp>& operator=(const indirect_array<_Tp>&);
182 public: // Element access
183 value_type operator[](size_t __n) const { return this->_M_first[__n]; }
184 value_type& operator[](size_t __n) { return this->_M_first[__n]; }
185 size_t size() const { return this->_M_size; }
187 public: // Subsetting operations with auxiliary type
188 valarray<_Tp> operator[](slice) const;
189 slice_array<_Tp> operator[](slice);
190 valarray<_Tp> operator[](const gslice&) const;
191 gslice_array<_Tp> operator[](const gslice&);
192 valarray<_Tp> operator[](const _Valarray_bool&) const;
193 mask_array<_Tp> operator[](const _Valarray_bool&);
194 valarray<_Tp> operator[](const _Valarray_size_t&) const;
195 indirect_array<_Tp> operator[](const _Valarray_size_t&);
197 public: // Unary operators.
198 valarray<_Tp> operator+() const { return *this; }
200 valarray<_Tp> operator-() const {
201 valarray<_Tp> __tmp(this->size(), _NoInit());
202 for (size_t __i = 0; __i < this->size(); ++__i)
203 __tmp[__i] = -(*this)[__i];
207 valarray<_Tp> operator~() const {
208 valarray<_Tp> __tmp(this->size(), _NoInit());
209 for (size_t __i = 0; __i < this->size(); ++__i)
210 __tmp[__i] = ~(*this)[__i];
214 _Valarray_bool operator!() const;
216 public: // Scalar computed assignment.
217 valarray<_Tp>& operator*= (const value_type& __x) {
218 for (size_t __i = 0; __i < this->size(); ++__i)
223 valarray<_Tp>& operator/= (const value_type& __x) {
224 for (size_t __i = 0; __i < this->size(); ++__i)
229 valarray<_Tp>& operator%= (const value_type& __x) {
230 for (size_t __i = 0; __i < this->size(); ++__i)
235 valarray<_Tp>& operator+= (const value_type& __x) {
236 for (size_t __i = 0; __i < this->size(); ++__i)
241 valarray<_Tp>& operator-= (const value_type& __x) {
242 for (size_t __i = 0; __i < this->size(); ++__i)
247 valarray<_Tp>& operator^= (const value_type& __x) {
248 for (size_t __i = 0; __i < this->size(); ++__i)
253 valarray<_Tp>& operator&= (const value_type& __x) {
254 for (size_t __i = 0; __i < this->size(); ++__i)
259 valarray<_Tp>& operator|= (const value_type& __x) {
260 for (size_t __i = 0; __i < this->size(); ++__i)
265 valarray<_Tp>& operator<<= (const value_type& __x) {
266 for (size_t __i = 0; __i < this->size(); ++__i)
267 (*this)[__i] <<= __x;
271 valarray<_Tp>& operator>>= (const value_type& __x) {
272 for (size_t __i = 0; __i < this->size(); ++__i)
273 (*this)[__i] >>= __x;
277 public: // Array computed assignment.
278 valarray<_Tp>& operator*= (const valarray<_Tp>& __x) {
279 for (size_t __i = 0; __i < this->size(); ++__i)
280 (*this)[__i] *= __x[__i];
284 valarray<_Tp>& operator/= (const valarray<_Tp>& __x) {
285 for (size_t __i = 0; __i < this->size(); ++__i)
286 (*this)[__i] /= __x[__i];
290 valarray<_Tp>& operator%= (const valarray<_Tp>& __x) {
291 for (size_t __i = 0; __i < this->size(); ++__i)
292 (*this)[__i] %= __x[__i];
296 valarray<_Tp>& operator+= (const valarray<_Tp>& __x) {
297 for (size_t __i = 0; __i < this->size(); ++__i)
298 (*this)[__i] += __x[__i];
302 valarray<_Tp>& operator-= (const valarray<_Tp>& __x) {
303 for (size_t __i = 0; __i < this->size(); ++__i)
304 (*this)[__i] -= __x[__i];
308 valarray<_Tp>& operator^= (const valarray<_Tp>& __x) {
309 for (size_t __i = 0; __i < this->size(); ++__i)
310 (*this)[__i] ^= __x[__i];
314 valarray<_Tp>& operator&= (const valarray<_Tp>& __x) {
315 for (size_t __i = 0; __i < this->size(); ++__i)
316 (*this)[__i] &= __x[__i];
320 valarray<_Tp>& operator|= (const valarray<_Tp>& __x) {
321 for (size_t __i = 0; __i < this->size(); ++__i)
322 (*this)[__i] |= __x[__i];
326 valarray<_Tp>& operator<<= (const valarray<_Tp>& __x) {
327 for (size_t __i = 0; __i < this->size(); ++__i)
328 (*this)[__i] <<= __x[__i];
332 valarray<_Tp>& operator>>= (const valarray<_Tp>& __x) {
333 for (size_t __i = 0; __i < this->size(); ++__i)
334 (*this)[__i] >>= __x[__i];
338 public: // Other member functions.
340 // The result is undefined for zero-length arrays
341 value_type sum() const {
342 return accumulate(this->_M_first + 1, this->_M_first + this->_M_size,
346 // The result is undefined for zero-length arrays
347 value_type (min) () const {
348 return *min_element(this->_M_first + 0, this->_M_first + this->_M_size);
351 value_type (max) () const {
352 return *max_element(this->_M_first + 0, this->_M_first + this->_M_size);
355 valarray<_Tp> shift(int __n) const;
356 valarray<_Tp> cshift(int __n) const;
358 valarray<_Tp> apply(value_type __f(value_type)) const {
359 valarray<_Tp> __tmp(this->size());
360 transform(this->_M_first + 0, this->_M_first + this->_M_size, __tmp._M_first,
364 valarray<_Tp> apply(value_type __f(const value_type&)) const {
365 valarray<_Tp> __tmp(this->size());
366 transform(this->_M_first + 0, this->_M_first + this->_M_size, __tmp._M_first,
371 void resize(size_t __n, value_type __x = value_type()) {
372 _STLP_STD::_Destroy_Range(this->_M_first, this->_M_first + this->_M_size);
373 _Valarray_base<_Tp>::_M_deallocate();
374 _Valarray_base<_Tp>::_M_allocate(__n);
375 uninitialized_fill_n(this->_M_first, this->_M_size, __x);
379 //----------------------------------------------------------------------
380 // valarray non-member functions.
382 // Binary arithmetic operations between two arrays. Behavior is
383 // undefined if the two arrays do not have the same length.
386 inline valarray<_Tp> _STLP_CALL operator*(const valarray<_Tp>& __x,
387 const valarray<_Tp>& __y) {
388 typedef typename valarray<_Tp>::_NoInit _NoInit;
389 valarray<_Tp> __tmp(__x.size(), _NoInit());
390 for (size_t __i = 0; __i < __x.size(); ++__i)
391 __tmp[__i] = __x[__i] * __y[__i];
396 inline valarray<_Tp> _STLP_CALL operator/(const valarray<_Tp>& __x,
397 const valarray<_Tp>& __y) {
398 typedef typename valarray<_Tp>::_NoInit _NoInit;
399 valarray<_Tp> __tmp(__x.size(), _NoInit());
400 for (size_t __i = 0; __i < __x.size(); ++__i)
401 __tmp[__i] = __x[__i] / __y[__i];
406 inline valarray<_Tp> _STLP_CALL operator%(const valarray<_Tp>& __x,
407 const valarray<_Tp>& __y) {
408 typedef typename valarray<_Tp>::_NoInit _NoInit;
409 valarray<_Tp> __tmp(__x.size(), _NoInit());
410 for (size_t __i = 0; __i < __x.size(); ++__i)
411 __tmp[__i] = __x[__i] % __y[__i];
416 inline valarray<_Tp> _STLP_CALL operator+(const valarray<_Tp>& __x,
417 const valarray<_Tp>& __y) {
418 typedef typename valarray<_Tp>::_NoInit _NoInit;
419 valarray<_Tp> __tmp(__x.size(), _NoInit());
420 for (size_t __i = 0; __i < __x.size(); ++__i)
421 __tmp[__i] = __x[__i] + __y[__i];
426 inline valarray<_Tp> _STLP_CALL operator-(const valarray<_Tp>& __x,
427 const valarray<_Tp>& __y) {
428 typedef typename valarray<_Tp>::_NoInit _NoInit;
429 valarray<_Tp> __tmp(__x.size(), _NoInit());
430 for (size_t __i = 0; __i < __x.size(); ++__i)
431 __tmp[__i] = __x[__i] - __y[__i];
436 inline valarray<_Tp> _STLP_CALL operator^(const valarray<_Tp>& __x,
437 const valarray<_Tp>& __y) {
438 typedef typename valarray<_Tp>::_NoInit _NoInit;
439 valarray<_Tp> __tmp(__x.size(), _NoInit());
440 for (size_t __i = 0; __i < __x.size(); ++__i)
441 __tmp[__i] = __x[__i] ^ __y[__i];
446 inline valarray<_Tp> _STLP_CALL operator&(const valarray<_Tp>& __x,
447 const valarray<_Tp>& __y) {
448 typedef typename valarray<_Tp>::_NoInit _NoInit;
449 valarray<_Tp> __tmp(__x.size(), _NoInit());
450 for (size_t __i = 0; __i < __x.size(); ++__i)
451 __tmp[__i] = __x[__i] & __y[__i];
456 inline valarray<_Tp> _STLP_CALL operator|(const valarray<_Tp>& __x,
457 const valarray<_Tp>& __y) {
458 typedef typename valarray<_Tp>::_NoInit _NoInit;
459 valarray<_Tp> __tmp(__x.size(), _NoInit());
460 for (size_t __i = 0; __i < __x.size(); ++__i)
461 __tmp[__i] = __x[__i] | __y[__i];
466 inline valarray<_Tp> _STLP_CALL operator<<(const valarray<_Tp>& __x,
467 const valarray<_Tp>& __y) {
468 typedef typename valarray<_Tp>::_NoInit _NoInit;
469 valarray<_Tp> __tmp(__x.size(), _NoInit());
470 for (size_t __i = 0; __i < __x.size(); ++__i)
471 __tmp[__i] = __x[__i] << __y[__i];
476 inline valarray<_Tp> _STLP_CALL operator>>(const valarray<_Tp>& __x,
477 const valarray<_Tp>& __y) {
478 typedef typename valarray<_Tp>::_NoInit _NoInit;
479 valarray<_Tp> __tmp(__x.size(), _NoInit());
480 for (size_t __i = 0; __i < __x.size(); ++__i)
481 __tmp[__i] = __x[__i] >> __y[__i];
485 // Binary arithmetic operations between an array and a scalar.
488 inline valarray<_Tp> _STLP_CALL operator*(const valarray<_Tp>& __x, const _Tp& __c) {
489 typedef typename valarray<_Tp>::_NoInit _NoInit;
490 valarray<_Tp> __tmp(__x.size(), _NoInit());
491 for (size_t __i = 0; __i < __x.size(); ++__i)
492 __tmp[__i] = __x[__i] * __c;
497 inline valarray<_Tp> _STLP_CALL operator*(const _Tp& __c, const valarray<_Tp>& __x) {
498 typedef typename valarray<_Tp>::_NoInit _NoInit;
499 valarray<_Tp> __tmp(__x.size(), _NoInit());
500 for (size_t __i = 0; __i < __x.size(); ++__i)
501 __tmp[__i] = __c * __x[__i];
506 inline valarray<_Tp> _STLP_CALL operator/(const valarray<_Tp>& __x, const _Tp& __c) {
507 typedef typename valarray<_Tp>::_NoInit _NoInit;
508 valarray<_Tp> __tmp(__x.size(), _NoInit());
509 for (size_t __i = 0; __i < __x.size(); ++__i)
510 __tmp[__i] = __x[__i] / __c;
515 inline valarray<_Tp> _STLP_CALL operator/(const _Tp& __c, const valarray<_Tp>& __x) {
516 typedef typename valarray<_Tp>::_NoInit _NoInit;
517 valarray<_Tp> __tmp(__x.size(), _NoInit());
518 for (size_t __i = 0; __i < __x.size(); ++__i)
519 __tmp[__i] = __c / __x[__i];
524 inline valarray<_Tp> _STLP_CALL operator%(const valarray<_Tp>& __x, const _Tp& __c) {
525 typedef typename valarray<_Tp>::_NoInit _NoInit;
526 valarray<_Tp> __tmp(__x.size(), _NoInit());
527 for (size_t __i = 0; __i < __x.size(); ++__i)
528 __tmp[__i] = __x[__i] % __c;
533 inline valarray<_Tp> _STLP_CALL operator%(const _Tp& __c, const valarray<_Tp>& __x) {
534 typedef typename valarray<_Tp>::_NoInit _NoInit;
535 valarray<_Tp> __tmp(__x.size(), _NoInit());
536 for (size_t __i = 0; __i < __x.size(); ++__i)
537 __tmp[__i] = __c % __x[__i];
542 inline valarray<_Tp> _STLP_CALL operator+(const valarray<_Tp>& __x, const _Tp& __c) {
543 typedef typename valarray<_Tp>::_NoInit _NoInit;
544 valarray<_Tp> __tmp(__x.size(), _NoInit());
545 for (size_t __i = 0; __i < __x.size(); ++__i)
546 __tmp[__i] = __x[__i] + __c;
551 inline valarray<_Tp> _STLP_CALL operator+(const _Tp& __c, const valarray<_Tp>& __x) {
552 typedef typename valarray<_Tp>::_NoInit _NoInit;
553 valarray<_Tp> __tmp(__x.size(), _NoInit());
554 for (size_t __i = 0; __i < __x.size(); ++__i)
555 __tmp[__i] = __c + __x[__i];
560 inline valarray<_Tp> _STLP_CALL operator-(const valarray<_Tp>& __x, const _Tp& __c) {
561 typedef typename valarray<_Tp>::_NoInit _NoInit;
562 valarray<_Tp> __tmp(__x.size(), _NoInit());
563 for (size_t __i = 0; __i < __x.size(); ++__i)
564 __tmp[__i] = __x[__i] - __c;
569 inline valarray<_Tp> _STLP_CALL operator-(const _Tp& __c, const valarray<_Tp>& __x) {
570 typedef typename valarray<_Tp>::_NoInit _NoInit;
571 valarray<_Tp> __tmp(__x.size(), _NoInit());
572 for (size_t __i = 0; __i < __x.size(); ++__i)
573 __tmp[__i] = __c - __x[__i];
578 inline valarray<_Tp> _STLP_CALL operator^(const valarray<_Tp>& __x, const _Tp& __c) {
579 typedef typename valarray<_Tp>::_NoInit _NoInit;
580 valarray<_Tp> __tmp(__x.size(), _NoInit());
581 for (size_t __i = 0; __i < __x.size(); ++__i)
582 __tmp[__i] = __x[__i] ^ __c;
587 inline valarray<_Tp> _STLP_CALL operator^(const _Tp& __c, const valarray<_Tp>& __x) {
588 typedef typename valarray<_Tp>::_NoInit _NoInit;
589 valarray<_Tp> __tmp(__x.size(), _NoInit());
590 for (size_t __i = 0; __i < __x.size(); ++__i)
591 __tmp[__i] = __c ^ __x[__i];
596 inline valarray<_Tp> _STLP_CALL operator&(const valarray<_Tp>& __x, const _Tp& __c) {
597 typedef typename valarray<_Tp>::_NoInit _NoInit;
598 valarray<_Tp> __tmp(__x.size(), _NoInit());
599 for (size_t __i = 0; __i < __x.size(); ++__i)
600 __tmp[__i] = __x[__i] & __c;
605 inline valarray<_Tp> _STLP_CALL operator&(const _Tp& __c, const valarray<_Tp>& __x) {
606 typedef typename valarray<_Tp>::_NoInit _NoInit;
607 valarray<_Tp> __tmp(__x.size(), _NoInit());
608 for (size_t __i = 0; __i < __x.size(); ++__i)
609 __tmp[__i] = __c & __x[__i];
614 inline valarray<_Tp> _STLP_CALL operator|(const valarray<_Tp>& __x, const _Tp& __c) {
615 typedef typename valarray<_Tp>::_NoInit _NoInit;
616 valarray<_Tp> __tmp(__x.size(), _NoInit());
617 for (size_t __i = 0; __i < __x.size(); ++__i)
618 __tmp[__i] = __x[__i] | __c;
623 inline valarray<_Tp> _STLP_CALL operator|(const _Tp& __c, const valarray<_Tp>& __x) {
624 typedef typename valarray<_Tp>::_NoInit _NoInit;
625 valarray<_Tp> __tmp(__x.size(), _NoInit());
626 for (size_t __i = 0; __i < __x.size(); ++__i)
627 __tmp[__i] = __c | __x[__i];
632 inline valarray<_Tp> _STLP_CALL operator<<(const valarray<_Tp>& __x, const _Tp& __c) {
633 typedef typename valarray<_Tp>::_NoInit _NoInit;
634 valarray<_Tp> __tmp(__x.size(), _NoInit());
635 for (size_t __i = 0; __i < __x.size(); ++__i)
636 __tmp[__i] = __x[__i] << __c;
641 inline valarray<_Tp> _STLP_CALL operator<<(const _Tp& __c, const valarray<_Tp>& __x) {
642 typedef typename valarray<_Tp>::_NoInit _NoInit;
643 valarray<_Tp> __tmp(__x.size(), _NoInit());
644 for (size_t __i = 0; __i < __x.size(); ++__i)
645 __tmp[__i] = __c << __x[__i];
650 inline valarray<_Tp> _STLP_CALL operator>>(const valarray<_Tp>& __x, const _Tp& __c) {
651 typedef typename valarray<_Tp>::_NoInit _NoInit;
652 valarray<_Tp> __tmp(__x.size(), _NoInit());
653 for (size_t __i = 0; __i < __x.size(); ++__i)
654 __tmp[__i] = __x[__i] >> __c;
659 inline valarray<_Tp> _STLP_CALL operator>>(const _Tp& __c, const valarray<_Tp>& __x) {
660 typedef typename valarray<_Tp>::_NoInit _NoInit;
661 valarray<_Tp> __tmp(__x.size(), _NoInit());
662 for (size_t __i = 0; __i < __x.size(); ++__i)
663 __tmp[__i] = __c >> __x[__i];
667 // Binary logical operations between two arrays. Behavior is undefined
668 // if the two arrays have different lengths. Note that operator== does
669 // not do what you might at first expect.
672 inline _Valarray_bool _STLP_CALL operator==(const valarray<_Tp>& __x,
673 const valarray<_Tp>& __y)
675 _Valarray_bool __tmp(__x.size(), _Valarray_bool::_NoInit());
676 for (size_t __i = 0; __i < __x.size(); ++__i)
677 __tmp[__i] = __x[__i] == __y[__i];
682 inline _Valarray_bool _STLP_CALL operator<(const valarray<_Tp>& __x,
683 const valarray<_Tp>& __y)
685 _Valarray_bool __tmp(__x.size(), _Valarray_bool::_NoInit());
686 for (size_t __i = 0; __i < __x.size(); ++__i)
687 __tmp[__i] = __x[__i] < __y[__i];
691 #ifdef _STLP_USE_SEPARATE_RELOPS_NAMESPACE
694 inline _Valarray_bool _STLP_CALL operator!=(const valarray<_Tp>& __x,
695 const valarray<_Tp>& __y)
697 _Valarray_bool __tmp(__x.size(), _Valarray_bool::_NoInit());
698 for (size_t __i = 0; __i < __x.size(); ++__i)
699 __tmp[__i] = __x[__i] != __y[__i];
704 inline _Valarray_bool _STLP_CALL operator>(const valarray<_Tp>& __x,
705 const valarray<_Tp>& __y)
707 _Valarray_bool __tmp(__x.size(), _Valarray_bool::_NoInit());
708 for (size_t __i = 0; __i < __x.size(); ++__i)
709 __tmp[__i] = __x[__i] > __y[__i];
714 inline _Valarray_bool _STLP_CALL operator<=(const valarray<_Tp>& __x,
715 const valarray<_Tp>& __y)
717 _Valarray_bool __tmp(__x.size(), _Valarray_bool::_NoInit());
718 for (size_t __i = 0; __i < __x.size(); ++__i)
719 __tmp[__i] = __x[__i] <= __y[__i];
724 inline _Valarray_bool _STLP_CALL operator>=(const valarray<_Tp>& __x,
725 const valarray<_Tp>& __y)
727 _Valarray_bool __tmp(__x.size(), _Valarray_bool::_NoInit());
728 for (size_t __i = 0; __i < __x.size(); ++__i)
729 __tmp[__i] = __x[__i] >= __y[__i];
733 #endif /* _STLP_USE_SEPARATE_RELOPS_NAMESPACE */
737 inline _Valarray_bool _STLP_CALL operator&&(const valarray<_Tp>& __x,
738 const valarray<_Tp>& __y)
740 _Valarray_bool __tmp(__x.size(), _Valarray_bool::_NoInit());
741 for (size_t __i = 0; __i < __x.size(); ++__i)
742 __tmp[__i] = __x[__i] && __y[__i];
747 inline _Valarray_bool _STLP_CALL operator||(const valarray<_Tp>& __x,
748 const valarray<_Tp>& __y)
750 _Valarray_bool __tmp(__x.size(), _Valarray_bool::_NoInit());
751 for (size_t __i = 0; __i < __x.size(); ++__i)
752 __tmp[__i] = __x[__i] || __y[__i];
756 // Logical operations between an array and a scalar.
759 inline _Valarray_bool _STLP_CALL operator==(const valarray<_Tp>& __x, const _Tp& __c)
761 _Valarray_bool __tmp(__x.size(), _Valarray_bool::_NoInit());
762 for (size_t __i = 0; __i < __x.size(); ++__i)
763 __tmp[__i] = __x[__i] == __c;
768 inline _Valarray_bool _STLP_CALL operator==(const _Tp& __c, const valarray<_Tp>& __x)
770 _Valarray_bool __tmp(__x.size(), _Valarray_bool::_NoInit());
771 for (size_t __i = 0; __i < __x.size(); ++__i)
772 __tmp[__i] = __c == __x[__i];
777 inline _Valarray_bool _STLP_CALL operator!=(const valarray<_Tp>& __x, const _Tp& __c)
779 _Valarray_bool __tmp(__x.size(), _Valarray_bool::_NoInit());
780 for (size_t __i = 0; __i < __x.size(); ++__i)
781 __tmp[__i] = __x[__i] != __c;
786 inline _Valarray_bool _STLP_CALL operator!=(const _Tp& __c, const valarray<_Tp>& __x)
788 _Valarray_bool __tmp(__x.size(), _Valarray_bool::_NoInit());
789 for (size_t __i = 0; __i < __x.size(); ++__i)
790 __tmp[__i] = __c != __x[__i];
795 inline _Valarray_bool _STLP_CALL operator<(const valarray<_Tp>& __x, const _Tp& __c)
797 _Valarray_bool __tmp(__x.size(), _Valarray_bool::_NoInit());
798 for (size_t __i = 0; __i < __x.size(); ++__i)
799 __tmp[__i] = __x[__i] < __c;
804 inline _Valarray_bool _STLP_CALL operator<(const _Tp& __c, const valarray<_Tp>& __x)
806 _Valarray_bool __tmp(__x.size(), _Valarray_bool::_NoInit());
807 for (size_t __i = 0; __i < __x.size(); ++__i)
808 __tmp[__i] = __c < __x[__i];
813 inline _Valarray_bool _STLP_CALL operator>(const valarray<_Tp>& __x, const _Tp& __c)
815 _Valarray_bool __tmp(__x.size(), _Valarray_bool::_NoInit());
816 for (size_t __i = 0; __i < __x.size(); ++__i)
817 __tmp[__i] = __x[__i] > __c;
822 inline _Valarray_bool _STLP_CALL operator>(const _Tp& __c, const valarray<_Tp>& __x)
824 _Valarray_bool __tmp(__x.size(), _Valarray_bool::_NoInit());
825 for (size_t __i = 0; __i < __x.size(); ++__i)
826 __tmp[__i] = __c > __x[__i];
831 inline _Valarray_bool _STLP_CALL operator<=(const valarray<_Tp>& __x, const _Tp& __c)
833 _Valarray_bool __tmp(__x.size(), _Valarray_bool::_NoInit());
834 for (size_t __i = 0; __i < __x.size(); ++__i)
835 __tmp[__i] = __x[__i] <= __c;
840 inline _Valarray_bool _STLP_CALL operator<=(const _Tp& __c, const valarray<_Tp>& __x)
842 _Valarray_bool __tmp(__x.size(), _Valarray_bool::_NoInit());
843 for (size_t __i = 0; __i < __x.size(); ++__i)
844 __tmp[__i] = __c <= __x[__i];
849 inline _Valarray_bool _STLP_CALL operator>=(const valarray<_Tp>& __x, const _Tp& __c)
851 _Valarray_bool __tmp(__x.size(), _Valarray_bool::_NoInit());
852 for (size_t __i = 0; __i < __x.size(); ++__i)
853 __tmp[__i] = __x[__i] >= __c;
858 inline _Valarray_bool _STLP_CALL operator>=(const _Tp& __c, const valarray<_Tp>& __x)
860 _Valarray_bool __tmp(__x.size(), _Valarray_bool::_NoInit());
861 for (size_t __i = 0; __i < __x.size(); ++__i)
862 __tmp[__i] = __c >= __x[__i];
867 inline _Valarray_bool _STLP_CALL operator&&(const valarray<_Tp>& __x, const _Tp& __c)
869 _Valarray_bool __tmp(__x.size(), _Valarray_bool::_NoInit());
870 for (size_t __i = 0; __i < __x.size(); ++__i)
871 __tmp[__i] = __x[__i] && __c;
876 inline _Valarray_bool _STLP_CALL operator&&(const _Tp& __c, const valarray<_Tp>& __x)
878 _Valarray_bool __tmp(__x.size(), _Valarray_bool::_NoInit());
879 for (size_t __i = 0; __i < __x.size(); ++__i)
880 __tmp[__i] = __c && __x[__i];
885 inline _Valarray_bool _STLP_CALL operator||(const valarray<_Tp>& __x, const _Tp& __c)
887 _Valarray_bool __tmp(__x.size(), _Valarray_bool::_NoInit());
888 for (size_t __i = 0; __i < __x.size(); ++__i)
889 __tmp[__i] = __x[__i] || __c;
894 inline _Valarray_bool _STLP_CALL operator||(const _Tp& __c, const valarray<_Tp>& __x)
896 _Valarray_bool __tmp(__x.size(), _Valarray_bool::_NoInit());
897 for (size_t __i = 0; __i < __x.size(); ++__i)
898 __tmp[__i] = __c || __x[__i];
902 // valarray "transcendentals" (the list includes abs and sqrt, which,
903 // of course, are not transcendental).
906 inline valarray<_Tp> abs(const valarray<_Tp>& __x) {
907 typedef typename valarray<_Tp>::_NoInit _NoInit;
908 valarray<_Tp> __tmp(__x.size(), _NoInit());
909 for (size_t __i = 0; __i < __x.size(); ++__i)
910 __tmp[__i] = ::abs(__x[__i]);
915 inline valarray<_Tp> acos(const valarray<_Tp>& __x) {
916 typedef typename valarray<_Tp>::_NoInit _NoInit;
917 valarray<_Tp> __tmp(__x.size(), _NoInit());
918 for (size_t __i = 0; __i < __x.size(); ++__i)
919 __tmp[__i] = ::acos(__x[__i]);
924 inline valarray<_Tp> asin(const valarray<_Tp>& __x) {
925 typedef typename valarray<_Tp>::_NoInit _NoInit;
926 valarray<_Tp> __tmp(__x.size(), _NoInit());
927 for (size_t __i = 0; __i < __x.size(); ++__i)
928 __tmp[__i] = ::asin(__x[__i]);
933 inline valarray<_Tp> atan(const valarray<_Tp>& __x) {
934 typedef typename valarray<_Tp>::_NoInit _NoInit;
935 valarray<_Tp> __tmp(__x.size(), _NoInit());
936 for (size_t __i = 0; __i < __x.size(); ++__i)
937 __tmp[__i] = ::atan(__x[__i]);
942 inline valarray<_Tp> atan2(const valarray<_Tp>& __x,
943 const valarray<_Tp>& __y) {
944 typedef typename valarray<_Tp>::_NoInit _NoInit;
945 valarray<_Tp> __tmp(__x.size(), _NoInit());
946 for (size_t __i = 0; __i < __x.size(); ++__i)
947 __tmp[__i] = ::atan2(__x[__i], __y[__i]);
952 inline valarray<_Tp> atan2(const valarray<_Tp>& __x, const _Tp& __c) {
953 typedef typename valarray<_Tp>::_NoInit _NoInit;
954 valarray<_Tp> __tmp(__x.size(), _NoInit());
955 for (size_t __i = 0; __i < __x.size(); ++__i)
956 __tmp[__i] = ::atan2(__x[__i], __c);
961 inline valarray<_Tp> atan2(const _Tp& __c, const valarray<_Tp>& __x) {
962 typedef typename valarray<_Tp>::_NoInit _NoInit;
963 valarray<_Tp> __tmp(__x.size(), _NoInit());
964 for (size_t __i = 0; __i < __x.size(); ++__i)
965 __tmp[__i] = ::atan2(__c, __x[__i]);
970 inline valarray<_Tp> cos(const valarray<_Tp>& __x) {
971 typedef typename valarray<_Tp>::_NoInit _NoInit;
972 valarray<_Tp> __tmp(__x.size(), _NoInit());
973 for (size_t __i = 0; __i < __x.size(); ++__i)
974 __tmp[__i] = ::cos(__x[__i]);
979 inline valarray<_Tp> cosh(const valarray<_Tp>& __x) {
980 typedef typename valarray<_Tp>::_NoInit _NoInit;
981 valarray<_Tp> __tmp(__x.size(), _NoInit());
982 for (size_t __i = 0; __i < __x.size(); ++__i)
983 __tmp[__i] = ::cosh(__x[__i]);
988 inline valarray<_Tp> exp(const valarray<_Tp>& __x) {
989 typedef typename valarray<_Tp>::_NoInit _NoInit;
990 valarray<_Tp> __tmp(__x.size(), _NoInit());
991 for (size_t __i = 0; __i < __x.size(); ++__i)
992 __tmp[__i] = ::exp(__x[__i]);
997 inline valarray<_Tp> log(const valarray<_Tp>& __x) {
998 typedef typename valarray<_Tp>::_NoInit _NoInit;
999 valarray<_Tp> __tmp(__x.size(), _NoInit());
1000 for (size_t __i = 0; __i < __x.size(); ++__i)
1001 __tmp[__i] = ::log(__x[__i]);
1005 template <class _Tp>
1006 inline valarray<_Tp> log10(const valarray<_Tp>& __x) {
1007 typedef typename valarray<_Tp>::_NoInit _NoInit;
1008 valarray<_Tp> __tmp(__x.size(), _NoInit());
1009 for (size_t __i = 0; __i < __x.size(); ++__i)
1010 __tmp[__i] = ::log10(__x[__i]);
1014 template <class _Tp>
1015 inline valarray<_Tp> pow(const valarray<_Tp>& __x,
1016 const valarray<_Tp>& __y) {
1017 typedef typename valarray<_Tp>::_NoInit _NoInit;
1018 valarray<_Tp> __tmp(__x.size(), _NoInit());
1019 for (size_t __i = 0; __i < __x.size(); ++__i)
1020 __tmp[__i] = ::pow(__x[__i], __y[__i]);
1024 template <class _Tp>
1025 inline valarray<_Tp> pow(const valarray<_Tp>& __x, const _Tp& __c) {
1026 typedef typename valarray<_Tp>::_NoInit _NoInit;
1027 valarray<_Tp> __tmp(__x.size(), _NoInit());
1028 for (size_t __i = 0; __i < __x.size(); ++__i)
1029 __tmp[__i] = ::pow(__x[__i], __c);
1033 template <class _Tp>
1034 inline valarray<_Tp> pow(const _Tp& __c, const valarray<_Tp>& __x) {
1035 typedef typename valarray<_Tp>::_NoInit _NoInit;
1036 valarray<_Tp> __tmp(__x.size(), _NoInit());
1037 for (size_t __i = 0; __i < __x.size(); ++__i)
1038 __tmp[__i] = ::pow(__c, __x[__i]);
1042 template <class _Tp>
1043 inline valarray<_Tp> sin(const valarray<_Tp>& __x) {
1044 typedef typename valarray<_Tp>::_NoInit _NoInit;
1045 valarray<_Tp> __tmp(__x.size(), _NoInit());
1046 for (size_t __i = 0; __i < __x.size(); ++__i)
1047 __tmp[__i] = ::sin(__x[__i]);
1051 template <class _Tp>
1052 inline valarray<_Tp> sinh(const valarray<_Tp>& __x) {
1053 typedef typename valarray<_Tp>::_NoInit _NoInit;
1054 valarray<_Tp> __tmp(__x.size(), _NoInit());
1055 for (size_t __i = 0; __i < __x.size(); ++__i)
1056 __tmp[__i] = ::sinh(__x[__i]);
1060 template <class _Tp>
1061 inline valarray<_Tp> sqrt(const valarray<_Tp>& __x) {
1062 typedef typename valarray<_Tp>::_NoInit _NoInit;
1063 valarray<_Tp> __tmp(__x.size(), _NoInit());
1064 for (size_t __i = 0; __i < __x.size(); ++__i)
1065 __tmp[__i] = ::sqrt(__x[__i]);
1069 template <class _Tp>
1070 inline valarray<_Tp> tan(const valarray<_Tp>& __x) {
1071 typedef typename valarray<_Tp>::_NoInit _NoInit;
1072 valarray<_Tp> __tmp(__x.size(), _NoInit());
1073 for (size_t __i = 0; __i < __x.size(); ++__i)
1074 __tmp[__i] = ::tan(__x[__i]);
1078 template <class _Tp>
1079 inline valarray<_Tp> tanh(const valarray<_Tp>& __x) {
1080 typedef typename valarray<_Tp>::_NoInit _NoInit;
1081 valarray<_Tp> __tmp(__x.size(), _NoInit());
1082 for (size_t __i = 0; __i < __x.size(); ++__i)
1083 __tmp[__i] = ::tanh(__x[__i]);
1087 //----------------------------------------------------------------------
1088 // slice and slice_array
1092 slice() : _M_start(0), _M_length(0), _M_stride(0) {}
1093 slice(size_t __start, size_t __length, size_t __stride)
1094 : _M_start(__start), _M_length(__length), _M_stride(__stride)
1096 __TRIVIAL_DESTRUCTOR(slice)
1098 size_t start() const { return _M_start; }
1099 size_t size() const { return _M_length; }
1100 size_t stride() const { return _M_stride; }
1108 template <class _Tp>
1110 friend class valarray<_Tp>;
1112 typedef _Tp value_type;
1114 void operator=(const valarray<value_type>& __x) const {
1115 size_t __index = _M_slice.start();
1116 for (size_t __i = 0;
1117 __i < _M_slice.size();
1118 ++__i, __index += _M_slice.stride())
1119 _M_array[__index] = __x[__i];
1122 void operator*=(const valarray<value_type>& __x) const {
1123 size_t __index = _M_slice.start();
1124 for (size_t __i = 0;
1125 __i < _M_slice.size();
1126 ++__i, __index += _M_slice.stride())
1127 _M_array[__index] *= __x[__i];
1130 void operator/=(const valarray<value_type>& __x) const {
1131 size_t __index = _M_slice.start();
1132 for (size_t __i = 0;
1133 __i < _M_slice.size();
1134 ++__i, __index += _M_slice.stride())
1135 _M_array[__index] /= __x[__i];
1138 void operator%=(const valarray<value_type>& __x) const {
1139 size_t __index = _M_slice.start();
1140 for (size_t __i = 0;
1141 __i < _M_slice.size();
1142 ++__i, __index += _M_slice.stride())
1143 _M_array[__index] %= __x[__i];
1146 void operator+=(const valarray<value_type>& __x) const {
1147 size_t __index = _M_slice.start();
1148 for (size_t __i = 0;
1149 __i < _M_slice.size();
1150 ++__i, __index += _M_slice.stride())
1151 _M_array[__index] += __x[__i];
1154 void operator-=(const valarray<value_type>& __x) const {
1155 size_t __index = _M_slice.start();
1156 for (size_t __i = 0;
1157 __i < _M_slice.size();
1158 ++__i, __index += _M_slice.stride())
1159 _M_array[__index] -= __x[__i];
1162 void operator^=(const valarray<value_type>& __x) const {
1163 size_t __index = _M_slice.start();
1164 for (size_t __i = 0;
1165 __i < _M_slice.size();
1166 ++__i, __index += _M_slice.stride())
1167 _M_array[__index] ^= __x[__i];
1170 void operator&=(const valarray<value_type>& __x) const {
1171 size_t __index = _M_slice.start();
1172 for (size_t __i = 0;
1173 __i < _M_slice.size();
1174 ++__i, __index += _M_slice.stride())
1175 _M_array[__index] &= __x[__i];
1178 void operator|=(const valarray<value_type>& __x) const {
1179 size_t __index = _M_slice.start();
1180 for (size_t __i = 0;
1181 __i < _M_slice.size();
1182 ++__i, __index += _M_slice.stride())
1183 _M_array[__index] |= __x[__i];
1186 void operator<<=(const valarray<value_type>& __x) const {
1187 size_t __index = _M_slice.start();
1188 for (size_t __i = 0;
1189 __i < _M_slice.size();
1190 ++__i, __index += _M_slice.stride())
1191 _M_array[__index] <<= __x[__i];
1194 void operator>>=(const valarray<value_type>& __x) const {
1195 size_t __index = _M_slice.start();
1196 for (size_t __i = 0;
1197 __i < _M_slice.size();
1198 ++__i, __index += _M_slice.stride())
1199 _M_array[__index] >>= __x[__i];
1202 void operator=(const value_type& __c) /*const could be const but standard says NO (26.3.5.4-1)*/ {
1203 size_t __index = _M_slice.start();
1204 for (size_t __i = 0;
1205 __i < _M_slice.size();
1206 ++__i, __index += _M_slice.stride())
1207 _M_array[__index] = __c;
1213 slice_array(const slice& __slice, valarray<_Tp>& __array)
1214 : _M_slice(__slice), _M_array(__array)
1218 valarray<_Tp>& _M_array;
1220 private: // Disable assignment and default constructor
1222 slice_array(const slice_array&);
1223 slice_array& operator=(const slice_array&);
1226 // valarray member functions dealing with slice and slice_array
1228 template <class _Tp>
1229 inline valarray<_Tp>::valarray(const slice_array<_Tp>& __x)
1230 : _Valarray_base<_Tp>(__x._M_slice.size()) {
1231 typedef typename __type_traits<_Tp>::has_trivial_default_constructor
1233 _M_initialize(_Is_Trivial());
1238 template <class _Tp>
1239 inline slice_array<_Tp> valarray<_Tp>::operator[](slice __slice) {
1240 return slice_array<_Tp>(__slice, *this);
1243 //----------------------------------------------------------------------
1244 // gslice and gslice_array
1246 template <class _Size>
1247 struct _Gslice_Iter_tmpl;
1250 friend struct _Gslice_Iter_tmpl<size_t>;
1252 gslice() : _M_start(0), _M_lengths(0), _M_strides(0) {}
1253 gslice(size_t __start,
1254 const _Valarray_size_t& __lengths, const _Valarray_size_t& __strides)
1255 : _M_start(__start), _M_lengths(__lengths), _M_strides(__strides)
1257 __TRIVIAL_DESTRUCTOR(gslice)
1259 size_t start() const { return _M_start; }
1260 _Valarray_size_t size() const { return _M_lengths; }
1261 _Valarray_size_t stride() const { return _M_strides; }
1263 // Extension: check for an empty gslice.
1264 bool _M_empty() const { return _M_lengths.size() == 0; }
1266 // Extension: number of indices this gslice represents. (For a degenerate
1267 // gslice, they're not necessarily all distinct.)
1268 size_t _M_size() const {
1269 return !this->_M_empty()
1270 ? accumulate(_M_lengths._M_first + 1,
1271 _M_lengths._M_first + _M_lengths._M_size,
1273 multiplies<size_t>())
1282 _Valarray_size_t _M_lengths;
1283 _Valarray_size_t _M_strides;
1286 // This is not an STL iterator. It is constructed from a gslice, and it
1287 // steps through the gslice indices in sequence. See 23.3.6 of the C++
1288 // standard, paragraphs 2-3, for an explanation of the sequence. At
1289 // each step we get two things: the ordinal (i.e. number of steps taken),
1290 // and the one-dimensional index.
1292 template <class _Size>
1293 struct _Gslice_Iter_tmpl {
1294 _Gslice_Iter_tmpl(const gslice& __gslice)
1295 : _M_step(0), _M_1d_idx(__gslice.start()),
1296 _M_indices(size_t(0), __gslice._M_lengths.size()),
1300 bool _M_done() const { return _M_indices[0] == _M_gslice._M_lengths[0]; }
1307 valarray<_Size> _M_indices;
1308 const gslice& _M_gslice;
1311 typedef _Gslice_Iter_tmpl<size_t> _Gslice_Iter;
1313 template <class _Tp>
1314 class gslice_array {
1315 friend class valarray<_Tp>;
1317 typedef _Tp value_type;
1319 void operator= (const valarray<value_type>& __x) const {
1320 if (!_M_gslice._M_empty()) {
1321 _Gslice_Iter __i(_M_gslice);
1322 do _M_array[__i._M_1d_idx] = __x[__i._M_step]; while(__i._M_incr());
1326 void operator*= (const valarray<value_type>& __x) const {
1327 if (!_M_gslice._M_empty()) {
1328 _Gslice_Iter __i(_M_gslice);
1329 do _M_array[__i._M_1d_idx] *= __x[__i._M_step]; while(__i._M_incr());
1333 void operator/= (const valarray<value_type>& __x) const {
1334 if (!_M_gslice._M_empty()) {
1335 _Gslice_Iter __i(_M_gslice);
1336 do _M_array[__i._M_1d_idx] /= __x[__i._M_step]; while(__i._M_incr());
1340 void operator%= (const valarray<value_type>& __x) const {
1341 if (!_M_gslice._M_empty()) {
1342 _Gslice_Iter __i(_M_gslice);
1343 do _M_array[__i._M_1d_idx] %= __x[__i._M_step]; while(__i._M_incr());
1347 void operator+= (const valarray<value_type>& __x) const {
1348 if (!_M_gslice._M_empty()) {
1349 _Gslice_Iter __i(_M_gslice);
1350 do _M_array[__i._M_1d_idx] += __x[__i._M_step]; while(__i._M_incr());
1354 void operator-= (const valarray<value_type>& __x) const {
1355 if (!_M_gslice._M_empty()) {
1356 _Gslice_Iter __i(_M_gslice);
1357 do _M_array[__i._M_1d_idx] -= __x[__i._M_step]; while(__i._M_incr());
1361 void operator^= (const valarray<value_type>& __x) const {
1362 if (!_M_gslice._M_empty()) {
1363 _Gslice_Iter __i(_M_gslice);
1364 do _M_array[__i._M_1d_idx] ^= __x[__i._M_step]; while(__i._M_incr());
1368 void operator&= (const valarray<value_type>& __x) const {
1369 if (!_M_gslice._M_empty()) {
1370 _Gslice_Iter __i(_M_gslice);
1371 do _M_array[__i._M_1d_idx] &= __x[__i._M_step]; while(__i._M_incr());
1375 void operator|= (const valarray<value_type>& __x) const {
1376 if (!_M_gslice._M_empty()) {
1377 _Gslice_Iter __i(_M_gslice);
1378 do _M_array[__i._M_1d_idx] |= __x[__i._M_step]; while(__i._M_incr());
1382 void operator<<= (const valarray<value_type>& __x) const {
1383 if (!_M_gslice._M_empty()) {
1384 _Gslice_Iter __i(_M_gslice);
1385 do _M_array[__i._M_1d_idx] <<= __x[__i._M_step]; while(__i._M_incr());
1389 void operator>>= (const valarray<value_type>& __x) const {
1390 if (!_M_gslice._M_empty()) {
1391 _Gslice_Iter __i(_M_gslice);
1392 do _M_array[__i._M_1d_idx] >>= __x[__i._M_step]; while(__i._M_incr());
1396 void operator= (const value_type& __c) /*const could be const but standard says NO (26.3.7.4-1)*/ {
1397 if (!_M_gslice._M_empty()) {
1398 _Gslice_Iter __i(_M_gslice);
1399 do _M_array[__i._M_1d_idx] = __c; while(__i._M_incr());
1406 gslice_array(const gslice& __gslice, valarray<_Tp>& __array)
1407 : _M_gslice(__gslice), _M_array(__array)
1411 valarray<value_type>& _M_array;
1413 private: // Disable assignment
1414 void operator=(const gslice_array<_Tp>&);
1417 // valarray member functions dealing with gslice and gslice_array. Note
1418 // that it is illegal (behavior is undefined) to construct a gslice_array
1419 // from a degenerate gslice.
1421 template <class _Tp>
1422 inline valarray<_Tp>::valarray(const gslice_array<_Tp>& __x)
1423 : _Valarray_base<_Tp>(__x._M_gslice._M_size()) {
1424 typedef typename __type_traits<_Tp>::has_trivial_default_constructor
1426 _M_initialize(_Is_Trivial());
1430 template <class _Tp>
1431 inline gslice_array<_Tp> valarray<_Tp>::operator[](const gslice& __slice) {
1432 return gslice_array<_Tp>(__slice, *this);
1436 //----------------------------------------------------------------------
1439 template <class _Tp>
1441 friend class valarray<_Tp>;
1443 typedef _Tp value_type;
1445 void operator=(const valarray<value_type>& __x) const {
1447 for (size_t __i = 0; __i < _M_array.size(); ++__i)
1448 if (_M_mask[__i]) _M_array[__i] = __x[__idx++];
1451 void operator*=(const valarray<value_type>& __x) const {
1453 for (size_t __i = 0; __i < _M_array.size(); ++__i)
1454 if (_M_mask[__i]) _M_array[__i] *= __x[__idx++];
1457 void operator/=(const valarray<value_type>& __x) const {
1459 for (size_t __i = 0; __i < _M_array.size(); ++__i)
1460 if (_M_mask[__i]) _M_array[__i] /= __x[__idx++];
1463 void operator%=(const valarray<value_type>& __x) const {
1465 for (size_t __i = 0; __i < _M_array.size(); ++__i)
1466 if (_M_mask[__i]) _M_array[__i] %= __x[__idx++];
1469 void operator+=(const valarray<value_type>& __x) const {
1471 for (size_t __i = 0; __i < _M_array.size(); ++__i)
1472 if (_M_mask[__i]) _M_array[__i] += __x[__idx++];
1475 void operator-=(const valarray<value_type>& __x) const {
1477 for (size_t __i = 0; __i < _M_array.size(); ++__i)
1478 if (_M_mask[__i]) _M_array[__i] -= __x[__idx++];
1481 void operator^=(const valarray<value_type>& __x) const {
1483 for (size_t __i = 0; __i < _M_array.size(); ++__i)
1484 if (_M_mask[__i]) _M_array[__i] ^= __x[__idx++];
1487 void operator&=(const valarray<value_type>& __x) const {
1489 for (size_t __i = 0; __i < _M_array.size(); ++__i)
1490 if (_M_mask[__i]) _M_array[__i] &= __x[__idx++];
1493 void operator|=(const valarray<value_type>& __x) const {
1495 for (size_t __i = 0; __i < _M_array.size(); ++__i)
1496 if (_M_mask[__i]) _M_array[__i] |= __x[__idx++];
1499 void operator<<=(const valarray<value_type>& __x) const {
1501 for (size_t __i = 0; __i < _M_array.size(); ++__i)
1502 if (_M_mask[__i]) _M_array[__i] <<= __x[__idx++];
1505 void operator>>=(const valarray<value_type>& __x) const {
1507 for (size_t __i = 0; __i < _M_array.size(); ++__i)
1508 if (_M_mask[__i]) _M_array[__i] >>= __x[__idx++];
1511 void operator=(const value_type& __c) const {
1512 for (size_t __i = 0; __i < _M_array.size(); ++__i)
1513 if (_M_mask[__i]) _M_array[__i] = __c;
1518 // Extension: number of true values in the mask
1519 size_t _M_num_true() const {
1520 size_t __result = 0;
1521 for (size_t __i = 0; __i < _M_mask.size(); ++__i)
1522 if (_M_mask[__i]) ++__result;
1527 mask_array(const _Valarray_bool& __mask, valarray<_Tp>& __array)
1528 : _M_mask(__mask), _M_array(__array)
1531 _Valarray_bool _M_mask;
1532 valarray<_Tp>& _M_array;
1534 private: // Disable assignment
1535 void operator=(const mask_array<_Tp>&);
1538 // valarray member functions dealing with mask_array
1540 template <class _Tp>
1541 inline valarray<_Tp>::valarray(const mask_array<_Tp>& __x)
1542 : _Valarray_base<_Tp>(__x._M_num_true())
1544 typedef typename __type_traits<_Tp>::has_trivial_default_constructor
1546 _M_initialize(_Is_Trivial());
1550 // Behavior is undefined if __x._M_num_true() != this->size()
1551 template <class _Tp>
1552 inline valarray<_Tp>& valarray<_Tp>::operator=(const mask_array<_Tp>& __x) {
1554 for (size_t __i = 0; __i < __x._M_array.size(); ++__i)
1555 if (__x._M_mask[__i]) (*this)[__idx++] = __x._M_array[__i];
1559 template <class _Tp>
1560 inline mask_array<_Tp> valarray<_Tp>::operator[](const _Valarray_bool& __mask)
1562 return mask_array<_Tp>(__mask, *this);
1566 //----------------------------------------------------------------------
1569 template <class _Tp>
1570 class indirect_array {
1571 friend class valarray<_Tp>;
1573 typedef _Tp value_type;
1575 void operator=(const valarray<value_type>& __x) const {
1576 for (size_t __i = 0; __i < _M_addr.size(); ++__i)
1577 _M_array[_M_addr[__i]] = __x[__i];
1580 void operator*=(const valarray<value_type>& __x) const {
1581 for (size_t __i = 0; __i < _M_addr.size(); ++__i)
1582 _M_array[_M_addr[__i]] *= __x[__i];
1585 void operator/=(const valarray<value_type>& __x) const {
1586 for (size_t __i = 0; __i < _M_addr.size(); ++__i)
1587 _M_array[_M_addr[__i]] /= __x[__i];
1590 void operator%=(const valarray<value_type>& __x) const {
1591 for (size_t __i = 0; __i < _M_addr.size(); ++__i)
1592 _M_array[_M_addr[__i]] %= __x[__i];
1595 void operator+=(const valarray<value_type>& __x) const {
1596 for (size_t __i = 0; __i < _M_addr.size(); ++__i)
1597 _M_array[_M_addr[__i]] += __x[__i];
1600 void operator-=(const valarray<value_type>& __x) const {
1601 for (size_t __i = 0; __i < _M_addr.size(); ++__i)
1602 _M_array[_M_addr[__i]] -= __x[__i];
1605 void operator^=(const valarray<value_type>& __x) const {
1606 for (size_t __i = 0; __i < _M_addr.size(); ++__i)
1607 _M_array[_M_addr[__i]] ^= __x[__i];
1610 void operator&=(const valarray<value_type>& __x) const {
1611 for (size_t __i = 0; __i < _M_addr.size(); ++__i)
1612 _M_array[_M_addr[__i]] &= __x[__i];
1615 void operator|=(const valarray<value_type>& __x) const {
1616 for (size_t __i = 0; __i < _M_addr.size(); ++__i)
1617 _M_array[_M_addr[__i]] |= __x[__i];
1620 void operator<<=(const valarray<value_type>& __x) const {
1621 for (size_t __i = 0; __i < _M_addr.size(); ++__i)
1622 _M_array[_M_addr[__i]] <<= __x[__i];
1625 void operator>>=(const valarray<value_type>& __x) const {
1626 for (size_t __i = 0; __i < _M_addr.size(); ++__i)
1627 _M_array[_M_addr[__i]] >>= __x[__i];
1630 void operator=(const value_type& __c) const {
1631 for (size_t __i = 0; __i < _M_addr.size(); ++__i)
1632 _M_array[_M_addr[__i]] = __c;
1635 ~indirect_array() {}
1638 indirect_array(const _Valarray_size_t& __addr, valarray<_Tp>& __array)
1639 : _M_addr(__addr), _M_array(__array)
1642 _Valarray_size_t _M_addr;
1643 valarray<_Tp>& _M_array;
1645 private: // Disable assignment
1646 void operator=(const indirect_array<_Tp>&);
1649 // valarray member functions dealing with indirect_array
1651 template <class _Tp>
1652 inline valarray<_Tp>::valarray(const indirect_array<_Tp>& __x)
1653 : _Valarray_base<_Tp>(__x._M_addr.size())
1655 typedef typename __type_traits<_Tp>::has_trivial_default_constructor
1657 _M_initialize(_Is_Trivial());
1662 template <class _Tp>
1663 inline indirect_array<_Tp>
1664 valarray<_Tp>::operator[](const _Valarray_size_t& __addr)
1666 return indirect_array<_Tp>(__addr, *this);
1671 # if !defined (_STLP_LINK_TIME_INSTANTIATION)
1672 # include <stl/_valarray.c>
1675 #endif /* _STLP_VALARRAY */