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