1 // The template and inlines for the -*- C++ -*- valarray class.
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007, 2008, 2009, 2010
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/>.
27 /** @file include/valarray
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>
41 #include <debug/debug.h>
42 #include <initializer_list>
44 namespace std _GLIBCXX_VISIBILITY(default)
46 _GLIBCXX_BEGIN_NAMESPACE_VERSION
48 template<class _Clos, typename _Tp>
51 template<typename _Tp1, typename _Tp2>
54 template<class _Oper, template<class, class> class _Meta, class _Dom>
58 template<class, class> class _Meta1,
59 template<class, class> class _Meta2,
60 class _Dom1, class _Dom2>
63 template<template<class, class> class _Meta, class _Dom>
66 template<template<class, class> class _Meta, class _Dom>
69 template<template<class, class> class _Meta, class _Dom>
72 template<template<class, class> class _Meta, class _Dom>
75 template<template<class, class> class _Meta, class _Dom>
78 template<class _Tp> class valarray; // An array of type _Tp
79 class slice; // BLAS-like slice out of an array
80 template<class _Tp> class slice_array;
81 class gslice; // generalized slice out of an array
82 template<class _Tp> class gslice_array;
83 template<class _Tp> class mask_array; // masked array
84 template<class _Tp> class indirect_array; // indirected array
86 _GLIBCXX_END_NAMESPACE_VERSION
89 #include <bits/valarray_array.h>
90 #include <bits/valarray_before.h>
92 namespace std _GLIBCXX_VISIBILITY(default)
94 _GLIBCXX_BEGIN_NAMESPACE_VERSION
97 * @defgroup numeric_arrays Numeric Arrays
100 * Classes and functions for representing and manipulating arrays of elements.
105 * @brief Smart array designed to support numeric processing.
107 * A valarray is an array that provides constraints intended to allow for
108 * effective optimization of numeric array processing by reducing the
109 * aliasing that can result from pointer representations. It represents a
110 * one-dimensional array from which different multidimensional subsets can
111 * be accessed and modified.
113 * @param Tp Type of object in the array.
121 typedef typename __fun<_Op, _Tp>::result_type __rt;
122 typedef _Expr<_UnClos<_Op, _ValArray, _Tp>, __rt> _Rt;
125 typedef _Tp value_type;
127 // _lib.valarray.cons_ construct/destroy:
128 /// Construct an empty array.
131 /// Construct an array with @a n elements.
132 explicit valarray(size_t);
134 /// Construct an array with @a n elements initialized to @a t.
135 valarray(const _Tp&, size_t);
137 /// Construct an array initialized to the first @a n elements of @a t.
138 valarray(const _Tp* __restrict__, size_t);
140 /// Copy constructor.
141 valarray(const valarray&);
143 /// Construct an array with the same size and values in @a sa.
144 valarray(const slice_array<_Tp>&);
146 /// Construct an array with the same size and values in @a ga.
147 valarray(const gslice_array<_Tp>&);
149 /// Construct an array with the same size and values in @a ma.
150 valarray(const mask_array<_Tp>&);
152 /// Construct an array with the same size and values in @a ia.
153 valarray(const indirect_array<_Tp>&);
155 #ifdef __GXX_EXPERIMENTAL_CXX0X__
156 /// Construct an array with an initializer_list of values.
157 valarray(initializer_list<_Tp>);
161 valarray(const _Expr<_Dom, _Tp>& __e);
165 // _lib.valarray.assign_ assignment:
167 * @brief Assign elements to an array.
169 * Assign elements of array to values in @a v. Results are undefined
170 * if @a v does not have the same size as this array.
172 * @param v Valarray to get values from.
174 valarray<_Tp>& operator=(const valarray<_Tp>&);
177 * @brief Assign elements to a value.
179 * Assign all elements of array to @a t.
181 * @param t Value for elements.
183 valarray<_Tp>& operator=(const _Tp&);
186 * @brief Assign elements to an array subset.
188 * Assign elements of array to values in @a sa. Results are undefined
189 * if @a sa does not have the same size as this array.
191 * @param sa Array slice to get values from.
193 valarray<_Tp>& operator=(const slice_array<_Tp>&);
196 * @brief Assign elements to an array subset.
198 * Assign elements of array to values in @a ga. Results are undefined
199 * if @a ga does not have the same size as this array.
201 * @param ga Array slice to get values from.
203 valarray<_Tp>& operator=(const gslice_array<_Tp>&);
206 * @brief Assign elements to an array subset.
208 * Assign elements of array to values in @a ma. Results are undefined
209 * if @a ma does not have the same size as this array.
211 * @param ma Array slice to get values from.
213 valarray<_Tp>& operator=(const mask_array<_Tp>&);
216 * @brief Assign elements to an array subset.
218 * Assign elements of array to values in @a ia. Results are undefined
219 * if @a ia does not have the same size as this array.
221 * @param ia Array slice to get values from.
223 valarray<_Tp>& operator=(const indirect_array<_Tp>&);
225 #ifdef __GXX_EXPERIMENTAL_CXX0X__
227 * @brief Assign elements to an initializer_list.
229 * Assign elements of array to values in @a l. Results are undefined
230 * if @a l does not have the same size as this array.
232 * @param l initializer_list to get values from.
234 valarray& operator=(initializer_list<_Tp>);
237 template<class _Dom> valarray<_Tp>&
238 operator= (const _Expr<_Dom, _Tp>&);
240 // _lib.valarray.access_ element access:
242 * Return a reference to the i'th array element.
244 * @param i Index of element to return.
245 * @return Reference to the i'th element.
247 _Tp& operator[](size_t);
249 // _GLIBCXX_RESOLVE_LIB_DEFECTS
250 // 389. Const overload of valarray::operator[] returns by value.
251 const _Tp& operator[](size_t) const;
253 // _lib.valarray.sub_ subset operations:
255 * @brief Return an array subset.
257 * Returns a new valarray containing the elements of the array
258 * indicated by the slice argument. The new valarray has the same size
259 * as the input slice. @see slice.
261 * @param s The source slice.
262 * @return New valarray containing elements in @a s.
264 _Expr<_SClos<_ValArray, _Tp>, _Tp> operator[](slice) const;
267 * @brief Return a reference to an array subset.
269 * Returns a new valarray containing the elements of the array
270 * indicated by the slice argument. The new valarray has the same size
271 * as the input slice. @see slice.
273 * @param s The source slice.
274 * @return New valarray containing elements in @a s.
276 slice_array<_Tp> operator[](slice);
279 * @brief Return an array subset.
281 * Returns a slice_array referencing the elements of the array
282 * indicated by the slice argument. @see gslice.
284 * @param s The source slice.
285 * @return Slice_array referencing elements indicated by @a s.
287 _Expr<_GClos<_ValArray, _Tp>, _Tp> operator[](const gslice&) const;
290 * @brief Return a reference to an array subset.
292 * Returns a new valarray containing the elements of the array
293 * indicated by the gslice argument. The new valarray has
294 * the same size as the input gslice. @see gslice.
296 * @param s The source gslice.
297 * @return New valarray containing elements in @a s.
299 gslice_array<_Tp> operator[](const gslice&);
302 * @brief Return an array subset.
304 * Returns a new valarray containing the elements of the array
305 * indicated by the argument. The input is a valarray of bool which
306 * represents a bitmask indicating which elements should be copied into
307 * the new valarray. Each element of the array is added to the return
308 * valarray if the corresponding element of the argument is true.
310 * @param m The valarray bitmask.
311 * @return New valarray containing elements indicated by @a m.
313 valarray<_Tp> operator[](const valarray<bool>&) const;
316 * @brief Return a reference to an array subset.
318 * Returns a new mask_array referencing the elements of the array
319 * indicated by the argument. The input is a valarray of bool which
320 * represents a bitmask indicating which elements are part of the
321 * subset. Elements of the array are part of the subset if the
322 * corresponding element of the argument is true.
324 * @param m The valarray bitmask.
325 * @return New valarray containing elements indicated by @a m.
327 mask_array<_Tp> operator[](const valarray<bool>&);
330 * @brief Return an array subset.
332 * Returns a new valarray containing the elements of the array
333 * indicated by the argument. The elements in the argument are
334 * interpreted as the indices of elements of this valarray to copy to
335 * the return valarray.
337 * @param i The valarray element index list.
338 * @return New valarray containing elements in @a s.
340 _Expr<_IClos<_ValArray, _Tp>, _Tp>
341 operator[](const valarray<size_t>&) const;
344 * @brief Return a reference to an array subset.
346 * Returns an indirect_array referencing the elements of the array
347 * indicated by the argument. The elements in the argument are
348 * interpreted as the indices of elements of this valarray to include
349 * in the subset. The returned indirect_array refers to these
352 * @param i The valarray element index list.
353 * @return Indirect_array referencing elements in @a i.
355 indirect_array<_Tp> operator[](const valarray<size_t>&);
357 // _lib.valarray.unary_ unary operators:
358 /// Return a new valarray by applying unary + to each element.
359 typename _UnaryOp<__unary_plus>::_Rt operator+() const;
361 /// Return a new valarray by applying unary - to each element.
362 typename _UnaryOp<__negate>::_Rt operator-() const;
364 /// Return a new valarray by applying unary ~ to each element.
365 typename _UnaryOp<__bitwise_not>::_Rt operator~() const;
367 /// Return a new valarray by applying unary ! to each element.
368 typename _UnaryOp<__logical_not>::_Rt operator!() const;
370 // _lib.valarray.cassign_ computed assignment:
371 /// Multiply each element of array by @a t.
372 valarray<_Tp>& operator*=(const _Tp&);
374 /// Divide each element of array by @a t.
375 valarray<_Tp>& operator/=(const _Tp&);
377 /// Set each element e of array to e % @a t.
378 valarray<_Tp>& operator%=(const _Tp&);
380 /// Add @a t to each element of array.
381 valarray<_Tp>& operator+=(const _Tp&);
383 /// Subtract @a t to each element of array.
384 valarray<_Tp>& operator-=(const _Tp&);
386 /// Set each element e of array to e ^ @a t.
387 valarray<_Tp>& operator^=(const _Tp&);
389 /// Set each element e of array to e & @a t.
390 valarray<_Tp>& operator&=(const _Tp&);
392 /// Set each element e of array to e | @a t.
393 valarray<_Tp>& operator|=(const _Tp&);
395 /// Left shift each element e of array by @a t bits.
396 valarray<_Tp>& operator<<=(const _Tp&);
398 /// Right shift each element e of array by @a t bits.
399 valarray<_Tp>& operator>>=(const _Tp&);
401 /// Multiply elements of array by corresponding elements of @a v.
402 valarray<_Tp>& operator*=(const valarray<_Tp>&);
404 /// Divide elements of array by corresponding elements of @a v.
405 valarray<_Tp>& operator/=(const valarray<_Tp>&);
407 /// Modulo elements of array by corresponding elements of @a v.
408 valarray<_Tp>& operator%=(const valarray<_Tp>&);
410 /// Add corresponding elements of @a v to elements of array.
411 valarray<_Tp>& operator+=(const valarray<_Tp>&);
413 /// Subtract corresponding elements of @a v from elements of array.
414 valarray<_Tp>& operator-=(const valarray<_Tp>&);
416 /// Logical xor corresponding elements of @a v with elements of array.
417 valarray<_Tp>& operator^=(const valarray<_Tp>&);
419 /// Logical or corresponding elements of @a v with elements of array.
420 valarray<_Tp>& operator|=(const valarray<_Tp>&);
422 /// Logical and corresponding elements of @a v with elements of array.
423 valarray<_Tp>& operator&=(const valarray<_Tp>&);
425 /// Left shift elements of array by corresponding elements of @a v.
426 valarray<_Tp>& operator<<=(const valarray<_Tp>&);
428 /// Right shift elements of array by corresponding elements of @a v.
429 valarray<_Tp>& operator>>=(const valarray<_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 valarray<_Tp>& operator<<=(const _Expr<_Dom, _Tp>&);
450 valarray<_Tp>& operator>>=(const _Expr<_Dom, _Tp>&);
452 // _lib.valarray.members_ member functions:
453 /// Return the number of elements in array.
457 * @brief Return the sum of all elements in the array.
459 * Accumulates the sum of all elements into a Tp using +=. The order
460 * of adding the elements is unspecified.
464 /// Return the minimum element using operator<().
467 /// Return the maximum element using operator<().
471 * @brief Return a shifted array.
473 * A new valarray is constructed as a copy of this array with elements
474 * in shifted positions. For an element with index i, the new position
475 * is i - n. The new valarray has the same size as the current one.
476 * New elements without a value are set to 0. Elements whose new
477 * position is outside the bounds of the array are discarded.
479 * Positive arguments shift toward index 0, discarding elements [0, n).
480 * Negative arguments discard elements from the top of the array.
482 * @param n Number of element positions to shift.
483 * @return New valarray with elements in shifted positions.
485 valarray<_Tp> shift (int) const;
488 * @brief Return a rotated array.
490 * A new valarray is constructed as a copy of this array with elements
491 * in shifted positions. For an element with index i, the new position
492 * is (i - n) % size(). The new valarray has the same size as the
493 * current one. Elements that are shifted beyond the array bounds are
494 * shifted into the other end of the array. No elements are lost.
496 * Positive arguments shift toward index 0, wrapping around the top.
497 * Negative arguments shift towards the top, wrapping around to 0.
499 * @param n Number of element positions to rotate.
500 * @return New valarray with elements in shifted positions.
502 valarray<_Tp> cshift(int) const;
505 * @brief Apply a function to the array.
507 * Returns a new valarray with elements assigned to the result of
508 * applying func to the corresponding element of this array. The new
509 * array has the same size as this one.
511 * @param func Function of Tp returning Tp to apply.
512 * @return New valarray with transformed elements.
514 _Expr<_ValFunClos<_ValArray, _Tp>, _Tp> apply(_Tp func(_Tp)) const;
517 * @brief Apply a function to the array.
519 * Returns a new valarray with elements assigned to the result of
520 * applying func to the corresponding element of this array. The new
521 * array has the same size as this one.
523 * @param func Function of const Tp& returning Tp to apply.
524 * @return New valarray with transformed elements.
526 _Expr<_RefFunClos<_ValArray, _Tp>, _Tp> apply(_Tp func(const _Tp&)) const;
529 * @brief Resize array.
531 * Resize this array to @a size and set all elements to @a c. All
532 * references and iterators are invalidated.
534 * @param size New array size.
535 * @param c New value for all elements.
537 void resize(size_t __size, _Tp __c = _Tp());
541 _Tp* __restrict__ _M_data;
543 friend class _Array<_Tp>;
546 template<typename _Tp>
548 valarray<_Tp>::operator[](size_t __i) const
550 __glibcxx_requires_subscript(__i);
554 template<typename _Tp>
556 valarray<_Tp>::operator[](size_t __i)
558 __glibcxx_requires_subscript(__i);
562 // @} group numeric_arrays
564 _GLIBCXX_END_NAMESPACE_VERSION
567 #include <bits/valarray_after.h>
568 #include <bits/slice_array.h>
569 #include <bits/gslice.h>
570 #include <bits/gslice_array.h>
571 #include <bits/mask_array.h>
572 #include <bits/indirect_array.h>
574 namespace std _GLIBCXX_VISIBILITY(default)
576 _GLIBCXX_BEGIN_NAMESPACE_VERSION
579 * @addtogroup numeric_arrays
583 template<typename _Tp>
585 valarray<_Tp>::valarray() : _M_size(0), _M_data(0) {}
587 template<typename _Tp>
589 valarray<_Tp>::valarray(size_t __n)
590 : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n))
591 { std::__valarray_default_construct(_M_data, _M_data + __n); }
593 template<typename _Tp>
595 valarray<_Tp>::valarray(const _Tp& __t, size_t __n)
596 : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n))
597 { std::__valarray_fill_construct(_M_data, _M_data + __n, __t); }
599 template<typename _Tp>
601 valarray<_Tp>::valarray(const _Tp* __restrict__ __p, size_t __n)
602 : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n))
604 _GLIBCXX_DEBUG_ASSERT(__p != 0 || __n == 0);
605 std::__valarray_copy_construct(__p, __p + __n, _M_data);
608 template<typename _Tp>
610 valarray<_Tp>::valarray(const valarray<_Tp>& __v)
611 : _M_size(__v._M_size), _M_data(__valarray_get_storage<_Tp>(__v._M_size))
612 { std::__valarray_copy_construct(__v._M_data, __v._M_data + _M_size,
615 template<typename _Tp>
617 valarray<_Tp>::valarray(const slice_array<_Tp>& __sa)
618 : _M_size(__sa._M_sz), _M_data(__valarray_get_storage<_Tp>(__sa._M_sz))
620 std::__valarray_copy_construct
621 (__sa._M_array, __sa._M_sz, __sa._M_stride, _Array<_Tp>(_M_data));
624 template<typename _Tp>
626 valarray<_Tp>::valarray(const gslice_array<_Tp>& __ga)
627 : _M_size(__ga._M_index.size()),
628 _M_data(__valarray_get_storage<_Tp>(_M_size))
630 std::__valarray_copy_construct
631 (__ga._M_array, _Array<size_t>(__ga._M_index),
632 _Array<_Tp>(_M_data), _M_size);
635 template<typename _Tp>
637 valarray<_Tp>::valarray(const mask_array<_Tp>& __ma)
638 : _M_size(__ma._M_sz), _M_data(__valarray_get_storage<_Tp>(__ma._M_sz))
640 std::__valarray_copy_construct
641 (__ma._M_array, __ma._M_mask, _Array<_Tp>(_M_data), _M_size);
644 template<typename _Tp>
646 valarray<_Tp>::valarray(const indirect_array<_Tp>& __ia)
647 : _M_size(__ia._M_sz), _M_data(__valarray_get_storage<_Tp>(__ia._M_sz))
649 std::__valarray_copy_construct
650 (__ia._M_array, __ia._M_index, _Array<_Tp>(_M_data), _M_size);
653 #ifdef __GXX_EXPERIMENTAL_CXX0X__
654 template<typename _Tp>
656 valarray<_Tp>::valarray(initializer_list<_Tp> __l)
657 : _M_size(__l.size()), _M_data(__valarray_get_storage<_Tp>(__l.size()))
658 { std::__valarray_copy_construct (__l.begin(), __l.end(), _M_data); }
661 template<typename _Tp> template<class _Dom>
663 valarray<_Tp>::valarray(const _Expr<_Dom, _Tp>& __e)
664 : _M_size(__e.size()), _M_data(__valarray_get_storage<_Tp>(_M_size))
665 { std::__valarray_copy_construct(__e, _M_size, _Array<_Tp>(_M_data)); }
667 template<typename _Tp>
669 valarray<_Tp>::~valarray()
671 std::__valarray_destroy_elements(_M_data, _M_data + _M_size);
672 std::__valarray_release_memory(_M_data);
675 template<typename _Tp>
676 inline valarray<_Tp>&
677 valarray<_Tp>::operator=(const valarray<_Tp>& __v)
679 // _GLIBCXX_RESOLVE_LIB_DEFECTS
680 // 630. arrays of valarray.
681 if (_M_size == __v._M_size)
682 std::__valarray_copy(__v._M_data, _M_size, _M_data);
687 std::__valarray_destroy_elements(_M_data, _M_data + _M_size);
688 std::__valarray_release_memory(_M_data);
690 _M_size = __v._M_size;
691 _M_data = __valarray_get_storage<_Tp>(_M_size);
692 std::__valarray_copy_construct(__v._M_data, __v._M_data + _M_size,
698 #ifdef __GXX_EXPERIMENTAL_CXX0X__
699 template<typename _Tp>
700 inline valarray<_Tp>&
701 valarray<_Tp>::operator=(initializer_list<_Tp> __l)
703 // _GLIBCXX_RESOLVE_LIB_DEFECTS
704 // 630. arrays of valarray.
705 if (_M_size == __l.size())
706 std::__valarray_copy(__l.begin(), __l.size(), _M_data);
711 std::__valarray_destroy_elements(_M_data, _M_data + _M_size);
712 std::__valarray_release_memory(_M_data);
714 _M_size = __l.size();
715 _M_data = __valarray_get_storage<_Tp>(_M_size);
716 std::__valarray_copy_construct(__l.begin(), __l.begin() + _M_size,
723 template<typename _Tp>
724 inline valarray<_Tp>&
725 valarray<_Tp>::operator=(const _Tp& __t)
727 std::__valarray_fill(_M_data, _M_size, __t);
731 template<typename _Tp>
732 inline valarray<_Tp>&
733 valarray<_Tp>::operator=(const slice_array<_Tp>& __sa)
735 _GLIBCXX_DEBUG_ASSERT(_M_size == __sa._M_sz);
736 std::__valarray_copy(__sa._M_array, __sa._M_sz,
737 __sa._M_stride, _Array<_Tp>(_M_data));
741 template<typename _Tp>
742 inline valarray<_Tp>&
743 valarray<_Tp>::operator=(const gslice_array<_Tp>& __ga)
745 _GLIBCXX_DEBUG_ASSERT(_M_size == __ga._M_index.size());
746 std::__valarray_copy(__ga._M_array, _Array<size_t>(__ga._M_index),
747 _Array<_Tp>(_M_data), _M_size);
751 template<typename _Tp>
752 inline valarray<_Tp>&
753 valarray<_Tp>::operator=(const mask_array<_Tp>& __ma)
755 _GLIBCXX_DEBUG_ASSERT(_M_size == __ma._M_sz);
756 std::__valarray_copy(__ma._M_array, __ma._M_mask,
757 _Array<_Tp>(_M_data), _M_size);
761 template<typename _Tp>
762 inline valarray<_Tp>&
763 valarray<_Tp>::operator=(const indirect_array<_Tp>& __ia)
765 _GLIBCXX_DEBUG_ASSERT(_M_size == __ia._M_sz);
766 std::__valarray_copy(__ia._M_array, __ia._M_index,
767 _Array<_Tp>(_M_data), _M_size);
771 template<typename _Tp> template<class _Dom>
772 inline valarray<_Tp>&
773 valarray<_Tp>::operator=(const _Expr<_Dom, _Tp>& __e)
775 _GLIBCXX_DEBUG_ASSERT(_M_size == __e.size());
776 std::__valarray_copy(__e, _M_size, _Array<_Tp>(_M_data));
780 template<typename _Tp>
781 inline _Expr<_SClos<_ValArray,_Tp>, _Tp>
782 valarray<_Tp>::operator[](slice __s) const
784 typedef _SClos<_ValArray,_Tp> _Closure;
785 return _Expr<_Closure, _Tp>(_Closure (_Array<_Tp>(_M_data), __s));
788 template<typename _Tp>
789 inline slice_array<_Tp>
790 valarray<_Tp>::operator[](slice __s)
791 { return slice_array<_Tp>(_Array<_Tp>(_M_data), __s); }
793 template<typename _Tp>
794 inline _Expr<_GClos<_ValArray,_Tp>, _Tp>
795 valarray<_Tp>::operator[](const gslice& __gs) const
797 typedef _GClos<_ValArray,_Tp> _Closure;
798 return _Expr<_Closure, _Tp>
799 (_Closure(_Array<_Tp>(_M_data), __gs._M_index->_M_index));
802 template<typename _Tp>
803 inline gslice_array<_Tp>
804 valarray<_Tp>::operator[](const gslice& __gs)
806 return gslice_array<_Tp>
807 (_Array<_Tp>(_M_data), __gs._M_index->_M_index);
810 template<typename _Tp>
812 valarray<_Tp>::operator[](const valarray<bool>& __m) const
815 size_t __e = __m.size();
816 for (size_t __i=0; __i<__e; ++__i)
818 return valarray<_Tp>(mask_array<_Tp>(_Array<_Tp>(_M_data), __s,
819 _Array<bool> (__m)));
822 template<typename _Tp>
823 inline mask_array<_Tp>
824 valarray<_Tp>::operator[](const valarray<bool>& __m)
827 size_t __e = __m.size();
828 for (size_t __i=0; __i<__e; ++__i)
830 return mask_array<_Tp>(_Array<_Tp>(_M_data), __s, _Array<bool>(__m));
833 template<typename _Tp>
834 inline _Expr<_IClos<_ValArray,_Tp>, _Tp>
835 valarray<_Tp>::operator[](const valarray<size_t>& __i) const
837 typedef _IClos<_ValArray,_Tp> _Closure;
838 return _Expr<_Closure, _Tp>(_Closure(*this, __i));
841 template<typename _Tp>
842 inline indirect_array<_Tp>
843 valarray<_Tp>::operator[](const valarray<size_t>& __i)
845 return indirect_array<_Tp>(_Array<_Tp>(_M_data), __i.size(),
846 _Array<size_t>(__i));
851 valarray<_Tp>::size() const
856 valarray<_Tp>::sum() const
858 _GLIBCXX_DEBUG_ASSERT(_M_size > 0);
859 return std::__valarray_sum(_M_data, _M_data + _M_size);
864 valarray<_Tp>::shift(int __n) const
871 _Tp* __restrict__ __tmp_M_data =
872 std::__valarray_get_storage<_Tp>(_M_size);
875 std::__valarray_copy_construct(_M_data,
876 _M_data + _M_size, __tmp_M_data);
877 else if (__n > 0) // shift left
879 if (size_t(__n) > _M_size)
882 std::__valarray_copy_construct(_M_data + __n,
883 _M_data + _M_size, __tmp_M_data);
884 std::__valarray_default_construct(__tmp_M_data + _M_size - __n,
885 __tmp_M_data + _M_size);
889 if (-size_t(__n) > _M_size)
892 std::__valarray_copy_construct(_M_data, _M_data + _M_size + __n,
894 std::__valarray_default_construct(__tmp_M_data,
898 __ret._M_size = _M_size;
899 __ret._M_data = __tmp_M_data;
905 valarray<_Tp>::cshift(int __n) const
912 _Tp* __restrict__ __tmp_M_data =
913 std::__valarray_get_storage<_Tp>(_M_size);
916 std::__valarray_copy_construct(_M_data,
917 _M_data + _M_size, __tmp_M_data);
918 else if (__n > 0) // cshift left
920 if (size_t(__n) > _M_size)
921 __n = int(__n % _M_size);
923 std::__valarray_copy_construct(_M_data, _M_data + __n,
924 __tmp_M_data + _M_size - __n);
925 std::__valarray_copy_construct(_M_data + __n, _M_data + _M_size,
930 if (-size_t(__n) > _M_size)
931 __n = -int(-size_t(__n) % _M_size);
933 std::__valarray_copy_construct(_M_data + _M_size + __n,
934 _M_data + _M_size, __tmp_M_data);
935 std::__valarray_copy_construct(_M_data, _M_data + _M_size + __n,
939 __ret._M_size = _M_size;
940 __ret._M_data = __tmp_M_data;
946 valarray<_Tp>::resize(size_t __n, _Tp __c)
948 // This complication is so to make valarray<valarray<T> > work
949 // even though it is not required by the standard. Nobody should
950 // be saying valarray<valarray<T> > anyway. See the specs.
951 std::__valarray_destroy_elements(_M_data, _M_data + _M_size);
954 std::__valarray_release_memory(_M_data);
956 _M_data = __valarray_get_storage<_Tp>(__n);
958 std::__valarray_fill_construct(_M_data, _M_data + __n, __c);
961 template<typename _Tp>
963 valarray<_Tp>::min() const
965 _GLIBCXX_DEBUG_ASSERT(_M_size > 0);
966 return *std::min_element(_M_data, _M_data + _M_size);
969 template<typename _Tp>
971 valarray<_Tp>::max() const
973 _GLIBCXX_DEBUG_ASSERT(_M_size > 0);
974 return *std::max_element(_M_data, _M_data + _M_size);
978 inline _Expr<_ValFunClos<_ValArray, _Tp>, _Tp>
979 valarray<_Tp>::apply(_Tp func(_Tp)) const
981 typedef _ValFunClos<_ValArray, _Tp> _Closure;
982 return _Expr<_Closure, _Tp>(_Closure(*this, func));
986 inline _Expr<_RefFunClos<_ValArray, _Tp>, _Tp>
987 valarray<_Tp>::apply(_Tp func(const _Tp &)) const
989 typedef _RefFunClos<_ValArray, _Tp> _Closure;
990 return _Expr<_Closure, _Tp>(_Closure(*this, func));
993 #define _DEFINE_VALARRAY_UNARY_OPERATOR(_Op, _Name) \
994 template<typename _Tp> \
995 inline typename valarray<_Tp>::template _UnaryOp<_Name>::_Rt \
996 valarray<_Tp>::operator _Op() const \
998 typedef _UnClos<_Name, _ValArray, _Tp> _Closure; \
999 typedef typename __fun<_Name, _Tp>::result_type _Rt; \
1000 return _Expr<_Closure, _Rt>(_Closure(*this)); \
1003 _DEFINE_VALARRAY_UNARY_OPERATOR(+, __unary_plus)
1004 _DEFINE_VALARRAY_UNARY_OPERATOR(-, __negate)
1005 _DEFINE_VALARRAY_UNARY_OPERATOR(~, __bitwise_not)
1006 _DEFINE_VALARRAY_UNARY_OPERATOR (!, __logical_not)
1008 #undef _DEFINE_VALARRAY_UNARY_OPERATOR
1010 #define _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(_Op, _Name) \
1011 template<class _Tp> \
1012 inline valarray<_Tp>& \
1013 valarray<_Tp>::operator _Op##=(const _Tp &__t) \
1015 _Array_augmented_##_Name(_Array<_Tp>(_M_data), _M_size, __t); \
1019 template<class _Tp> \
1020 inline valarray<_Tp>& \
1021 valarray<_Tp>::operator _Op##=(const valarray<_Tp> &__v) \
1023 _GLIBCXX_DEBUG_ASSERT(_M_size == __v._M_size); \
1024 _Array_augmented_##_Name(_Array<_Tp>(_M_data), _M_size, \
1025 _Array<_Tp>(__v._M_data)); \
1029 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(+, __plus)
1030 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(-, __minus)
1031 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(*, __multiplies)
1032 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(/, __divides)
1033 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(%, __modulus)
1034 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(^, __bitwise_xor)
1035 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(&, __bitwise_and)
1036 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(|, __bitwise_or)
1037 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(<<, __shift_left)
1038 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(>>, __shift_right)
1040 #undef _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT
1042 #define _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(_Op, _Name) \
1043 template<class _Tp> template<class _Dom> \
1044 inline valarray<_Tp>& \
1045 valarray<_Tp>::operator _Op##=(const _Expr<_Dom, _Tp>& __e) \
1047 _Array_augmented_##_Name(_Array<_Tp>(_M_data), __e, _M_size); \
1051 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(+, __plus)
1052 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(-, __minus)
1053 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(*, __multiplies)
1054 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(/, __divides)
1055 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(%, __modulus)
1056 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(^, __bitwise_xor)
1057 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(&, __bitwise_and)
1058 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(|, __bitwise_or)
1059 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(<<, __shift_left)
1060 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(>>, __shift_right)
1062 #undef _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT
1065 #define _DEFINE_BINARY_OPERATOR(_Op, _Name) \
1066 template<typename _Tp> \
1067 inline _Expr<_BinClos<_Name, _ValArray, _ValArray, _Tp, _Tp>, \
1068 typename __fun<_Name, _Tp>::result_type> \
1069 operator _Op(const valarray<_Tp>& __v, const valarray<_Tp>& __w) \
1071 _GLIBCXX_DEBUG_ASSERT(__v.size() == __w.size()); \
1072 typedef _BinClos<_Name, _ValArray, _ValArray, _Tp, _Tp> _Closure; \
1073 typedef typename __fun<_Name, _Tp>::result_type _Rt; \
1074 return _Expr<_Closure, _Rt>(_Closure(__v, __w)); \
1077 template<typename _Tp> \
1078 inline _Expr<_BinClos<_Name, _ValArray,_Constant, _Tp, _Tp>, \
1079 typename __fun<_Name, _Tp>::result_type> \
1080 operator _Op(const valarray<_Tp>& __v, const _Tp& __t) \
1082 typedef _BinClos<_Name, _ValArray, _Constant, _Tp, _Tp> _Closure; \
1083 typedef typename __fun<_Name, _Tp>::result_type _Rt; \
1084 return _Expr<_Closure, _Rt>(_Closure(__v, __t)); \
1087 template<typename _Tp> \
1088 inline _Expr<_BinClos<_Name, _Constant, _ValArray, _Tp, _Tp>, \
1089 typename __fun<_Name, _Tp>::result_type> \
1090 operator _Op(const _Tp& __t, const valarray<_Tp>& __v) \
1092 typedef _BinClos<_Name, _Constant, _ValArray, _Tp, _Tp> _Closure; \
1093 typedef typename __fun<_Name, _Tp>::result_type _Rt; \
1094 return _Expr<_Closure, _Rt>(_Closure(__t, __v)); \
1097 _DEFINE_BINARY_OPERATOR(+, __plus)
1098 _DEFINE_BINARY_OPERATOR(-, __minus)
1099 _DEFINE_BINARY_OPERATOR(*, __multiplies)
1100 _DEFINE_BINARY_OPERATOR(/, __divides)
1101 _DEFINE_BINARY_OPERATOR(%, __modulus)
1102 _DEFINE_BINARY_OPERATOR(^, __bitwise_xor)
1103 _DEFINE_BINARY_OPERATOR(&, __bitwise_and)
1104 _DEFINE_BINARY_OPERATOR(|, __bitwise_or)
1105 _DEFINE_BINARY_OPERATOR(<<, __shift_left)
1106 _DEFINE_BINARY_OPERATOR(>>, __shift_right)
1107 _DEFINE_BINARY_OPERATOR(&&, __logical_and)
1108 _DEFINE_BINARY_OPERATOR(||, __logical_or)
1109 _DEFINE_BINARY_OPERATOR(==, __equal_to)
1110 _DEFINE_BINARY_OPERATOR(!=, __not_equal_to)
1111 _DEFINE_BINARY_OPERATOR(<, __less)
1112 _DEFINE_BINARY_OPERATOR(>, __greater)
1113 _DEFINE_BINARY_OPERATOR(<=, __less_equal)
1114 _DEFINE_BINARY_OPERATOR(>=, __greater_equal)
1116 #undef _DEFINE_BINARY_OPERATOR
1118 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1120 * @brief Return an iterator pointing to the first element of
1122 * @param va valarray.
1126 begin(valarray<_Tp>& __va)
1127 { return std::__addressof(__va[0]); }
1130 * @brief Return an iterator pointing to the first element of
1131 * the const valarray.
1132 * @param va valarray.
1136 begin(const valarray<_Tp>& __va)
1137 { return std::__addressof(__va[0]); }
1140 * @brief Return an iterator pointing to one past the last element of
1142 * @param va valarray.
1146 end(valarray<_Tp>& __va)
1147 { return std::__addressof(__va[0]) + __va.size(); }
1150 * @brief Return an iterator pointing to one past the last element of
1151 * the const valarray.
1152 * @param va valarray.
1156 end(const valarray<_Tp>& __va)
1157 { return std::__addressof(__va[0]) + __va.size(); }
1158 #endif // __GXX_EXPERIMENTAL_CXX0X__
1160 // @} group numeric_arrays
1162 _GLIBCXX_END_NAMESPACE_VERSION
1165 #endif /* _GLIBCXX_VALARRAY */