]>
Commit | Line | Data |
---|---|---|
54c1bf78 | 1 | // The template and inlines for the -*- C++ -*- valarray class. |
de96ac46 | 2 | |
8d9254fc | 3 | // Copyright (C) 1997-2020 Free Software Foundation, Inc. |
de96ac46 BK |
4 | // |
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 | |
748086b7 | 8 | // Free Software Foundation; either version 3, or (at your option) |
de96ac46 BK |
9 | // any later version. |
10 | ||
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. | |
15 | ||
748086b7 JJ |
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. | |
19 | ||
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/>. | |
de96ac46 | 24 | |
f910786b | 25 | /** @file include/valarray |
33ac58d5 | 26 | * This is a Standard C++ Library header. |
2f9d51b8 PE |
27 | */ |
28 | ||
143c27b0 BK |
29 | // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr> |
30 | ||
1143680e SE |
31 | #ifndef _GLIBCXX_VALARRAY |
32 | #define _GLIBCXX_VALARRAY 1 | |
54c1bf78 BK |
33 | |
34 | #pragma GCC system_header | |
35 | ||
36 | #include <bits/c++config.h> | |
54c1bf78 | 37 | #include <cmath> |
54c1bf78 | 38 | #include <algorithm> |
285b36d6 | 39 | #include <debug/debug.h> |
734f5023 | 40 | #if __cplusplus >= 201103L |
988499f4 | 41 | #include <initializer_list> |
a7d5d7e2 | 42 | #endif |
54c1bf78 | 43 | |
12ffa228 BK |
44 | namespace std _GLIBCXX_VISIBILITY(default) |
45 | { | |
46 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
3cbc7af0 | 47 | |
33ac58d5 | 48 | template<class _Clos, typename _Tp> |
74d6b8ca | 49 | class _Expr; |
54c1bf78 | 50 | |
33ac58d5 JW |
51 | template<typename _Tp1, typename _Tp2> |
52 | class _ValArray; | |
54c1bf78 | 53 | |
1b749ae9 JW |
54 | namespace __detail |
55 | { | |
74d6b8ca GDR |
56 | template<class _Oper, template<class, class> class _Meta, class _Dom> |
57 | struct _UnClos; | |
54c1bf78 | 58 | |
971cfc6f | 59 | template<class _Oper, |
54c1bf78 BK |
60 | template<class, class> class _Meta1, |
61 | template<class, class> class _Meta2, | |
33ac58d5 | 62 | class _Dom1, class _Dom2> |
74d6b8ca | 63 | class _BinClos; |
54c1bf78 | 64 | |
33ac58d5 | 65 | template<template<class, class> class _Meta, class _Dom> |
74d6b8ca | 66 | class _SClos; |
54c1bf78 | 67 | |
33ac58d5 | 68 | template<template<class, class> class _Meta, class _Dom> |
74d6b8ca | 69 | class _GClos; |
33ac58d5 JW |
70 | |
71 | template<template<class, class> class _Meta, class _Dom> | |
74d6b8ca | 72 | class _IClos; |
33ac58d5 JW |
73 | |
74 | template<template<class, class> class _Meta, class _Dom> | |
74d6b8ca | 75 | class _ValFunClos; |
33ac58d5 JW |
76 | |
77 | template<template<class, class> class _Meta, class _Dom> | |
74d6b8ca | 78 | class _RefFunClos; |
1b749ae9 JW |
79 | } // namespace __detail |
80 | ||
81 | using __detail::_UnClos; | |
82 | using __detail::_BinClos; | |
83 | using __detail::_SClos; | |
84 | using __detail::_GClos; | |
85 | using __detail::_IClos; | |
86 | using __detail::_ValFunClos; | |
87 | using __detail::_RefFunClos; | |
74d6b8ca | 88 | |
74d6b8ca GDR |
89 | template<class _Tp> class valarray; // An array of type _Tp |
90 | class slice; // BLAS-like slice out of an array | |
91 | template<class _Tp> class slice_array; | |
92 | class gslice; // generalized slice out of an array | |
93 | template<class _Tp> class gslice_array; | |
94 | template<class _Tp> class mask_array; // masked array | |
95 | template<class _Tp> class indirect_array; // indirected array | |
54c1bf78 | 96 | |
12ffa228 BK |
97 | _GLIBCXX_END_NAMESPACE_VERSION |
98 | } // namespace | |
54c1bf78 BK |
99 | |
100 | #include <bits/valarray_array.h> | |
c13bea50 | 101 | #include <bits/valarray_before.h> |
33ac58d5 | 102 | |
12ffa228 BK |
103 | namespace std _GLIBCXX_VISIBILITY(default) |
104 | { | |
105 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
3cbc7af0 | 106 | |
5b9daa7e BK |
107 | /** |
108 | * @defgroup numeric_arrays Numeric Arrays | |
109 | * @ingroup numerics | |
110 | * | |
111 | * Classes and functions for representing and manipulating arrays of elements. | |
112 | * @{ | |
113 | */ | |
114 | ||
7fb397a4 JQ |
115 | /** |
116 | * @brief Smart array designed to support numeric processing. | |
117 | * | |
118 | * A valarray is an array that provides constraints intended to allow for | |
119 | * effective optimization of numeric array processing by reducing the | |
120 | * aliasing that can result from pointer representations. It represents a | |
121 | * one-dimensional array from which different multidimensional subsets can | |
122 | * be accessed and modified. | |
33ac58d5 | 123 | * |
93c66bc6 | 124 | * @tparam _Tp Type of object in the array. |
7fb397a4 | 125 | */ |
33ac58d5 | 126 | template<class _Tp> |
74d6b8ca GDR |
127 | class valarray |
128 | { | |
129 | template<class _Op> | |
33ac58d5 | 130 | struct _UnaryOp |
74d6b8ca GDR |
131 | { |
132 | typedef typename __fun<_Op, _Tp>::result_type __rt; | |
133 | typedef _Expr<_UnClos<_Op, _ValArray, _Tp>, __rt> _Rt; | |
134 | }; | |
135 | public: | |
54c1bf78 | 136 | typedef _Tp value_type; |
33ac58d5 | 137 | |
74d6b8ca | 138 | // _lib.valarray.cons_ construct/destroy: |
7fb397a4 | 139 | /// Construct an empty array. |
54c1bf78 | 140 | valarray(); |
7fb397a4 JQ |
141 | |
142 | /// Construct an array with @a n elements. | |
54c1bf78 | 143 | explicit valarray(size_t); |
7fb397a4 JQ |
144 | |
145 | /// Construct an array with @a n elements initialized to @a t. | |
54c1bf78 | 146 | valarray(const _Tp&, size_t); |
7fb397a4 JQ |
147 | |
148 | /// Construct an array initialized to the first @a n elements of @a t. | |
54c1bf78 | 149 | valarray(const _Tp* __restrict__, size_t); |
7fb397a4 JQ |
150 | |
151 | /// Copy constructor. | |
54c1bf78 | 152 | valarray(const valarray&); |
7fb397a4 | 153 | |
734f5023 | 154 | #if __cplusplus >= 201103L |
8a3cabe3 PC |
155 | /// Move constructor. |
156 | valarray(valarray&&) noexcept; | |
157 | #endif | |
158 | ||
7fb397a4 | 159 | /// Construct an array with the same size and values in @a sa. |
54c1bf78 | 160 | valarray(const slice_array<_Tp>&); |
7fb397a4 JQ |
161 | |
162 | /// Construct an array with the same size and values in @a ga. | |
54c1bf78 | 163 | valarray(const gslice_array<_Tp>&); |
7fb397a4 JQ |
164 | |
165 | /// Construct an array with the same size and values in @a ma. | |
54c1bf78 | 166 | valarray(const mask_array<_Tp>&); |
7fb397a4 JQ |
167 | |
168 | /// Construct an array with the same size and values in @a ia. | |
54c1bf78 | 169 | valarray(const indirect_array<_Tp>&); |
7fb397a4 | 170 | |
734f5023 | 171 | #if __cplusplus >= 201103L |
988499f4 JM |
172 | /// Construct an array with an initializer_list of values. |
173 | valarray(initializer_list<_Tp>); | |
174 | #endif | |
175 | ||
54c1bf78 | 176 | template<class _Dom> |
b714a419 PC |
177 | valarray(const _Expr<_Dom, _Tp>& __e); |
178 | ||
6a97dbf7 | 179 | ~valarray() _GLIBCXX_NOEXCEPT; |
54c1bf78 BK |
180 | |
181 | // _lib.valarray.assign_ assignment: | |
7fb397a4 JQ |
182 | /** |
183 | * @brief Assign elements to an array. | |
184 | * | |
8a3cabe3 | 185 | * Assign elements of array to values in @a v. |
7fb397a4 | 186 | * |
93c66bc6 | 187 | * @param __v Valarray to get values from. |
7fb397a4 | 188 | */ |
93c66bc6 | 189 | valarray<_Tp>& operator=(const valarray<_Tp>& __v); |
7fb397a4 | 190 | |
734f5023 | 191 | #if __cplusplus >= 201103L |
8a3cabe3 PC |
192 | /** |
193 | * @brief Move assign elements to an array. | |
194 | * | |
195 | * Move assign elements of array to values in @a v. | |
196 | * | |
93c66bc6 | 197 | * @param __v Valarray to get values from. |
8a3cabe3 | 198 | */ |
93c66bc6 | 199 | valarray<_Tp>& operator=(valarray<_Tp>&& __v) noexcept; |
8a3cabe3 PC |
200 | #endif |
201 | ||
7fb397a4 JQ |
202 | /** |
203 | * @brief Assign elements to a value. | |
204 | * | |
205 | * Assign all elements of array to @a t. | |
206 | * | |
93c66bc6 | 207 | * @param __t Value for elements. |
7fb397a4 | 208 | */ |
93c66bc6 | 209 | valarray<_Tp>& operator=(const _Tp& __t); |
7fb397a4 JQ |
210 | |
211 | /** | |
212 | * @brief Assign elements to an array subset. | |
213 | * | |
214 | * Assign elements of array to values in @a sa. Results are undefined | |
dc5fef6a | 215 | * if @a sa does not have the same size as this array. |
7fb397a4 | 216 | * |
93c66bc6 | 217 | * @param __sa Array slice to get values from. |
7fb397a4 | 218 | */ |
93c66bc6 | 219 | valarray<_Tp>& operator=(const slice_array<_Tp>& __sa); |
7fb397a4 JQ |
220 | |
221 | /** | |
222 | * @brief Assign elements to an array subset. | |
223 | * | |
224 | * Assign elements of array to values in @a ga. Results are undefined | |
dc5fef6a | 225 | * if @a ga does not have the same size as this array. |
7fb397a4 | 226 | * |
93c66bc6 | 227 | * @param __ga Array slice to get values from. |
7fb397a4 | 228 | */ |
93c66bc6 | 229 | valarray<_Tp>& operator=(const gslice_array<_Tp>& __ga); |
7fb397a4 JQ |
230 | |
231 | /** | |
232 | * @brief Assign elements to an array subset. | |
233 | * | |
234 | * Assign elements of array to values in @a ma. Results are undefined | |
dc5fef6a | 235 | * if @a ma does not have the same size as this array. |
7fb397a4 | 236 | * |
93c66bc6 | 237 | * @param __ma Array slice to get values from. |
7fb397a4 | 238 | */ |
93c66bc6 | 239 | valarray<_Tp>& operator=(const mask_array<_Tp>& __ma); |
7fb397a4 JQ |
240 | |
241 | /** | |
242 | * @brief Assign elements to an array subset. | |
243 | * | |
244 | * Assign elements of array to values in @a ia. Results are undefined | |
dc5fef6a | 245 | * if @a ia does not have the same size as this array. |
7fb397a4 | 246 | * |
93c66bc6 | 247 | * @param __ia Array slice to get values from. |
7fb397a4 | 248 | */ |
93c66bc6 | 249 | valarray<_Tp>& operator=(const indirect_array<_Tp>& __ia); |
54c1bf78 | 250 | |
734f5023 | 251 | #if __cplusplus >= 201103L |
988499f4 JM |
252 | /** |
253 | * @brief Assign elements to an initializer_list. | |
254 | * | |
93c66bc6 BK |
255 | * Assign elements of array to values in @a __l. Results are undefined |
256 | * if @a __l does not have the same size as this array. | |
988499f4 | 257 | * |
93c66bc6 | 258 | * @param __l initializer_list to get values from. |
988499f4 | 259 | */ |
93c66bc6 | 260 | valarray& operator=(initializer_list<_Tp> __l); |
988499f4 JM |
261 | #endif |
262 | ||
54c1bf78 | 263 | template<class _Dom> valarray<_Tp>& |
b714a419 | 264 | operator= (const _Expr<_Dom, _Tp>&); |
54c1bf78 BK |
265 | |
266 | // _lib.valarray.access_ element access: | |
7fb397a4 | 267 | /** |
33ac58d5 | 268 | * Return a reference to the i'th array element. |
7fb397a4 | 269 | * |
93c66bc6 | 270 | * @param __i Index of element to return. |
7fb397a4 JQ |
271 | * @return Reference to the i'th element. |
272 | */ | |
93c66bc6 | 273 | _Tp& operator[](size_t __i); |
7fb397a4 | 274 | |
2787b59a PC |
275 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
276 | // 389. Const overload of valarray::operator[] returns by value. | |
277 | const _Tp& operator[](size_t) const; | |
7fb397a4 | 278 | |
54c1bf78 | 279 | // _lib.valarray.sub_ subset operations: |
7fb397a4 JQ |
280 | /** |
281 | * @brief Return an array subset. | |
282 | * | |
283 | * Returns a new valarray containing the elements of the array | |
dc5fef6a VR |
284 | * indicated by the slice argument. The new valarray has the same size |
285 | * as the input slice. @see slice. | |
7fb397a4 | 286 | * |
93c66bc6 BK |
287 | * @param __s The source slice. |
288 | * @return New valarray containing elements in @a __s. | |
7fb397a4 | 289 | */ |
93c66bc6 | 290 | _Expr<_SClos<_ValArray, _Tp>, _Tp> operator[](slice __s) const; |
7fb397a4 JQ |
291 | |
292 | /** | |
293 | * @brief Return a reference to an array subset. | |
294 | * | |
295 | * Returns a new valarray containing the elements of the array | |
dc5fef6a VR |
296 | * indicated by the slice argument. The new valarray has the same size |
297 | * as the input slice. @see slice. | |
7fb397a4 | 298 | * |
93c66bc6 BK |
299 | * @param __s The source slice. |
300 | * @return New valarray containing elements in @a __s. | |
7fb397a4 | 301 | */ |
93c66bc6 | 302 | slice_array<_Tp> operator[](slice __s); |
7fb397a4 JQ |
303 | |
304 | /** | |
305 | * @brief Return an array subset. | |
306 | * | |
307 | * Returns a slice_array referencing the elements of the array | |
308 | * indicated by the slice argument. @see gslice. | |
309 | * | |
93c66bc6 BK |
310 | * @param __s The source slice. |
311 | * @return Slice_array referencing elements indicated by @a __s. | |
7fb397a4 | 312 | */ |
93c66bc6 | 313 | _Expr<_GClos<_ValArray, _Tp>, _Tp> operator[](const gslice& __s) const; |
7fb397a4 JQ |
314 | |
315 | /** | |
316 | * @brief Return a reference to an array subset. | |
317 | * | |
318 | * Returns a new valarray containing the elements of the array | |
dc5fef6a VR |
319 | * indicated by the gslice argument. The new valarray has |
320 | * the same size as the input gslice. @see gslice. | |
7fb397a4 | 321 | * |
93c66bc6 BK |
322 | * @param __s The source gslice. |
323 | * @return New valarray containing elements in @a __s. | |
7fb397a4 | 324 | */ |
93c66bc6 | 325 | gslice_array<_Tp> operator[](const gslice& __s); |
7fb397a4 JQ |
326 | |
327 | /** | |
328 | * @brief Return an array subset. | |
329 | * | |
330 | * Returns a new valarray containing the elements of the array | |
331 | * indicated by the argument. The input is a valarray of bool which | |
332 | * represents a bitmask indicating which elements should be copied into | |
333 | * the new valarray. Each element of the array is added to the return | |
334 | * valarray if the corresponding element of the argument is true. | |
335 | * | |
93c66bc6 BK |
336 | * @param __m The valarray bitmask. |
337 | * @return New valarray containing elements indicated by @a __m. | |
7fb397a4 | 338 | */ |
93c66bc6 | 339 | valarray<_Tp> operator[](const valarray<bool>& __m) const; |
7fb397a4 JQ |
340 | |
341 | /** | |
342 | * @brief Return a reference to an array subset. | |
343 | * | |
344 | * Returns a new mask_array referencing the elements of the array | |
345 | * indicated by the argument. The input is a valarray of bool which | |
346 | * represents a bitmask indicating which elements are part of the | |
347 | * subset. Elements of the array are part of the subset if the | |
348 | * corresponding element of the argument is true. | |
349 | * | |
93c66bc6 BK |
350 | * @param __m The valarray bitmask. |
351 | * @return New valarray containing elements indicated by @a __m. | |
7fb397a4 | 352 | */ |
93c66bc6 | 353 | mask_array<_Tp> operator[](const valarray<bool>& __m); |
7fb397a4 JQ |
354 | |
355 | /** | |
356 | * @brief Return an array subset. | |
357 | * | |
358 | * Returns a new valarray containing the elements of the array | |
359 | * indicated by the argument. The elements in the argument are | |
360 | * interpreted as the indices of elements of this valarray to copy to | |
361 | * the return valarray. | |
362 | * | |
93c66bc6 BK |
363 | * @param __i The valarray element index list. |
364 | * @return New valarray containing elements in @a __s. | |
7fb397a4 | 365 | */ |
54c1bf78 | 366 | _Expr<_IClos<_ValArray, _Tp>, _Tp> |
93c66bc6 | 367 | operator[](const valarray<size_t>& __i) const; |
7fb397a4 JQ |
368 | |
369 | /** | |
370 | * @brief Return a reference to an array subset. | |
371 | * | |
372 | * Returns an indirect_array referencing the elements of the array | |
373 | * indicated by the argument. The elements in the argument are | |
374 | * interpreted as the indices of elements of this valarray to include | |
375 | * in the subset. The returned indirect_array refers to these | |
376 | * elements. | |
377 | * | |
93c66bc6 BK |
378 | * @param __i The valarray element index list. |
379 | * @return Indirect_array referencing elements in @a __i. | |
7fb397a4 | 380 | */ |
93c66bc6 | 381 | indirect_array<_Tp> operator[](const valarray<size_t>& __i); |
54c1bf78 BK |
382 | |
383 | // _lib.valarray.unary_ unary operators: | |
7fb397a4 | 384 | /// Return a new valarray by applying unary + to each element. |
74d6b8ca | 385 | typename _UnaryOp<__unary_plus>::_Rt operator+() const; |
7fb397a4 JQ |
386 | |
387 | /// Return a new valarray by applying unary - to each element. | |
74d6b8ca | 388 | typename _UnaryOp<__negate>::_Rt operator-() const; |
7fb397a4 JQ |
389 | |
390 | /// Return a new valarray by applying unary ~ to each element. | |
74d6b8ca | 391 | typename _UnaryOp<__bitwise_not>::_Rt operator~() const; |
7fb397a4 JQ |
392 | |
393 | /// Return a new valarray by applying unary ! to each element. | |
74d6b8ca GDR |
394 | typename _UnaryOp<__logical_not>::_Rt operator!() const; |
395 | ||
54c1bf78 | 396 | // _lib.valarray.cassign_ computed assignment: |
7fb397a4 | 397 | /// Multiply each element of array by @a t. |
74d6b8ca | 398 | valarray<_Tp>& operator*=(const _Tp&); |
7fb397a4 JQ |
399 | |
400 | /// Divide each element of array by @a t. | |
74d6b8ca | 401 | valarray<_Tp>& operator/=(const _Tp&); |
7fb397a4 JQ |
402 | |
403 | /// Set each element e of array to e % @a t. | |
74d6b8ca | 404 | valarray<_Tp>& operator%=(const _Tp&); |
7fb397a4 JQ |
405 | |
406 | /// Add @a t to each element of array. | |
74d6b8ca | 407 | valarray<_Tp>& operator+=(const _Tp&); |
7fb397a4 JQ |
408 | |
409 | /// Subtract @a t to each element of array. | |
74d6b8ca | 410 | valarray<_Tp>& operator-=(const _Tp&); |
7fb397a4 JQ |
411 | |
412 | /// Set each element e of array to e ^ @a t. | |
74d6b8ca | 413 | valarray<_Tp>& operator^=(const _Tp&); |
7fb397a4 JQ |
414 | |
415 | /// Set each element e of array to e & @a t. | |
74d6b8ca | 416 | valarray<_Tp>& operator&=(const _Tp&); |
7fb397a4 JQ |
417 | |
418 | /// Set each element e of array to e | @a t. | |
74d6b8ca | 419 | valarray<_Tp>& operator|=(const _Tp&); |
7fb397a4 JQ |
420 | |
421 | /// Left shift each element e of array by @a t bits. | |
54c1bf78 | 422 | valarray<_Tp>& operator<<=(const _Tp&); |
7fb397a4 JQ |
423 | |
424 | /// Right shift each element e of array by @a t bits. | |
54c1bf78 | 425 | valarray<_Tp>& operator>>=(const _Tp&); |
7fb397a4 JQ |
426 | |
427 | /// Multiply elements of array by corresponding elements of @a v. | |
74d6b8ca | 428 | valarray<_Tp>& operator*=(const valarray<_Tp>&); |
7fb397a4 JQ |
429 | |
430 | /// Divide elements of array by corresponding elements of @a v. | |
74d6b8ca | 431 | valarray<_Tp>& operator/=(const valarray<_Tp>&); |
7fb397a4 JQ |
432 | |
433 | /// Modulo elements of array by corresponding elements of @a v. | |
74d6b8ca | 434 | valarray<_Tp>& operator%=(const valarray<_Tp>&); |
7fb397a4 JQ |
435 | |
436 | /// Add corresponding elements of @a v to elements of array. | |
74d6b8ca | 437 | valarray<_Tp>& operator+=(const valarray<_Tp>&); |
7fb397a4 JQ |
438 | |
439 | /// Subtract corresponding elements of @a v from elements of array. | |
74d6b8ca | 440 | valarray<_Tp>& operator-=(const valarray<_Tp>&); |
7fb397a4 JQ |
441 | |
442 | /// Logical xor corresponding elements of @a v with elements of array. | |
74d6b8ca | 443 | valarray<_Tp>& operator^=(const valarray<_Tp>&); |
7fb397a4 JQ |
444 | |
445 | /// Logical or corresponding elements of @a v with elements of array. | |
74d6b8ca | 446 | valarray<_Tp>& operator|=(const valarray<_Tp>&); |
7fb397a4 JQ |
447 | |
448 | /// Logical and corresponding elements of @a v with elements of array. | |
74d6b8ca | 449 | valarray<_Tp>& operator&=(const valarray<_Tp>&); |
7fb397a4 JQ |
450 | |
451 | /// Left shift elements of array by corresponding elements of @a v. | |
54c1bf78 | 452 | valarray<_Tp>& operator<<=(const valarray<_Tp>&); |
7fb397a4 JQ |
453 | |
454 | /// Right shift elements of array by corresponding elements of @a v. | |
54c1bf78 BK |
455 | valarray<_Tp>& operator>>=(const valarray<_Tp>&); |
456 | ||
457 | template<class _Dom> | |
b714a419 | 458 | valarray<_Tp>& operator*=(const _Expr<_Dom, _Tp>&); |
54c1bf78 | 459 | template<class _Dom> |
b714a419 | 460 | valarray<_Tp>& operator/=(const _Expr<_Dom, _Tp>&); |
54c1bf78 | 461 | template<class _Dom> |
b714a419 | 462 | valarray<_Tp>& operator%=(const _Expr<_Dom, _Tp>&); |
54c1bf78 | 463 | template<class _Dom> |
b714a419 | 464 | valarray<_Tp>& operator+=(const _Expr<_Dom, _Tp>&); |
54c1bf78 | 465 | template<class _Dom> |
b714a419 | 466 | valarray<_Tp>& operator-=(const _Expr<_Dom, _Tp>&); |
54c1bf78 | 467 | template<class _Dom> |
b714a419 | 468 | valarray<_Tp>& operator^=(const _Expr<_Dom, _Tp>&); |
54c1bf78 | 469 | template<class _Dom> |
b714a419 | 470 | valarray<_Tp>& operator|=(const _Expr<_Dom, _Tp>&); |
54c1bf78 | 471 | template<class _Dom> |
b714a419 | 472 | valarray<_Tp>& operator&=(const _Expr<_Dom, _Tp>&); |
54c1bf78 | 473 | template<class _Dom> |
b714a419 | 474 | valarray<_Tp>& operator<<=(const _Expr<_Dom, _Tp>&); |
54c1bf78 | 475 | template<class _Dom> |
b714a419 | 476 | valarray<_Tp>& operator>>=(const _Expr<_Dom, _Tp>&); |
54c1bf78 | 477 | |
54c1bf78 | 478 | // _lib.valarray.members_ member functions: |
734f5023 | 479 | #if __cplusplus >= 201103L |
8a3cabe3 PC |
480 | /// Swap. |
481 | void swap(valarray<_Tp>& __v) noexcept; | |
482 | #endif | |
483 | ||
7fb397a4 | 484 | /// Return the number of elements in array. |
54c1bf78 | 485 | size_t size() const; |
7fb397a4 JQ |
486 | |
487 | /** | |
488 | * @brief Return the sum of all elements in the array. | |
489 | * | |
490 | * Accumulates the sum of all elements into a Tp using +=. The order | |
491 | * of adding the elements is unspecified. | |
492 | */ | |
493 | _Tp sum() const; | |
494 | ||
495 | /// Return the minimum element using operator<(). | |
33ac58d5 | 496 | _Tp min() const; |
7fb397a4 JQ |
497 | |
498 | /// Return the maximum element using operator<(). | |
33ac58d5 | 499 | _Tp max() const; |
54c1bf78 | 500 | |
7fb397a4 JQ |
501 | /** |
502 | * @brief Return a shifted array. | |
503 | * | |
504 | * A new valarray is constructed as a copy of this array with elements | |
505 | * in shifted positions. For an element with index i, the new position | |
dc5fef6a VR |
506 | * is i - n. The new valarray has the same size as the current one. |
507 | * New elements without a value are set to 0. Elements whose new | |
7fb397a4 JQ |
508 | * position is outside the bounds of the array are discarded. |
509 | * | |
510 | * Positive arguments shift toward index 0, discarding elements [0, n). | |
511 | * Negative arguments discard elements from the top of the array. | |
512 | * | |
93c66bc6 | 513 | * @param __n Number of element positions to shift. |
7fb397a4 JQ |
514 | * @return New valarray with elements in shifted positions. |
515 | */ | |
93c66bc6 | 516 | valarray<_Tp> shift (int __n) const; |
7fb397a4 JQ |
517 | |
518 | /** | |
519 | * @brief Return a rotated array. | |
520 | * | |
521 | * A new valarray is constructed as a copy of this array with elements | |
522 | * in shifted positions. For an element with index i, the new position | |
dc5fef6a | 523 | * is (i - n) % size(). The new valarray has the same size as the |
7fb397a4 JQ |
524 | * current one. Elements that are shifted beyond the array bounds are |
525 | * shifted into the other end of the array. No elements are lost. | |
526 | * | |
527 | * Positive arguments shift toward index 0, wrapping around the top. | |
528 | * Negative arguments shift towards the top, wrapping around to 0. | |
529 | * | |
93c66bc6 | 530 | * @param __n Number of element positions to rotate. |
7fb397a4 JQ |
531 | * @return New valarray with elements in shifted positions. |
532 | */ | |
93c66bc6 | 533 | valarray<_Tp> cshift(int __n) const; |
7fb397a4 JQ |
534 | |
535 | /** | |
536 | * @brief Apply a function to the array. | |
537 | * | |
538 | * Returns a new valarray with elements assigned to the result of | |
539 | * applying func to the corresponding element of this array. The new | |
dc5fef6a | 540 | * array has the same size as this one. |
7fb397a4 JQ |
541 | * |
542 | * @param func Function of Tp returning Tp to apply. | |
543 | * @return New valarray with transformed elements. | |
544 | */ | |
b714a419 | 545 | _Expr<_ValFunClos<_ValArray, _Tp>, _Tp> apply(_Tp func(_Tp)) const; |
7fb397a4 JQ |
546 | |
547 | /** | |
548 | * @brief Apply a function to the array. | |
549 | * | |
550 | * Returns a new valarray with elements assigned to the result of | |
551 | * applying func to the corresponding element of this array. The new | |
dc5fef6a | 552 | * array has the same size as this one. |
7fb397a4 JQ |
553 | * |
554 | * @param func Function of const Tp& returning Tp to apply. | |
555 | * @return New valarray with transformed elements. | |
556 | */ | |
b714a419 | 557 | _Expr<_RefFunClos<_ValArray, _Tp>, _Tp> apply(_Tp func(const _Tp&)) const; |
7fb397a4 JQ |
558 | |
559 | /** | |
560 | * @brief Resize array. | |
561 | * | |
dc5fef6a | 562 | * Resize this array to @a size and set all elements to @a c. All |
7fb397a4 JQ |
563 | * references and iterators are invalidated. |
564 | * | |
93c66bc6 BK |
565 | * @param __size New array size. |
566 | * @param __c New value for all elements. | |
7fb397a4 | 567 | */ |
54c1bf78 BK |
568 | void resize(size_t __size, _Tp __c = _Tp()); |
569 | ||
74d6b8ca | 570 | private: |
54c1bf78 BK |
571 | size_t _M_size; |
572 | _Tp* __restrict__ _M_data; | |
33ac58d5 | 573 | |
54c1bf78 | 574 | friend class _Array<_Tp>; |
74d6b8ca | 575 | }; |
33ac58d5 | 576 | |
af181c91 JW |
577 | #if __cpp_deduction_guides >= 201606 |
578 | template<typename _Tp, size_t _Nm> | |
579 | valarray(const _Tp(&)[_Nm], size_t) -> valarray<_Tp>; | |
580 | #endif | |
581 | ||
54c1bf78 | 582 | template<typename _Tp> |
74d6b8ca GDR |
583 | inline const _Tp& |
584 | valarray<_Tp>::operator[](size_t __i) const | |
33ac58d5 | 585 | { |
285b36d6 | 586 | __glibcxx_requires_subscript(__i); |
b714a419 | 587 | return _M_data[__i]; |
285b36d6 | 588 | } |
54c1bf78 BK |
589 | |
590 | template<typename _Tp> | |
74d6b8ca GDR |
591 | inline _Tp& |
592 | valarray<_Tp>::operator[](size_t __i) | |
33ac58d5 | 593 | { |
285b36d6 | 594 | __glibcxx_requires_subscript(__i); |
b714a419 | 595 | return _M_data[__i]; |
285b36d6 | 596 | } |
54c1bf78 | 597 | |
5b9daa7e BK |
598 | // @} group numeric_arrays |
599 | ||
12ffa228 BK |
600 | _GLIBCXX_END_NAMESPACE_VERSION |
601 | } // namespace | |
c13bea50 NS |
602 | |
603 | #include <bits/valarray_after.h> | |
54c1bf78 BK |
604 | #include <bits/slice_array.h> |
605 | #include <bits/gslice.h> | |
606 | #include <bits/gslice_array.h> | |
607 | #include <bits/mask_array.h> | |
608 | #include <bits/indirect_array.h> | |
609 | ||
12ffa228 BK |
610 | namespace std _GLIBCXX_VISIBILITY(default) |
611 | { | |
612 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
3cbc7af0 | 613 | |
5b9daa7e BK |
614 | /** |
615 | * @addtogroup numeric_arrays | |
616 | * @{ | |
617 | */ | |
618 | ||
54c1bf78 | 619 | template<typename _Tp> |
74d6b8ca GDR |
620 | inline |
621 | valarray<_Tp>::valarray() : _M_size(0), _M_data(0) {} | |
54c1bf78 BK |
622 | |
623 | template<typename _Tp> | |
33ac58d5 JW |
624 | inline |
625 | valarray<_Tp>::valarray(size_t __n) | |
b714a419 | 626 | : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n)) |
eb9a4231 | 627 | { std::__valarray_default_construct(_M_data, _M_data + __n); } |
54c1bf78 BK |
628 | |
629 | template<typename _Tp> | |
74d6b8ca GDR |
630 | inline |
631 | valarray<_Tp>::valarray(const _Tp& __t, size_t __n) | |
b714a419 | 632 | : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n)) |
eb9a4231 | 633 | { std::__valarray_fill_construct(_M_data, _M_data + __n, __t); } |
54c1bf78 BK |
634 | |
635 | template<typename _Tp> | |
74d6b8ca GDR |
636 | inline |
637 | valarray<_Tp>::valarray(const _Tp* __restrict__ __p, size_t __n) | |
b714a419 | 638 | : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n)) |
33ac58d5 | 639 | { |
2f1e8e7c | 640 | __glibcxx_assert(__p != 0 || __n == 0); |
33ac58d5 | 641 | std::__valarray_copy_construct(__p, __p + __n, _M_data); |
285b36d6 | 642 | } |
54c1bf78 BK |
643 | |
644 | template<typename _Tp> | |
74d6b8ca GDR |
645 | inline |
646 | valarray<_Tp>::valarray(const valarray<_Tp>& __v) | |
b714a419 PC |
647 | : _M_size(__v._M_size), _M_data(__valarray_get_storage<_Tp>(__v._M_size)) |
648 | { std::__valarray_copy_construct(__v._M_data, __v._M_data + _M_size, | |
649 | _M_data); } | |
54c1bf78 | 650 | |
734f5023 | 651 | #if __cplusplus >= 201103L |
8a3cabe3 PC |
652 | template<typename _Tp> |
653 | inline | |
654 | valarray<_Tp>::valarray(valarray<_Tp>&& __v) noexcept | |
655 | : _M_size(__v._M_size), _M_data(__v._M_data) | |
656 | { | |
657 | __v._M_size = 0; | |
658 | __v._M_data = 0; | |
659 | } | |
660 | #endif | |
661 | ||
54c1bf78 | 662 | template<typename _Tp> |
74d6b8ca GDR |
663 | inline |
664 | valarray<_Tp>::valarray(const slice_array<_Tp>& __sa) | |
b714a419 | 665 | : _M_size(__sa._M_sz), _M_data(__valarray_get_storage<_Tp>(__sa._M_sz)) |
74d6b8ca | 666 | { |
6085dc49 | 667 | std::__valarray_copy_construct |
74d6b8ca GDR |
668 | (__sa._M_array, __sa._M_sz, __sa._M_stride, _Array<_Tp>(_M_data)); |
669 | } | |
54c1bf78 BK |
670 | |
671 | template<typename _Tp> | |
74d6b8ca GDR |
672 | inline |
673 | valarray<_Tp>::valarray(const gslice_array<_Tp>& __ga) | |
b714a419 PC |
674 | : _M_size(__ga._M_index.size()), |
675 | _M_data(__valarray_get_storage<_Tp>(_M_size)) | |
74d6b8ca | 676 | { |
6085dc49 | 677 | std::__valarray_copy_construct |
74d6b8ca GDR |
678 | (__ga._M_array, _Array<size_t>(__ga._M_index), |
679 | _Array<_Tp>(_M_data), _M_size); | |
680 | } | |
54c1bf78 BK |
681 | |
682 | template<typename _Tp> | |
74d6b8ca GDR |
683 | inline |
684 | valarray<_Tp>::valarray(const mask_array<_Tp>& __ma) | |
b714a419 | 685 | : _M_size(__ma._M_sz), _M_data(__valarray_get_storage<_Tp>(__ma._M_sz)) |
74d6b8ca | 686 | { |
6085dc49 | 687 | std::__valarray_copy_construct |
74d6b8ca GDR |
688 | (__ma._M_array, __ma._M_mask, _Array<_Tp>(_M_data), _M_size); |
689 | } | |
54c1bf78 BK |
690 | |
691 | template<typename _Tp> | |
74d6b8ca GDR |
692 | inline |
693 | valarray<_Tp>::valarray(const indirect_array<_Tp>& __ia) | |
b714a419 | 694 | : _M_size(__ia._M_sz), _M_data(__valarray_get_storage<_Tp>(__ia._M_sz)) |
74d6b8ca | 695 | { |
6085dc49 | 696 | std::__valarray_copy_construct |
74d6b8ca GDR |
697 | (__ia._M_array, __ia._M_index, _Array<_Tp>(_M_data), _M_size); |
698 | } | |
54c1bf78 | 699 | |
734f5023 | 700 | #if __cplusplus >= 201103L |
988499f4 JM |
701 | template<typename _Tp> |
702 | inline | |
703 | valarray<_Tp>::valarray(initializer_list<_Tp> __l) | |
af4beb4b | 704 | : _M_size(__l.size()), _M_data(__valarray_get_storage<_Tp>(__l.size())) |
8a3cabe3 | 705 | { std::__valarray_copy_construct(__l.begin(), __l.end(), _M_data); } |
988499f4 JM |
706 | #endif |
707 | ||
54c1bf78 | 708 | template<typename _Tp> template<class _Dom> |
74d6b8ca GDR |
709 | inline |
710 | valarray<_Tp>::valarray(const _Expr<_Dom, _Tp>& __e) | |
b714a419 | 711 | : _M_size(__e.size()), _M_data(__valarray_get_storage<_Tp>(_M_size)) |
6085dc49 | 712 | { std::__valarray_copy_construct(__e, _M_size, _Array<_Tp>(_M_data)); } |
54c1bf78 BK |
713 | |
714 | template<typename _Tp> | |
74d6b8ca | 715 | inline |
6a97dbf7 | 716 | valarray<_Tp>::~valarray() _GLIBCXX_NOEXCEPT |
74d6b8ca | 717 | { |
eb9a4231 PC |
718 | std::__valarray_destroy_elements(_M_data, _M_data + _M_size); |
719 | std::__valarray_release_memory(_M_data); | |
74d6b8ca | 720 | } |
54c1bf78 BK |
721 | |
722 | template<typename _Tp> | |
74d6b8ca GDR |
723 | inline valarray<_Tp>& |
724 | valarray<_Tp>::operator=(const valarray<_Tp>& __v) | |
725 | { | |
af4beb4b PC |
726 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
727 | // 630. arrays of valarray. | |
728 | if (_M_size == __v._M_size) | |
729 | std::__valarray_copy(__v._M_data, _M_size, _M_data); | |
730 | else | |
731 | { | |
732 | if (_M_data) | |
733 | { | |
734 | std::__valarray_destroy_elements(_M_data, _M_data + _M_size); | |
735 | std::__valarray_release_memory(_M_data); | |
736 | } | |
737 | _M_size = __v._M_size; | |
738 | _M_data = __valarray_get_storage<_Tp>(_M_size); | |
739 | std::__valarray_copy_construct(__v._M_data, __v._M_data + _M_size, | |
740 | _M_data); | |
741 | } | |
54c1bf78 | 742 | return *this; |
74d6b8ca | 743 | } |
54c1bf78 | 744 | |
734f5023 | 745 | #if __cplusplus >= 201103L |
8a3cabe3 PC |
746 | template<typename _Tp> |
747 | inline valarray<_Tp>& | |
748 | valarray<_Tp>::operator=(valarray<_Tp>&& __v) noexcept | |
749 | { | |
750 | if (_M_data) | |
751 | { | |
752 | std::__valarray_destroy_elements(_M_data, _M_data + _M_size); | |
753 | std::__valarray_release_memory(_M_data); | |
754 | } | |
755 | _M_size = __v._M_size; | |
756 | _M_data = __v._M_data; | |
757 | __v._M_size = 0; | |
758 | __v._M_data = 0; | |
759 | return *this; | |
760 | } | |
761 | ||
988499f4 JM |
762 | template<typename _Tp> |
763 | inline valarray<_Tp>& | |
764 | valarray<_Tp>::operator=(initializer_list<_Tp> __l) | |
765 | { | |
af4beb4b PC |
766 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
767 | // 630. arrays of valarray. | |
768 | if (_M_size == __l.size()) | |
769 | std::__valarray_copy(__l.begin(), __l.size(), _M_data); | |
770 | else | |
771 | { | |
772 | if (_M_data) | |
773 | { | |
774 | std::__valarray_destroy_elements(_M_data, _M_data + _M_size); | |
775 | std::__valarray_release_memory(_M_data); | |
776 | } | |
777 | _M_size = __l.size(); | |
778 | _M_data = __valarray_get_storage<_Tp>(_M_size); | |
779 | std::__valarray_copy_construct(__l.begin(), __l.begin() + _M_size, | |
780 | _M_data); | |
781 | } | |
782 | return *this; | |
988499f4 JM |
783 | } |
784 | #endif | |
785 | ||
54c1bf78 | 786 | template<typename _Tp> |
74d6b8ca GDR |
787 | inline valarray<_Tp>& |
788 | valarray<_Tp>::operator=(const _Tp& __t) | |
789 | { | |
eb9a4231 | 790 | std::__valarray_fill(_M_data, _M_size, __t); |
54c1bf78 | 791 | return *this; |
74d6b8ca | 792 | } |
54c1bf78 BK |
793 | |
794 | template<typename _Tp> | |
74d6b8ca GDR |
795 | inline valarray<_Tp>& |
796 | valarray<_Tp>::operator=(const slice_array<_Tp>& __sa) | |
797 | { | |
2f1e8e7c | 798 | __glibcxx_assert(_M_size == __sa._M_sz); |
eb9a4231 PC |
799 | std::__valarray_copy(__sa._M_array, __sa._M_sz, |
800 | __sa._M_stride, _Array<_Tp>(_M_data)); | |
54c1bf78 | 801 | return *this; |
74d6b8ca | 802 | } |
54c1bf78 BK |
803 | |
804 | template<typename _Tp> | |
74d6b8ca GDR |
805 | inline valarray<_Tp>& |
806 | valarray<_Tp>::operator=(const gslice_array<_Tp>& __ga) | |
807 | { | |
2f1e8e7c | 808 | __glibcxx_assert(_M_size == __ga._M_index.size()); |
eb9a4231 PC |
809 | std::__valarray_copy(__ga._M_array, _Array<size_t>(__ga._M_index), |
810 | _Array<_Tp>(_M_data), _M_size); | |
54c1bf78 | 811 | return *this; |
74d6b8ca | 812 | } |
54c1bf78 BK |
813 | |
814 | template<typename _Tp> | |
74d6b8ca GDR |
815 | inline valarray<_Tp>& |
816 | valarray<_Tp>::operator=(const mask_array<_Tp>& __ma) | |
817 | { | |
2f1e8e7c | 818 | __glibcxx_assert(_M_size == __ma._M_sz); |
eb9a4231 PC |
819 | std::__valarray_copy(__ma._M_array, __ma._M_mask, |
820 | _Array<_Tp>(_M_data), _M_size); | |
54c1bf78 | 821 | return *this; |
74d6b8ca | 822 | } |
54c1bf78 BK |
823 | |
824 | template<typename _Tp> | |
74d6b8ca GDR |
825 | inline valarray<_Tp>& |
826 | valarray<_Tp>::operator=(const indirect_array<_Tp>& __ia) | |
827 | { | |
2f1e8e7c | 828 | __glibcxx_assert(_M_size == __ia._M_sz); |
eb9a4231 PC |
829 | std::__valarray_copy(__ia._M_array, __ia._M_index, |
830 | _Array<_Tp>(_M_data), _M_size); | |
54c1bf78 | 831 | return *this; |
74d6b8ca | 832 | } |
54c1bf78 BK |
833 | |
834 | template<typename _Tp> template<class _Dom> | |
74d6b8ca GDR |
835 | inline valarray<_Tp>& |
836 | valarray<_Tp>::operator=(const _Expr<_Dom, _Tp>& __e) | |
837 | { | |
b0ad3635 PC |
838 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
839 | // 630. arrays of valarray. | |
840 | if (_M_size == __e.size()) | |
841 | std::__valarray_copy(__e, _M_size, _Array<_Tp>(_M_data)); | |
842 | else | |
843 | { | |
844 | if (_M_data) | |
845 | { | |
846 | std::__valarray_destroy_elements(_M_data, _M_data + _M_size); | |
847 | std::__valarray_release_memory(_M_data); | |
848 | } | |
849 | _M_size = __e.size(); | |
850 | _M_data = __valarray_get_storage<_Tp>(_M_size); | |
851 | std::__valarray_copy_construct(__e, _M_size, _Array<_Tp>(_M_data)); | |
852 | } | |
eb9a4231 | 853 | return *this; |
74d6b8ca | 854 | } |
54c1bf78 BK |
855 | |
856 | template<typename _Tp> | |
74d6b8ca GDR |
857 | inline _Expr<_SClos<_ValArray,_Tp>, _Tp> |
858 | valarray<_Tp>::operator[](slice __s) const | |
859 | { | |
54c1bf78 | 860 | typedef _SClos<_ValArray,_Tp> _Closure; |
74d6b8ca GDR |
861 | return _Expr<_Closure, _Tp>(_Closure (_Array<_Tp>(_M_data), __s)); |
862 | } | |
54c1bf78 BK |
863 | |
864 | template<typename _Tp> | |
74d6b8ca GDR |
865 | inline slice_array<_Tp> |
866 | valarray<_Tp>::operator[](slice __s) | |
b714a419 | 867 | { return slice_array<_Tp>(_Array<_Tp>(_M_data), __s); } |
54c1bf78 BK |
868 | |
869 | template<typename _Tp> | |
74d6b8ca GDR |
870 | inline _Expr<_GClos<_ValArray,_Tp>, _Tp> |
871 | valarray<_Tp>::operator[](const gslice& __gs) const | |
872 | { | |
54c1bf78 BK |
873 | typedef _GClos<_ValArray,_Tp> _Closure; |
874 | return _Expr<_Closure, _Tp> | |
74d6b8ca GDR |
875 | (_Closure(_Array<_Tp>(_M_data), __gs._M_index->_M_index)); |
876 | } | |
54c1bf78 BK |
877 | |
878 | template<typename _Tp> | |
74d6b8ca GDR |
879 | inline gslice_array<_Tp> |
880 | valarray<_Tp>::operator[](const gslice& __gs) | |
881 | { | |
54c1bf78 | 882 | return gslice_array<_Tp> |
74d6b8ca GDR |
883 | (_Array<_Tp>(_M_data), __gs._M_index->_M_index); |
884 | } | |
54c1bf78 BK |
885 | |
886 | template<typename _Tp> | |
74d6b8ca GDR |
887 | inline valarray<_Tp> |
888 | valarray<_Tp>::operator[](const valarray<bool>& __m) const | |
889 | { | |
890 | size_t __s = 0; | |
891 | size_t __e = __m.size(); | |
54c1bf78 | 892 | for (size_t __i=0; __i<__e; ++__i) |
74d6b8ca GDR |
893 | if (__m[__i]) ++__s; |
894 | return valarray<_Tp>(mask_array<_Tp>(_Array<_Tp>(_M_data), __s, | |
895 | _Array<bool> (__m))); | |
896 | } | |
54c1bf78 BK |
897 | |
898 | template<typename _Tp> | |
74d6b8ca GDR |
899 | inline mask_array<_Tp> |
900 | valarray<_Tp>::operator[](const valarray<bool>& __m) | |
901 | { | |
902 | size_t __s = 0; | |
903 | size_t __e = __m.size(); | |
54c1bf78 | 904 | for (size_t __i=0; __i<__e; ++__i) |
74d6b8ca GDR |
905 | if (__m[__i]) ++__s; |
906 | return mask_array<_Tp>(_Array<_Tp>(_M_data), __s, _Array<bool>(__m)); | |
907 | } | |
54c1bf78 BK |
908 | |
909 | template<typename _Tp> | |
74d6b8ca GDR |
910 | inline _Expr<_IClos<_ValArray,_Tp>, _Tp> |
911 | valarray<_Tp>::operator[](const valarray<size_t>& __i) const | |
912 | { | |
54c1bf78 | 913 | typedef _IClos<_ValArray,_Tp> _Closure; |
74d6b8ca GDR |
914 | return _Expr<_Closure, _Tp>(_Closure(*this, __i)); |
915 | } | |
54c1bf78 BK |
916 | |
917 | template<typename _Tp> | |
74d6b8ca GDR |
918 | inline indirect_array<_Tp> |
919 | valarray<_Tp>::operator[](const valarray<size_t>& __i) | |
920 | { | |
921 | return indirect_array<_Tp>(_Array<_Tp>(_M_data), __i.size(), | |
922 | _Array<size_t>(__i)); | |
923 | } | |
54c1bf78 | 924 | |
734f5023 | 925 | #if __cplusplus >= 201103L |
8a3cabe3 PC |
926 | template<class _Tp> |
927 | inline void | |
928 | valarray<_Tp>::swap(valarray<_Tp>& __v) noexcept | |
929 | { | |
930 | std::swap(_M_size, __v._M_size); | |
931 | std::swap(_M_data, __v._M_data); | |
932 | } | |
933 | #endif | |
934 | ||
54c1bf78 | 935 | template<class _Tp> |
33ac58d5 | 936 | inline size_t |
74d6b8ca GDR |
937 | valarray<_Tp>::size() const |
938 | { return _M_size; } | |
54c1bf78 BK |
939 | |
940 | template<class _Tp> | |
74d6b8ca GDR |
941 | inline _Tp |
942 | valarray<_Tp>::sum() const | |
943 | { | |
2f1e8e7c | 944 | __glibcxx_assert(_M_size > 0); |
eb9a4231 | 945 | return std::__valarray_sum(_M_data, _M_data + _M_size); |
74d6b8ca | 946 | } |
54c1bf78 | 947 | |
22d67c60 | 948 | template<class _Tp> |
54c1bf78 BK |
949 | inline valarray<_Tp> |
950 | valarray<_Tp>::shift(int __n) const | |
951 | { | |
22d67c60 | 952 | valarray<_Tp> __ret; |
102693c7 PC |
953 | |
954 | if (_M_size == 0) | |
955 | return __ret; | |
956 | ||
22d67c60 PC |
957 | _Tp* __restrict__ __tmp_M_data = |
958 | std::__valarray_get_storage<_Tp>(_M_size); | |
959 | ||
102693c7 PC |
960 | if (__n == 0) |
961 | std::__valarray_copy_construct(_M_data, | |
962 | _M_data + _M_size, __tmp_M_data); | |
963 | else if (__n > 0) // shift left | |
8e768214 PC |
964 | { |
965 | if (size_t(__n) > _M_size) | |
226a2e08 | 966 | __n = int(_M_size); |
8e768214 PC |
967 | |
968 | std::__valarray_copy_construct(_M_data + __n, | |
969 | _M_data + _M_size, __tmp_M_data); | |
970 | std::__valarray_default_construct(__tmp_M_data + _M_size - __n, | |
971 | __tmp_M_data + _M_size); | |
972 | } | |
102693c7 | 973 | else // shift right |
8e768214 | 974 | { |
226a2e08 PC |
975 | if (-size_t(__n) > _M_size) |
976 | __n = -int(_M_size); | |
8e768214 PC |
977 | |
978 | std::__valarray_copy_construct(_M_data, _M_data + _M_size + __n, | |
22d67c60 | 979 | __tmp_M_data - __n); |
8e768214 | 980 | std::__valarray_default_construct(__tmp_M_data, |
22d67c60 | 981 | __tmp_M_data - __n); |
8e768214 | 982 | } |
22d67c60 PC |
983 | |
984 | __ret._M_size = _M_size; | |
985 | __ret._M_data = __tmp_M_data; | |
986 | return __ret; | |
54c1bf78 BK |
987 | } |
988 | ||
22d67c60 | 989 | template<class _Tp> |
54c1bf78 | 990 | inline valarray<_Tp> |
22d67c60 | 991 | valarray<_Tp>::cshift(int __n) const |
54c1bf78 | 992 | { |
22d67c60 | 993 | valarray<_Tp> __ret; |
102693c7 PC |
994 | |
995 | if (_M_size == 0) | |
996 | return __ret; | |
997 | ||
22d67c60 PC |
998 | _Tp* __restrict__ __tmp_M_data = |
999 | std::__valarray_get_storage<_Tp>(_M_size); | |
102693c7 PC |
1000 | |
1001 | if (__n == 0) | |
1002 | std::__valarray_copy_construct(_M_data, | |
1003 | _M_data + _M_size, __tmp_M_data); | |
1004 | else if (__n > 0) // cshift left | |
8e768214 PC |
1005 | { |
1006 | if (size_t(__n) > _M_size) | |
226a2e08 | 1007 | __n = int(__n % _M_size); |
22d67c60 | 1008 | |
8e768214 | 1009 | std::__valarray_copy_construct(_M_data, _M_data + __n, |
22d67c60 | 1010 | __tmp_M_data + _M_size - __n); |
8e768214 | 1011 | std::__valarray_copy_construct(_M_data + __n, _M_data + _M_size, |
22d67c60 | 1012 | __tmp_M_data); |
8e768214 | 1013 | } |
102693c7 | 1014 | else // cshift right |
8e768214 | 1015 | { |
226a2e08 PC |
1016 | if (-size_t(__n) > _M_size) |
1017 | __n = -int(-size_t(__n) % _M_size); | |
8e768214 PC |
1018 | |
1019 | std::__valarray_copy_construct(_M_data + _M_size + __n, | |
1020 | _M_data + _M_size, __tmp_M_data); | |
1021 | std::__valarray_copy_construct(_M_data, _M_data + _M_size + __n, | |
1022 | __tmp_M_data - __n); | |
1023 | } | |
22d67c60 PC |
1024 | |
1025 | __ret._M_size = _M_size; | |
1026 | __ret._M_data = __tmp_M_data; | |
1027 | return __ret; | |
54c1bf78 BK |
1028 | } |
1029 | ||
22d67c60 | 1030 | template<class _Tp> |
74d6b8ca | 1031 | inline void |
22d67c60 | 1032 | valarray<_Tp>::resize(size_t __n, _Tp __c) |
74d6b8ca GDR |
1033 | { |
1034 | // This complication is so to make valarray<valarray<T> > work | |
1035 | // even though it is not required by the standard. Nobody should | |
1036 | // be saying valarray<valarray<T> > anyway. See the specs. | |
eb9a4231 | 1037 | std::__valarray_destroy_elements(_M_data, _M_data + _M_size); |
74d6b8ca GDR |
1038 | if (_M_size != __n) |
1039 | { | |
eb9a4231 | 1040 | std::__valarray_release_memory(_M_data); |
74d6b8ca GDR |
1041 | _M_size = __n; |
1042 | _M_data = __valarray_get_storage<_Tp>(__n); | |
1043 | } | |
eb9a4231 | 1044 | std::__valarray_fill_construct(_M_data, _M_data + __n, __c); |
74d6b8ca | 1045 | } |
33ac58d5 | 1046 | |
54c1bf78 | 1047 | template<typename _Tp> |
74d6b8ca GDR |
1048 | inline _Tp |
1049 | valarray<_Tp>::min() const | |
1050 | { | |
2f1e8e7c | 1051 | __glibcxx_assert(_M_size > 0); |
65be6ddd | 1052 | return *std::min_element(_M_data, _M_data + _M_size); |
74d6b8ca | 1053 | } |
54c1bf78 BK |
1054 | |
1055 | template<typename _Tp> | |
74d6b8ca GDR |
1056 | inline _Tp |
1057 | valarray<_Tp>::max() const | |
1058 | { | |
2f1e8e7c | 1059 | __glibcxx_assert(_M_size > 0); |
65be6ddd | 1060 | return *std::max_element(_M_data, _M_data + _M_size); |
74d6b8ca | 1061 | } |
33ac58d5 | 1062 | |
54c1bf78 | 1063 | template<class _Tp> |
b714a419 | 1064 | inline _Expr<_ValFunClos<_ValArray, _Tp>, _Tp> |
74d6b8ca GDR |
1065 | valarray<_Tp>::apply(_Tp func(_Tp)) const |
1066 | { | |
b714a419 PC |
1067 | typedef _ValFunClos<_ValArray, _Tp> _Closure; |
1068 | return _Expr<_Closure, _Tp>(_Closure(*this, func)); | |
74d6b8ca | 1069 | } |
54c1bf78 BK |
1070 | |
1071 | template<class _Tp> | |
b714a419 | 1072 | inline _Expr<_RefFunClos<_ValArray, _Tp>, _Tp> |
74d6b8ca GDR |
1073 | valarray<_Tp>::apply(_Tp func(const _Tp &)) const |
1074 | { | |
b714a419 PC |
1075 | typedef _RefFunClos<_ValArray, _Tp> _Closure; |
1076 | return _Expr<_Closure, _Tp>(_Closure(*this, func)); | |
74d6b8ca | 1077 | } |
54c1bf78 BK |
1078 | |
1079 | #define _DEFINE_VALARRAY_UNARY_OPERATOR(_Op, _Name) \ | |
1080 | template<typename _Tp> \ | |
db5ab3aa | 1081 | inline typename valarray<_Tp>::template _UnaryOp<_Name>::_Rt \ |
b714a419 PC |
1082 | valarray<_Tp>::operator _Op() const \ |
1083 | { \ | |
db5ab3aa JW |
1084 | typedef _UnClos<_Name, _ValArray, _Tp> _Closure; \ |
1085 | typedef typename __fun<_Name, _Tp>::result_type _Rt; \ | |
b714a419 PC |
1086 | return _Expr<_Closure, _Rt>(_Closure(*this)); \ |
1087 | } | |
54c1bf78 | 1088 | |
74d6b8ca GDR |
1089 | _DEFINE_VALARRAY_UNARY_OPERATOR(+, __unary_plus) |
1090 | _DEFINE_VALARRAY_UNARY_OPERATOR(-, __negate) | |
1091 | _DEFINE_VALARRAY_UNARY_OPERATOR(~, __bitwise_not) | |
1092 | _DEFINE_VALARRAY_UNARY_OPERATOR (!, __logical_not) | |
54c1bf78 BK |
1093 | |
1094 | #undef _DEFINE_VALARRAY_UNARY_OPERATOR | |
54c1bf78 BK |
1095 | |
1096 | #define _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(_Op, _Name) \ | |
1097 | template<class _Tp> \ | |
971cfc6f GDR |
1098 | inline valarray<_Tp>& \ |
1099 | valarray<_Tp>::operator _Op##=(const _Tp &__t) \ | |
1100 | { \ | |
1101 | _Array_augmented_##_Name(_Array<_Tp>(_M_data), _M_size, __t); \ | |
54c1bf78 | 1102 | return *this; \ |
971cfc6f | 1103 | } \ |
54c1bf78 BK |
1104 | \ |
1105 | template<class _Tp> \ | |
971cfc6f GDR |
1106 | inline valarray<_Tp>& \ |
1107 | valarray<_Tp>::operator _Op##=(const valarray<_Tp> &__v) \ | |
1108 | { \ | |
2f1e8e7c | 1109 | __glibcxx_assert(_M_size == __v._M_size); \ |
971cfc6f GDR |
1110 | _Array_augmented_##_Name(_Array<_Tp>(_M_data), _M_size, \ |
1111 | _Array<_Tp>(__v._M_data)); \ | |
54c1bf78 | 1112 | return *this; \ |
971cfc6f | 1113 | } |
54c1bf78 | 1114 | |
971cfc6f GDR |
1115 | _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(+, __plus) |
1116 | _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(-, __minus) | |
1117 | _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(*, __multiplies) | |
1118 | _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(/, __divides) | |
1119 | _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(%, __modulus) | |
1120 | _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(^, __bitwise_xor) | |
1121 | _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(&, __bitwise_and) | |
1122 | _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(|, __bitwise_or) | |
1123 | _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(<<, __shift_left) | |
1124 | _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(>>, __shift_right) | |
54c1bf78 BK |
1125 | |
1126 | #undef _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT | |
1127 | ||
54c1bf78 BK |
1128 | #define _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(_Op, _Name) \ |
1129 | template<class _Tp> template<class _Dom> \ | |
971cfc6f | 1130 | inline valarray<_Tp>& \ |
b714a419 | 1131 | valarray<_Tp>::operator _Op##=(const _Expr<_Dom, _Tp>& __e) \ |
971cfc6f GDR |
1132 | { \ |
1133 | _Array_augmented_##_Name(_Array<_Tp>(_M_data), __e, _M_size); \ | |
54c1bf78 | 1134 | return *this; \ |
971cfc6f | 1135 | } |
54c1bf78 | 1136 | |
971cfc6f GDR |
1137 | _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(+, __plus) |
1138 | _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(-, __minus) | |
1139 | _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(*, __multiplies) | |
1140 | _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(/, __divides) | |
1141 | _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(%, __modulus) | |
1142 | _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(^, __bitwise_xor) | |
1143 | _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(&, __bitwise_and) | |
1144 | _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(|, __bitwise_or) | |
1145 | _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(<<, __shift_left) | |
1146 | _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(>>, __shift_right) | |
54c1bf78 BK |
1147 | |
1148 | #undef _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT | |
33ac58d5 | 1149 | |
54c1bf78 BK |
1150 | |
1151 | #define _DEFINE_BINARY_OPERATOR(_Op, _Name) \ | |
1152 | template<typename _Tp> \ | |
db5ab3aa JW |
1153 | inline _Expr<_BinClos<_Name, _ValArray, _ValArray, _Tp, _Tp>, \ |
1154 | typename __fun<_Name, _Tp>::result_type> \ | |
971cfc6f GDR |
1155 | operator _Op(const valarray<_Tp>& __v, const valarray<_Tp>& __w) \ |
1156 | { \ | |
db5ab3aa JW |
1157 | __glibcxx_assert(__v.size() == __w.size()); \ |
1158 | typedef _BinClos<_Name, _ValArray, _ValArray, _Tp, _Tp> _Closure; \ | |
1159 | typedef typename __fun<_Name, _Tp>::result_type _Rt; \ | |
1160 | return _Expr<_Closure, _Rt>(_Closure(__v, __w)); \ | |
971cfc6f | 1161 | } \ |
54c1bf78 BK |
1162 | \ |
1163 | template<typename _Tp> \ | |
db5ab3aa JW |
1164 | inline _Expr<_BinClos<_Name, _ValArray,_Constant, _Tp, _Tp>, \ |
1165 | typename __fun<_Name, _Tp>::result_type> \ | |
1166 | operator _Op(const valarray<_Tp>& __v, \ | |
1167 | const typename valarray<_Tp>::value_type& __t) \ | |
b714a419 PC |
1168 | { \ |
1169 | typedef _BinClos<_Name, _ValArray, _Constant, _Tp, _Tp> _Closure; \ | |
db5ab3aa JW |
1170 | typedef typename __fun<_Name, _Tp>::result_type _Rt; \ |
1171 | return _Expr<_Closure, _Rt>(_Closure(__v, __t)); \ | |
b714a419 | 1172 | } \ |
54c1bf78 BK |
1173 | \ |
1174 | template<typename _Tp> \ | |
db5ab3aa JW |
1175 | inline _Expr<_BinClos<_Name, _Constant, _ValArray, _Tp, _Tp>, \ |
1176 | typename __fun<_Name, _Tp>::result_type> \ | |
1177 | operator _Op(const typename valarray<_Tp>::value_type& __t, \ | |
1178 | const valarray<_Tp>& __v) \ | |
b714a419 | 1179 | { \ |
db5ab3aa JW |
1180 | typedef _BinClos<_Name, _Constant, _ValArray, _Tp, _Tp> _Closure; \ |
1181 | typedef typename __fun<_Name, _Tp>::result_type _Rt; \ | |
1182 | return _Expr<_Closure, _Rt>(_Closure(__t, __v)); \ | |
b714a419 | 1183 | } |
54c1bf78 | 1184 | |
971cfc6f GDR |
1185 | _DEFINE_BINARY_OPERATOR(+, __plus) |
1186 | _DEFINE_BINARY_OPERATOR(-, __minus) | |
1187 | _DEFINE_BINARY_OPERATOR(*, __multiplies) | |
1188 | _DEFINE_BINARY_OPERATOR(/, __divides) | |
1189 | _DEFINE_BINARY_OPERATOR(%, __modulus) | |
1190 | _DEFINE_BINARY_OPERATOR(^, __bitwise_xor) | |
1191 | _DEFINE_BINARY_OPERATOR(&, __bitwise_and) | |
1192 | _DEFINE_BINARY_OPERATOR(|, __bitwise_or) | |
1193 | _DEFINE_BINARY_OPERATOR(<<, __shift_left) | |
1194 | _DEFINE_BINARY_OPERATOR(>>, __shift_right) | |
1195 | _DEFINE_BINARY_OPERATOR(&&, __logical_and) | |
1196 | _DEFINE_BINARY_OPERATOR(||, __logical_or) | |
1197 | _DEFINE_BINARY_OPERATOR(==, __equal_to) | |
1198 | _DEFINE_BINARY_OPERATOR(!=, __not_equal_to) | |
1199 | _DEFINE_BINARY_OPERATOR(<, __less) | |
1200 | _DEFINE_BINARY_OPERATOR(>, __greater) | |
1201 | _DEFINE_BINARY_OPERATOR(<=, __less_equal) | |
1202 | _DEFINE_BINARY_OPERATOR(>=, __greater_equal) | |
54c1bf78 | 1203 | |
bcc4a44f PC |
1204 | #undef _DEFINE_BINARY_OPERATOR |
1205 | ||
734f5023 | 1206 | #if __cplusplus >= 201103L |
f67a9881 PC |
1207 | /** |
1208 | * @brief Return an iterator pointing to the first element of | |
1209 | * the valarray. | |
93c66bc6 | 1210 | * @param __va valarray. |
f67a9881 PC |
1211 | */ |
1212 | template<class _Tp> | |
1213 | inline _Tp* | |
1214 | begin(valarray<_Tp>& __va) | |
1215 | { return std::__addressof(__va[0]); } | |
1216 | ||
1217 | /** | |
1218 | * @brief Return an iterator pointing to the first element of | |
1219 | * the const valarray. | |
93c66bc6 | 1220 | * @param __va valarray. |
f67a9881 PC |
1221 | */ |
1222 | template<class _Tp> | |
1223 | inline const _Tp* | |
1224 | begin(const valarray<_Tp>& __va) | |
1225 | { return std::__addressof(__va[0]); } | |
1226 | ||
1227 | /** | |
1228 | * @brief Return an iterator pointing to one past the last element of | |
1229 | * the valarray. | |
93c66bc6 | 1230 | * @param __va valarray. |
f67a9881 PC |
1231 | */ |
1232 | template<class _Tp> | |
1233 | inline _Tp* | |
1234 | end(valarray<_Tp>& __va) | |
1235 | { return std::__addressof(__va[0]) + __va.size(); } | |
1236 | ||
1237 | /** | |
1238 | * @brief Return an iterator pointing to one past the last element of | |
1239 | * the const valarray. | |
93c66bc6 | 1240 | * @param __va valarray. |
f67a9881 PC |
1241 | */ |
1242 | template<class _Tp> | |
1243 | inline const _Tp* | |
1244 | end(const valarray<_Tp>& __va) | |
1245 | { return std::__addressof(__va[0]) + __va.size(); } | |
734f5023 | 1246 | #endif // C++11 |
f67a9881 | 1247 | |
5b9daa7e BK |
1248 | // @} group numeric_arrays |
1249 | ||
12ffa228 BK |
1250 | _GLIBCXX_END_NAMESPACE_VERSION |
1251 | } // namespace | |
54c1bf78 | 1252 | |
1143680e | 1253 | #endif /* _GLIBCXX_VALARRAY */ |