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