1// The template and inlines for the -*- C++ -*- valarray class.
3// Copyright (C) 1997-2025 Free Software Foundation, Inc.
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
25/** @file include/valarray
26 * This is a Standard C++ Library header.
29// Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr>
31#ifndef _GLIBCXX_VALARRAY
32#define _GLIBCXX_VALARRAY 1
35#pragma GCC system_header
38#include <bits/requires_hosted.h> // <cmath> dependant
40#include <bits/c++config.h>
43#include <debug/debug.h>
44#if __cplusplus >= 201103L
45#include <initializer_list>
48namespace std _GLIBCXX_VISIBILITY(default)
50_GLIBCXX_BEGIN_NAMESPACE_VERSION
52 template<class _Clos, typename _Tp>
55 template<typename _Tp1, typename _Tp2>
60 template<class _Oper, template<class, class> class _Meta, class _Dom>
64 template<class, class> class _Meta1,
65 template<class, class> class _Meta2,
66 class _Dom1, class _Dom2>
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<template<class, class> class _Meta, class _Dom>
81 template<template<class, class> class _Meta, class _Dom>
83} // namespace __detail
85 using __detail::_UnClos;
86 using __detail::_BinClos;
87 using __detail::_SClos;
88 using __detail::_GClos;
89 using __detail::_IClos;
90 using __detail::_ValFunClos;
91 using __detail::_RefFunClos;
93 template<class _Tp> class valarray; // An array of type _Tp
94 class slice; // BLAS-like slice out of an array
95 template<class _Tp> class slice_array;
96 class gslice; // generalized slice out of an array
97 template<class _Tp> class gslice_array;
98 template<class _Tp> class mask_array; // masked array
99 template<class _Tp> class indirect_array; // indirected array
101_GLIBCXX_END_NAMESPACE_VERSION
104#include <bits/valarray_array.h>
105#include <bits/valarray_before.h>
107namespace std _GLIBCXX_VISIBILITY(default)
109_GLIBCXX_BEGIN_NAMESPACE_VERSION
112 * @defgroup numeric_arrays Numeric Arrays
115 * Classes and functions for representing and manipulating arrays of elements.
120 * @brief Smart array designed to support numeric processing.
122 * A valarray is an array that provides constraints intended to allow for
123 * effective optimization of numeric array processing by reducing the
124 * aliasing that can result from pointer representations. It represents a
125 * one-dimensional array from which different multidimensional subsets can
126 * be accessed and modified.
128 * @tparam _Tp Type of object in the array.
136 typedef typename __fun<_Op, _Tp>::result_type __rt;
137 typedef _Expr<_UnClos<_Op, _ValArray, _Tp>, __rt> _Rt;
140 typedef _Tp value_type;
142 // _lib.valarray.cons_ construct/destroy:
143 /// Construct an empty array.
144 valarray() _GLIBCXX_NOTHROW;
146 /// Construct an array with @a n elements.
147 explicit valarray(size_t);
149 /// Construct an array with @a n elements initialized to @a t.
150 valarray(const _Tp&, size_t);
152 /// Construct an array initialized to the first @a n elements of @a t.
153 valarray(const _Tp* __restrict__, size_t);
155 /// Copy constructor.
156 valarray(const valarray&);
158#if __cplusplus >= 201103L
159 /// Move constructor.
160 valarray(valarray&&) noexcept;
163 /// Construct an array with the same size and values in @a sa.
164 valarray(const slice_array<_Tp>&);
166 /// Construct an array with the same size and values in @a ga.
167 valarray(const gslice_array<_Tp>&);
169 /// Construct an array with the same size and values in @a ma.
170 valarray(const mask_array<_Tp>&);
172 /// Construct an array with the same size and values in @a ia.
173 valarray(const indirect_array<_Tp>&);
175#if __cplusplus >= 201103L
176 /// Construct an array with an initializer_list of values.
177 valarray(initializer_list<_Tp>);
181 valarray(const _Expr<_Dom, _Tp>& __e);
183 ~valarray() _GLIBCXX_NOEXCEPT;
185 // _lib.valarray.assign_ assignment:
187 * @brief Assign elements to an array.
189 * Assign elements of array to values in @a v.
191 * @param __v Valarray to get values from.
193 valarray<_Tp>& operator=(const valarray<_Tp>& __v);
195#if __cplusplus >= 201103L
197 * @brief Move assign elements to an array.
199 * Move assign elements of array to values in @a v.
201 * @param __v Valarray to get values from.
203 valarray<_Tp>& operator=(valarray<_Tp>&& __v) noexcept;
207 * @brief Assign elements to a value.
209 * Assign all elements of array to @a t.
211 * @param __t Value for elements.
213 valarray<_Tp>& operator=(const _Tp& __t);
216 * @brief Assign elements to an array subset.
218 * Assign elements of array to values in @a sa. Results are undefined
219 * if @a sa does not have the same size as this array.
221 * @param __sa Array slice to get values from.
223 valarray<_Tp>& operator=(const slice_array<_Tp>& __sa);
226 * @brief Assign elements to an array subset.
228 * Assign elements of array to values in @a ga. Results are undefined
229 * if @a ga does not have the same size as this array.
231 * @param __ga Array slice to get values from.
233 valarray<_Tp>& operator=(const gslice_array<_Tp>& __ga);
236 * @brief Assign elements to an array subset.
238 * Assign elements of array to values in @a ma. Results are undefined
239 * if @a ma does not have the same size as this array.
241 * @param __ma Array slice to get values from.
243 valarray<_Tp>& operator=(const mask_array<_Tp>& __ma);
246 * @brief Assign elements to an array subset.
248 * Assign elements of array to values in @a ia. Results are undefined
249 * if @a ia does not have the same size as this array.
251 * @param __ia Array slice to get values from.
253 valarray<_Tp>& operator=(const indirect_array<_Tp>& __ia);
255#if __cplusplus >= 201103L
257 * @brief Assign elements to an initializer_list.
259 * Assign elements of array to values in @a __l. Results are undefined
260 * if @a __l does not have the same size as this array.
262 * @param __l initializer_list to get values from.
264 valarray& operator=(initializer_list<_Tp> __l);
267 template<class _Dom> valarray<_Tp>&
268 operator= (const _Expr<_Dom, _Tp>&);
270 // _lib.valarray.access_ element access:
272 * Return a reference to the i'th array element.
274 * @param __i Index of element to return.
275 * @return Reference to the i'th element.
277 _Tp& operator[](size_t __i) _GLIBCXX_NOTHROW;
279 // _GLIBCXX_RESOLVE_LIB_DEFECTS
280 // 389. Const overload of valarray::operator[] returns by value.
281 const _Tp& operator[](size_t) const _GLIBCXX_NOTHROW;
283 // _lib.valarray.sub_ subset operations:
285 * @brief Return an array subset.
287 * Returns a new valarray containing the elements of the array
288 * indicated by the slice argument. The new valarray has the same size
289 * as the input slice. @see slice.
291 * @param __s The source slice.
292 * @return New valarray containing elements in @a __s.
294 _Expr<_SClos<_ValArray, _Tp>, _Tp> operator[](slice __s) const;
297 * @brief Return a reference to an array subset.
299 * Returns a new valarray containing the elements of the array
300 * indicated by the slice argument. The new valarray has the same size
301 * as the input slice. @see slice.
303 * @param __s The source slice.
304 * @return New valarray containing elements in @a __s.
306 slice_array<_Tp> operator[](slice __s);
309 * @brief Return an array subset.
311 * Returns a slice_array referencing the elements of the array
312 * indicated by the slice argument. @see gslice.
314 * @param __s The source slice.
315 * @return Slice_array referencing elements indicated by @a __s.
317 _Expr<_GClos<_ValArray, _Tp>, _Tp> operator[](const gslice& __s) const;
320 * @brief Return a reference to an array subset.
322 * Returns a new valarray containing the elements of the array
323 * indicated by the gslice argument. The new valarray has
324 * the same size as the input gslice. @see gslice.
326 * @param __s The source gslice.
327 * @return New valarray containing elements in @a __s.
329 gslice_array<_Tp> operator[](const gslice& __s);
332 * @brief Return an array subset.
334 * Returns a new valarray containing the elements of the array
335 * indicated by the argument. The input is a valarray of bool which
336 * represents a bitmask indicating which elements should be copied into
337 * the new valarray. Each element of the array is added to the return
338 * valarray if the corresponding element of the argument is true.
340 * @param __m The valarray bitmask.
341 * @return New valarray containing elements indicated by @a __m.
343 valarray<_Tp> operator[](const valarray<bool>& __m) const;
346 * @brief Return a reference to an array subset.
348 * Returns a new mask_array referencing the elements of the array
349 * indicated by the argument. The input is a valarray of bool which
350 * represents a bitmask indicating which elements are part of the
351 * subset. Elements of the array are part of the subset if the
352 * corresponding element of the argument is true.
354 * @param __m The valarray bitmask.
355 * @return New valarray containing elements indicated by @a __m.
357 mask_array<_Tp> operator[](const valarray<bool>& __m);
360 * @brief Return an array subset.
362 * Returns a new valarray containing the elements of the array
363 * indicated by the argument. The elements in the argument are
364 * interpreted as the indices of elements of this valarray to copy to
365 * the return valarray.
367 * @param __i The valarray element index list.
368 * @return New valarray containing elements in @a __s.
370 _Expr<_IClos<_ValArray, _Tp>, _Tp>
371 operator[](const valarray<size_t>& __i) const;
374 * @brief Return a reference to an array subset.
376 * Returns an indirect_array referencing the elements of the array
377 * indicated by the argument. The elements in the argument are
378 * interpreted as the indices of elements of this valarray to include
379 * in the subset. The returned indirect_array refers to these
382 * @param __i The valarray element index list.
383 * @return Indirect_array referencing elements in @a __i.
385 indirect_array<_Tp> operator[](const valarray<size_t>& __i);
387 // _lib.valarray.unary_ unary operators:
388 /// Return a new valarray by applying unary + to each element.
389 typename _UnaryOp<__unary_plus>::_Rt operator+() const;
391 /// Return a new valarray by applying unary - to each element.
392 typename _UnaryOp<__negate>::_Rt operator-() const;
394 /// Return a new valarray by applying unary ~ to each element.
395 typename _UnaryOp<__bitwise_not>::_Rt operator~() const;
397 /// Return a new valarray by applying unary ! to each element.
398 typename _UnaryOp<__logical_not>::_Rt operator!() const;
400 // _lib.valarray.cassign_ computed assignment:
401 /// Multiply each element of array by @a t.
402 valarray<_Tp>& operator*=(const _Tp&);
404 /// Divide each element of array by @a t.
405 valarray<_Tp>& operator/=(const _Tp&);
407 /// Set each element e of array to e % @a t.
408 valarray<_Tp>& operator%=(const _Tp&);
410 /// Add @a t to each element of array.
411 valarray<_Tp>& operator+=(const _Tp&);
413 /// Subtract @a t to each element of array.
414 valarray<_Tp>& operator-=(const _Tp&);
416 /// Set each element e of array to e ^ @a t.
417 valarray<_Tp>& operator^=(const _Tp&);
419 /// Set each element e of array to e & @a t.
420 valarray<_Tp>& operator&=(const _Tp&);
422 /// Set each element e of array to e | @a t.
423 valarray<_Tp>& operator|=(const _Tp&);
425 /// Left shift each element e of array by @a t bits.
426 valarray<_Tp>& operator<<=(const _Tp&);
428 /// Right shift each element e of array by @a t bits.
429 valarray<_Tp>& operator>>=(const _Tp&);
431 /// Multiply elements of array by corresponding elements of @a v.
432 valarray<_Tp>& operator*=(const valarray<_Tp>&);
434 /// Divide elements of array by corresponding elements of @a v.
435 valarray<_Tp>& operator/=(const valarray<_Tp>&);
437 /// Modulo elements of array by corresponding elements of @a v.
438 valarray<_Tp>& operator%=(const valarray<_Tp>&);
440 /// Add corresponding elements of @a v to elements of array.
441 valarray<_Tp>& operator+=(const valarray<_Tp>&);
443 /// Subtract corresponding elements of @a v from elements of array.
444 valarray<_Tp>& operator-=(const valarray<_Tp>&);
446 /// Logical xor corresponding elements of @a v with elements of array.
447 valarray<_Tp>& operator^=(const valarray<_Tp>&);
449 /// Logical or corresponding elements of @a v with elements of array.
450 valarray<_Tp>& operator|=(const valarray<_Tp>&);
452 /// Logical and corresponding elements of @a v with elements of array.
453 valarray<_Tp>& operator&=(const valarray<_Tp>&);
455 /// Left shift elements of array by corresponding elements of @a v.
456 valarray<_Tp>& operator<<=(const valarray<_Tp>&);
458 /// Right shift elements of array by corresponding elements of @a v.
459 valarray<_Tp>& operator>>=(const valarray<_Tp>&);
462 valarray<_Tp>& operator*=(const _Expr<_Dom, _Tp>&);
464 valarray<_Tp>& operator/=(const _Expr<_Dom, _Tp>&);
466 valarray<_Tp>& operator%=(const _Expr<_Dom, _Tp>&);
468 valarray<_Tp>& operator+=(const _Expr<_Dom, _Tp>&);
470 valarray<_Tp>& operator-=(const _Expr<_Dom, _Tp>&);
472 valarray<_Tp>& operator^=(const _Expr<_Dom, _Tp>&);
474 valarray<_Tp>& operator|=(const _Expr<_Dom, _Tp>&);
476 valarray<_Tp>& operator&=(const _Expr<_Dom, _Tp>&);
478 valarray<_Tp>& operator<<=(const _Expr<_Dom, _Tp>&);
480 valarray<_Tp>& operator>>=(const _Expr<_Dom, _Tp>&);
482 // _lib.valarray.members_ member functions:
483#if __cplusplus >= 201103L
485 void swap(valarray<_Tp>& __v) noexcept;
488 /// Return the number of elements in array.
492 * @brief Return the sum of all elements in the array.
494 * Accumulates the sum of all elements into a Tp using +=. The order
495 * of adding the elements is unspecified.
499 /// Return the minimum element using operator<().
502 /// Return the maximum element using operator<().
506 * @brief Return a shifted array.
508 * A new valarray is constructed as a copy of this array with elements
509 * in shifted positions. For an element with index i, the new position
510 * is i - n. The new valarray has the same size as the current one.
511 * New elements without a value are set to 0. Elements whose new
512 * position is outside the bounds of the array are discarded.
514 * Positive arguments shift toward index 0, discarding elements [0, n).
515 * Negative arguments discard elements from the top of the array.
517 * @param __n Number of element positions to shift.
518 * @return New valarray with elements in shifted positions.
520 valarray<_Tp> shift (int __n) const;
523 * @brief Return a rotated array.
525 * A new valarray is constructed as a copy of this array with elements
526 * in shifted positions. For an element with index i, the new position
527 * is (i - n) % size(). The new valarray has the same size as the
528 * current one. Elements that are shifted beyond the array bounds are
529 * shifted into the other end of the array. No elements are lost.
531 * Positive arguments shift toward index 0, wrapping around the top.
532 * Negative arguments shift towards the top, wrapping around to 0.
534 * @param __n Number of element positions to rotate.
535 * @return New valarray with elements in shifted positions.
537 valarray<_Tp> cshift(int __n) const;
540 * @brief Apply a function to the array.
542 * Returns a new valarray with elements assigned to the result of
543 * applying __func to the corresponding element of this array. The new
544 * array has the same size as this one.
546 * @param __func Function of Tp returning Tp to apply.
547 * @return New valarray with transformed elements.
549 _Expr<_ValFunClos<_ValArray, _Tp>, _Tp> apply(_Tp __func(_Tp)) const;
552 * @brief Apply a function to the array.
554 * Returns a new valarray with elements assigned to the result of
555 * applying __func to the corresponding element of this array. The new
556 * array has the same size as this one.
558 * @param __func Function of const Tp& returning Tp to apply.
559 * @return New valarray with transformed elements.
561 _Expr<_RefFunClos<_ValArray, _Tp>, _Tp> apply(_Tp __func(const _Tp&)) const;
564 * @brief Resize array.
566 * Resize this array to @a size and set all elements to @a c. All
567 * references and iterators are invalidated.
569 * @param __size New array size.
570 * @param __c New value for all elements.
572 void resize(size_t __size, _Tp __c = _Tp());
576 _Tp* __restrict__ _M_data;
578 friend struct _Array<_Tp>;
581#if __cpp_deduction_guides >= 201606
582 template<typename _Tp, size_t _Nm>
583 valarray(const _Tp(&)[_Nm], size_t) -> valarray<_Tp>;
586 template<typename _Tp>
588 valarray<_Tp>::operator[](size_t __i) const _GLIBCXX_NOTHROW
590 __glibcxx_requires_subscript(__i);
594 template<typename _Tp>
596 valarray<_Tp>::operator[](size_t __i) _GLIBCXX_NOTHROW
598 __glibcxx_requires_subscript(__i);
602 /// @} group numeric_arrays
604_GLIBCXX_END_NAMESPACE_VERSION
607#include <bits/valarray_after.h>
608#include <bits/slice_array.h>
609#include <bits/gslice.h>
610#include <bits/gslice_array.h>
611#include <bits/mask_array.h>
612#include <bits/indirect_array.h>
614namespace std _GLIBCXX_VISIBILITY(default)
616_GLIBCXX_BEGIN_NAMESPACE_VERSION
619 * @addtogroup numeric_arrays
623 template<typename _Tp>
625 valarray<_Tp>::valarray() _GLIBCXX_NOTHROW : _M_size(0), _M_data(0) {}
627 template<typename _Tp>
629 valarray<_Tp>::valarray(size_t __n)
630 : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n))
631 { std::__valarray_default_construct(_M_data, _M_data + __n); }
633 template<typename _Tp>
635 valarray<_Tp>::valarray(const _Tp& __t, size_t __n)
636 : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n))
637 { std::__valarray_fill_construct(_M_data, _M_data + __n, __t); }
639 template<typename _Tp>
641 valarray<_Tp>::valarray(const _Tp* __restrict__ __p, size_t __n)
642 : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n))
644 __glibcxx_assert(__p != 0 || __n == 0);
645 std::__valarray_copy_construct(__p, __p + __n, _M_data);
648 template<typename _Tp>
650 valarray<_Tp>::valarray(const valarray<_Tp>& __v)
651 : _M_size(__v._M_size), _M_data(__valarray_get_storage<_Tp>(__v._M_size))
652 { std::__valarray_copy_construct(__v._M_data, __v._M_data + _M_size,
655#if __cplusplus >= 201103L
656 template<typename _Tp>
658 valarray<_Tp>::valarray(valarray<_Tp>&& __v) noexcept
659 : _M_size(__v._M_size), _M_data(__v._M_data)
666 template<typename _Tp>
668 valarray<_Tp>::valarray(const slice_array<_Tp>& __sa)
669 : _M_size(__sa._M_sz), _M_data(__valarray_get_storage<_Tp>(__sa._M_sz))
671 std::__valarray_copy_construct
672 (__sa._M_array, __sa._M_sz, __sa._M_stride, _Array<_Tp>(_M_data));
675 template<typename _Tp>
677 valarray<_Tp>::valarray(const gslice_array<_Tp>& __ga)
678 : _M_size(__ga._M_index.size()),
679 _M_data(__valarray_get_storage<_Tp>(_M_size))
681 std::__valarray_copy_construct
682 (__ga._M_array, _Array<size_t>(__ga._M_index),
683 _Array<_Tp>(_M_data), _M_size);
686 template<typename _Tp>
688 valarray<_Tp>::valarray(const mask_array<_Tp>& __ma)
689 : _M_size(__ma._M_sz), _M_data(__valarray_get_storage<_Tp>(__ma._M_sz))
691 std::__valarray_copy_construct
692 (__ma._M_array, __ma._M_mask, _Array<_Tp>(_M_data), _M_size);
695 template<typename _Tp>
697 valarray<_Tp>::valarray(const indirect_array<_Tp>& __ia)
698 : _M_size(__ia._M_sz), _M_data(__valarray_get_storage<_Tp>(__ia._M_sz))
700 std::__valarray_copy_construct
701 (__ia._M_array, __ia._M_index, _Array<_Tp>(_M_data), _M_size);
704#if __cplusplus >= 201103L
705 template<typename _Tp>
707 valarray<_Tp>::valarray(initializer_list<_Tp> __l)
708 : _M_size(__l.size()), _M_data(__valarray_get_storage<_Tp>(__l.size()))
709 { std::__valarray_copy_construct(__l.begin(), __l.end(), _M_data); }
712 template<typename _Tp> template<class _Dom>
714 valarray<_Tp>::valarray(const _Expr<_Dom, _Tp>& __e)
715 : _M_size(__e.size()), _M_data(__valarray_get_storage<_Tp>(_M_size))
716 { std::__valarray_copy_construct(__e, _M_size, _Array<_Tp>(_M_data)); }
718 template<typename _Tp>
720 valarray<_Tp>::~valarray() _GLIBCXX_NOEXCEPT
722 std::__valarray_destroy_elements(_M_data, _M_data + _M_size);
723 std::__valarray_release_memory(_M_data);
726 template<typename _Tp>
727 inline valarray<_Tp>&
728 valarray<_Tp>::operator=(const valarray<_Tp>& __v)
730 // _GLIBCXX_RESOLVE_LIB_DEFECTS
731 // 630. arrays of valarray.
732 if (_M_size == __v._M_size)
733 std::__valarray_copy(__v._M_data, _M_size, _M_data);
738 std::__valarray_destroy_elements(_M_data, _M_data + _M_size);
739 std::__valarray_release_memory(_M_data);
741 _M_size = __v._M_size;
742 _M_data = __valarray_get_storage<_Tp>(_M_size);
743 std::__valarray_copy_construct(__v._M_data, __v._M_data + _M_size,
749#if __cplusplus >= 201103L
750 template<typename _Tp>
751 inline valarray<_Tp>&
752 valarray<_Tp>::operator=(valarray<_Tp>&& __v) noexcept
756 std::__valarray_destroy_elements(_M_data, _M_data + _M_size);
757 std::__valarray_release_memory(_M_data);
759 _M_size = __v._M_size;
760 _M_data = __v._M_data;
766 template<typename _Tp>
767 inline valarray<_Tp>&
768 valarray<_Tp>::operator=(initializer_list<_Tp> __l)
770 // _GLIBCXX_RESOLVE_LIB_DEFECTS
771 // 630. arrays of valarray.
772 if (_M_size == __l.size())
773 std::__valarray_copy(__l.begin(), __l.size(), _M_data);
778 std::__valarray_destroy_elements(_M_data, _M_data + _M_size);
779 std::__valarray_release_memory(_M_data);
781 _M_size = __l.size();
782 _M_data = __valarray_get_storage<_Tp>(_M_size);
783 std::__valarray_copy_construct(__l.begin(), __l.begin() + _M_size,
790 template<typename _Tp>
791 inline valarray<_Tp>&
792 valarray<_Tp>::operator=(const _Tp& __t)
794 std::__valarray_fill(_M_data, _M_size, __t);
798 template<typename _Tp>
799 inline valarray<_Tp>&
800 valarray<_Tp>::operator=(const slice_array<_Tp>& __sa)
802 __glibcxx_assert(_M_size == __sa._M_sz);
803 std::__valarray_copy(__sa._M_array, __sa._M_sz,
804 __sa._M_stride, _Array<_Tp>(_M_data));
808 template<typename _Tp>
809 inline valarray<_Tp>&
810 valarray<_Tp>::operator=(const gslice_array<_Tp>& __ga)
812 __glibcxx_assert(_M_size == __ga._M_index.size());
813 std::__valarray_copy(__ga._M_array, _Array<size_t>(__ga._M_index),
814 _Array<_Tp>(_M_data), _M_size);
818 template<typename _Tp>
819 inline valarray<_Tp>&
820 valarray<_Tp>::operator=(const mask_array<_Tp>& __ma)
822 __glibcxx_assert(_M_size == __ma._M_sz);
823 std::__valarray_copy(__ma._M_array, __ma._M_mask,
824 _Array<_Tp>(_M_data), _M_size);
828 template<typename _Tp>
829 inline valarray<_Tp>&
830 valarray<_Tp>::operator=(const indirect_array<_Tp>& __ia)
832 __glibcxx_assert(_M_size == __ia._M_sz);
833 std::__valarray_copy(__ia._M_array, __ia._M_index,
834 _Array<_Tp>(_M_data), _M_size);
838 template<typename _Tp> template<class _Dom>
839 inline valarray<_Tp>&
840 valarray<_Tp>::operator=(const _Expr<_Dom, _Tp>& __e)
842 // _GLIBCXX_RESOLVE_LIB_DEFECTS
843 // 630. arrays of valarray.
844 if (_M_size == __e.size())
846 // Copy manually instead of using __valarray_copy, because __e might
847 // alias _M_data and the _Array param type of __valarray_copy uses
848 // restrict which doesn't allow aliasing.
849 for (size_t __i = 0; __i < _M_size; ++__i)
850 _M_data[__i] = __e[__i];
856 std::__valarray_destroy_elements(_M_data, _M_data + _M_size);
857 std::__valarray_release_memory(_M_data);
859 _M_size = __e.size();
860 _M_data = __valarray_get_storage<_Tp>(_M_size);
861 std::__valarray_copy_construct(__e, _M_size, _Array<_Tp>(_M_data));
866 template<typename _Tp>
867 inline _Expr<_SClos<_ValArray,_Tp>, _Tp>
868 valarray<_Tp>::operator[](slice __s) const
870 typedef _SClos<_ValArray,_Tp> _Closure;
871 return _Expr<_Closure, _Tp>(_Closure (_Array<_Tp>(_M_data), __s));
874 template<typename _Tp>
875 inline slice_array<_Tp>
876 valarray<_Tp>::operator[](slice __s)
877 { return slice_array<_Tp>(_Array<_Tp>(_M_data), __s); }
879 template<typename _Tp>
880 inline _Expr<_GClos<_ValArray,_Tp>, _Tp>
881 valarray<_Tp>::operator[](const gslice& __gs) const
883 typedef _GClos<_ValArray,_Tp> _Closure;
884 return _Expr<_Closure, _Tp>
885 (_Closure(_Array<_Tp>(_M_data), __gs._M_index->_M_index));
888 template<typename _Tp>
889 inline gslice_array<_Tp>
890 valarray<_Tp>::operator[](const gslice& __gs)
892 return gslice_array<_Tp>
893 (_Array<_Tp>(_M_data), __gs._M_index->_M_index);
896 template<typename _Tp>
898 valarray<_Tp>::operator[](const valarray<bool>& __m) const
901 size_t __e = __m.size();
902 for (size_t __i=0; __i<__e; ++__i)
904 __glibcxx_assert(__s <= _M_size);
905 return valarray<_Tp>(mask_array<_Tp>(_Array<_Tp>(_M_data), __s,
906 _Array<bool> (__m)));
909 template<typename _Tp>
910 inline mask_array<_Tp>
911 valarray<_Tp>::operator[](const valarray<bool>& __m)
914 size_t __e = __m.size();
915 for (size_t __i=0; __i<__e; ++__i)
917 __glibcxx_assert(__s <= _M_size);
918 return mask_array<_Tp>(_Array<_Tp>(_M_data), __s, _Array<bool>(__m));
921 template<typename _Tp>
922 inline _Expr<_IClos<_ValArray,_Tp>, _Tp>
923 valarray<_Tp>::operator[](const valarray<size_t>& __i) const
925 typedef _IClos<_ValArray,_Tp> _Closure;
926 return _Expr<_Closure, _Tp>(_Closure(*this, __i));
929 template<typename _Tp>
930 inline indirect_array<_Tp>
931 valarray<_Tp>::operator[](const valarray<size_t>& __i)
933 return indirect_array<_Tp>(_Array<_Tp>(_M_data), __i.size(),
934 _Array<size_t>(__i));
937#if __cplusplus >= 201103L
940 valarray<_Tp>::swap(valarray<_Tp>& __v) noexcept
942 std::swap(_M_size, __v._M_size);
943 std::swap(_M_data, __v._M_data);
949 valarray<_Tp>::size() const
954 valarray<_Tp>::sum() const
956 __glibcxx_assert(_M_size > 0);
957 return std::__valarray_sum(_M_data, _M_data + _M_size);
962 valarray<_Tp>::shift(int __n) const
969 _Tp* __restrict__ __tmp_M_data =
970 std::__valarray_get_storage<_Tp>(_M_size);
973 std::__valarray_copy_construct(_M_data,
974 _M_data + _M_size, __tmp_M_data);
975 else if (__n > 0) // shift left
977 if (size_t(__n) > _M_size)
980 std::__valarray_copy_construct(_M_data + __n,
981 _M_data + _M_size, __tmp_M_data);
982 std::__valarray_default_construct(__tmp_M_data + _M_size - __n,
983 __tmp_M_data + _M_size);
987 if (-size_t(__n) > _M_size)
990 std::__valarray_copy_construct(_M_data, _M_data + _M_size + __n,
992 std::__valarray_default_construct(__tmp_M_data,
996 __ret._M_size = _M_size;
997 __ret._M_data = __tmp_M_data;
1002 inline valarray<_Tp>
1003 valarray<_Tp>::cshift(int __n) const
1005 valarray<_Tp> __ret;
1010 _Tp* __restrict__ __tmp_M_data =
1011 std::__valarray_get_storage<_Tp>(_M_size);
1014 std::__valarray_copy_construct(_M_data,
1015 _M_data + _M_size, __tmp_M_data);
1016 else if (__n > 0) // cshift left
1018 if (size_t(__n) > _M_size)
1019 __n = int(__n % _M_size);
1021 std::__valarray_copy_construct(_M_data, _M_data + __n,
1022 __tmp_M_data + _M_size - __n);
1023 std::__valarray_copy_construct(_M_data + __n, _M_data + _M_size,
1026 else // cshift right
1028 if (-size_t(__n) > _M_size)
1029 __n = -int(-size_t(__n) % _M_size);
1031 std::__valarray_copy_construct(_M_data + _M_size + __n,
1032 _M_data + _M_size, __tmp_M_data);
1033 std::__valarray_copy_construct(_M_data, _M_data + _M_size + __n,
1034 __tmp_M_data - __n);
1037 __ret._M_size = _M_size;
1038 __ret._M_data = __tmp_M_data;
1044 valarray<_Tp>::resize(size_t __n, _Tp __c)
1046 // This complication is so to make valarray<valarray<T> > work
1047 // even though it is not required by the standard. Nobody should
1048 // be saying valarray<valarray<T> > anyway. See the specs.
1049 std::__valarray_destroy_elements(_M_data, _M_data + _M_size);
1052 std::__valarray_release_memory(_M_data);
1054 _M_data = __valarray_get_storage<_Tp>(__n);
1056 std::__valarray_fill_construct(_M_data, _M_data + __n, __c);
1059 template<typename _Tp>
1061 valarray<_Tp>::min() const
1063 __glibcxx_assert(_M_size > 0);
1064 return *std::min_element(_M_data, _M_data + _M_size);
1067 template<typename _Tp>
1069 valarray<_Tp>::max() const
1071 __glibcxx_assert(_M_size > 0);
1072 return *std::max_element(_M_data, _M_data + _M_size);
1076 inline _Expr<_ValFunClos<_ValArray, _Tp>, _Tp>
1077 valarray<_Tp>::apply(_Tp __func(_Tp)) const
1079 typedef _ValFunClos<_ValArray, _Tp> _Closure;
1080 return _Expr<_Closure, _Tp>(_Closure(*this, __func));
1084 inline _Expr<_RefFunClos<_ValArray, _Tp>, _Tp>
1085 valarray<_Tp>::apply(_Tp __func(const _Tp &)) const
1087 typedef _RefFunClos<_ValArray, _Tp> _Closure;
1088 return _Expr<_Closure, _Tp>(_Closure(*this, __func));
1091 /// @cond undocumented
1092#define _DEFINE_VALARRAY_UNARY_OPERATOR(_Op, _Name) \
1093 template<typename _Tp> \
1094 inline typename valarray<_Tp>::template _UnaryOp<_Name>::_Rt \
1095 valarray<_Tp>::operator _Op() const \
1097 typedef _UnClos<_Name, _ValArray, _Tp> _Closure; \
1098 typedef typename __fun<_Name, _Tp>::result_type _Rt; \
1099 return _Expr<_Closure, _Rt>(_Closure(*this)); \
1102 _DEFINE_VALARRAY_UNARY_OPERATOR(+, __unary_plus)
1103 _DEFINE_VALARRAY_UNARY_OPERATOR(-, __negate)
1104 _DEFINE_VALARRAY_UNARY_OPERATOR(~, __bitwise_not)
1105 _DEFINE_VALARRAY_UNARY_OPERATOR (!, __logical_not)
1107#undef _DEFINE_VALARRAY_UNARY_OPERATOR
1109#define _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(_Op, _Name) \
1110 template<class _Tp> \
1111 inline valarray<_Tp>& \
1112 valarray<_Tp>::operator _Op##=(const _Tp &__t) \
1114 _Array_augmented_##_Name(_Array<_Tp>(_M_data), _M_size, __t); \
1118 template<class _Tp> \
1119 inline valarray<_Tp>& \
1120 valarray<_Tp>::operator _Op##=(const valarray<_Tp> &__v) \
1122 __glibcxx_assert(_M_size == __v._M_size); \
1123 _Array_augmented_##_Name(_Array<_Tp>(_M_data), _M_size, \
1124 _Array<_Tp>(__v._M_data)); \
1128_DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(+, __plus)
1129_DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(-, __minus)
1130_DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(*, __multiplies)
1131_DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(/, __divides)
1132_DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(%, __modulus)
1133_DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(^, __bitwise_xor)
1134_DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(&, __bitwise_and)
1135_DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(|, __bitwise_or)
1136_DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(<<, __shift_left)
1137_DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(>>, __shift_right)
1139#undef _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT
1141#define _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(_Op, _Name) \
1142 template<class _Tp> template<class _Dom> \
1143 inline valarray<_Tp>& \
1144 valarray<_Tp>::operator _Op##=(const _Expr<_Dom, _Tp>& __e) \
1146 _Array_augmented_##_Name(_Array<_Tp>(_M_data), __e, _M_size); \
1150_DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(+, __plus)
1151_DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(-, __minus)
1152_DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(*, __multiplies)
1153_DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(/, __divides)
1154_DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(%, __modulus)
1155_DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(^, __bitwise_xor)
1156_DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(&, __bitwise_and)
1157_DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(|, __bitwise_or)
1158_DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(<<, __shift_left)
1159_DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(>>, __shift_right)
1161#undef _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT
1164#define _DEFINE_BINARY_OPERATOR(_Op, _Name) \
1165 template<typename _Tp> \
1166 inline _Expr<_BinClos<_Name, _ValArray, _ValArray, _Tp, _Tp>, \
1167 typename __fun<_Name, _Tp>::result_type> \
1168 operator _Op(const valarray<_Tp>& __v, const valarray<_Tp>& __w) \
1170 __glibcxx_assert(__v.size() == __w.size()); \
1171 typedef _BinClos<_Name, _ValArray, _ValArray, _Tp, _Tp> _Closure; \
1172 typedef typename __fun<_Name, _Tp>::result_type _Rt; \
1173 return _Expr<_Closure, _Rt>(_Closure(__v, __w)); \
1176 template<typename _Tp> \
1177 inline _Expr<_BinClos<_Name, _ValArray,_Constant, _Tp, _Tp>, \
1178 typename __fun<_Name, _Tp>::result_type> \
1179 operator _Op(const valarray<_Tp>& __v, \
1180 const typename valarray<_Tp>::value_type& __t) \
1182 typedef _BinClos<_Name, _ValArray, _Constant, _Tp, _Tp> _Closure; \
1183 typedef typename __fun<_Name, _Tp>::result_type _Rt; \
1184 return _Expr<_Closure, _Rt>(_Closure(__v, __t)); \
1187 template<typename _Tp> \
1188 inline _Expr<_BinClos<_Name, _Constant, _ValArray, _Tp, _Tp>, \
1189 typename __fun<_Name, _Tp>::result_type> \
1190 operator _Op(const typename valarray<_Tp>::value_type& __t, \
1191 const valarray<_Tp>& __v) \
1193 typedef _BinClos<_Name, _Constant, _ValArray, _Tp, _Tp> _Closure; \
1194 typedef typename __fun<_Name, _Tp>::result_type _Rt; \
1195 return _Expr<_Closure, _Rt>(_Closure(__t, __v)); \
1198_DEFINE_BINARY_OPERATOR(+, __plus)
1199_DEFINE_BINARY_OPERATOR(-, __minus)
1200_DEFINE_BINARY_OPERATOR(*, __multiplies)
1201_DEFINE_BINARY_OPERATOR(/, __divides)
1202_DEFINE_BINARY_OPERATOR(%, __modulus)
1203_DEFINE_BINARY_OPERATOR(^, __bitwise_xor)
1204_DEFINE_BINARY_OPERATOR(&, __bitwise_and)
1205_DEFINE_BINARY_OPERATOR(|, __bitwise_or)
1206_DEFINE_BINARY_OPERATOR(<<, __shift_left)
1207_DEFINE_BINARY_OPERATOR(>>, __shift_right)
1208_DEFINE_BINARY_OPERATOR(&&, __logical_and)
1209_DEFINE_BINARY_OPERATOR(||, __logical_or)
1210_DEFINE_BINARY_OPERATOR(==, __equal_to)
1211_DEFINE_BINARY_OPERATOR(!=, __not_equal_to)
1212_DEFINE_BINARY_OPERATOR(<, __less)
1213_DEFINE_BINARY_OPERATOR(>, __greater)
1214_DEFINE_BINARY_OPERATOR(<=, __less_equal)
1215_DEFINE_BINARY_OPERATOR(>=, __greater_equal)
1217#undef _DEFINE_BINARY_OPERATOR
1220#if __cplusplus >= 201103L
1222 * @brief Return an iterator pointing to the first element of
1224 * @param __va valarray.
1229 begin(valarray<_Tp>& __va) noexcept
1230 { return __va.size() ? std::__addressof(__va[0]) : nullptr; }
1233 * @brief Return an iterator pointing to the first element of
1234 * the const valarray.
1235 * @param __va valarray.
1240 begin(const valarray<_Tp>& __va) noexcept
1241 { return __va.size() ? std::__addressof(__va[0]) : nullptr; }
1244 * @brief Return an iterator pointing to one past the last element of
1246 * @param __va valarray.
1251 end(valarray<_Tp>& __va) noexcept
1253 if (auto __n = __va.size())
1254 return std::__addressof(__va[0]) + __n;
1260 * @brief Return an iterator pointing to one past the last element of
1261 * the const valarray.
1262 * @param __va valarray.
1267 end(const valarray<_Tp>& __va) noexcept
1269 if (auto __n = __va.size())
1270 return std::__addressof(__va[0]) + __n;
1276 /// @} group numeric_arrays
1278_GLIBCXX_END_NAMESPACE_VERSION
1281#endif /* _GLIBCXX_VALARRAY */