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/>.
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 _GLIBCXX_BEGIN_NAMESPACE(std)
46 template<class _Clos, typename _Tp>
49 template<typename _Tp1, typename _Tp2>
52 template<class _Oper, template<class, class> class _Meta, class _Dom>
56 template<class, class> class _Meta1,
57 template<class, class> class _Meta2,
58 class _Dom1, class _Dom2>
61 template<template<class, class> class _Meta, class _Dom>
64 template<template<class, class> class _Meta, class _Dom>
67 template<template<class, class> class _Meta, class _Dom>
70 template<template<class, class> class _Meta, class _Dom>
73 template<template<class, class> class _Meta, class _Dom>
76 template<class _Tp> class valarray; // An array of type _Tp
77 class slice; // BLAS-like slice out of an array
78 template<class _Tp> class slice_array;
79 class gslice; // generalized slice out of an array
80 template<class _Tp> class gslice_array;
81 template<class _Tp> class mask_array; // masked array
82 template<class _Tp> class indirect_array; // indirected array
84 _GLIBCXX_END_NAMESPACE
86 #include <bits/valarray_array.h>
87 #include <bits/valarray_before.h>
89 _GLIBCXX_BEGIN_NAMESPACE(std)
92 * @defgroup numeric_arrays Numeric Arrays
95 * Classes and functions for representing and manipulating arrays of elements.
100 * @brief Smart array designed to support numeric processing.
102 * A valarray is an array that provides constraints intended to allow for
103 * effective optimization of numeric array processing by reducing the
104 * aliasing that can result from pointer representations. It represents a
105 * one-dimensional array from which different multidimensional subsets can
106 * be accessed and modified.
108 * @param Tp Type of object in the array.
116 typedef typename __fun<_Op, _Tp>::result_type __rt;
117 typedef _Expr<_UnClos<_Op, _ValArray, _Tp>, __rt> _Rt;
120 typedef _Tp value_type;
122 // _lib.valarray.cons_ construct/destroy:
123 /// Construct an empty array.
126 /// Construct an array with @a n elements.
127 explicit valarray(size_t);
129 /// Construct an array with @a n elements initialized to @a t.
130 valarray(const _Tp&, size_t);
132 /// Construct an array initialized to the first @a n elements of @a t.
133 valarray(const _Tp* __restrict__, size_t);
135 /// Copy constructor.
136 valarray(const valarray&);
138 /// Construct an array with the same size and values in @a sa.
139 valarray(const slice_array<_Tp>&);
141 /// Construct an array with the same size and values in @a ga.
142 valarray(const gslice_array<_Tp>&);
144 /// Construct an array with the same size and values in @a ma.
145 valarray(const mask_array<_Tp>&);
147 /// Construct an array with the same size and values in @a ia.
148 valarray(const indirect_array<_Tp>&);
150 #ifdef __GXX_EXPERIMENTAL_CXX0X__
151 /// Construct an array with an initializer_list of values.
152 valarray(initializer_list<_Tp>);
156 valarray(const _Expr<_Dom, _Tp>& __e);
160 // _lib.valarray.assign_ assignment:
162 * @brief Assign elements to an array.
164 * Assign elements of array to values in @a v. Results are undefined
165 * if @a v does not have the same size as this array.
167 * @param v Valarray to get values from.
169 valarray<_Tp>& operator=(const valarray<_Tp>&);
172 * @brief Assign elements to a value.
174 * Assign all elements of array to @a t.
176 * @param t Value for elements.
178 valarray<_Tp>& operator=(const _Tp&);
181 * @brief Assign elements to an array subset.
183 * Assign elements of array to values in @a sa. Results are undefined
184 * if @a sa does not have the same size as this array.
186 * @param sa Array slice to get values from.
188 valarray<_Tp>& operator=(const slice_array<_Tp>&);
191 * @brief Assign elements to an array subset.
193 * Assign elements of array to values in @a ga. Results are undefined
194 * if @a ga does not have the same size as this array.
196 * @param ga Array slice to get values from.
198 valarray<_Tp>& operator=(const gslice_array<_Tp>&);
201 * @brief Assign elements to an array subset.
203 * Assign elements of array to values in @a ma. Results are undefined
204 * if @a ma does not have the same size as this array.
206 * @param ma Array slice to get values from.
208 valarray<_Tp>& operator=(const mask_array<_Tp>&);
211 * @brief Assign elements to an array subset.
213 * Assign elements of array to values in @a ia. Results are undefined
214 * if @a ia does not have the same size as this array.
216 * @param ia Array slice to get values from.
218 valarray<_Tp>& operator=(const indirect_array<_Tp>&);
220 #ifdef __GXX_EXPERIMENTAL_CXX0X__
222 * @brief Assign elements to an initializer_list.
224 * Assign elements of array to values in @a l. Results are undefined
225 * if @a l does not have the same size as this array.
227 * @param l initializer_list to get values from.
229 valarray& operator=(initializer_list<_Tp>);
232 template<class _Dom> valarray<_Tp>&
233 operator= (const _Expr<_Dom, _Tp>&);
235 // _lib.valarray.access_ element access:
237 * Return a reference to the i'th array element.
239 * @param i Index of element to return.
240 * @return Reference to the i'th element.
242 _Tp& operator[](size_t);
244 // _GLIBCXX_RESOLVE_LIB_DEFECTS
245 // 389. Const overload of valarray::operator[] returns by value.
246 const _Tp& operator[](size_t) const;
248 // _lib.valarray.sub_ subset operations:
250 * @brief Return an array subset.
252 * Returns a new valarray containing the elements of the array
253 * indicated by the slice argument. The new valarray has the same size
254 * as the input slice. @see slice.
256 * @param s The source slice.
257 * @return New valarray containing elements in @a s.
259 _Expr<_SClos<_ValArray, _Tp>, _Tp> operator[](slice) const;
262 * @brief Return a reference to an array subset.
264 * Returns a new valarray containing the elements of the array
265 * indicated by the slice argument. The new valarray has the same size
266 * as the input slice. @see slice.
268 * @param s The source slice.
269 * @return New valarray containing elements in @a s.
271 slice_array<_Tp> operator[](slice);
274 * @brief Return an array subset.
276 * Returns a slice_array referencing the elements of the array
277 * indicated by the slice argument. @see gslice.
279 * @param s The source slice.
280 * @return Slice_array referencing elements indicated by @a s.
282 _Expr<_GClos<_ValArray, _Tp>, _Tp> operator[](const gslice&) const;
285 * @brief Return a reference to an array subset.
287 * Returns a new valarray containing the elements of the array
288 * indicated by the gslice argument. The new valarray has
289 * the same size as the input gslice. @see gslice.
291 * @param s The source gslice.
292 * @return New valarray containing elements in @a s.
294 gslice_array<_Tp> operator[](const gslice&);
297 * @brief Return an array subset.
299 * Returns a new valarray containing the elements of the array
300 * indicated by the argument. The input is a valarray of bool which
301 * represents a bitmask indicating which elements should be copied into
302 * the new valarray. Each element of the array is added to the return
303 * valarray if the corresponding element of the argument is true.
305 * @param m The valarray bitmask.
306 * @return New valarray containing elements indicated by @a m.
308 valarray<_Tp> operator[](const valarray<bool>&) const;
311 * @brief Return a reference to an array subset.
313 * Returns a new mask_array referencing the elements of the array
314 * indicated by the argument. The input is a valarray of bool which
315 * represents a bitmask indicating which elements are part of the
316 * subset. Elements of the array are part of the subset if the
317 * corresponding element of the argument is true.
319 * @param m The valarray bitmask.
320 * @return New valarray containing elements indicated by @a m.
322 mask_array<_Tp> operator[](const valarray<bool>&);
325 * @brief Return an array subset.
327 * Returns a new valarray containing the elements of the array
328 * indicated by the argument. The elements in the argument are
329 * interpreted as the indices of elements of this valarray to copy to
330 * the return valarray.
332 * @param i The valarray element index list.
333 * @return New valarray containing elements in @a s.
335 _Expr<_IClos<_ValArray, _Tp>, _Tp>
336 operator[](const valarray<size_t>&) const;
339 * @brief Return a reference to an array subset.
341 * Returns an indirect_array referencing the elements of the array
342 * indicated by the argument. The elements in the argument are
343 * interpreted as the indices of elements of this valarray to include
344 * in the subset. The returned indirect_array refers to these
347 * @param i The valarray element index list.
348 * @return Indirect_array referencing elements in @a i.
350 indirect_array<_Tp> operator[](const valarray<size_t>&);
352 // _lib.valarray.unary_ unary operators:
353 /// Return a new valarray by applying unary + to each element.
354 typename _UnaryOp<__unary_plus>::_Rt operator+() const;
356 /// Return a new valarray by applying unary - to each element.
357 typename _UnaryOp<__negate>::_Rt operator-() const;
359 /// Return a new valarray by applying unary ~ to each element.
360 typename _UnaryOp<__bitwise_not>::_Rt operator~() const;
362 /// Return a new valarray by applying unary ! to each element.
363 typename _UnaryOp<__logical_not>::_Rt operator!() const;
365 // _lib.valarray.cassign_ computed assignment:
366 /// Multiply each element of array by @a t.
367 valarray<_Tp>& operator*=(const _Tp&);
369 /// Divide each element of array by @a t.
370 valarray<_Tp>& operator/=(const _Tp&);
372 /// Set each element e of array to e % @a t.
373 valarray<_Tp>& operator%=(const _Tp&);
375 /// Add @a t to each element of array.
376 valarray<_Tp>& operator+=(const _Tp&);
378 /// Subtract @a t to each element of array.
379 valarray<_Tp>& operator-=(const _Tp&);
381 /// Set each element e of array to e ^ @a t.
382 valarray<_Tp>& operator^=(const _Tp&);
384 /// Set each element e of array to e & @a t.
385 valarray<_Tp>& operator&=(const _Tp&);
387 /// Set each element e of array to e | @a t.
388 valarray<_Tp>& operator|=(const _Tp&);
390 /// Left shift each element e of array by @a t bits.
391 valarray<_Tp>& operator<<=(const _Tp&);
393 /// Right shift each element e of array by @a t bits.
394 valarray<_Tp>& operator>>=(const _Tp&);
396 /// Multiply elements of array by corresponding elements of @a v.
397 valarray<_Tp>& operator*=(const valarray<_Tp>&);
399 /// Divide elements of array by corresponding elements of @a v.
400 valarray<_Tp>& operator/=(const valarray<_Tp>&);
402 /// Modulo elements of array by corresponding elements of @a v.
403 valarray<_Tp>& operator%=(const valarray<_Tp>&);
405 /// Add corresponding elements of @a v to elements of array.
406 valarray<_Tp>& operator+=(const valarray<_Tp>&);
408 /// Subtract corresponding elements of @a v from elements of array.
409 valarray<_Tp>& operator-=(const valarray<_Tp>&);
411 /// Logical xor corresponding elements of @a v with elements of array.
412 valarray<_Tp>& operator^=(const valarray<_Tp>&);
414 /// Logical or corresponding elements of @a v with elements of array.
415 valarray<_Tp>& operator|=(const valarray<_Tp>&);
417 /// Logical and corresponding elements of @a v with elements of array.
418 valarray<_Tp>& operator&=(const valarray<_Tp>&);
420 /// Left shift elements of array by corresponding elements of @a v.
421 valarray<_Tp>& operator<<=(const valarray<_Tp>&);
423 /// Right shift elements of array by corresponding elements of @a v.
424 valarray<_Tp>& operator>>=(const valarray<_Tp>&);
427 valarray<_Tp>& operator*=(const _Expr<_Dom, _Tp>&);
429 valarray<_Tp>& operator/=(const _Expr<_Dom, _Tp>&);
431 valarray<_Tp>& operator%=(const _Expr<_Dom, _Tp>&);
433 valarray<_Tp>& operator+=(const _Expr<_Dom, _Tp>&);
435 valarray<_Tp>& operator-=(const _Expr<_Dom, _Tp>&);
437 valarray<_Tp>& operator^=(const _Expr<_Dom, _Tp>&);
439 valarray<_Tp>& operator|=(const _Expr<_Dom, _Tp>&);
441 valarray<_Tp>& operator&=(const _Expr<_Dom, _Tp>&);
443 valarray<_Tp>& operator<<=(const _Expr<_Dom, _Tp>&);
445 valarray<_Tp>& operator>>=(const _Expr<_Dom, _Tp>&);
447 // _lib.valarray.members_ member functions:
448 /// Return the number of elements in array.
452 * @brief Return the sum of all elements in the array.
454 * Accumulates the sum of all elements into a Tp using +=. The order
455 * of adding the elements is unspecified.
459 /// Return the minimum element using operator<().
462 /// Return the maximum element using operator<().
466 * @brief Return a shifted array.
468 * A new valarray is constructed as a copy of this array with elements
469 * in shifted positions. For an element with index i, the new position
470 * is i - n. The new valarray has the same size as the current one.
471 * New elements without a value are set to 0. Elements whose new
472 * position is outside the bounds of the array are discarded.
474 * Positive arguments shift toward index 0, discarding elements [0, n).
475 * Negative arguments discard elements from the top of the array.
477 * @param n Number of element positions to shift.
478 * @return New valarray with elements in shifted positions.
480 valarray<_Tp> shift (int) const;
483 * @brief Return a rotated array.
485 * A new valarray is constructed as a copy of this array with elements
486 * in shifted positions. For an element with index i, the new position
487 * is (i - n) % size(). The new valarray has the same size as the
488 * current one. Elements that are shifted beyond the array bounds are
489 * shifted into the other end of the array. No elements are lost.
491 * Positive arguments shift toward index 0, wrapping around the top.
492 * Negative arguments shift towards the top, wrapping around to 0.
494 * @param n Number of element positions to rotate.
495 * @return New valarray with elements in shifted positions.
497 valarray<_Tp> cshift(int) const;
500 * @brief Apply a function to the array.
502 * Returns a new valarray with elements assigned to the result of
503 * applying func to the corresponding element of this array. The new
504 * array has the same size as this one.
506 * @param func Function of Tp returning Tp to apply.
507 * @return New valarray with transformed elements.
509 _Expr<_ValFunClos<_ValArray, _Tp>, _Tp> apply(_Tp func(_Tp)) const;
512 * @brief Apply a function to the array.
514 * Returns a new valarray with elements assigned to the result of
515 * applying func to the corresponding element of this array. The new
516 * array has the same size as this one.
518 * @param func Function of const Tp& returning Tp to apply.
519 * @return New valarray with transformed elements.
521 _Expr<_RefFunClos<_ValArray, _Tp>, _Tp> apply(_Tp func(const _Tp&)) const;
524 * @brief Resize array.
526 * Resize this array to @a size and set all elements to @a c. All
527 * references and iterators are invalidated.
529 * @param size New array size.
530 * @param c New value for all elements.
532 void resize(size_t __size, _Tp __c = _Tp());
536 _Tp* __restrict__ _M_data;
538 friend class _Array<_Tp>;
541 template<typename _Tp>
543 valarray<_Tp>::operator[](size_t __i) const
545 __glibcxx_requires_subscript(__i);
549 template<typename _Tp>
551 valarray<_Tp>::operator[](size_t __i)
553 __glibcxx_requires_subscript(__i);
557 // @} group numeric_arrays
559 _GLIBCXX_END_NAMESPACE
561 #include <bits/valarray_after.h>
562 #include <bits/slice_array.h>
563 #include <bits/gslice.h>
564 #include <bits/gslice_array.h>
565 #include <bits/mask_array.h>
566 #include <bits/indirect_array.h>
568 _GLIBCXX_BEGIN_NAMESPACE(std)
571 * @addtogroup numeric_arrays
575 template<typename _Tp>
577 valarray<_Tp>::valarray() : _M_size(0), _M_data(0) {}
579 template<typename _Tp>
581 valarray<_Tp>::valarray(size_t __n)
582 : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n))
583 { std::__valarray_default_construct(_M_data, _M_data + __n); }
585 template<typename _Tp>
587 valarray<_Tp>::valarray(const _Tp& __t, size_t __n)
588 : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n))
589 { std::__valarray_fill_construct(_M_data, _M_data + __n, __t); }
591 template<typename _Tp>
593 valarray<_Tp>::valarray(const _Tp* __restrict__ __p, size_t __n)
594 : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n))
596 _GLIBCXX_DEBUG_ASSERT(__p != 0 || __n == 0);
597 std::__valarray_copy_construct(__p, __p + __n, _M_data);
600 template<typename _Tp>
602 valarray<_Tp>::valarray(const valarray<_Tp>& __v)
603 : _M_size(__v._M_size), _M_data(__valarray_get_storage<_Tp>(__v._M_size))
604 { std::__valarray_copy_construct(__v._M_data, __v._M_data + _M_size,
607 template<typename _Tp>
609 valarray<_Tp>::valarray(const slice_array<_Tp>& __sa)
610 : _M_size(__sa._M_sz), _M_data(__valarray_get_storage<_Tp>(__sa._M_sz))
612 std::__valarray_copy_construct
613 (__sa._M_array, __sa._M_sz, __sa._M_stride, _Array<_Tp>(_M_data));
616 template<typename _Tp>
618 valarray<_Tp>::valarray(const gslice_array<_Tp>& __ga)
619 : _M_size(__ga._M_index.size()),
620 _M_data(__valarray_get_storage<_Tp>(_M_size))
622 std::__valarray_copy_construct
623 (__ga._M_array, _Array<size_t>(__ga._M_index),
624 _Array<_Tp>(_M_data), _M_size);
627 template<typename _Tp>
629 valarray<_Tp>::valarray(const mask_array<_Tp>& __ma)
630 : _M_size(__ma._M_sz), _M_data(__valarray_get_storage<_Tp>(__ma._M_sz))
632 std::__valarray_copy_construct
633 (__ma._M_array, __ma._M_mask, _Array<_Tp>(_M_data), _M_size);
636 template<typename _Tp>
638 valarray<_Tp>::valarray(const indirect_array<_Tp>& __ia)
639 : _M_size(__ia._M_sz), _M_data(__valarray_get_storage<_Tp>(__ia._M_sz))
641 std::__valarray_copy_construct
642 (__ia._M_array, __ia._M_index, _Array<_Tp>(_M_data), _M_size);
645 #ifdef __GXX_EXPERIMENTAL_CXX0X__
646 template<typename _Tp>
648 valarray<_Tp>::valarray(initializer_list<_Tp> __l)
649 : _M_size(__l.size()), _M_data(__valarray_get_storage<_Tp>(__l.size()))
650 { std::__valarray_copy_construct (__l.begin(), __l.end(), _M_data); }
653 template<typename _Tp> template<class _Dom>
655 valarray<_Tp>::valarray(const _Expr<_Dom, _Tp>& __e)
656 : _M_size(__e.size()), _M_data(__valarray_get_storage<_Tp>(_M_size))
657 { std::__valarray_copy_construct(__e, _M_size, _Array<_Tp>(_M_data)); }
659 template<typename _Tp>
661 valarray<_Tp>::~valarray()
663 std::__valarray_destroy_elements(_M_data, _M_data + _M_size);
664 std::__valarray_release_memory(_M_data);
667 template<typename _Tp>
668 inline valarray<_Tp>&
669 valarray<_Tp>::operator=(const valarray<_Tp>& __v)
671 // _GLIBCXX_RESOLVE_LIB_DEFECTS
672 // 630. arrays of valarray.
673 if (_M_size == __v._M_size)
674 std::__valarray_copy(__v._M_data, _M_size, _M_data);
679 std::__valarray_destroy_elements(_M_data, _M_data + _M_size);
680 std::__valarray_release_memory(_M_data);
682 _M_size = __v._M_size;
683 _M_data = __valarray_get_storage<_Tp>(_M_size);
684 std::__valarray_copy_construct(__v._M_data, __v._M_data + _M_size,
690 #ifdef __GXX_EXPERIMENTAL_CXX0X__
691 template<typename _Tp>
692 inline valarray<_Tp>&
693 valarray<_Tp>::operator=(initializer_list<_Tp> __l)
695 // _GLIBCXX_RESOLVE_LIB_DEFECTS
696 // 630. arrays of valarray.
697 if (_M_size == __l.size())
698 std::__valarray_copy(__l.begin(), __l.size(), _M_data);
703 std::__valarray_destroy_elements(_M_data, _M_data + _M_size);
704 std::__valarray_release_memory(_M_data);
706 _M_size = __l.size();
707 _M_data = __valarray_get_storage<_Tp>(_M_size);
708 std::__valarray_copy_construct(__l.begin(), __l.begin() + _M_size,
715 template<typename _Tp>
716 inline valarray<_Tp>&
717 valarray<_Tp>::operator=(const _Tp& __t)
719 std::__valarray_fill(_M_data, _M_size, __t);
723 template<typename _Tp>
724 inline valarray<_Tp>&
725 valarray<_Tp>::operator=(const slice_array<_Tp>& __sa)
727 _GLIBCXX_DEBUG_ASSERT(_M_size == __sa._M_sz);
728 std::__valarray_copy(__sa._M_array, __sa._M_sz,
729 __sa._M_stride, _Array<_Tp>(_M_data));
733 template<typename _Tp>
734 inline valarray<_Tp>&
735 valarray<_Tp>::operator=(const gslice_array<_Tp>& __ga)
737 _GLIBCXX_DEBUG_ASSERT(_M_size == __ga._M_index.size());
738 std::__valarray_copy(__ga._M_array, _Array<size_t>(__ga._M_index),
739 _Array<_Tp>(_M_data), _M_size);
743 template<typename _Tp>
744 inline valarray<_Tp>&
745 valarray<_Tp>::operator=(const mask_array<_Tp>& __ma)
747 _GLIBCXX_DEBUG_ASSERT(_M_size == __ma._M_sz);
748 std::__valarray_copy(__ma._M_array, __ma._M_mask,
749 _Array<_Tp>(_M_data), _M_size);
753 template<typename _Tp>
754 inline valarray<_Tp>&
755 valarray<_Tp>::operator=(const indirect_array<_Tp>& __ia)
757 _GLIBCXX_DEBUG_ASSERT(_M_size == __ia._M_sz);
758 std::__valarray_copy(__ia._M_array, __ia._M_index,
759 _Array<_Tp>(_M_data), _M_size);
763 template<typename _Tp> template<class _Dom>
764 inline valarray<_Tp>&
765 valarray<_Tp>::operator=(const _Expr<_Dom, _Tp>& __e)
767 _GLIBCXX_DEBUG_ASSERT(_M_size == __e.size());
768 std::__valarray_copy(__e, _M_size, _Array<_Tp>(_M_data));
772 template<typename _Tp>
773 inline _Expr<_SClos<_ValArray,_Tp>, _Tp>
774 valarray<_Tp>::operator[](slice __s) const
776 typedef _SClos<_ValArray,_Tp> _Closure;
777 return _Expr<_Closure, _Tp>(_Closure (_Array<_Tp>(_M_data), __s));
780 template<typename _Tp>
781 inline slice_array<_Tp>
782 valarray<_Tp>::operator[](slice __s)
783 { return slice_array<_Tp>(_Array<_Tp>(_M_data), __s); }
785 template<typename _Tp>
786 inline _Expr<_GClos<_ValArray,_Tp>, _Tp>
787 valarray<_Tp>::operator[](const gslice& __gs) const
789 typedef _GClos<_ValArray,_Tp> _Closure;
790 return _Expr<_Closure, _Tp>
791 (_Closure(_Array<_Tp>(_M_data), __gs._M_index->_M_index));
794 template<typename _Tp>
795 inline gslice_array<_Tp>
796 valarray<_Tp>::operator[](const gslice& __gs)
798 return gslice_array<_Tp>
799 (_Array<_Tp>(_M_data), __gs._M_index->_M_index);
802 template<typename _Tp>
804 valarray<_Tp>::operator[](const valarray<bool>& __m) const
807 size_t __e = __m.size();
808 for (size_t __i=0; __i<__e; ++__i)
810 return valarray<_Tp>(mask_array<_Tp>(_Array<_Tp>(_M_data), __s,
811 _Array<bool> (__m)));
814 template<typename _Tp>
815 inline mask_array<_Tp>
816 valarray<_Tp>::operator[](const valarray<bool>& __m)
819 size_t __e = __m.size();
820 for (size_t __i=0; __i<__e; ++__i)
822 return mask_array<_Tp>(_Array<_Tp>(_M_data), __s, _Array<bool>(__m));
825 template<typename _Tp>
826 inline _Expr<_IClos<_ValArray,_Tp>, _Tp>
827 valarray<_Tp>::operator[](const valarray<size_t>& __i) const
829 typedef _IClos<_ValArray,_Tp> _Closure;
830 return _Expr<_Closure, _Tp>(_Closure(*this, __i));
833 template<typename _Tp>
834 inline indirect_array<_Tp>
835 valarray<_Tp>::operator[](const valarray<size_t>& __i)
837 return indirect_array<_Tp>(_Array<_Tp>(_M_data), __i.size(),
838 _Array<size_t>(__i));
843 valarray<_Tp>::size() const
848 valarray<_Tp>::sum() const
850 _GLIBCXX_DEBUG_ASSERT(_M_size > 0);
851 return std::__valarray_sum(_M_data, _M_data + _M_size);
856 valarray<_Tp>::shift(int __n) const
863 _Tp* __restrict__ __tmp_M_data =
864 std::__valarray_get_storage<_Tp>(_M_size);
867 std::__valarray_copy_construct(_M_data,
868 _M_data + _M_size, __tmp_M_data);
869 else if (__n > 0) // shift left
871 if (size_t(__n) > _M_size)
874 std::__valarray_copy_construct(_M_data + __n,
875 _M_data + _M_size, __tmp_M_data);
876 std::__valarray_default_construct(__tmp_M_data + _M_size - __n,
877 __tmp_M_data + _M_size);
881 if (-size_t(__n) > _M_size)
884 std::__valarray_copy_construct(_M_data, _M_data + _M_size + __n,
886 std::__valarray_default_construct(__tmp_M_data,
890 __ret._M_size = _M_size;
891 __ret._M_data = __tmp_M_data;
897 valarray<_Tp>::cshift(int __n) const
904 _Tp* __restrict__ __tmp_M_data =
905 std::__valarray_get_storage<_Tp>(_M_size);
908 std::__valarray_copy_construct(_M_data,
909 _M_data + _M_size, __tmp_M_data);
910 else if (__n > 0) // cshift left
912 if (size_t(__n) > _M_size)
913 __n = int(__n % _M_size);
915 std::__valarray_copy_construct(_M_data, _M_data + __n,
916 __tmp_M_data + _M_size - __n);
917 std::__valarray_copy_construct(_M_data + __n, _M_data + _M_size,
922 if (-size_t(__n) > _M_size)
923 __n = -int(-size_t(__n) % _M_size);
925 std::__valarray_copy_construct(_M_data + _M_size + __n,
926 _M_data + _M_size, __tmp_M_data);
927 std::__valarray_copy_construct(_M_data, _M_data + _M_size + __n,
931 __ret._M_size = _M_size;
932 __ret._M_data = __tmp_M_data;
938 valarray<_Tp>::resize(size_t __n, _Tp __c)
940 // This complication is so to make valarray<valarray<T> > work
941 // even though it is not required by the standard. Nobody should
942 // be saying valarray<valarray<T> > anyway. See the specs.
943 std::__valarray_destroy_elements(_M_data, _M_data + _M_size);
946 std::__valarray_release_memory(_M_data);
948 _M_data = __valarray_get_storage<_Tp>(__n);
950 std::__valarray_fill_construct(_M_data, _M_data + __n, __c);
953 template<typename _Tp>
955 valarray<_Tp>::min() const
957 _GLIBCXX_DEBUG_ASSERT(_M_size > 0);
958 return *std::min_element(_M_data, _M_data + _M_size);
961 template<typename _Tp>
963 valarray<_Tp>::max() const
965 _GLIBCXX_DEBUG_ASSERT(_M_size > 0);
966 return *std::max_element(_M_data, _M_data + _M_size);
970 inline _Expr<_ValFunClos<_ValArray, _Tp>, _Tp>
971 valarray<_Tp>::apply(_Tp func(_Tp)) const
973 typedef _ValFunClos<_ValArray, _Tp> _Closure;
974 return _Expr<_Closure, _Tp>(_Closure(*this, func));
978 inline _Expr<_RefFunClos<_ValArray, _Tp>, _Tp>
979 valarray<_Tp>::apply(_Tp func(const _Tp &)) const
981 typedef _RefFunClos<_ValArray, _Tp> _Closure;
982 return _Expr<_Closure, _Tp>(_Closure(*this, func));
985 #define _DEFINE_VALARRAY_UNARY_OPERATOR(_Op, _Name) \
986 template<typename _Tp> \
987 inline typename valarray<_Tp>::template _UnaryOp<_Name>::_Rt \
988 valarray<_Tp>::operator _Op() const \
990 typedef _UnClos<_Name, _ValArray, _Tp> _Closure; \
991 typedef typename __fun<_Name, _Tp>::result_type _Rt; \
992 return _Expr<_Closure, _Rt>(_Closure(*this)); \
995 _DEFINE_VALARRAY_UNARY_OPERATOR(+, __unary_plus)
996 _DEFINE_VALARRAY_UNARY_OPERATOR(-, __negate)
997 _DEFINE_VALARRAY_UNARY_OPERATOR(~, __bitwise_not)
998 _DEFINE_VALARRAY_UNARY_OPERATOR (!, __logical_not)
1000 #undef _DEFINE_VALARRAY_UNARY_OPERATOR
1002 #define _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(_Op, _Name) \
1003 template<class _Tp> \
1004 inline valarray<_Tp>& \
1005 valarray<_Tp>::operator _Op##=(const _Tp &__t) \
1007 _Array_augmented_##_Name(_Array<_Tp>(_M_data), _M_size, __t); \
1011 template<class _Tp> \
1012 inline valarray<_Tp>& \
1013 valarray<_Tp>::operator _Op##=(const valarray<_Tp> &__v) \
1015 _GLIBCXX_DEBUG_ASSERT(_M_size == __v._M_size); \
1016 _Array_augmented_##_Name(_Array<_Tp>(_M_data), _M_size, \
1017 _Array<_Tp>(__v._M_data)); \
1021 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(+, __plus)
1022 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(-, __minus)
1023 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(*, __multiplies)
1024 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(/, __divides)
1025 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(%, __modulus)
1026 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(^, __bitwise_xor)
1027 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(&, __bitwise_and)
1028 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(|, __bitwise_or)
1029 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(<<, __shift_left)
1030 _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(>>, __shift_right)
1032 #undef _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT
1034 #define _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(_Op, _Name) \
1035 template<class _Tp> template<class _Dom> \
1036 inline valarray<_Tp>& \
1037 valarray<_Tp>::operator _Op##=(const _Expr<_Dom, _Tp>& __e) \
1039 _Array_augmented_##_Name(_Array<_Tp>(_M_data), __e, _M_size); \
1043 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(+, __plus)
1044 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(-, __minus)
1045 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(*, __multiplies)
1046 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(/, __divides)
1047 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(%, __modulus)
1048 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(^, __bitwise_xor)
1049 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(&, __bitwise_and)
1050 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(|, __bitwise_or)
1051 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(<<, __shift_left)
1052 _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(>>, __shift_right)
1054 #undef _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT
1057 #define _DEFINE_BINARY_OPERATOR(_Op, _Name) \
1058 template<typename _Tp> \
1059 inline _Expr<_BinClos<_Name, _ValArray, _ValArray, _Tp, _Tp>, \
1060 typename __fun<_Name, _Tp>::result_type> \
1061 operator _Op(const valarray<_Tp>& __v, const valarray<_Tp>& __w) \
1063 _GLIBCXX_DEBUG_ASSERT(__v.size() == __w.size()); \
1064 typedef _BinClos<_Name, _ValArray, _ValArray, _Tp, _Tp> _Closure; \
1065 typedef typename __fun<_Name, _Tp>::result_type _Rt; \
1066 return _Expr<_Closure, _Rt>(_Closure(__v, __w)); \
1069 template<typename _Tp> \
1070 inline _Expr<_BinClos<_Name, _ValArray,_Constant, _Tp, _Tp>, \
1071 typename __fun<_Name, _Tp>::result_type> \
1072 operator _Op(const valarray<_Tp>& __v, const _Tp& __t) \
1074 typedef _BinClos<_Name, _ValArray, _Constant, _Tp, _Tp> _Closure; \
1075 typedef typename __fun<_Name, _Tp>::result_type _Rt; \
1076 return _Expr<_Closure, _Rt>(_Closure(__v, __t)); \
1079 template<typename _Tp> \
1080 inline _Expr<_BinClos<_Name, _Constant, _ValArray, _Tp, _Tp>, \
1081 typename __fun<_Name, _Tp>::result_type> \
1082 operator _Op(const _Tp& __t, const valarray<_Tp>& __v) \
1084 typedef _BinClos<_Name, _Constant, _ValArray, _Tp, _Tp> _Closure; \
1085 typedef typename __fun<_Name, _Tp>::result_type _Rt; \
1086 return _Expr<_Closure, _Rt>(_Closure(__t, __v)); \
1089 _DEFINE_BINARY_OPERATOR(+, __plus)
1090 _DEFINE_BINARY_OPERATOR(-, __minus)
1091 _DEFINE_BINARY_OPERATOR(*, __multiplies)
1092 _DEFINE_BINARY_OPERATOR(/, __divides)
1093 _DEFINE_BINARY_OPERATOR(%, __modulus)
1094 _DEFINE_BINARY_OPERATOR(^, __bitwise_xor)
1095 _DEFINE_BINARY_OPERATOR(&, __bitwise_and)
1096 _DEFINE_BINARY_OPERATOR(|, __bitwise_or)
1097 _DEFINE_BINARY_OPERATOR(<<, __shift_left)
1098 _DEFINE_BINARY_OPERATOR(>>, __shift_right)
1099 _DEFINE_BINARY_OPERATOR(&&, __logical_and)
1100 _DEFINE_BINARY_OPERATOR(||, __logical_or)
1101 _DEFINE_BINARY_OPERATOR(==, __equal_to)
1102 _DEFINE_BINARY_OPERATOR(!=, __not_equal_to)
1103 _DEFINE_BINARY_OPERATOR(<, __less)
1104 _DEFINE_BINARY_OPERATOR(>, __greater)
1105 _DEFINE_BINARY_OPERATOR(<=, __less_equal)
1106 _DEFINE_BINARY_OPERATOR(>=, __greater_equal)
1108 #undef _DEFINE_BINARY_OPERATOR
1110 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1112 * @brief Return an iterator pointing to the first element of
1114 * @param va valarray.
1118 begin(valarray<_Tp>& __va)
1119 { return std::__addressof(__va[0]); }
1122 * @brief Return an iterator pointing to the first element of
1123 * the const valarray.
1124 * @param va valarray.
1128 begin(const valarray<_Tp>& __va)
1129 { return std::__addressof(__va[0]); }
1132 * @brief Return an iterator pointing to one past the last element of
1134 * @param va valarray.
1138 end(valarray<_Tp>& __va)
1139 { return std::__addressof(__va[0]) + __va.size(); }
1142 * @brief Return an iterator pointing to one past the last element of
1143 * the const valarray.
1144 * @param va valarray.
1148 end(const valarray<_Tp>& __va)
1149 { return std::__addressof(__va[0]) + __va.size(); }
1150 #endif // __GXX_EXPERIMENTAL_CXX0X__
1152 // @} group numeric_arrays
1154 _GLIBCXX_END_NAMESPACE
1156 #endif /* _GLIBCXX_VALARRAY */