]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/std/std_valarray.h
tree.c (commutative_tree_code, [...]): New functions.
[thirdparty/gcc.git] / libstdc++-v3 / include / std / std_valarray.h
CommitLineData
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 48#include <algorithm>
285b36d6 49#include <debug/debug.h>
54c1bf78
BK
50
51namespace std
52{
74d6b8ca
GDR
53 template<class _Clos, typename _Tp>
54 class _Expr;
54c1bf78 55
74d6b8ca
GDR
56 template<typename _Tp1, typename _Tp2>
57 class _ValArray;
54c1bf78 58
74d6b8ca
GDR
59 template<class _Oper, template<class, class> class _Meta, class _Dom>
60 struct _UnClos;
54c1bf78 61
971cfc6f 62 template<class _Oper,
54c1bf78
BK
63 template<class, class> class _Meta1,
64 template<class, class> class _Meta2,
74d6b8ca
GDR
65 class _Dom1, class _Dom2>
66 class _BinClos;
54c1bf78 67
74d6b8ca
GDR
68 template<template<class, class> class _Meta, class _Dom>
69 class _SClos;
54c1bf78 70
74d6b8ca
GDR
71 template<template<class, class> class _Meta, class _Dom>
72 class _GClos;
54c1bf78 73
74d6b8ca
GDR
74 template<template<class, class> class _Meta, class _Dom>
75 class _IClos;
54c1bf78 76
74d6b8ca
GDR
77 template<template<class, class> class _Meta, class _Dom>
78 class _ValFunClos;
54c1bf78 79
74d6b8ca
GDR
80 template<template<class, class> class _Meta, class _Dom>
81 class _RefFunClos;
82
74d6b8ca
GDR
83 template<class _Tp> class valarray; // An array of type _Tp
84 class slice; // BLAS-like slice out of an array
85 template<class _Tp> class slice_array;
86 class gslice; // generalized slice out of an array
87 template<class _Tp> class gslice_array;
88 template<class _Tp> class mask_array; // masked array
89 template<class _Tp> class indirect_array; // indirected array
54c1bf78
BK
90
91} // namespace std
92
93#include <bits/valarray_array.h>
c13bea50 94#include <bits/valarray_before.h>
54c1bf78
BK
95
96namespace std
97{
74d6b8ca
GDR
98 template<class _Tp>
99 class valarray
100 {
101 template<class _Op>
102 struct _UnaryOp
103 {
104 typedef typename __fun<_Op, _Tp>::result_type __rt;
105 typedef _Expr<_UnClos<_Op, _ValArray, _Tp>, __rt> _Rt;
106 };
107 public:
54c1bf78 108 typedef _Tp value_type;
74d6b8ca
GDR
109
110 // _lib.valarray.cons_ construct/destroy:
54c1bf78
BK
111 valarray();
112 explicit valarray(size_t);
113 valarray(const _Tp&, size_t);
114 valarray(const _Tp* __restrict__, size_t);
115 valarray(const valarray&);
116 valarray(const slice_array<_Tp>&);
117 valarray(const gslice_array<_Tp>&);
118 valarray(const mask_array<_Tp>&);
119 valarray(const indirect_array<_Tp>&);
120 template<class _Dom>
74d6b8ca
GDR
121 valarray(const _Expr<_Dom,_Tp>& __e);
122 ~valarray();
54c1bf78
BK
123
124 // _lib.valarray.assign_ assignment:
125 valarray<_Tp>& operator=(const valarray<_Tp>&);
126 valarray<_Tp>& operator=(const _Tp&);
127 valarray<_Tp>& operator=(const slice_array<_Tp>&);
128 valarray<_Tp>& operator=(const gslice_array<_Tp>&);
129 valarray<_Tp>& operator=(const mask_array<_Tp>&);
130 valarray<_Tp>& operator=(const indirect_array<_Tp>&);
131
132 template<class _Dom> valarray<_Tp>&
74d6b8ca 133 operator= (const _Expr<_Dom,_Tp>&);
54c1bf78
BK
134
135 // _lib.valarray.access_ element access:
136 // XXX: LWG to be resolved.
137 const _Tp& operator[](size_t) const;
138 _Tp& operator[](size_t);
139 // _lib.valarray.sub_ subset operations:
140 _Expr<_SClos<_ValArray,_Tp>, _Tp> operator[](slice) const;
141 slice_array<_Tp> operator[](slice);
142 _Expr<_GClos<_ValArray,_Tp>, _Tp> operator[](const gslice&) const;
143 gslice_array<_Tp> operator[](const gslice&);
144 valarray<_Tp> operator[](const valarray<bool>&) const;
145 mask_array<_Tp> operator[](const valarray<bool>&);
146 _Expr<_IClos<_ValArray, _Tp>, _Tp>
74d6b8ca 147 operator[](const valarray<size_t>&) const;
54c1bf78
BK
148 indirect_array<_Tp> operator[](const valarray<size_t>&);
149
150 // _lib.valarray.unary_ unary operators:
74d6b8ca
GDR
151 typename _UnaryOp<__unary_plus>::_Rt operator+() const;
152 typename _UnaryOp<__negate>::_Rt operator-() const;
153 typename _UnaryOp<__bitwise_not>::_Rt operator~() const;
154 typename _UnaryOp<__logical_not>::_Rt operator!() const;
155
54c1bf78 156 // _lib.valarray.cassign_ computed assignment:
74d6b8ca
GDR
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&);
164 valarray<_Tp>& operator|=(const _Tp&);
54c1bf78
BK
165 valarray<_Tp>& operator<<=(const _Tp&);
166 valarray<_Tp>& operator>>=(const _Tp&);
74d6b8ca
GDR
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>&);
174 valarray<_Tp>& operator&=(const valarray<_Tp>&);
54c1bf78
BK
175 valarray<_Tp>& operator<<=(const valarray<_Tp>&);
176 valarray<_Tp>& operator>>=(const valarray<_Tp>&);
177
178 template<class _Dom>
74d6b8ca 179 valarray<_Tp>& operator*=(const _Expr<_Dom,_Tp>&);
54c1bf78 180 template<class _Dom>
74d6b8ca 181 valarray<_Tp>& operator/=(const _Expr<_Dom,_Tp>&);
54c1bf78 182 template<class _Dom>
74d6b8ca 183 valarray<_Tp>& operator%=(const _Expr<_Dom,_Tp>&);
54c1bf78 184 template<class _Dom>
74d6b8ca 185 valarray<_Tp>& operator+=(const _Expr<_Dom,_Tp>&);
54c1bf78 186 template<class _Dom>
74d6b8ca 187 valarray<_Tp>& operator-=(const _Expr<_Dom,_Tp>&);
54c1bf78 188 template<class _Dom>
74d6b8ca 189 valarray<_Tp>& operator^=(const _Expr<_Dom,_Tp>&);
54c1bf78 190 template<class _Dom>
74d6b8ca 191 valarray<_Tp>& operator|=(const _Expr<_Dom,_Tp>&);
54c1bf78 192 template<class _Dom>
74d6b8ca 193 valarray<_Tp>& operator&=(const _Expr<_Dom,_Tp>&);
54c1bf78 194 template<class _Dom>
74d6b8ca 195 valarray<_Tp>& operator<<=(const _Expr<_Dom,_Tp>&);
54c1bf78 196 template<class _Dom>
74d6b8ca
GDR
197 valarray<_Tp>& operator>>=(const _Expr<_Dom,_Tp>&);
198
54c1bf78 199
54c1bf78
BK
200 // _lib.valarray.members_ member functions:
201 size_t size() const;
202 _Tp sum() const;
203 _Tp min() const;
204 _Tp max() const;
205
74d6b8ca
GDR
206 // // FIXME: Extension
207 // _Tp product () const;
54c1bf78
BK
208
209 valarray<_Tp> shift (int) const;
210 valarray<_Tp> cshift(int) const;
211 _Expr<_ValFunClos<_ValArray,_Tp>,_Tp> apply(_Tp func(_Tp)) const;
212 _Expr<_RefFunClos<_ValArray,_Tp>,_Tp> apply(_Tp func(const _Tp&)) const;
213 void resize(size_t __size, _Tp __c = _Tp());
214
74d6b8ca 215 private:
54c1bf78
BK
216 size_t _M_size;
217 _Tp* __restrict__ _M_data;
74d6b8ca 218
54c1bf78 219 friend class _Array<_Tp>;
74d6b8ca 220 };
54c1bf78
BK
221
222 template<typename _Tp>
74d6b8ca
GDR
223 inline const _Tp&
224 valarray<_Tp>::operator[](size_t __i) const
285b36d6
BK
225 {
226 __glibcxx_requires_subscript(__i);
227 return _M_data[__i];
228 }
54c1bf78
BK
229
230 template<typename _Tp>
74d6b8ca
GDR
231 inline _Tp&
232 valarray<_Tp>::operator[](size_t __i)
285b36d6
BK
233 {
234 __glibcxx_requires_subscript(__i);
235 return _M_data[__i];
236 }
54c1bf78
BK
237
238} // std::
c13bea50
NS
239
240#include <bits/valarray_after.h>
241
54c1bf78
BK
242#include <bits/slice_array.h>
243#include <bits/gslice.h>
244#include <bits/gslice_array.h>
245#include <bits/mask_array.h>
246#include <bits/indirect_array.h>
247
248namespace std
249{
250 template<typename _Tp>
74d6b8ca
GDR
251 inline
252 valarray<_Tp>::valarray() : _M_size(0), _M_data(0) {}
54c1bf78
BK
253
254 template<typename _Tp>
74d6b8ca
GDR
255 inline
256 valarray<_Tp>::valarray(size_t __n)
257 : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n))
eb9a4231 258 { std::__valarray_default_construct(_M_data, _M_data + __n); }
54c1bf78
BK
259
260 template<typename _Tp>
74d6b8ca
GDR
261 inline
262 valarray<_Tp>::valarray(const _Tp& __t, size_t __n)
263 : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n))
eb9a4231 264 { std::__valarray_fill_construct(_M_data, _M_data + __n, __t); }
54c1bf78
BK
265
266 template<typename _Tp>
74d6b8ca
GDR
267 inline
268 valarray<_Tp>::valarray(const _Tp* __restrict__ __p, size_t __n)
269 : _M_size(__n), _M_data(__valarray_get_storage<_Tp>(__n))
285b36d6
BK
270 {
271 _GLIBCXX_DEBUG_ASSERT(__p != 0 || __n == 0);
272 std::__valarray_copy_construct(__p, __p + __n, _M_data);
273 }
54c1bf78
BK
274
275 template<typename _Tp>
74d6b8ca
GDR
276 inline
277 valarray<_Tp>::valarray(const valarray<_Tp>& __v)
278 : _M_size(__v._M_size), _M_data(__valarray_get_storage<_Tp>(__v._M_size))
eb9a4231 279 { std::__valarray_copy_construct(__v._M_data, __v._M_data + _M_size, _M_data); }
54c1bf78
BK
280
281 template<typename _Tp>
74d6b8ca
GDR
282 inline
283 valarray<_Tp>::valarray(const slice_array<_Tp>& __sa)
284 : _M_size(__sa._M_sz), _M_data(__valarray_get_storage<_Tp>(__sa._M_sz))
285 {
eb9a4231 286 std::__valarray_copy
74d6b8ca
GDR
287 (__sa._M_array, __sa._M_sz, __sa._M_stride, _Array<_Tp>(_M_data));
288 }
54c1bf78
BK
289
290 template<typename _Tp>
74d6b8ca
GDR
291 inline
292 valarray<_Tp>::valarray(const gslice_array<_Tp>& __ga)
293 : _M_size(__ga._M_index.size()),
294 _M_data(__valarray_get_storage<_Tp>(_M_size))
295 {
eb9a4231 296 std::__valarray_copy
74d6b8ca
GDR
297 (__ga._M_array, _Array<size_t>(__ga._M_index),
298 _Array<_Tp>(_M_data), _M_size);
299 }
54c1bf78
BK
300
301 template<typename _Tp>
74d6b8ca
GDR
302 inline
303 valarray<_Tp>::valarray(const mask_array<_Tp>& __ma)
304 : _M_size(__ma._M_sz), _M_data(__valarray_get_storage<_Tp>(__ma._M_sz))
305 {
eb9a4231 306 std::__valarray_copy
74d6b8ca
GDR
307 (__ma._M_array, __ma._M_mask, _Array<_Tp>(_M_data), _M_size);
308 }
54c1bf78
BK
309
310 template<typename _Tp>
74d6b8ca
GDR
311 inline
312 valarray<_Tp>::valarray(const indirect_array<_Tp>& __ia)
313 : _M_size(__ia._M_sz), _M_data(__valarray_get_storage<_Tp>(__ia._M_sz))
314 {
eb9a4231 315 std::__valarray_copy
74d6b8ca
GDR
316 (__ia._M_array, __ia._M_index, _Array<_Tp>(_M_data), _M_size);
317 }
54c1bf78
BK
318
319 template<typename _Tp> template<class _Dom>
74d6b8ca
GDR
320 inline
321 valarray<_Tp>::valarray(const _Expr<_Dom, _Tp>& __e)
322 : _M_size(__e.size()), _M_data(__valarray_get_storage<_Tp>(_M_size))
eb9a4231 323 { std::__valarray_copy(__e, _M_size, _Array<_Tp>(_M_data)); }
54c1bf78
BK
324
325 template<typename _Tp>
74d6b8ca
GDR
326 inline
327 valarray<_Tp>::~valarray()
328 {
eb9a4231
PC
329 std::__valarray_destroy_elements(_M_data, _M_data + _M_size);
330 std::__valarray_release_memory(_M_data);
74d6b8ca 331 }
54c1bf78
BK
332
333 template<typename _Tp>
74d6b8ca
GDR
334 inline valarray<_Tp>&
335 valarray<_Tp>::operator=(const valarray<_Tp>& __v)
336 {
285b36d6 337 _GLIBCXX_DEBUG_ASSERT(_M_size == __v._M_size);
eb9a4231 338 std::__valarray_copy(__v._M_data, _M_size, _M_data);
54c1bf78 339 return *this;
74d6b8ca 340 }
54c1bf78
BK
341
342 template<typename _Tp>
74d6b8ca
GDR
343 inline valarray<_Tp>&
344 valarray<_Tp>::operator=(const _Tp& __t)
345 {
eb9a4231 346 std::__valarray_fill(_M_data, _M_size, __t);
54c1bf78 347 return *this;
74d6b8ca 348 }
54c1bf78
BK
349
350 template<typename _Tp>
74d6b8ca
GDR
351 inline valarray<_Tp>&
352 valarray<_Tp>::operator=(const slice_array<_Tp>& __sa)
353 {
285b36d6 354 _GLIBCXX_DEBUG_ASSERT(_M_size == __sa._M_sz);
eb9a4231
PC
355 std::__valarray_copy(__sa._M_array, __sa._M_sz,
356 __sa._M_stride, _Array<_Tp>(_M_data));
54c1bf78 357 return *this;
74d6b8ca 358 }
54c1bf78
BK
359
360 template<typename _Tp>
74d6b8ca
GDR
361 inline valarray<_Tp>&
362 valarray<_Tp>::operator=(const gslice_array<_Tp>& __ga)
363 {
285b36d6 364 _GLIBCXX_DEBUG_ASSERT(_M_size == __ga._M_index.size());
eb9a4231
PC
365 std::__valarray_copy(__ga._M_array, _Array<size_t>(__ga._M_index),
366 _Array<_Tp>(_M_data), _M_size);
54c1bf78 367 return *this;
74d6b8ca 368 }
54c1bf78
BK
369
370 template<typename _Tp>
74d6b8ca
GDR
371 inline valarray<_Tp>&
372 valarray<_Tp>::operator=(const mask_array<_Tp>& __ma)
373 {
285b36d6 374 _GLIBCXX_DEBUG_ASSERT(_M_size == __ma._M_sz);
eb9a4231
PC
375 std::__valarray_copy(__ma._M_array, __ma._M_mask,
376 _Array<_Tp>(_M_data), _M_size);
54c1bf78 377 return *this;
74d6b8ca 378 }
54c1bf78
BK
379
380 template<typename _Tp>
74d6b8ca
GDR
381 inline valarray<_Tp>&
382 valarray<_Tp>::operator=(const indirect_array<_Tp>& __ia)
383 {
285b36d6 384 _GLIBCXX_DEBUG_ASSERT(_M_size == __ia._M_sz);
eb9a4231
PC
385 std::__valarray_copy(__ia._M_array, __ia._M_index,
386 _Array<_Tp>(_M_data), _M_size);
54c1bf78 387 return *this;
74d6b8ca 388 }
54c1bf78
BK
389
390 template<typename _Tp> template<class _Dom>
74d6b8ca
GDR
391 inline valarray<_Tp>&
392 valarray<_Tp>::operator=(const _Expr<_Dom, _Tp>& __e)
393 {
285b36d6 394 _GLIBCXX_DEBUG_ASSERT(_M_size == __e.size());
eb9a4231
PC
395 std::__valarray_copy(__e, _M_size, _Array<_Tp>(_M_data));
396 return *this;
74d6b8ca 397 }
54c1bf78
BK
398
399 template<typename _Tp>
74d6b8ca
GDR
400 inline _Expr<_SClos<_ValArray,_Tp>, _Tp>
401 valarray<_Tp>::operator[](slice __s) const
402 {
54c1bf78 403 typedef _SClos<_ValArray,_Tp> _Closure;
74d6b8ca
GDR
404 return _Expr<_Closure, _Tp>(_Closure (_Array<_Tp>(_M_data), __s));
405 }
54c1bf78
BK
406
407 template<typename _Tp>
74d6b8ca
GDR
408 inline slice_array<_Tp>
409 valarray<_Tp>::operator[](slice __s)
410 {
411 return slice_array<_Tp>(_Array<_Tp>(_M_data), __s);
412 }
54c1bf78
BK
413
414 template<typename _Tp>
74d6b8ca
GDR
415 inline _Expr<_GClos<_ValArray,_Tp>, _Tp>
416 valarray<_Tp>::operator[](const gslice& __gs) const
417 {
54c1bf78
BK
418 typedef _GClos<_ValArray,_Tp> _Closure;
419 return _Expr<_Closure, _Tp>
74d6b8ca
GDR
420 (_Closure(_Array<_Tp>(_M_data), __gs._M_index->_M_index));
421 }
54c1bf78
BK
422
423 template<typename _Tp>
74d6b8ca
GDR
424 inline gslice_array<_Tp>
425 valarray<_Tp>::operator[](const gslice& __gs)
426 {
54c1bf78 427 return gslice_array<_Tp>
74d6b8ca
GDR
428 (_Array<_Tp>(_M_data), __gs._M_index->_M_index);
429 }
54c1bf78
BK
430
431 template<typename _Tp>
74d6b8ca
GDR
432 inline valarray<_Tp>
433 valarray<_Tp>::operator[](const valarray<bool>& __m) const
434 {
435 size_t __s = 0;
436 size_t __e = __m.size();
54c1bf78 437 for (size_t __i=0; __i<__e; ++__i)
74d6b8ca
GDR
438 if (__m[__i]) ++__s;
439 return valarray<_Tp>(mask_array<_Tp>(_Array<_Tp>(_M_data), __s,
440 _Array<bool> (__m)));
441 }
54c1bf78
BK
442
443 template<typename _Tp>
74d6b8ca
GDR
444 inline mask_array<_Tp>
445 valarray<_Tp>::operator[](const valarray<bool>& __m)
446 {
447 size_t __s = 0;
448 size_t __e = __m.size();
54c1bf78 449 for (size_t __i=0; __i<__e; ++__i)
74d6b8ca
GDR
450 if (__m[__i]) ++__s;
451 return mask_array<_Tp>(_Array<_Tp>(_M_data), __s, _Array<bool>(__m));
452 }
54c1bf78
BK
453
454 template<typename _Tp>
74d6b8ca
GDR
455 inline _Expr<_IClos<_ValArray,_Tp>, _Tp>
456 valarray<_Tp>::operator[](const valarray<size_t>& __i) const
457 {
54c1bf78 458 typedef _IClos<_ValArray,_Tp> _Closure;
74d6b8ca
GDR
459 return _Expr<_Closure, _Tp>(_Closure(*this, __i));
460 }
54c1bf78
BK
461
462 template<typename _Tp>
74d6b8ca
GDR
463 inline indirect_array<_Tp>
464 valarray<_Tp>::operator[](const valarray<size_t>& __i)
465 {
466 return indirect_array<_Tp>(_Array<_Tp>(_M_data), __i.size(),
467 _Array<size_t>(__i));
468 }
54c1bf78
BK
469
470 template<class _Tp>
74d6b8ca
GDR
471 inline size_t
472 valarray<_Tp>::size() const
473 { return _M_size; }
54c1bf78
BK
474
475 template<class _Tp>
74d6b8ca
GDR
476 inline _Tp
477 valarray<_Tp>::sum() const
478 {
285b36d6 479 _GLIBCXX_DEBUG_ASSERT(_M_size > 0);
eb9a4231 480 return std::__valarray_sum(_M_data, _M_data + _M_size);
74d6b8ca 481 }
54c1bf78
BK
482
483// template<typename _Tp>
484// inline _Tp
485// valarray<_Tp>::product () const
486// {
487// return __valarray_product(_M_data, _M_data + _M_size);
488// }
489
490 template <class _Tp>
491 inline valarray<_Tp>
492 valarray<_Tp>::shift(int __n) const
493 {
494 _Tp* const __a = static_cast<_Tp*>
495 (__builtin_alloca(sizeof(_Tp) * _M_size));
496 if (__n == 0) // no shift
eb9a4231 497 std::__valarray_copy_construct(_M_data, _M_data + _M_size, __a);
54c1bf78
BK
498 else if (__n > 0) // __n > 0: shift left
499 {
500 if (size_t(__n) > _M_size)
eb9a4231 501 std::__valarray_default_construct(__a, __a + __n);
54c1bf78
BK
502 else
503 {
eb9a4231
PC
504 std::__valarray_copy_construct(_M_data+__n, _M_data + _M_size, __a);
505 std::__valarray_default_construct(__a+_M_size-__n, __a + _M_size);
54c1bf78
BK
506 }
507 }
508 else // __n < 0: shift right
509 {
eb9a4231
PC
510 std::__valarray_copy_construct (_M_data, _M_data+_M_size+__n, __a-__n);
511 std::__valarray_default_construct(__a, __a - __n);
54c1bf78
BK
512 }
513 return valarray<_Tp> (__a, _M_size);
514 }
515
516 template <class _Tp>
517 inline valarray<_Tp>
518 valarray<_Tp>::cshift (int __n) const
519 {
520 _Tp* const __a = static_cast<_Tp*>
521 (__builtin_alloca (sizeof(_Tp) * _M_size));
522 if (__n == 0) // no cshift
eb9a4231 523 std::__valarray_copy_construct(_M_data, _M_data + _M_size, __a);
54c1bf78
BK
524 else if (__n > 0) // cshift left
525 {
eb9a4231
PC
526 std::__valarray_copy_construct(_M_data, _M_data+__n, __a+_M_size-__n);
527 std::__valarray_copy_construct(_M_data+__n, _M_data + _M_size, __a);
54c1bf78
BK
528 }
529 else // cshift right
530 {
eb9a4231 531 std::__valarray_copy_construct
54c1bf78 532 (_M_data + _M_size+__n, _M_data + _M_size, __a);
eb9a4231 533 std::__valarray_copy_construct
54c1bf78
BK
534 (_M_data, _M_data + _M_size+__n, __a - __n);
535 }
536 return valarray<_Tp>(__a, _M_size);
537 }
538
539 template <class _Tp>
74d6b8ca
GDR
540 inline void
541 valarray<_Tp>::resize (size_t __n, _Tp __c)
542 {
543 // This complication is so to make valarray<valarray<T> > work
544 // even though it is not required by the standard. Nobody should
545 // be saying valarray<valarray<T> > anyway. See the specs.
eb9a4231 546 std::__valarray_destroy_elements(_M_data, _M_data + _M_size);
74d6b8ca
GDR
547 if (_M_size != __n)
548 {
eb9a4231 549 std::__valarray_release_memory(_M_data);
74d6b8ca
GDR
550 _M_size = __n;
551 _M_data = __valarray_get_storage<_Tp>(__n);
552 }
eb9a4231 553 std::__valarray_fill_construct(_M_data, _M_data + __n, __c);
74d6b8ca 554 }
54c1bf78
BK
555
556 template<typename _Tp>
74d6b8ca
GDR
557 inline _Tp
558 valarray<_Tp>::min() const
559 {
285b36d6 560 _GLIBCXX_DEBUG_ASSERT(_M_size > 0);
eb9a4231 561 return *std::min_element (_M_data, _M_data+_M_size);
74d6b8ca 562 }
54c1bf78
BK
563
564 template<typename _Tp>
74d6b8ca
GDR
565 inline _Tp
566 valarray<_Tp>::max() const
567 {
285b36d6 568 _GLIBCXX_DEBUG_ASSERT(_M_size > 0);
eb9a4231 569 return *std::max_element (_M_data, _M_data+_M_size);
74d6b8ca 570 }
54c1bf78
BK
571
572 template<class _Tp>
74d6b8ca
GDR
573 inline _Expr<_ValFunClos<_ValArray,_Tp>,_Tp>
574 valarray<_Tp>::apply(_Tp func(_Tp)) const
575 {
54c1bf78 576 typedef _ValFunClos<_ValArray,_Tp> _Closure;
74d6b8ca
GDR
577 return _Expr<_Closure,_Tp>(_Closure(*this, func));
578 }
54c1bf78
BK
579
580 template<class _Tp>
74d6b8ca
GDR
581 inline _Expr<_RefFunClos<_ValArray,_Tp>,_Tp>
582 valarray<_Tp>::apply(_Tp func(const _Tp &)) const
583 {
54c1bf78 584 typedef _RefFunClos<_ValArray,_Tp> _Closure;
74d6b8ca
GDR
585 return _Expr<_Closure,_Tp>(_Closure(*this, func));
586 }
54c1bf78
BK
587
588#define _DEFINE_VALARRAY_UNARY_OPERATOR(_Op, _Name) \
589 template<typename _Tp> \
74d6b8ca 590 inline typename valarray<_Tp>::template _UnaryOp<_Name>::_Rt \
54c1bf78
BK
591 valarray<_Tp>::operator _Op() const \
592 { \
74d6b8ca
GDR
593 typedef _UnClos<_Name,_ValArray,_Tp> _Closure; \
594 typedef typename __fun<_Name, _Tp>::result_type _Rt; \
595 return _Expr<_Closure, _Rt>(_Closure(*this)); \
54c1bf78
BK
596 }
597
74d6b8ca
GDR
598 _DEFINE_VALARRAY_UNARY_OPERATOR(+, __unary_plus)
599 _DEFINE_VALARRAY_UNARY_OPERATOR(-, __negate)
600 _DEFINE_VALARRAY_UNARY_OPERATOR(~, __bitwise_not)
601 _DEFINE_VALARRAY_UNARY_OPERATOR (!, __logical_not)
54c1bf78
BK
602
603#undef _DEFINE_VALARRAY_UNARY_OPERATOR
54c1bf78
BK
604
605#define _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(_Op, _Name) \
606 template<class _Tp> \
971cfc6f
GDR
607 inline valarray<_Tp>& \
608 valarray<_Tp>::operator _Op##=(const _Tp &__t) \
609 { \
610 _Array_augmented_##_Name(_Array<_Tp>(_M_data), _M_size, __t); \
54c1bf78 611 return *this; \
971cfc6f 612 } \
54c1bf78
BK
613 \
614 template<class _Tp> \
971cfc6f
GDR
615 inline valarray<_Tp>& \
616 valarray<_Tp>::operator _Op##=(const valarray<_Tp> &__v) \
617 { \
285b36d6 618 _GLIBCXX_DEBUG_ASSERT(_M_size == __v._M_size); \
971cfc6f
GDR
619 _Array_augmented_##_Name(_Array<_Tp>(_M_data), _M_size, \
620 _Array<_Tp>(__v._M_data)); \
54c1bf78 621 return *this; \
971cfc6f 622 }
54c1bf78 623
971cfc6f
GDR
624_DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(+, __plus)
625_DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(-, __minus)
626_DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(*, __multiplies)
627_DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(/, __divides)
628_DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(%, __modulus)
629_DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(^, __bitwise_xor)
630_DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(&, __bitwise_and)
631_DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(|, __bitwise_or)
632_DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(<<, __shift_left)
633_DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(>>, __shift_right)
54c1bf78
BK
634
635#undef _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT
636
54c1bf78
BK
637#define _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(_Op, _Name) \
638 template<class _Tp> template<class _Dom> \
971cfc6f
GDR
639 inline valarray<_Tp>& \
640 valarray<_Tp>::operator _Op##=(const _Expr<_Dom,_Tp>& __e) \
641 { \
642 _Array_augmented_##_Name(_Array<_Tp>(_M_data), __e, _M_size); \
54c1bf78 643 return *this; \
971cfc6f 644 }
54c1bf78 645
971cfc6f
GDR
646_DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(+, __plus)
647_DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(-, __minus)
648_DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(*, __multiplies)
649_DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(/, __divides)
650_DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(%, __modulus)
651_DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(^, __bitwise_xor)
652_DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(&, __bitwise_and)
653_DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(|, __bitwise_or)
654_DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(<<, __shift_left)
655_DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(>>, __shift_right)
54c1bf78
BK
656
657#undef _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT
658
659
660#define _DEFINE_BINARY_OPERATOR(_Op, _Name) \
661 template<typename _Tp> \
971cfc6f
GDR
662 inline _Expr<_BinClos<_Name,_ValArray,_ValArray,_Tp,_Tp>, \
663 typename __fun<_Name, _Tp>::result_type> \
664 operator _Op(const valarray<_Tp>& __v, const valarray<_Tp>& __w) \
665 { \
285b36d6 666 _GLIBCXX_DEBUG_ASSERT(__v.size() == __w.size()); \
54c1bf78 667 typedef _BinClos<_Name,_ValArray,_ValArray,_Tp,_Tp> _Closure; \
971cfc6f
GDR
668 typedef typename __fun<_Name, _Tp>::result_type _Rt; \
669 return _Expr<_Closure, _Rt>(_Closure(__v, __w)); \
670 } \
54c1bf78
BK
671 \
672 template<typename _Tp> \
971cfc6f
GDR
673 inline _Expr<_BinClos<_Name,_ValArray,_Constant,_Tp,_Tp>, \
674 typename __fun<_Name, _Tp>::result_type> \
675 operator _Op(const valarray<_Tp>& __v, const _Tp& __t) \
54c1bf78 676 { \
971cfc6f
GDR
677 typedef _BinClos<_Name,_ValArray,_Constant,_Tp,_Tp> _Closure; \
678 typedef typename __fun<_Name, _Tp>::result_type _Rt; \
679 return _Expr<_Closure, _Rt>(_Closure(__v, __t)); \
54c1bf78
BK
680 } \
681 \
682 template<typename _Tp> \
971cfc6f
GDR
683 inline _Expr<_BinClos<_Name,_Constant,_ValArray,_Tp,_Tp>, \
684 typename __fun<_Name, _Tp>::result_type> \
685 operator _Op(const _Tp& __t, const valarray<_Tp>& __v) \
54c1bf78 686 { \
971cfc6f
GDR
687 typedef _BinClos<_Name,_Constant,_ValArray,_Tp,_Tp> _Closure; \
688 typedef typename __fun<_Name, _Tp>::result_type _Rt; \
689 return _Expr<_Closure, _Tp>(_Closure(__t, __v)); \
54c1bf78
BK
690 }
691
971cfc6f
GDR
692_DEFINE_BINARY_OPERATOR(+, __plus)
693_DEFINE_BINARY_OPERATOR(-, __minus)
694_DEFINE_BINARY_OPERATOR(*, __multiplies)
695_DEFINE_BINARY_OPERATOR(/, __divides)
696_DEFINE_BINARY_OPERATOR(%, __modulus)
697_DEFINE_BINARY_OPERATOR(^, __bitwise_xor)
698_DEFINE_BINARY_OPERATOR(&, __bitwise_and)
699_DEFINE_BINARY_OPERATOR(|, __bitwise_or)
700_DEFINE_BINARY_OPERATOR(<<, __shift_left)
701_DEFINE_BINARY_OPERATOR(>>, __shift_right)
702_DEFINE_BINARY_OPERATOR(&&, __logical_and)
703_DEFINE_BINARY_OPERATOR(||, __logical_or)
704_DEFINE_BINARY_OPERATOR(==, __equal_to)
705_DEFINE_BINARY_OPERATOR(!=, __not_equal_to)
706_DEFINE_BINARY_OPERATOR(<, __less)
707_DEFINE_BINARY_OPERATOR(>, __greater)
708_DEFINE_BINARY_OPERATOR(<=, __less_equal)
709_DEFINE_BINARY_OPERATOR(>=, __greater_equal)
54c1bf78
BK
710
711} // namespace std
712
1143680e 713#endif /* _GLIBCXX_VALARRAY */