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