1 // The template and inlines for the -*- C++ -*- valarray class.
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
5 // Free Software Foundation, Inc.
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 3, or (at your option)
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // Under Section 7 of GPL version 3, you are granted additional
19 // permissions described in the GCC Runtime Library Exception, version
20 // 3.1, as published by the Free Software Foundation.
22 // You should have received a copy of the GNU General Public License and
23 // a copy of the GCC Runtime Library Exception along with this program;
24 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 // <http://www.gnu.org/licenses/>.
28 * This is a Standard C++ Library header.
31 // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr>
33 #ifndef _GLIBCXX_VALARRAY
34 #define _GLIBCXX_VALARRAY 1
36 #pragma GCC system_header
38 #include <bits/c++config.h>
42 #include <debug/debug.h>
43 #include <initializer_list>
45 _GLIBCXX_BEGIN_NAMESPACE(std)
47 template<class _Clos, typename _Tp>
50 template<typename _Tp1, typename _Tp2>
53 template<class _Oper, template<class, class> class _Meta, class _Dom>
57 template<class, class> class _Meta1,
58 template<class, class> class _Meta2,
59 class _Dom1, class _Dom2>
62 template<template<class, class> class _Meta, class _Dom>
65 template<template<class, class> class _Meta, class _Dom>
68 template<template<class, class> class _Meta, class _Dom>
71 template<template<class, class> class _Meta, class _Dom>
74 template<template<class, class> class _Meta, class _Dom>
77 template<class _Tp> class valarray; // An array of type _Tp
78 class slice; // BLAS-like slice out of an array
79 template<class _Tp> class slice_array;
80 class gslice; // generalized slice out of an array
81 template<class _Tp> class gslice_array;
82 template<class _Tp> class mask_array; // masked array
83 template<class _Tp> class indirect_array; // indirected array
85 _GLIBCXX_END_NAMESPACE
87 #include <bits/valarray_array.h>
88 #include <bits/valarray_before.h>
90 _GLIBCXX_BEGIN_NAMESPACE(std)
93 * @defgroup numeric_arrays Numeric Arrays
96 * Classes and functions for representing and manipulating arrays of elements.
101 * @brief Smart array designed to support numeric processing.
103 * A valarray is an array that provides constraints intended to allow for
104 * effective optimization of numeric array processing by reducing the
105 * aliasing that can result from pointer representations. It represents a
106 * one-dimensional array from which different multidimensional subsets can
107 * be accessed and modified.
109 * @param Tp Type of object in the array.
117 typedef typename __fun<_Op, _Tp>::result_type __rt;
118 typedef _Expr<_UnClos<_Op, _ValArray, _Tp>, __rt> _Rt;
121 typedef _Tp value_type;
123 // _lib.valarray.cons_ construct/destroy:
124 /// Construct an empty array.
127 /// Construct an array with @a n elements.
128 explicit valarray(size_t);
130 /// Construct an array with @a n elements initialized to @a t.
131 valarray(const _Tp&, size_t);
133 /// Construct an array initialized to the first @a n elements of @a t.
134 valarray(const _Tp* __restrict__, size_t);
136 /// Copy constructor.
137 valarray(const valarray&);
139 /// Construct an array with the same size and values in @a sa.
140 valarray(const slice_array<_Tp>&);
142 /// Construct an array with the same size and values in @a ga.
143 valarray(const gslice_array<_Tp>&);
145 /// Construct an array with the same size and values in @a ma.
146 valarray(const mask_array<_Tp>&);
148 /// Construct an array with the same size and values in @a ia.
149 valarray(const indirect_array<_Tp>&);
151 #ifdef __GXX_EXPERIMENTAL_CXX0X__
152 /// Construct an array with an initializer_list of values.
153 valarray(initializer_list<_Tp>);
157 valarray(const _Expr<_Dom, _Tp>& __e);
161 // _lib.valarray.assign_ assignment:
163 * @brief Assign elements to an array.
165 * Assign elements of array to values in @a v. Results are undefined
166 * if @a v does not have the same size as this array.
168 * @param v Valarray to get values from.
170 valarray<_Tp>& operator=(const valarray<_Tp>&);
173 * @brief Assign elements to a value.
175 * Assign all elements of array to @a t.
177 * @param t Value for elements.
179 valarray<_Tp>& operator=(const _Tp&);
182 * @brief Assign elements to an array subset.
184 * Assign elements of array to values in @a sa. Results are undefined
185 * if @a sa does not have the same size as this array.
187 * @param sa Array slice to get values from.
189 valarray<_Tp>& operator=(const slice_array<_Tp>&);
192 * @brief Assign elements to an array subset.
194 * Assign elements of array to values in @a ga. Results are undefined
195 * if @a ga does not have the same size as this array.
197 * @param ga Array slice to get values from.
199 valarray<_Tp>& operator=(const gslice_array<_Tp>&);
202 * @brief Assign elements to an array subset.
204 * Assign elements of array to values in @a ma. Results are undefined
205 * if @a ma does not have the same size as this array.
207 * @param ma Array slice to get values from.
209 valarray<_Tp>& operator=(const mask_array<_Tp>&);
212 * @brief Assign elements to an array subset.
214 * Assign elements of array to values in @a ia. Results are undefined
215 * if @a ia does not have the same size as this array.
217 * @param ia Array slice to get values from.
219 valarray<_Tp>& operator=(const indirect_array<_Tp>&);
221 #ifdef __GXX_EXPERIMENTAL_CXX0X__
223 * @brief Assign elements to an initializer_list.
225 * Assign elements of array to values in @a l. Results are undefined
226 * if @a l does not have the same size as this array.
228 * @param l initializer_list to get values from.
230 valarray& operator=(initializer_list<_Tp>);
233 template<class _Dom> valarray<_Tp>&
234 operator= (const _Expr<_Dom, _Tp>&);
236 // _lib.valarray.access_ element access:
238 * Return a reference to the i'th array element.
240 * @param i Index of element to return.
241 * @return Reference to the i'th element.
243 _Tp& operator[](size_t);
245 // _GLIBCXX_RESOLVE_LIB_DEFECTS
246 // 389. Const overload of valarray::operator[] returns by value.
247 const _Tp& operator[](size_t) const;
249 // _lib.valarray.sub_ subset operations:
251 * @brief Return an array subset.
253 * Returns a new valarray containing the elements of the array
254 * indicated by the slice argument. The new valarray has the same size
255 * as the input slice. @see slice.
257 * @param s The source slice.
258 * @return New valarray containing elements in @a s.
260 _Expr<_SClos<_ValArray, _Tp>, _Tp> operator[](slice) const;
263 * @brief Return a reference to an array subset.
265 * Returns a new valarray containing the elements of the array
266 * indicated by the slice argument. The new valarray has the same size
267 * as the input slice. @see slice.
269 * @param s The source slice.
270 * @return New valarray containing elements in @a s.
272 slice_array<_Tp> operator[](slice);
275 * @brief Return an array subset.
277 * Returns a slice_array referencing the elements of the array
278 * indicated by the slice argument. @see gslice.
280 * @param s The source slice.
281 * @return Slice_array referencing elements indicated by @a s.
283 _Expr<_GClos<_ValArray, _Tp>, _Tp> operator[](const gslice&) const;
286 * @brief Return a reference to an array subset.
288 * Returns a new valarray containing the elements of the array
289 * indicated by the gslice argument. The new valarray has
290 * the same size as the input gslice. @see gslice.
292 * @param s The source gslice.
293 * @return New valarray containing elements in @a s.
295 gslice_array<_Tp> operator[](const gslice&);
298 * @brief Return an array subset.
300 * Returns a new valarray containing the elements of the array
301 * indicated by the argument. The input is a valarray of bool which
302 * represents a bitmask indicating which elements should be copied into
303 * the new valarray. Each element of the array is added to the return
304 * valarray if the corresponding element of the argument is true.
306 * @param m The valarray bitmask.
307 * @return New valarray containing elements indicated by @a m.
309 valarray<_Tp> operator[](const valarray<bool>&) const;
312 * @brief Return a reference to an array subset.
314 * Returns a new mask_array referencing the elements of the array
315 * indicated by the argument. The input is a valarray of bool which
316 * represents a bitmask indicating which elements are part of the
317 * subset. Elements of the array are part of the subset if the
318 * corresponding element of the argument is true.
320 * @param m The valarray bitmask.
321 * @return New valarray containing elements indicated by @a m.
323 mask_array<_Tp> operator[](const valarray<bool>&);
326 * @brief Return an array subset.
328 * Returns a new valarray containing the elements of the array
329 * indicated by the argument. The elements in the argument are
330 * interpreted as the indices of elements of this valarray to copy to
331 * the return valarray.
333 * @param i The valarray element index list.
334 * @return New valarray containing elements in @a s.
336 _Expr<_IClos<_ValArray, _Tp>, _Tp>
337 operator[](const valarray<size_t>&) const;
340 * @brief Return a reference to an array subset.
342 * Returns an indirect_array referencing the elements of the array
343 * indicated by the argument. The elements in the argument are
344 * interpreted as the indices of elements of this valarray to include
345 * in the subset. The returned indirect_array refers to these
348 * @param i The valarray element index list.
349 * @return Indirect_array referencing elements in @a i.
351 indirect_array<_Tp> operator[](const valarray<size_t>&);
353 // _lib.valarray.unary_ unary operators:
354 /// Return a new valarray by applying unary + to each element.
355 typename _UnaryOp<__unary_plus>::_Rt operator+() const;
357 /// Return a new valarray by applying unary - to each element.
358 typename _UnaryOp<__negate>::_Rt operator-() const;
360 /// Return a new valarray by applying unary ~ to each element.
361 typename _UnaryOp<__bitwise_not>::_Rt operator~() const;
363 /// Return a new valarray by applying unary ! to each element.
364 typename _UnaryOp<__logical_not>::_Rt operator!() const;
366 // _lib.valarray.cassign_ computed assignment:
367 /// Multiply each element of array by @a t.
368 valarray<_Tp>& operator*=(const _Tp&);
370 /// Divide each element of array by @a t.
371 valarray<_Tp>& operator/=(const _Tp&);
373 /// Set each element e of array to e % @a t.
374 valarray<_Tp>& operator%=(const _Tp&);
376 /// Add @a t to each element of array.
377 valarray<_Tp>& operator+=(const _Tp&);
379 /// Subtract @a t to each element of array.
380 valarray<_Tp>& operator-=(const _Tp&);
382 /// Set each element e of array to e ^ @a t.
383 valarray<_Tp>& operator^=(const _Tp&);
385 /// Set each element e of array to e & @a t.
386 valarray<_Tp>& operator&=(const _Tp&);
388 /// Set each element e of array to e | @a t.
389 valarray<_Tp>& operator|=(const _Tp&);
391 /// Left shift each element e of array by @a t bits.
392 valarray<_Tp>& operator<<=(const _Tp&);
394 /// Right shift each element e of array by @a t bits.
395 valarray<_Tp>& operator>>=(const _Tp&);
397 /// Multiply elements of array by corresponding elements of @a v.
398 valarray<_Tp>& operator*=(const valarray<_Tp>&);
400 /// Divide elements of array by corresponding elements of @a v.
401 valarray<_Tp>& operator/=(const valarray<_Tp>&);
403 /// Modulo elements of array by corresponding elements of @a v.
404 valarray<_Tp>& operator%=(const valarray<_Tp>&);
406 /// Add corresponding elements of @a v to elements of array.
407 valarray<_Tp>& operator+=(const valarray<_Tp>&);
409 /// Subtract corresponding elements of @a v from elements of array.
410 valarray<_Tp>& operator-=(const valarray<_Tp>&);
412 /// Logical xor corresponding elements of @a v with elements of array.
413 valarray<_Tp>& operator^=(const valarray<_Tp>&);
415 /// Logical or corresponding elements of @a v with elements of array.
416 valarray<_Tp>& operator|=(const valarray<_Tp>&);
418 /// Logical and corresponding elements of @a v with elements of array.
419 valarray<_Tp>& operator&=(const valarray<_Tp>&);
421 /// Left shift elements of array by corresponding elements of @a v.
422 valarray<_Tp>& operator<<=(const valarray<_Tp>&);
424 /// Right shift elements of array by corresponding elements of @a v.
425 valarray<_Tp>& operator>>=(const valarray<_Tp>&);
428 valarray<_Tp>& operator*=(const _Expr<_Dom, _Tp>&);
430 valarray<_Tp>& operator/=(const _Expr<_Dom, _Tp>&);
432 valarray<_Tp>& operator%=(const _Expr<_Dom, _Tp>&);
434 valarray<_Tp>& operator+=(const _Expr<_Dom, _Tp>&);
436 valarray<_Tp>& operator-=(const _Expr<_Dom, _Tp>&);
438 valarray<_Tp>& operator^=(const _Expr<_Dom, _Tp>&);
440 valarray<_Tp>& operator|=(const _Expr<_Dom, _Tp>&);
442 valarray<_Tp>& operator&=(const _Expr<_Dom, _Tp>&);
444 valarray<_Tp>& operator<<=(const _Expr<_Dom, _Tp>&);
446 valarray<_Tp>& operator>>=(const _Expr<_Dom, _Tp>&);
448 // _lib.valarray.members_ member functions:
449 /// Return the number of elements in array.
453 * @brief Return the sum of all elements in the array.
455 * Accumulates the sum of all elements into a Tp using +=. The order
456 * of adding the elements is unspecified.
460 /// Return the minimum element using operator<().
463 /// Return the maximum element using operator<().
467 * @brief Return a shifted array.
469 * A new valarray is constructed as a copy of this array with elements
470 * in shifted positions. For an element with index i, the new position
471 * is i - n. The new valarray has the same size as the current one.
472 * New elements without a value are set to 0. Elements whose new
473 * position is outside the bounds of the array are discarded.
475 * Positive arguments shift toward index 0, discarding elements [0, n).
476 * Negative arguments discard elements from the top of the array.
478 * @param n Number of element positions to shift.
479 * @return New valarray with elements in shifted positions.
481 valarray<_Tp> shift (int) const;
484 * @brief Return a rotated array.
486 * A new valarray is constructed as a copy of this array with elements
487 * in shifted positions. For an element with index i, the new position
488 * is (i - n) % size(). The new valarray has the same size as the
489 * current one. Elements that are shifted beyond the array bounds are
490 * shifted into the other end of the array. No elements are lost.
492 * Positive arguments shift toward index 0, wrapping around the top.
493 * Negative arguments shift towards the top, wrapping around to 0.
495 * @param n Number of element positions to rotate.
496 * @return New valarray with elements in shifted positions.
498 valarray<_Tp> cshift(int) const;
501 * @brief Apply a function to the array.
503 * Returns a new valarray with elements assigned to the result of
504 * applying func to the corresponding element of this array. The new
505 * array has the same size as this one.
507 * @param func Function of Tp returning Tp to apply.
508 * @return New valarray with transformed elements.
510 _Expr<_ValFunClos<_ValArray, _Tp>, _Tp> apply(_Tp func(_Tp)) const;
513 * @brief Apply a function to the array.
515 * Returns a new valarray with elements assigned to the result of
516 * applying func to the corresponding element of this array. The new
517 * array has the same size as this one.
519 * @param func Function of const Tp& returning Tp to apply.
520 * @return New valarray with transformed elements.
522 _Expr<_RefFunClos<_ValArray, _Tp>, _Tp> apply(_Tp func(const _Tp&)) const;
525 * @brief Resize array.
527 * Resize this array to @a size and set all elements to @a c. All
528 * references and iterators are invalidated.
530 * @param size New array size.
531 * @param c New value for all elements.
533 void resize(size_t __size, _Tp __c = _Tp());
537 _Tp* __restrict__ _M_data;
539 friend class _Array<_Tp>;
542 template<typename _Tp>
544 valarray<_Tp>::operator[](size_t __i) const
546 __glibcxx_requires_subscript(__i);
550 template<typename _Tp>
552 valarray<_Tp>::operator[](size_t __i)
554 __glibcxx_requires_subscript(__i);
558 // @} group numeric_arrays
560 _GLIBCXX_END_NAMESPACE
562 #include <bits/valarray_after.h>
563 #include <bits/slice_array.h>
564 #include <bits/gslice.h>
565 #include <bits/gslice_array.h>
566 #include <bits/mask_array.h>
567 #include <bits/indirect_array.h>
569 _GLIBCXX_BEGIN_NAMESPACE(std)
572 * @addtogroup numeric_arrays
576 template<typename _Tp>
578 valarray<_Tp>::valarray() : _M_size(0), _M_data(0) {}
580 template<typename _Tp>
582 valarray<_Tp>::valarray(size_t __n)
583 : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n))
584 { std::__valarray_default_construct(_M_data, _M_data + __n); }
586 template<typename _Tp>
588 valarray<_Tp>::valarray(const _Tp& __t, size_t __n)
589 : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n))
590 { std::__valarray_fill_construct(_M_data, _M_data + __n, __t); }
592 template<typename _Tp>
594 valarray<_Tp>::valarray(const _Tp* __restrict__ __p, size_t __n)
595 : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n))
597 _GLIBCXX_DEBUG_ASSERT(__p != 0 || __n == 0);
598 std::__valarray_copy_construct(__p, __p + __n, _M_data);
601 template<typename _Tp>
603 valarray<_Tp>::valarray(const valarray<_Tp>& __v)
604 : _M_size(__v._M_size), _M_data(__valarray_get_storage<_Tp>(__v._M_size))
605 { std::__valarray_copy_construct(__v._M_data, __v._M_data + _M_size,
608 template<typename _Tp>
610 valarray<_Tp>::valarray(const slice_array<_Tp>& __sa)
611 : _M_size(__sa._M_sz), _M_data(__valarray_get_storage<_Tp>(__sa._M_sz))
613 std::__valarray_copy_construct
614 (__sa._M_array, __sa._M_sz, __sa._M_stride, _Array<_Tp>(_M_data));
617 template<typename _Tp>
619 valarray<_Tp>::valarray(const gslice_array<_Tp>& __ga)
620 : _M_size(__ga._M_index.size()),
621 _M_data(__valarray_get_storage<_Tp>(_M_size))
623 std::__valarray_copy_construct
624 (__ga._M_array, _Array<size_t>(__ga._M_index),
625 _Array<_Tp>(_M_data), _M_size);
628 template<typename _Tp>
630 valarray<_Tp>::valarray(const mask_array<_Tp>& __ma)
631 : _M_size(__ma._M_sz), _M_data(__valarray_get_storage<_Tp>(__ma._M_sz))
633 std::__valarray_copy_construct
634 (__ma._M_array, __ma._M_mask, _Array<_Tp>(_M_data), _M_size);
637 template<typename _Tp>
639 valarray<_Tp>::valarray(const indirect_array<_Tp>& __ia)
640 : _M_size(__ia._M_sz), _M_data(__valarray_get_storage<_Tp>(__ia._M_sz))
642 std::__valarray_copy_construct
643 (__ia._M_array, __ia._M_index, _Array<_Tp>(_M_data), _M_size);
646 #ifdef __GXX_EXPERIMENTAL_CXX0X__
647 template<typename _Tp>
649 valarray<_Tp>::valarray(initializer_list<_Tp> __l)
650 : _M_size(__l.size()), _M_data(__valarray_get_storage<_Tp>(__l.size()))
651 { std::__valarray_copy_construct (__l.begin(), __l.end(), _M_data); }
654 template<typename _Tp> template<class _Dom>
656 valarray<_Tp>::valarray(const _Expr<_Dom, _Tp>& __e)
657 : _M_size(__e.size()), _M_data(__valarray_get_storage<_Tp>(_M_size))
658 { std::__valarray_copy_construct(__e, _M_size, _Array<_Tp>(_M_data)); }
660 template<typename _Tp>
662 valarray<_Tp>::~valarray()
664 std::__valarray_destroy_elements(_M_data, _M_data + _M_size);
665 std::__valarray_release_memory(_M_data);
668 template<typename _Tp>
669 inline valarray<_Tp>&
670 valarray<_Tp>::operator=(const valarray<_Tp>& __v)
672 _GLIBCXX_DEBUG_ASSERT(_M_size == __v._M_size);
673 std::__valarray_copy(__v._M_data, _M_size, _M_data);
677 #ifdef __GXX_EXPERIMENTAL_CXX0X__
678 template<typename _Tp>
679 inline valarray<_Tp>&
680 valarray<_Tp>::operator=(initializer_list<_Tp> __l)
682 _GLIBCXX_DEBUG_ASSERT(_M_size == __l.size());
683 std::__valarray_copy(__l.begin(), __l.size(), _M_data);
687 template<typename _Tp>
688 inline valarray<_Tp>&
689 valarray<_Tp>::operator=(const _Tp& __t)
691 std::__valarray_fill(_M_data, _M_size, __t);
695 template<typename _Tp>
696 inline valarray<_Tp>&
697 valarray<_Tp>::operator=(const slice_array<_Tp>& __sa)
699 _GLIBCXX_DEBUG_ASSERT(_M_size == __sa._M_sz);
700 std::__valarray_copy(__sa._M_array, __sa._M_sz,
701 __sa._M_stride, _Array<_Tp>(_M_data));
705 template<typename _Tp>
706 inline valarray<_Tp>&
707 valarray<_Tp>::operator=(const gslice_array<_Tp>& __ga)
709 _GLIBCXX_DEBUG_ASSERT(_M_size == __ga._M_index.size());
710 std::__valarray_copy(__ga._M_array, _Array<size_t>(__ga._M_index),
711 _Array<_Tp>(_M_data), _M_size);
715 template<typename _Tp>
716 inline valarray<_Tp>&
717 valarray<_Tp>::operator=(const mask_array<_Tp>& __ma)
719 _GLIBCXX_DEBUG_ASSERT(_M_size == __ma._M_sz);
720 std::__valarray_copy(__ma._M_array, __ma._M_mask,
721 _Array<_Tp>(_M_data), _M_size);
725 template<typename _Tp>
726 inline valarray<_Tp>&
727 valarray<_Tp>::operator=(const indirect_array<_Tp>& __ia)
729 _GLIBCXX_DEBUG_ASSERT(_M_size == __ia._M_sz);
730 std::__valarray_copy(__ia._M_array, __ia._M_index,
731 _Array<_Tp>(_M_data), _M_size);
735 template<typename _Tp> template<class _Dom>
736 inline valarray<_Tp>&
737 valarray<_Tp>::operator=(const _Expr<_Dom, _Tp>& __e)
739 _GLIBCXX_DEBUG_ASSERT(_M_size == __e.size());
740 std::__valarray_copy(__e, _M_size, _Array<_Tp>(_M_data));
744 template<typename _Tp>
745 inline _Expr<_SClos<_ValArray,_Tp>, _Tp>
746 valarray<_Tp>::operator[](slice __s) const
748 typedef _SClos<_ValArray,_Tp> _Closure;
749 return _Expr<_Closure, _Tp>(_Closure (_Array<_Tp>(_M_data), __s));
752 template<typename _Tp>
753 inline slice_array<_Tp>
754 valarray<_Tp>::operator[](slice __s)
755 { return slice_array<_Tp>(_Array<_Tp>(_M_data), __s); }
757 template<typename _Tp>
758 inline _Expr<_GClos<_ValArray,_Tp>, _Tp>
759 valarray<_Tp>::operator[](const gslice& __gs) const
761 typedef _GClos<_ValArray,_Tp> _Closure;
762 return _Expr<_Closure, _Tp>
763 (_Closure(_Array<_Tp>(_M_data), __gs._M_index->_M_index));
766 template<typename _Tp>
767 inline gslice_array<_Tp>
768 valarray<_Tp>::operator[](const gslice& __gs)
770 return gslice_array<_Tp>
771 (_Array<_Tp>(_M_data), __gs._M_index->_M_index);
774 template<typename _Tp>
776 valarray<_Tp>::operator[](const valarray<bool>& __m) const
779 size_t __e = __m.size();
780 for (size_t __i=0; __i<__e; ++__i)
782 return valarray<_Tp>(mask_array<_Tp>(_Array<_Tp>(_M_data), __s,
783 _Array<bool> (__m)));
786 template<typename _Tp>
787 inline mask_array<_Tp>
788 valarray<_Tp>::operator[](const valarray<bool>& __m)
791 size_t __e = __m.size();
792 for (size_t __i=0; __i<__e; ++__i)
794 return mask_array<_Tp>(_Array<_Tp>(_M_data), __s, _Array<bool>(__m));
797 template<typename _Tp>
798 inline _Expr<_IClos<_ValArray,_Tp>, _Tp>
799 valarray<_Tp>::operator[](const valarray<size_t>& __i) const
801 typedef _IClos<_ValArray,_Tp> _Closure;
802 return _Expr<_Closure, _Tp>(_Closure(*this, __i));
805 template<typename _Tp>
806 inline indirect_array<_Tp>
807 valarray<_Tp>::operator[](const valarray<size_t>& __i)
809 return indirect_array<_Tp>(_Array<_Tp>(_M_data), __i.size(),
810 _Array<size_t>(__i));
815 valarray<_Tp>::size() const
820 valarray<_Tp>::sum() const
822 _GLIBCXX_DEBUG_ASSERT(_M_size > 0);
823 return std::__valarray_sum(_M_data, _M_data + _M_size);
828 valarray<_Tp>::shift(int __n) const
835 _Tp* __restrict__ __tmp_M_data =
836 std::__valarray_get_storage<_Tp>(_M_size);
839 std::__valarray_copy_construct(_M_data,
840 _M_data + _M_size, __tmp_M_data);
841 else if (__n > 0) // shift left
843 if (size_t(__n) > _M_size)
846 std::__valarray_copy_construct(_M_data + __n,
847 _M_data + _M_size, __tmp_M_data);
848 std::__valarray_default_construct(__tmp_M_data + _M_size - __n,
849 __tmp_M_data + _M_size);
853 if (-size_t(__n) > _M_size)
856 std::__valarray_copy_construct(_M_data, _M_data + _M_size + __n,
858 std::__valarray_default_construct(__tmp_M_data,
862 __ret._M_size = _M_size;
863 __ret._M_data = __tmp_M_data;
869 valarray<_Tp>::cshift(int __n) const
876 _Tp* __restrict__ __tmp_M_data =
877 std::__valarray_get_storage<_Tp>(_M_size);
880 std::__valarray_copy_construct(_M_data,
881 _M_data + _M_size, __tmp_M_data);
882 else if (__n > 0) // cshift left
884 if (size_t(__n) > _M_size)
885 __n = int(__n % _M_size);
887 std::__valarray_copy_construct(_M_data, _M_data + __n,
888 __tmp_M_data + _M_size - __n);
889 std::__valarray_copy_construct(_M_data + __n, _M_data + _M_size,
894 if (-size_t(__n) > _M_size)
895 __n = -int(-size_t(__n) % _M_size);
897 std::__valarray_copy_construct(_M_data + _M_size + __n,
898 _M_data + _M_size, __tmp_M_data);
899 std::__valarray_copy_construct(_M_data, _M_data + _M_size + __n,
903 __ret._M_size = _M_size;
904 __ret._M_data = __tmp_M_data;
910 valarray<_Tp>::resize(size_t __n, _Tp __c)
912 // This complication is so to make valarray<valarray<T> > work
913 // even though it is not required by the standard. Nobody should
914 // be saying valarray<valarray<T> > anyway. See the specs.
915 std::__valarray_destroy_elements(_M_data, _M_data + _M_size);
918 std::__valarray_release_memory(_M_data);
920 _M_data = __valarray_get_storage<_Tp>(__n);
922 std::__valarray_fill_construct(_M_data, _M_data + __n, __c);
925 template<typename _Tp>
927 valarray<_Tp>::min() const
929 _GLIBCXX_DEBUG_ASSERT(_M_size > 0);
930 return *std::min_element(_M_data, _M_data + _M_size);
933 template<typename _Tp>
935 valarray<_Tp>::max() const
937 _GLIBCXX_DEBUG_ASSERT(_M_size > 0);
938 return *std::max_element(_M_data, _M_data + _M_size);
942 inline _Expr<_ValFunClos<_ValArray, _Tp>, _Tp>
943 valarray<_Tp>::apply(_Tp func(_Tp)) const
945 typedef _ValFunClos<_ValArray, _Tp> _Closure;
946 return _Expr<_Closure, _Tp>(_Closure(*this, func));
950 inline _Expr<_RefFunClos<_ValArray, _Tp>, _Tp>
951 valarray<_Tp>::apply(_Tp func(const _Tp &)) const
953 typedef _RefFunClos<_ValArray, _Tp> _Closure;
954 return _Expr<_Closure, _Tp>(_Closure(*this, func));
957 #define _DEFINE_VALARRAY_UNARY_OPERATOR(_Op, _Name) \
958 template<typename _Tp> \
959 inline typename valarray<_Tp>::template _UnaryOp<_Name>::_Rt \
960 valarray<_Tp>::operator _Op() const \
962 typedef _UnClos<_Name, _ValArray, _Tp> _Closure; \
963 typedef typename __fun<_Name, _Tp>::result_type _Rt; \
964 return _Expr<_Closure, _Rt>(_Closure(*this)); \
967 _DEFINE_VALARRAY_UNARY_OPERATOR(+, __unary_plus)
968 _DEFINE_VALARRAY_UNARY_OPERATOR(-, __negate)
969 _DEFINE_VALARRAY_UNARY_OPERATOR(~, __bitwise_not)
970 _DEFINE_VALARRAY_UNARY_OPERATOR (!, __logical_not)
972 #undef _DEFINE_VALARRAY_UNARY_OPERATOR
974 #define _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(_Op, _Name) \
975 template<class _Tp> \
976 inline valarray<_Tp>& \
977 valarray<_Tp>::operator _Op##=(const _Tp &__t) \
979 _Array_augmented_##_Name(_Array<_Tp>(_M_data), _M_size, __t); \
983 template<class _Tp> \
984 inline valarray<_Tp>& \
985 valarray<_Tp>::operator _Op##=(const valarray<_Tp> &__v) \
987 _GLIBCXX_DEBUG_ASSERT(_M_size == __v._M_size); \
988 _Array_augmented_##_Name(_Array<_Tp>(_M_data), _M_size, \
989 _Array<_Tp>(__v._M_data)); \
993 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(+, __plus)
994 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(-, __minus)
995 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(*, __multiplies)
996 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(/, __divides)
997 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(%, __modulus)
998 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(^, __bitwise_xor)
999 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(&, __bitwise_and)
1000 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(|, __bitwise_or)
1001 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(<<, __shift_left)
1002 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(>>, __shift_right)
1004 #undef _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT
1006 #define _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(_Op, _Name) \
1007 template<class _Tp> template<class _Dom> \
1008 inline valarray<_Tp>& \
1009 valarray<_Tp>::operator _Op##=(const _Expr<_Dom, _Tp>& __e) \
1011 _Array_augmented_##_Name(_Array<_Tp>(_M_data), __e, _M_size); \
1015 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(+, __plus)
1016 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(-, __minus)
1017 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(*, __multiplies)
1018 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(/, __divides)
1019 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(%, __modulus)
1020 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(^, __bitwise_xor)
1021 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(&, __bitwise_and)
1022 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(|, __bitwise_or)
1023 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(<<, __shift_left)
1024 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(>>, __shift_right)
1026 #undef _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT
1029 #define _DEFINE_BINARY_OPERATOR(_Op, _Name) \
1030 template<typename _Tp> \
1031 inline _Expr<_BinClos<_Name, _ValArray, _ValArray, _Tp, _Tp>, \
1032 typename __fun<_Name, _Tp>::result_type> \
1033 operator _Op(const valarray<_Tp>& __v, const valarray<_Tp>& __w) \
1035 _GLIBCXX_DEBUG_ASSERT(__v.size() == __w.size()); \
1036 typedef _BinClos<_Name, _ValArray, _ValArray, _Tp, _Tp> _Closure; \
1037 typedef typename __fun<_Name, _Tp>::result_type _Rt; \
1038 return _Expr<_Closure, _Rt>(_Closure(__v, __w)); \
1041 template<typename _Tp> \
1042 inline _Expr<_BinClos<_Name, _ValArray,_Constant, _Tp, _Tp>, \
1043 typename __fun<_Name, _Tp>::result_type> \
1044 operator _Op(const valarray<_Tp>& __v, const _Tp& __t) \
1046 typedef _BinClos<_Name, _ValArray, _Constant, _Tp, _Tp> _Closure; \
1047 typedef typename __fun<_Name, _Tp>::result_type _Rt; \
1048 return _Expr<_Closure, _Rt>(_Closure(__v, __t)); \
1051 template<typename _Tp> \
1052 inline _Expr<_BinClos<_Name, _Constant, _ValArray, _Tp, _Tp>, \
1053 typename __fun<_Name, _Tp>::result_type> \
1054 operator _Op(const _Tp& __t, const valarray<_Tp>& __v) \
1056 typedef _BinClos<_Name, _Constant, _ValArray, _Tp, _Tp> _Closure; \
1057 typedef typename __fun<_Name, _Tp>::result_type _Rt; \
1058 return _Expr<_Closure, _Rt>(_Closure(__t, __v)); \
1061 _DEFINE_BINARY_OPERATOR(+, __plus)
1062 _DEFINE_BINARY_OPERATOR(-, __minus)
1063 _DEFINE_BINARY_OPERATOR(*, __multiplies)
1064 _DEFINE_BINARY_OPERATOR(/, __divides)
1065 _DEFINE_BINARY_OPERATOR(%, __modulus)
1066 _DEFINE_BINARY_OPERATOR(^, __bitwise_xor)
1067 _DEFINE_BINARY_OPERATOR(&, __bitwise_and)
1068 _DEFINE_BINARY_OPERATOR(|, __bitwise_or)
1069 _DEFINE_BINARY_OPERATOR(<<, __shift_left)
1070 _DEFINE_BINARY_OPERATOR(>>, __shift_right)
1071 _DEFINE_BINARY_OPERATOR(&&, __logical_and)
1072 _DEFINE_BINARY_OPERATOR(||, __logical_or)
1073 _DEFINE_BINARY_OPERATOR(==, __equal_to)
1074 _DEFINE_BINARY_OPERATOR(!=, __not_equal_to)
1075 _DEFINE_BINARY_OPERATOR(<, __less)
1076 _DEFINE_BINARY_OPERATOR(>, __greater)
1077 _DEFINE_BINARY_OPERATOR(<=, __less_equal)
1078 _DEFINE_BINARY_OPERATOR(>=, __greater_equal)
1080 #undef _DEFINE_BINARY_OPERATOR
1082 // @} group numeric_arrays
1084 _GLIBCXX_END_NAMESPACE
1086 #endif /* _GLIBCXX_VALARRAY */