]>
Commit | Line | Data |
---|---|---|
54c1bf78 | 1 | // The template and inlines for the -*- C++ -*- valarray class. |
de96ac46 | 2 | |
ffe94f83 PE |
3 | // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 |
4 | // Free Software Foundation, Inc. | |
de96ac46 BK |
5 | // |
6 | // This file is part of the GNU ISO C++ Library. This library is free | |
7 | // software; you can redistribute it and/or modify it under the | |
8 | // terms of the GNU General Public License as published by the | |
9 | // Free Software Foundation; either version 2, or (at your option) | |
10 | // any later version. | |
11 | ||
12 | // This library is distributed in the hope that it will be useful, | |
13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | // GNU General Public License for more details. | |
16 | ||
17 | // You should have received a copy of the GNU General Public License along | |
18 | // with this library; see the file COPYING. If not, write to the Free | |
19 | // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
20 | // USA. | |
21 | ||
22 | // As a special exception, you may use this file as part of a free software | |
23 | // library without restriction. Specifically, if other files instantiate | |
24 | // templates or use macros or inline functions from this file, or you compile | |
25 | // this file and link it with other files to produce an executable, this | |
26 | // file does not by itself cause the resulting executable to be covered by | |
27 | // the GNU General Public License. This exception does not however | |
28 | // invalidate any other reasons why the executable file might be covered by | |
29 | // the GNU General Public License. | |
30 | ||
54c1bf78 BK |
31 | // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr> |
32 | ||
ffe94f83 PE |
33 | /** @file valarray |
34 | * This is a Standard C++ Library header. You should @c #include this header | |
35 | * in your programs, rather than any of the "st[dl]_*.h" implementation files. | |
2f9d51b8 PE |
36 | */ |
37 | ||
1143680e SE |
38 | #ifndef _GLIBCXX_VALARRAY |
39 | #define _GLIBCXX_VALARRAY 1 | |
54c1bf78 BK |
40 | |
41 | #pragma GCC system_header | |
42 | ||
43 | #include <bits/c++config.h> | |
44 | #include <cstddef> | |
45 | #include <cmath> | |
46 | #include <cstdlib> | |
47 | #include <numeric> | |
54c1bf78 BK |
48 | #include <algorithm> |
49 | ||
50 | namespace std | |
51 | { | |
74d6b8ca GDR |
52 | template<class _Clos, typename _Tp> |
53 | class _Expr; | |
54c1bf78 | 54 | |
74d6b8ca GDR |
55 | template<typename _Tp1, typename _Tp2> |
56 | class _ValArray; | |
54c1bf78 | 57 | |
74d6b8ca GDR |
58 | template<class _Oper, template<class, class> class _Meta, class _Dom> |
59 | struct _UnClos; | |
54c1bf78 | 60 | |
971cfc6f | 61 | template<class _Oper, |
54c1bf78 BK |
62 | template<class, class> class _Meta1, |
63 | template<class, class> class _Meta2, | |
74d6b8ca GDR |
64 | class _Dom1, class _Dom2> |
65 | class _BinClos; | |
54c1bf78 | 66 | |
74d6b8ca GDR |
67 | template<template<class, class> class _Meta, class _Dom> |
68 | class _SClos; | |
54c1bf78 | 69 | |
74d6b8ca GDR |
70 | template<template<class, class> class _Meta, class _Dom> |
71 | class _GClos; | |
54c1bf78 | 72 | |
74d6b8ca GDR |
73 | template<template<class, class> class _Meta, class _Dom> |
74 | class _IClos; | |
54c1bf78 | 75 | |
74d6b8ca GDR |
76 | template<template<class, class> class _Meta, class _Dom> |
77 | class _ValFunClos; | |
54c1bf78 | 78 | |
74d6b8ca GDR |
79 | template<template<class, class> class _Meta, class _Dom> |
80 | class _RefFunClos; | |
81 | ||
74d6b8ca GDR |
82 | template<class _Tp> class valarray; // An array of type _Tp |
83 | class slice; // BLAS-like slice out of an array | |
84 | template<class _Tp> class slice_array; | |
85 | class gslice; // generalized slice out of an array | |
86 | template<class _Tp> class gslice_array; | |
87 | template<class _Tp> class mask_array; // masked array | |
88 | template<class _Tp> class indirect_array; // indirected array | |
54c1bf78 BK |
89 | |
90 | } // namespace std | |
91 | ||
92 | #include <bits/valarray_array.h> | |
c13bea50 | 93 | #include <bits/valarray_before.h> |
54c1bf78 BK |
94 | |
95 | namespace std | |
96 | { | |
74d6b8ca GDR |
97 | template<class _Tp> |
98 | class valarray | |
99 | { | |
100 | template<class _Op> | |
101 | struct _UnaryOp | |
102 | { | |
103 | typedef typename __fun<_Op, _Tp>::result_type __rt; | |
104 | typedef _Expr<_UnClos<_Op, _ValArray, _Tp>, __rt> _Rt; | |
105 | }; | |
106 | public: | |
54c1bf78 | 107 | typedef _Tp value_type; |
74d6b8ca GDR |
108 | |
109 | // _lib.valarray.cons_ construct/destroy: | |
54c1bf78 BK |
110 | valarray(); |
111 | explicit valarray(size_t); | |
112 | valarray(const _Tp&, size_t); | |
113 | valarray(const _Tp* __restrict__, size_t); | |
114 | valarray(const valarray&); | |
115 | valarray(const slice_array<_Tp>&); | |
116 | valarray(const gslice_array<_Tp>&); | |
117 | valarray(const mask_array<_Tp>&); | |
118 | valarray(const indirect_array<_Tp>&); | |
119 | template<class _Dom> | |
74d6b8ca GDR |
120 | valarray(const _Expr<_Dom,_Tp>& __e); |
121 | ~valarray(); | |
54c1bf78 BK |
122 | |
123 | // _lib.valarray.assign_ assignment: | |
124 | valarray<_Tp>& operator=(const valarray<_Tp>&); | |
125 | valarray<_Tp>& operator=(const _Tp&); | |
126 | valarray<_Tp>& operator=(const slice_array<_Tp>&); | |
127 | valarray<_Tp>& operator=(const gslice_array<_Tp>&); | |
128 | valarray<_Tp>& operator=(const mask_array<_Tp>&); | |
129 | valarray<_Tp>& operator=(const indirect_array<_Tp>&); | |
130 | ||
131 | template<class _Dom> valarray<_Tp>& | |
74d6b8ca | 132 | operator= (const _Expr<_Dom,_Tp>&); |
54c1bf78 BK |
133 | |
134 | // _lib.valarray.access_ element access: | |
135 | // XXX: LWG to be resolved. | |
136 | const _Tp& operator[](size_t) const; | |
137 | _Tp& operator[](size_t); | |
138 | // _lib.valarray.sub_ subset operations: | |
139 | _Expr<_SClos<_ValArray,_Tp>, _Tp> operator[](slice) const; | |
140 | slice_array<_Tp> operator[](slice); | |
141 | _Expr<_GClos<_ValArray,_Tp>, _Tp> operator[](const gslice&) const; | |
142 | gslice_array<_Tp> operator[](const gslice&); | |
143 | valarray<_Tp> operator[](const valarray<bool>&) const; | |
144 | mask_array<_Tp> operator[](const valarray<bool>&); | |
145 | _Expr<_IClos<_ValArray, _Tp>, _Tp> | |
74d6b8ca | 146 | operator[](const valarray<size_t>&) const; |
54c1bf78 BK |
147 | indirect_array<_Tp> operator[](const valarray<size_t>&); |
148 | ||
149 | // _lib.valarray.unary_ unary operators: | |
74d6b8ca GDR |
150 | typename _UnaryOp<__unary_plus>::_Rt operator+() const; |
151 | typename _UnaryOp<__negate>::_Rt operator-() const; | |
152 | typename _UnaryOp<__bitwise_not>::_Rt operator~() const; | |
153 | typename _UnaryOp<__logical_not>::_Rt operator!() const; | |
154 | ||
54c1bf78 | 155 | // _lib.valarray.cassign_ computed assignment: |
74d6b8ca GDR |
156 | valarray<_Tp>& operator*=(const _Tp&); |
157 | valarray<_Tp>& operator/=(const _Tp&); | |
158 | valarray<_Tp>& operator%=(const _Tp&); | |
159 | valarray<_Tp>& operator+=(const _Tp&); | |
160 | valarray<_Tp>& operator-=(const _Tp&); | |
161 | valarray<_Tp>& operator^=(const _Tp&); | |
162 | valarray<_Tp>& operator&=(const _Tp&); | |
163 | valarray<_Tp>& operator|=(const _Tp&); | |
54c1bf78 BK |
164 | valarray<_Tp>& operator<<=(const _Tp&); |
165 | valarray<_Tp>& operator>>=(const _Tp&); | |
74d6b8ca GDR |
166 | valarray<_Tp>& operator*=(const valarray<_Tp>&); |
167 | valarray<_Tp>& operator/=(const valarray<_Tp>&); | |
168 | valarray<_Tp>& operator%=(const valarray<_Tp>&); | |
169 | valarray<_Tp>& operator+=(const valarray<_Tp>&); | |
170 | valarray<_Tp>& operator-=(const valarray<_Tp>&); | |
171 | valarray<_Tp>& operator^=(const valarray<_Tp>&); | |
172 | valarray<_Tp>& operator|=(const valarray<_Tp>&); | |
173 | valarray<_Tp>& operator&=(const valarray<_Tp>&); | |
54c1bf78 BK |
174 | valarray<_Tp>& operator<<=(const valarray<_Tp>&); |
175 | valarray<_Tp>& operator>>=(const valarray<_Tp>&); | |
176 | ||
177 | template<class _Dom> | |
74d6b8ca | 178 | valarray<_Tp>& operator*=(const _Expr<_Dom,_Tp>&); |
54c1bf78 | 179 | template<class _Dom> |
74d6b8ca | 180 | valarray<_Tp>& operator/=(const _Expr<_Dom,_Tp>&); |
54c1bf78 | 181 | template<class _Dom> |
74d6b8ca | 182 | valarray<_Tp>& operator%=(const _Expr<_Dom,_Tp>&); |
54c1bf78 | 183 | template<class _Dom> |
74d6b8ca | 184 | valarray<_Tp>& operator+=(const _Expr<_Dom,_Tp>&); |
54c1bf78 | 185 | template<class _Dom> |
74d6b8ca | 186 | valarray<_Tp>& operator-=(const _Expr<_Dom,_Tp>&); |
54c1bf78 | 187 | template<class _Dom> |
74d6b8ca | 188 | valarray<_Tp>& operator^=(const _Expr<_Dom,_Tp>&); |
54c1bf78 | 189 | template<class _Dom> |
74d6b8ca | 190 | valarray<_Tp>& operator|=(const _Expr<_Dom,_Tp>&); |
54c1bf78 | 191 | template<class _Dom> |
74d6b8ca | 192 | valarray<_Tp>& operator&=(const _Expr<_Dom,_Tp>&); |
54c1bf78 | 193 | template<class _Dom> |
74d6b8ca | 194 | valarray<_Tp>& operator<<=(const _Expr<_Dom,_Tp>&); |
54c1bf78 | 195 | template<class _Dom> |
74d6b8ca GDR |
196 | valarray<_Tp>& operator>>=(const _Expr<_Dom,_Tp>&); |
197 | ||
54c1bf78 | 198 | |
54c1bf78 BK |
199 | // _lib.valarray.members_ member functions: |
200 | size_t size() const; | |
201 | _Tp sum() const; | |
202 | _Tp min() const; | |
203 | _Tp max() const; | |
204 | ||
74d6b8ca GDR |
205 | // // FIXME: Extension |
206 | // _Tp product () const; | |
54c1bf78 BK |
207 | |
208 | valarray<_Tp> shift (int) const; | |
209 | valarray<_Tp> cshift(int) const; | |
210 | _Expr<_ValFunClos<_ValArray,_Tp>,_Tp> apply(_Tp func(_Tp)) const; | |
211 | _Expr<_RefFunClos<_ValArray,_Tp>,_Tp> apply(_Tp func(const _Tp&)) const; | |
212 | void resize(size_t __size, _Tp __c = _Tp()); | |
213 | ||
74d6b8ca | 214 | private: |
54c1bf78 BK |
215 | size_t _M_size; |
216 | _Tp* __restrict__ _M_data; | |
74d6b8ca | 217 | |
54c1bf78 | 218 | friend class _Array<_Tp>; |
74d6b8ca | 219 | }; |
54c1bf78 BK |
220 | |
221 | template<typename _Tp> | |
74d6b8ca GDR |
222 | inline const _Tp& |
223 | valarray<_Tp>::operator[](size_t __i) const | |
224 | { return _M_data[__i]; } | |
54c1bf78 BK |
225 | |
226 | template<typename _Tp> | |
74d6b8ca GDR |
227 | inline _Tp& |
228 | valarray<_Tp>::operator[](size_t __i) | |
229 | { return _M_data[__i]; } | |
54c1bf78 BK |
230 | |
231 | } // std:: | |
c13bea50 NS |
232 | |
233 | #include <bits/valarray_after.h> | |
234 | ||
54c1bf78 BK |
235 | #include <bits/slice_array.h> |
236 | #include <bits/gslice.h> | |
237 | #include <bits/gslice_array.h> | |
238 | #include <bits/mask_array.h> | |
239 | #include <bits/indirect_array.h> | |
240 | ||
241 | namespace std | |
242 | { | |
243 | template<typename _Tp> | |
74d6b8ca GDR |
244 | inline |
245 | valarray<_Tp>::valarray() : _M_size(0), _M_data(0) {} | |
54c1bf78 BK |
246 | |
247 | template<typename _Tp> | |
74d6b8ca GDR |
248 | inline |
249 | valarray<_Tp>::valarray(size_t __n) | |
250 | : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n)) | |
eb9a4231 | 251 | { std::__valarray_default_construct(_M_data, _M_data + __n); } |
54c1bf78 BK |
252 | |
253 | template<typename _Tp> | |
74d6b8ca GDR |
254 | inline |
255 | valarray<_Tp>::valarray(const _Tp& __t, size_t __n) | |
256 | : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n)) | |
eb9a4231 | 257 | { std::__valarray_fill_construct(_M_data, _M_data + __n, __t); } |
54c1bf78 BK |
258 | |
259 | template<typename _Tp> | |
74d6b8ca GDR |
260 | inline |
261 | valarray<_Tp>::valarray(const _Tp* __restrict__ __p, size_t __n) | |
262 | : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n)) | |
eb9a4231 | 263 | { std::__valarray_copy_construct(__p, __p + __n, _M_data); } |
54c1bf78 BK |
264 | |
265 | template<typename _Tp> | |
74d6b8ca GDR |
266 | inline |
267 | valarray<_Tp>::valarray(const valarray<_Tp>& __v) | |
268 | : _M_size(__v._M_size), _M_data(__valarray_get_storage<_Tp>(__v._M_size)) | |
eb9a4231 | 269 | { std::__valarray_copy_construct(__v._M_data, __v._M_data + _M_size, _M_data); } |
54c1bf78 BK |
270 | |
271 | template<typename _Tp> | |
74d6b8ca GDR |
272 | inline |
273 | valarray<_Tp>::valarray(const slice_array<_Tp>& __sa) | |
274 | : _M_size(__sa._M_sz), _M_data(__valarray_get_storage<_Tp>(__sa._M_sz)) | |
275 | { | |
eb9a4231 | 276 | std::__valarray_copy |
74d6b8ca GDR |
277 | (__sa._M_array, __sa._M_sz, __sa._M_stride, _Array<_Tp>(_M_data)); |
278 | } | |
54c1bf78 BK |
279 | |
280 | template<typename _Tp> | |
74d6b8ca GDR |
281 | inline |
282 | valarray<_Tp>::valarray(const gslice_array<_Tp>& __ga) | |
283 | : _M_size(__ga._M_index.size()), | |
284 | _M_data(__valarray_get_storage<_Tp>(_M_size)) | |
285 | { | |
eb9a4231 | 286 | std::__valarray_copy |
74d6b8ca GDR |
287 | (__ga._M_array, _Array<size_t>(__ga._M_index), |
288 | _Array<_Tp>(_M_data), _M_size); | |
289 | } | |
54c1bf78 BK |
290 | |
291 | template<typename _Tp> | |
74d6b8ca GDR |
292 | inline |
293 | valarray<_Tp>::valarray(const mask_array<_Tp>& __ma) | |
294 | : _M_size(__ma._M_sz), _M_data(__valarray_get_storage<_Tp>(__ma._M_sz)) | |
295 | { | |
eb9a4231 | 296 | std::__valarray_copy |
74d6b8ca GDR |
297 | (__ma._M_array, __ma._M_mask, _Array<_Tp>(_M_data), _M_size); |
298 | } | |
54c1bf78 BK |
299 | |
300 | template<typename _Tp> | |
74d6b8ca GDR |
301 | inline |
302 | valarray<_Tp>::valarray(const indirect_array<_Tp>& __ia) | |
303 | : _M_size(__ia._M_sz), _M_data(__valarray_get_storage<_Tp>(__ia._M_sz)) | |
304 | { | |
eb9a4231 | 305 | std::__valarray_copy |
74d6b8ca GDR |
306 | (__ia._M_array, __ia._M_index, _Array<_Tp>(_M_data), _M_size); |
307 | } | |
54c1bf78 BK |
308 | |
309 | template<typename _Tp> template<class _Dom> | |
74d6b8ca GDR |
310 | inline |
311 | valarray<_Tp>::valarray(const _Expr<_Dom, _Tp>& __e) | |
312 | : _M_size(__e.size()), _M_data(__valarray_get_storage<_Tp>(_M_size)) | |
eb9a4231 | 313 | { std::__valarray_copy(__e, _M_size, _Array<_Tp>(_M_data)); } |
54c1bf78 BK |
314 | |
315 | template<typename _Tp> | |
74d6b8ca GDR |
316 | inline |
317 | valarray<_Tp>::~valarray() | |
318 | { | |
eb9a4231 PC |
319 | std::__valarray_destroy_elements(_M_data, _M_data + _M_size); |
320 | std::__valarray_release_memory(_M_data); | |
74d6b8ca | 321 | } |
54c1bf78 BK |
322 | |
323 | template<typename _Tp> | |
74d6b8ca GDR |
324 | inline valarray<_Tp>& |
325 | valarray<_Tp>::operator=(const valarray<_Tp>& __v) | |
326 | { | |
eb9a4231 | 327 | std::__valarray_copy(__v._M_data, _M_size, _M_data); |
54c1bf78 | 328 | return *this; |
74d6b8ca | 329 | } |
54c1bf78 BK |
330 | |
331 | template<typename _Tp> | |
74d6b8ca GDR |
332 | inline valarray<_Tp>& |
333 | valarray<_Tp>::operator=(const _Tp& __t) | |
334 | { | |
eb9a4231 | 335 | std::__valarray_fill(_M_data, _M_size, __t); |
54c1bf78 | 336 | return *this; |
74d6b8ca | 337 | } |
54c1bf78 BK |
338 | |
339 | template<typename _Tp> | |
74d6b8ca GDR |
340 | inline valarray<_Tp>& |
341 | valarray<_Tp>::operator=(const slice_array<_Tp>& __sa) | |
342 | { | |
eb9a4231 PC |
343 | std::__valarray_copy(__sa._M_array, __sa._M_sz, |
344 | __sa._M_stride, _Array<_Tp>(_M_data)); | |
54c1bf78 | 345 | return *this; |
74d6b8ca | 346 | } |
54c1bf78 BK |
347 | |
348 | template<typename _Tp> | |
74d6b8ca GDR |
349 | inline valarray<_Tp>& |
350 | valarray<_Tp>::operator=(const gslice_array<_Tp>& __ga) | |
351 | { | |
eb9a4231 PC |
352 | std::__valarray_copy(__ga._M_array, _Array<size_t>(__ga._M_index), |
353 | _Array<_Tp>(_M_data), _M_size); | |
54c1bf78 | 354 | return *this; |
74d6b8ca | 355 | } |
54c1bf78 BK |
356 | |
357 | template<typename _Tp> | |
74d6b8ca GDR |
358 | inline valarray<_Tp>& |
359 | valarray<_Tp>::operator=(const mask_array<_Tp>& __ma) | |
360 | { | |
eb9a4231 PC |
361 | std::__valarray_copy(__ma._M_array, __ma._M_mask, |
362 | _Array<_Tp>(_M_data), _M_size); | |
54c1bf78 | 363 | return *this; |
74d6b8ca | 364 | } |
54c1bf78 BK |
365 | |
366 | template<typename _Tp> | |
74d6b8ca GDR |
367 | inline valarray<_Tp>& |
368 | valarray<_Tp>::operator=(const indirect_array<_Tp>& __ia) | |
369 | { | |
eb9a4231 PC |
370 | std::__valarray_copy(__ia._M_array, __ia._M_index, |
371 | _Array<_Tp>(_M_data), _M_size); | |
54c1bf78 | 372 | return *this; |
74d6b8ca | 373 | } |
54c1bf78 BK |
374 | |
375 | template<typename _Tp> template<class _Dom> | |
74d6b8ca GDR |
376 | inline valarray<_Tp>& |
377 | valarray<_Tp>::operator=(const _Expr<_Dom, _Tp>& __e) | |
378 | { | |
eb9a4231 PC |
379 | std::__valarray_copy(__e, _M_size, _Array<_Tp>(_M_data)); |
380 | return *this; | |
74d6b8ca | 381 | } |
54c1bf78 BK |
382 | |
383 | template<typename _Tp> | |
74d6b8ca GDR |
384 | inline _Expr<_SClos<_ValArray,_Tp>, _Tp> |
385 | valarray<_Tp>::operator[](slice __s) const | |
386 | { | |
54c1bf78 | 387 | typedef _SClos<_ValArray,_Tp> _Closure; |
74d6b8ca GDR |
388 | return _Expr<_Closure, _Tp>(_Closure (_Array<_Tp>(_M_data), __s)); |
389 | } | |
54c1bf78 BK |
390 | |
391 | template<typename _Tp> | |
74d6b8ca GDR |
392 | inline slice_array<_Tp> |
393 | valarray<_Tp>::operator[](slice __s) | |
394 | { | |
395 | return slice_array<_Tp>(_Array<_Tp>(_M_data), __s); | |
396 | } | |
54c1bf78 BK |
397 | |
398 | template<typename _Tp> | |
74d6b8ca GDR |
399 | inline _Expr<_GClos<_ValArray,_Tp>, _Tp> |
400 | valarray<_Tp>::operator[](const gslice& __gs) const | |
401 | { | |
54c1bf78 BK |
402 | typedef _GClos<_ValArray,_Tp> _Closure; |
403 | return _Expr<_Closure, _Tp> | |
74d6b8ca GDR |
404 | (_Closure(_Array<_Tp>(_M_data), __gs._M_index->_M_index)); |
405 | } | |
54c1bf78 BK |
406 | |
407 | template<typename _Tp> | |
74d6b8ca GDR |
408 | inline gslice_array<_Tp> |
409 | valarray<_Tp>::operator[](const gslice& __gs) | |
410 | { | |
54c1bf78 | 411 | return gslice_array<_Tp> |
74d6b8ca GDR |
412 | (_Array<_Tp>(_M_data), __gs._M_index->_M_index); |
413 | } | |
54c1bf78 BK |
414 | |
415 | template<typename _Tp> | |
74d6b8ca GDR |
416 | inline valarray<_Tp> |
417 | valarray<_Tp>::operator[](const valarray<bool>& __m) const | |
418 | { | |
419 | size_t __s = 0; | |
420 | size_t __e = __m.size(); | |
54c1bf78 | 421 | for (size_t __i=0; __i<__e; ++__i) |
74d6b8ca GDR |
422 | if (__m[__i]) ++__s; |
423 | return valarray<_Tp>(mask_array<_Tp>(_Array<_Tp>(_M_data), __s, | |
424 | _Array<bool> (__m))); | |
425 | } | |
54c1bf78 BK |
426 | |
427 | template<typename _Tp> | |
74d6b8ca GDR |
428 | inline mask_array<_Tp> |
429 | valarray<_Tp>::operator[](const valarray<bool>& __m) | |
430 | { | |
431 | size_t __s = 0; | |
432 | size_t __e = __m.size(); | |
54c1bf78 | 433 | for (size_t __i=0; __i<__e; ++__i) |
74d6b8ca GDR |
434 | if (__m[__i]) ++__s; |
435 | return mask_array<_Tp>(_Array<_Tp>(_M_data), __s, _Array<bool>(__m)); | |
436 | } | |
54c1bf78 BK |
437 | |
438 | template<typename _Tp> | |
74d6b8ca GDR |
439 | inline _Expr<_IClos<_ValArray,_Tp>, _Tp> |
440 | valarray<_Tp>::operator[](const valarray<size_t>& __i) const | |
441 | { | |
54c1bf78 | 442 | typedef _IClos<_ValArray,_Tp> _Closure; |
74d6b8ca GDR |
443 | return _Expr<_Closure, _Tp>(_Closure(*this, __i)); |
444 | } | |
54c1bf78 BK |
445 | |
446 | template<typename _Tp> | |
74d6b8ca GDR |
447 | inline indirect_array<_Tp> |
448 | valarray<_Tp>::operator[](const valarray<size_t>& __i) | |
449 | { | |
450 | return indirect_array<_Tp>(_Array<_Tp>(_M_data), __i.size(), | |
451 | _Array<size_t>(__i)); | |
452 | } | |
54c1bf78 BK |
453 | |
454 | template<class _Tp> | |
74d6b8ca GDR |
455 | inline size_t |
456 | valarray<_Tp>::size() const | |
457 | { return _M_size; } | |
54c1bf78 BK |
458 | |
459 | template<class _Tp> | |
74d6b8ca GDR |
460 | inline _Tp |
461 | valarray<_Tp>::sum() const | |
462 | { | |
eb9a4231 | 463 | return std::__valarray_sum(_M_data, _M_data + _M_size); |
74d6b8ca | 464 | } |
54c1bf78 BK |
465 | |
466 | // template<typename _Tp> | |
467 | // inline _Tp | |
468 | // valarray<_Tp>::product () const | |
469 | // { | |
470 | // return __valarray_product(_M_data, _M_data + _M_size); | |
471 | // } | |
472 | ||
473 | template <class _Tp> | |
474 | inline valarray<_Tp> | |
475 | valarray<_Tp>::shift(int __n) const | |
476 | { | |
477 | _Tp* const __a = static_cast<_Tp*> | |
478 | (__builtin_alloca(sizeof(_Tp) * _M_size)); | |
479 | if (__n == 0) // no shift | |
eb9a4231 | 480 | std::__valarray_copy_construct(_M_data, _M_data + _M_size, __a); |
54c1bf78 BK |
481 | else if (__n > 0) // __n > 0: shift left |
482 | { | |
483 | if (size_t(__n) > _M_size) | |
eb9a4231 | 484 | std::__valarray_default_construct(__a, __a + __n); |
54c1bf78 BK |
485 | else |
486 | { | |
eb9a4231 PC |
487 | std::__valarray_copy_construct(_M_data+__n, _M_data + _M_size, __a); |
488 | std::__valarray_default_construct(__a+_M_size-__n, __a + _M_size); | |
54c1bf78 BK |
489 | } |
490 | } | |
491 | else // __n < 0: shift right | |
492 | { | |
eb9a4231 PC |
493 | std::__valarray_copy_construct (_M_data, _M_data+_M_size+__n, __a-__n); |
494 | std::__valarray_default_construct(__a, __a - __n); | |
54c1bf78 BK |
495 | } |
496 | return valarray<_Tp> (__a, _M_size); | |
497 | } | |
498 | ||
499 | template <class _Tp> | |
500 | inline valarray<_Tp> | |
501 | valarray<_Tp>::cshift (int __n) const | |
502 | { | |
503 | _Tp* const __a = static_cast<_Tp*> | |
504 | (__builtin_alloca (sizeof(_Tp) * _M_size)); | |
505 | if (__n == 0) // no cshift | |
eb9a4231 | 506 | std::__valarray_copy_construct(_M_data, _M_data + _M_size, __a); |
54c1bf78 BK |
507 | else if (__n > 0) // cshift left |
508 | { | |
eb9a4231 PC |
509 | std::__valarray_copy_construct(_M_data, _M_data+__n, __a+_M_size-__n); |
510 | std::__valarray_copy_construct(_M_data+__n, _M_data + _M_size, __a); | |
54c1bf78 BK |
511 | } |
512 | else // cshift right | |
513 | { | |
eb9a4231 | 514 | std::__valarray_copy_construct |
54c1bf78 | 515 | (_M_data + _M_size+__n, _M_data + _M_size, __a); |
eb9a4231 | 516 | std::__valarray_copy_construct |
54c1bf78 BK |
517 | (_M_data, _M_data + _M_size+__n, __a - __n); |
518 | } | |
519 | return valarray<_Tp>(__a, _M_size); | |
520 | } | |
521 | ||
522 | template <class _Tp> | |
74d6b8ca GDR |
523 | inline void |
524 | valarray<_Tp>::resize (size_t __n, _Tp __c) | |
525 | { | |
526 | // This complication is so to make valarray<valarray<T> > work | |
527 | // even though it is not required by the standard. Nobody should | |
528 | // be saying valarray<valarray<T> > anyway. See the specs. | |
eb9a4231 | 529 | std::__valarray_destroy_elements(_M_data, _M_data + _M_size); |
74d6b8ca GDR |
530 | if (_M_size != __n) |
531 | { | |
eb9a4231 | 532 | std::__valarray_release_memory(_M_data); |
74d6b8ca GDR |
533 | _M_size = __n; |
534 | _M_data = __valarray_get_storage<_Tp>(__n); | |
535 | } | |
eb9a4231 | 536 | std::__valarray_fill_construct(_M_data, _M_data + __n, __c); |
74d6b8ca | 537 | } |
54c1bf78 BK |
538 | |
539 | template<typename _Tp> | |
74d6b8ca GDR |
540 | inline _Tp |
541 | valarray<_Tp>::min() const | |
542 | { | |
eb9a4231 | 543 | return *std::min_element (_M_data, _M_data+_M_size); |
74d6b8ca | 544 | } |
54c1bf78 BK |
545 | |
546 | template<typename _Tp> | |
74d6b8ca GDR |
547 | inline _Tp |
548 | valarray<_Tp>::max() const | |
549 | { | |
eb9a4231 | 550 | return *std::max_element (_M_data, _M_data+_M_size); |
74d6b8ca | 551 | } |
54c1bf78 BK |
552 | |
553 | template<class _Tp> | |
74d6b8ca GDR |
554 | inline _Expr<_ValFunClos<_ValArray,_Tp>,_Tp> |
555 | valarray<_Tp>::apply(_Tp func(_Tp)) const | |
556 | { | |
54c1bf78 | 557 | typedef _ValFunClos<_ValArray,_Tp> _Closure; |
74d6b8ca GDR |
558 | return _Expr<_Closure,_Tp>(_Closure(*this, func)); |
559 | } | |
54c1bf78 BK |
560 | |
561 | template<class _Tp> | |
74d6b8ca GDR |
562 | inline _Expr<_RefFunClos<_ValArray,_Tp>,_Tp> |
563 | valarray<_Tp>::apply(_Tp func(const _Tp &)) const | |
564 | { | |
54c1bf78 | 565 | typedef _RefFunClos<_ValArray,_Tp> _Closure; |
74d6b8ca GDR |
566 | return _Expr<_Closure,_Tp>(_Closure(*this, func)); |
567 | } | |
54c1bf78 BK |
568 | |
569 | #define _DEFINE_VALARRAY_UNARY_OPERATOR(_Op, _Name) \ | |
570 | template<typename _Tp> \ | |
74d6b8ca | 571 | inline typename valarray<_Tp>::template _UnaryOp<_Name>::_Rt \ |
54c1bf78 BK |
572 | valarray<_Tp>::operator _Op() const \ |
573 | { \ | |
74d6b8ca GDR |
574 | typedef _UnClos<_Name,_ValArray,_Tp> _Closure; \ |
575 | typedef typename __fun<_Name, _Tp>::result_type _Rt; \ | |
576 | return _Expr<_Closure, _Rt>(_Closure(*this)); \ | |
54c1bf78 BK |
577 | } |
578 | ||
74d6b8ca GDR |
579 | _DEFINE_VALARRAY_UNARY_OPERATOR(+, __unary_plus) |
580 | _DEFINE_VALARRAY_UNARY_OPERATOR(-, __negate) | |
581 | _DEFINE_VALARRAY_UNARY_OPERATOR(~, __bitwise_not) | |
582 | _DEFINE_VALARRAY_UNARY_OPERATOR (!, __logical_not) | |
54c1bf78 BK |
583 | |
584 | #undef _DEFINE_VALARRAY_UNARY_OPERATOR | |
54c1bf78 BK |
585 | |
586 | #define _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(_Op, _Name) \ | |
587 | template<class _Tp> \ | |
971cfc6f GDR |
588 | inline valarray<_Tp>& \ |
589 | valarray<_Tp>::operator _Op##=(const _Tp &__t) \ | |
590 | { \ | |
591 | _Array_augmented_##_Name(_Array<_Tp>(_M_data), _M_size, __t); \ | |
54c1bf78 | 592 | return *this; \ |
971cfc6f | 593 | } \ |
54c1bf78 BK |
594 | \ |
595 | template<class _Tp> \ | |
971cfc6f GDR |
596 | inline valarray<_Tp>& \ |
597 | valarray<_Tp>::operator _Op##=(const valarray<_Tp> &__v) \ | |
598 | { \ | |
599 | _Array_augmented_##_Name(_Array<_Tp>(_M_data), _M_size, \ | |
600 | _Array<_Tp>(__v._M_data)); \ | |
54c1bf78 | 601 | return *this; \ |
971cfc6f | 602 | } |
54c1bf78 | 603 | |
971cfc6f GDR |
604 | _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(+, __plus) |
605 | _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(-, __minus) | |
606 | _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(*, __multiplies) | |
607 | _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(/, __divides) | |
608 | _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(%, __modulus) | |
609 | _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(^, __bitwise_xor) | |
610 | _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(&, __bitwise_and) | |
611 | _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(|, __bitwise_or) | |
612 | _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(<<, __shift_left) | |
613 | _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(>>, __shift_right) | |
54c1bf78 BK |
614 | |
615 | #undef _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT | |
616 | ||
54c1bf78 BK |
617 | #define _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(_Op, _Name) \ |
618 | template<class _Tp> template<class _Dom> \ | |
971cfc6f GDR |
619 | inline valarray<_Tp>& \ |
620 | valarray<_Tp>::operator _Op##=(const _Expr<_Dom,_Tp>& __e) \ | |
621 | { \ | |
622 | _Array_augmented_##_Name(_Array<_Tp>(_M_data), __e, _M_size); \ | |
54c1bf78 | 623 | return *this; \ |
971cfc6f | 624 | } |
54c1bf78 | 625 | |
971cfc6f GDR |
626 | _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(+, __plus) |
627 | _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(-, __minus) | |
628 | _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(*, __multiplies) | |
629 | _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(/, __divides) | |
630 | _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(%, __modulus) | |
631 | _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(^, __bitwise_xor) | |
632 | _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(&, __bitwise_and) | |
633 | _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(|, __bitwise_or) | |
634 | _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(<<, __shift_left) | |
635 | _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(>>, __shift_right) | |
54c1bf78 BK |
636 | |
637 | #undef _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT | |
638 | ||
639 | ||
640 | #define _DEFINE_BINARY_OPERATOR(_Op, _Name) \ | |
641 | template<typename _Tp> \ | |
971cfc6f GDR |
642 | inline _Expr<_BinClos<_Name,_ValArray,_ValArray,_Tp,_Tp>, \ |
643 | typename __fun<_Name, _Tp>::result_type> \ | |
644 | operator _Op(const valarray<_Tp>& __v, const valarray<_Tp>& __w) \ | |
645 | { \ | |
54c1bf78 | 646 | typedef _BinClos<_Name,_ValArray,_ValArray,_Tp,_Tp> _Closure; \ |
971cfc6f GDR |
647 | typedef typename __fun<_Name, _Tp>::result_type _Rt; \ |
648 | return _Expr<_Closure, _Rt>(_Closure(__v, __w)); \ | |
649 | } \ | |
54c1bf78 BK |
650 | \ |
651 | template<typename _Tp> \ | |
971cfc6f GDR |
652 | inline _Expr<_BinClos<_Name,_ValArray,_Constant,_Tp,_Tp>, \ |
653 | typename __fun<_Name, _Tp>::result_type> \ | |
654 | operator _Op(const valarray<_Tp>& __v, const _Tp& __t) \ | |
54c1bf78 | 655 | { \ |
971cfc6f GDR |
656 | typedef _BinClos<_Name,_ValArray,_Constant,_Tp,_Tp> _Closure; \ |
657 | typedef typename __fun<_Name, _Tp>::result_type _Rt; \ | |
658 | return _Expr<_Closure, _Rt>(_Closure(__v, __t)); \ | |
54c1bf78 BK |
659 | } \ |
660 | \ | |
661 | template<typename _Tp> \ | |
971cfc6f GDR |
662 | inline _Expr<_BinClos<_Name,_Constant,_ValArray,_Tp,_Tp>, \ |
663 | typename __fun<_Name, _Tp>::result_type> \ | |
664 | operator _Op(const _Tp& __t, const valarray<_Tp>& __v) \ | |
54c1bf78 | 665 | { \ |
971cfc6f GDR |
666 | typedef _BinClos<_Name,_Constant,_ValArray,_Tp,_Tp> _Closure; \ |
667 | typedef typename __fun<_Name, _Tp>::result_type _Rt; \ | |
668 | return _Expr<_Closure, _Tp>(_Closure(__t, __v)); \ | |
54c1bf78 BK |
669 | } |
670 | ||
971cfc6f GDR |
671 | _DEFINE_BINARY_OPERATOR(+, __plus) |
672 | _DEFINE_BINARY_OPERATOR(-, __minus) | |
673 | _DEFINE_BINARY_OPERATOR(*, __multiplies) | |
674 | _DEFINE_BINARY_OPERATOR(/, __divides) | |
675 | _DEFINE_BINARY_OPERATOR(%, __modulus) | |
676 | _DEFINE_BINARY_OPERATOR(^, __bitwise_xor) | |
677 | _DEFINE_BINARY_OPERATOR(&, __bitwise_and) | |
678 | _DEFINE_BINARY_OPERATOR(|, __bitwise_or) | |
679 | _DEFINE_BINARY_OPERATOR(<<, __shift_left) | |
680 | _DEFINE_BINARY_OPERATOR(>>, __shift_right) | |
681 | _DEFINE_BINARY_OPERATOR(&&, __logical_and) | |
682 | _DEFINE_BINARY_OPERATOR(||, __logical_or) | |
683 | _DEFINE_BINARY_OPERATOR(==, __equal_to) | |
684 | _DEFINE_BINARY_OPERATOR(!=, __not_equal_to) | |
685 | _DEFINE_BINARY_OPERATOR(<, __less) | |
686 | _DEFINE_BINARY_OPERATOR(>, __greater) | |
687 | _DEFINE_BINARY_OPERATOR(<=, __less_equal) | |
688 | _DEFINE_BINARY_OPERATOR(>=, __greater_equal) | |
54c1bf78 BK |
689 | |
690 | } // namespace std | |
691 | ||
1143680e | 692 | #endif /* _GLIBCXX_VALARRAY */ |
54c1bf78 BK |
693 | |
694 | // Local Variables: | |
695 | // mode:c++ | |
696 | // End: |