]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/libc++/include/valarray
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / libc++ / include / valarray
1 // -*- C++ -*-
2 //===-------------------------- valarray ----------------------------------===//
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10
11 #ifndef _LIBCPP_VALARRAY
12 #define _LIBCPP_VALARRAY
13
14 /*
15     valarray synopsis
16
17 namespace std
18 {
19
20 template<class T>
21 class valarray
22 {
23 public:
24     typedef T value_type;
25
26     // construct/destroy:
27     valarray();
28     explicit valarray(size_t n);
29     valarray(const value_type& x, size_t n);
30     valarray(const value_type* px, size_t n);
31     valarray(const valarray& v);
32     valarray(valarray&& v) noexcept;
33     valarray(const slice_array<value_type>& sa);
34     valarray(const gslice_array<value_type>& ga);
35     valarray(const mask_array<value_type>& ma);
36     valarray(const indirect_array<value_type>& ia);
37     valarray(initializer_list<value_type> il);
38     ~valarray();
39
40     // assignment:
41     valarray& operator=(const valarray& v);
42     valarray& operator=(valarray&& v) noexcept;
43     valarray& operator=(initializer_list<value_type> il);
44     valarray& operator=(const value_type& x);
45     valarray& operator=(const slice_array<value_type>& sa);
46     valarray& operator=(const gslice_array<value_type>& ga);
47     valarray& operator=(const mask_array<value_type>& ma);
48     valarray& operator=(const indirect_array<value_type>& ia);
49
50     // element access:
51     const value_type& operator[](size_t i) const;
52     value_type&       operator[](size_t i);
53
54     // subset operations:
55     valarray                   operator[](slice s) const;
56     slice_array<value_type>    operator[](slice s);
57     valarray                   operator[](const gslice& gs) const;
58     gslice_array<value_type>   operator[](const gslice& gs);
59     valarray                   operator[](const valarray<bool>& vb) const;
60     mask_array<value_type>     operator[](const valarray<bool>& vb);
61     valarray                   operator[](const valarray<size_t>& vs) const;
62     indirect_array<value_type> operator[](const valarray<size_t>& vs);
63
64     // unary operators:
65     valarray       operator+() const;
66     valarray       operator-() const;
67     valarray       operator~() const;
68     valarray<bool> operator!() const;
69
70     // computed assignment:
71     valarray& operator*= (const value_type& x);
72     valarray& operator/= (const value_type& x);
73     valarray& operator%= (const value_type& x);
74     valarray& operator+= (const value_type& x);
75     valarray& operator-= (const value_type& x);
76     valarray& operator^= (const value_type& x);
77     valarray& operator&= (const value_type& x);
78     valarray& operator|= (const value_type& x);
79     valarray& operator<<=(const value_type& x);
80     valarray& operator>>=(const value_type& x);
81
82     valarray& operator*= (const valarray& v);
83     valarray& operator/= (const valarray& v);
84     valarray& operator%= (const valarray& v);
85     valarray& operator+= (const valarray& v);
86     valarray& operator-= (const valarray& v);
87     valarray& operator^= (const valarray& v);
88     valarray& operator|= (const valarray& v);
89     valarray& operator&= (const valarray& v);
90     valarray& operator<<=(const valarray& v);
91     valarray& operator>>=(const valarray& v);
92
93     // member functions:
94     void swap(valarray& v) noexcept;
95
96     size_t size() const;
97
98     value_type sum() const;
99     value_type min() const;
100     value_type max() const;
101
102     valarray shift (int i) const;
103     valarray cshift(int i) const;
104     valarray apply(value_type f(value_type)) const;
105     valarray apply(value_type f(const value_type&)) const;
106     void resize(size_t n, value_type x = value_type());
107 };
108
109 class slice
110 {
111 public:
112     slice();
113     slice(size_t start, size_t size, size_t stride);
114
115     size_t start()  const;
116     size_t size()   const;
117     size_t stride() const;
118 };
119
120 template <class T>
121 class slice_array
122 {
123 public:
124     typedef T value_type;
125
126     const slice_array& operator=(const slice_array& sa) const;
127     void operator=  (const valarray<value_type>& v) const;
128     void operator*= (const valarray<value_type>& v) const;
129     void operator/= (const valarray<value_type>& v) const;
130     void operator%= (const valarray<value_type>& v) const;
131     void operator+= (const valarray<value_type>& v) const;
132     void operator-= (const valarray<value_type>& v) const;
133     void operator^= (const valarray<value_type>& v) const;
134     void operator&= (const valarray<value_type>& v) const;
135     void operator|= (const valarray<value_type>& v) const;
136     void operator<<=(const valarray<value_type>& v) const;
137     void operator>>=(const valarray<value_type>& v) const;
138
139     void operator=(const value_type& x) const;
140
141     slice_array() = delete;
142 };
143
144 class gslice
145 {
146 public:
147     gslice();
148     gslice(size_t start, const valarray<size_t>& size,
149                          const valarray<size_t>& stride);
150
151     size_t           start()  const;
152     valarray<size_t> size()   const;
153     valarray<size_t> stride() const;
154 };
155
156 template <class T>
157 class gslice_array
158 {
159 public:
160     typedef T value_type;
161
162     void operator=  (const valarray<value_type>& v) const;
163     void operator*= (const valarray<value_type>& v) const;
164     void operator/= (const valarray<value_type>& v) const;
165     void operator%= (const valarray<value_type>& v) const;
166     void operator+= (const valarray<value_type>& v) const;
167     void operator-= (const valarray<value_type>& v) const;
168     void operator^= (const valarray<value_type>& v) const;
169     void operator&= (const valarray<value_type>& v) const;
170     void operator|= (const valarray<value_type>& v) const;
171     void operator<<=(const valarray<value_type>& v) const;
172     void operator>>=(const valarray<value_type>& v) const;
173
174     gslice_array(const gslice_array& ga);
175     ~gslice_array();
176     const gslice_array& operator=(const gslice_array& ga) const;
177     void operator=(const value_type& x) const;
178
179     gslice_array() = delete;
180 };
181
182 template <class T>
183 class mask_array
184 {
185 public:
186     typedef T value_type;
187
188     void operator=  (const valarray<value_type>& v) const;
189     void operator*= (const valarray<value_type>& v) const;
190     void operator/= (const valarray<value_type>& v) const;
191     void operator%= (const valarray<value_type>& v) const;
192     void operator+= (const valarray<value_type>& v) const;
193     void operator-= (const valarray<value_type>& v) const;
194     void operator^= (const valarray<value_type>& v) const;
195     void operator&= (const valarray<value_type>& v) const;
196     void operator|= (const valarray<value_type>& v) const;
197     void operator<<=(const valarray<value_type>& v) const;
198     void operator>>=(const valarray<value_type>& v) const;
199
200     mask_array(const mask_array& ma);
201     ~mask_array();
202     const mask_array& operator=(const mask_array& ma) const;
203     void operator=(const value_type& x) const;
204
205     mask_array() = delete;
206 };
207
208 template <class T>
209 class indirect_array
210 {
211 public:
212     typedef T value_type;
213
214     void operator=  (const valarray<value_type>& v) const;
215     void operator*= (const valarray<value_type>& v) const;
216     void operator/= (const valarray<value_type>& v) const;
217     void operator%= (const valarray<value_type>& v) const;
218     void operator+= (const valarray<value_type>& v) const;
219     void operator-= (const valarray<value_type>& v) const;
220     void operator^= (const valarray<value_type>& v) const;
221     void operator&= (const valarray<value_type>& v) const;
222     void operator|= (const valarray<value_type>& v) const;
223     void operator<<=(const valarray<value_type>& v) const;
224     void operator>>=(const valarray<value_type>& v) const;
225
226     indirect_array(const indirect_array& ia);
227     ~indirect_array();
228     const indirect_array& operator=(const indirect_array& ia) const;
229     void operator=(const value_type& x) const;
230
231     indirect_array() = delete;
232 };
233
234 template<class T> void swap(valarray<T>& x, valarray<T>& y) noexcept;
235
236 template<class T> valarray<T> operator* (const valarray<T>& x, const valarray<T>& y);
237 template<class T> valarray<T> operator* (const valarray<T>& x, const T& y);
238 template<class T> valarray<T> operator* (const T& x, const valarray<T>& y);
239
240 template<class T> valarray<T> operator/ (const valarray<T>& x, const valarray<T>& y);
241 template<class T> valarray<T> operator/ (const valarray<T>& x, const T& y);
242 template<class T> valarray<T> operator/ (const T& x, const valarray<T>& y);
243
244 template<class T> valarray<T> operator% (const valarray<T>& x, const valarray<T>& y);
245 template<class T> valarray<T> operator% (const valarray<T>& x, const T& y);
246 template<class T> valarray<T> operator% (const T& x, const valarray<T>& y);
247
248 template<class T> valarray<T> operator+ (const valarray<T>& x, const valarray<T>& y);
249 template<class T> valarray<T> operator+ (const valarray<T>& x, const T& y);
250 template<class T> valarray<T> operator+ (const T& x, const valarray<T>& y);
251
252 template<class T> valarray<T> operator- (const valarray<T>& x, const valarray<T>& y);
253 template<class T> valarray<T> operator- (const valarray<T>& x, const T& y);
254 template<class T> valarray<T> operator- (const T& x, const valarray<T>& y);
255
256 template<class T> valarray<T> operator^ (const valarray<T>& x, const valarray<T>& y);
257 template<class T> valarray<T> operator^ (const valarray<T>& x, const T& y);
258 template<class T> valarray<T> operator^ (const T& x, const valarray<T>& y);
259
260 template<class T> valarray<T> operator& (const valarray<T>& x, const valarray<T>& y);
261 template<class T> valarray<T> operator& (const valarray<T>& x, const T& y);
262 template<class T> valarray<T> operator& (const T& x, const valarray<T>& y);
263
264 template<class T> valarray<T> operator| (const valarray<T>& x, const valarray<T>& y);
265 template<class T> valarray<T> operator| (const valarray<T>& x, const T& y);
266 template<class T> valarray<T> operator| (const T& x, const valarray<T>& y);
267
268 template<class T> valarray<T> operator<<(const valarray<T>& x, const valarray<T>& y);
269 template<class T> valarray<T> operator<<(const valarray<T>& x, const T& y);
270 template<class T> valarray<T> operator<<(const T& x, const valarray<T>& y);
271
272 template<class T> valarray<T> operator>>(const valarray<T>& x, const valarray<T>& y);
273 template<class T> valarray<T> operator>>(const valarray<T>& x, const T& y);
274 template<class T> valarray<T> operator>>(const T& x, const valarray<T>& y);
275
276 template<class T> valarray<bool> operator&&(const valarray<T>& x, const valarray<T>& y);
277 template<class T> valarray<bool> operator&&(const valarray<T>& x, const T& y);
278 template<class T> valarray<bool> operator&&(const T& x, const valarray<T>& y);
279
280 template<class T> valarray<bool> operator||(const valarray<T>& x, const valarray<T>& y);
281 template<class T> valarray<bool> operator||(const valarray<T>& x, const T& y);
282 template<class T> valarray<bool> operator||(const T& x, const valarray<T>& y);
283
284 template<class T> valarray<bool> operator==(const valarray<T>& x, const valarray<T>& y);
285 template<class T> valarray<bool> operator==(const valarray<T>& x, const T& y);
286 template<class T> valarray<bool> operator==(const T& x, const valarray<T>& y);
287
288 template<class T> valarray<bool> operator!=(const valarray<T>& x, const valarray<T>& y);
289 template<class T> valarray<bool> operator!=(const valarray<T>& x, const T& y);
290 template<class T> valarray<bool> operator!=(const T& x, const valarray<T>& y);
291
292 template<class T> valarray<bool> operator< (const valarray<T>& x, const valarray<T>& y);
293 template<class T> valarray<bool> operator< (const valarray<T>& x, const T& y);
294 template<class T> valarray<bool> operator< (const T& x, const valarray<T>& y);
295
296 template<class T> valarray<bool> operator> (const valarray<T>& x, const valarray<T>& y);
297 template<class T> valarray<bool> operator> (const valarray<T>& x, const T& y);
298 template<class T> valarray<bool> operator> (const T& x, const valarray<T>& y);
299
300 template<class T> valarray<bool> operator<=(const valarray<T>& x, const valarray<T>& y);
301 template<class T> valarray<bool> operator<=(const valarray<T>& x, const T& y);
302 template<class T> valarray<bool> operator<=(const T& x, const valarray<T>& y);
303
304 template<class T> valarray<bool> operator>=(const valarray<T>& x, const valarray<T>& y);
305 template<class T> valarray<bool> operator>=(const valarray<T>& x, const T& y);
306 template<class T> valarray<bool> operator>=(const T& x, const valarray<T>& y);
307
308 template<class T> valarray<T> abs (const valarray<T>& x);
309 template<class T> valarray<T> acos (const valarray<T>& x);
310 template<class T> valarray<T> asin (const valarray<T>& x);
311 template<class T> valarray<T> atan (const valarray<T>& x);
312
313 template<class T> valarray<T> atan2(const valarray<T>& x, const valarray<T>& y);
314 template<class T> valarray<T> atan2(const valarray<T>& x, const T& y);
315 template<class T> valarray<T> atan2(const T& x, const valarray<T>& y);
316
317 template<class T> valarray<T> cos (const valarray<T>& x);
318 template<class T> valarray<T> cosh (const valarray<T>& x);
319 template<class T> valarray<T> exp (const valarray<T>& x);
320 template<class T> valarray<T> log (const valarray<T>& x);
321 template<class T> valarray<T> log10(const valarray<T>& x);
322
323 template<class T> valarray<T> pow(const valarray<T>& x, const valarray<T>& y);
324 template<class T> valarray<T> pow(const valarray<T>& x, const T& y);
325 template<class T> valarray<T> pow(const T& x, const valarray<T>& y);
326
327 template<class T> valarray<T> sin (const valarray<T>& x);
328 template<class T> valarray<T> sinh (const valarray<T>& x);
329 template<class T> valarray<T> sqrt (const valarray<T>& x);
330 template<class T> valarray<T> tan (const valarray<T>& x);
331 template<class T> valarray<T> tanh (const valarray<T>& x);
332
333 template <class T> unspecified1 begin(valarray<T>& v);
334 template <class T> unspecified2 begin(const valarray<T>& v);
335 template <class T> unspecified1 end(valarray<T>& v);
336 template <class T> unspecified2 end(const valarray<T>& v);
337
338 }  // std
339
340 */
341
342 #include <__config>
343 #include <cstddef>
344 #include <cmath>
345 #include <initializer_list>
346 #include <algorithm>
347 #include <functional>
348 #include <new>
349
350 #include <__undef_min_max>
351
352 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
353 #pragma GCC system_header
354 #endif
355
356 _LIBCPP_BEGIN_NAMESPACE_STD
357
358 template<class _Tp> class _LIBCPP_TYPE_VIS_ONLY valarray;
359
360 class _LIBCPP_TYPE_VIS_ONLY slice
361 {
362     size_t __start_;
363     size_t __size_;
364     size_t __stride_;
365 public:
366     _LIBCPP_INLINE_VISIBILITY
367     slice()
368         : __start_(0),
369           __size_(0),
370           __stride_(0)
371           {}
372
373     _LIBCPP_INLINE_VISIBILITY
374     slice(size_t __start, size_t __size, size_t __stride)
375         : __start_(__start),
376           __size_(__size),
377           __stride_(__stride)
378           {}
379
380     _LIBCPP_INLINE_VISIBILITY size_t start()  const {return __start_;}
381     _LIBCPP_INLINE_VISIBILITY size_t size()   const {return __size_;}
382     _LIBCPP_INLINE_VISIBILITY size_t stride() const {return __stride_;}
383 };
384
385 template <class _Tp> class _LIBCPP_TYPE_VIS_ONLY slice_array;
386 class _LIBCPP_TYPE_VIS gslice;
387 template <class _Tp> class _LIBCPP_TYPE_VIS_ONLY gslice_array;
388 template <class _Tp> class _LIBCPP_TYPE_VIS_ONLY mask_array;
389 template <class _Tp> class _LIBCPP_TYPE_VIS_ONLY indirect_array;
390
391 template <class _Tp>
392 _LIBCPP_INLINE_VISIBILITY
393 _Tp*
394 begin(valarray<_Tp>& __v);
395
396 template <class _Tp>
397 _LIBCPP_INLINE_VISIBILITY
398 const _Tp*
399 begin(const valarray<_Tp>& __v);
400
401 template <class _Tp>
402 _LIBCPP_INLINE_VISIBILITY
403 _Tp*
404 end(valarray<_Tp>& __v);
405
406 template <class _Tp>
407 _LIBCPP_INLINE_VISIBILITY
408 const _Tp*
409 end(const valarray<_Tp>& __v);
410
411 template <class _Op, class _A0>
412 struct _UnaryOp
413 {
414     typedef typename _Op::result_type result_type;
415     typedef typename _A0::value_type value_type;
416
417     _Op __op_;
418     _A0 __a0_;
419
420     _LIBCPP_INLINE_VISIBILITY
421     _UnaryOp(const _Op& __op, const _A0& __a0) : __op_(__op), __a0_(__a0) {}
422
423     _LIBCPP_INLINE_VISIBILITY
424     result_type operator[](size_t __i) const {return __op_(__a0_[__i]);}
425
426     _LIBCPP_INLINE_VISIBILITY
427     size_t size() const {return __a0_.size();}
428 };
429
430 template <class _Op, class _A0, class _A1>
431 struct _BinaryOp
432 {
433     typedef typename _Op::result_type result_type;
434     typedef typename _A0::value_type value_type;
435
436     _Op __op_;
437     _A0 __a0_;
438     _A1 __a1_;
439
440     _LIBCPP_INLINE_VISIBILITY
441     _BinaryOp(const _Op& __op, const _A0& __a0, const _A1& __a1)
442         : __op_(__op), __a0_(__a0), __a1_(__a1) {}
443
444     _LIBCPP_INLINE_VISIBILITY
445     value_type operator[](size_t __i) const {return __op_(__a0_[__i], __a1_[__i]);}
446
447     _LIBCPP_INLINE_VISIBILITY
448     size_t size() const {return __a0_.size();}
449 };
450
451 template <class _Tp>
452 class __scalar_expr
453 {
454 public:
455     typedef _Tp        value_type;
456     typedef const _Tp& result_type;
457 private:
458     const value_type& __t_;
459     size_t __s_;
460 public:
461     _LIBCPP_INLINE_VISIBILITY
462     explicit __scalar_expr(const value_type& __t, size_t __s) : __t_(__t), __s_(__s) {}
463
464     _LIBCPP_INLINE_VISIBILITY
465     result_type operator[](size_t) const {return __t_;}
466
467     _LIBCPP_INLINE_VISIBILITY
468     size_t size() const {return __s_;}
469 };
470
471 template <class _Tp>
472 struct __unary_plus : unary_function<_Tp, _Tp>
473 {
474     _LIBCPP_INLINE_VISIBILITY
475     _Tp operator()(const _Tp& __x) const
476         {return +__x;}
477 };
478
479 template <class _Tp>
480 struct __bit_not  : unary_function<_Tp, _Tp>
481 {
482     _LIBCPP_INLINE_VISIBILITY
483     _Tp operator()(const _Tp& __x) const
484         {return ~__x;}
485 };
486
487 template <class _Tp>
488 struct __bit_shift_left : binary_function<_Tp, _Tp, _Tp>
489 {
490     _LIBCPP_INLINE_VISIBILITY
491     _Tp operator()(const _Tp& __x, const _Tp& __y) const
492         {return __x << __y;}
493 };
494
495 template <class _Tp>
496 struct __bit_shift_right : binary_function<_Tp, _Tp, _Tp>
497 {
498     _LIBCPP_INLINE_VISIBILITY
499     _Tp operator()(const _Tp& __x, const _Tp& __y) const
500         {return __x >> __y;}
501 };
502
503 template <class _Tp, class _Fp>
504 struct __apply_expr   : unary_function<_Tp, _Tp>
505 {
506 private:
507     _Fp __f_;
508 public:
509     _LIBCPP_INLINE_VISIBILITY
510     explicit __apply_expr(_Fp __f) : __f_(__f) {}
511
512     _LIBCPP_INLINE_VISIBILITY
513     _Tp operator()(const _Tp& __x) const
514         {return __f_(__x);}
515 };
516
517 template <class _Tp>
518 struct __abs_expr : unary_function<_Tp, _Tp>
519 {
520     _LIBCPP_INLINE_VISIBILITY
521     _Tp operator()(const _Tp& __x) const
522         {return abs(__x);}
523 };
524
525 template <class _Tp>
526 struct __acos_expr : unary_function<_Tp, _Tp>
527 {
528     _LIBCPP_INLINE_VISIBILITY
529     _Tp operator()(const _Tp& __x) const
530         {return acos(__x);}
531 };
532
533 template <class _Tp>
534 struct __asin_expr : unary_function<_Tp, _Tp>
535 {
536     _LIBCPP_INLINE_VISIBILITY
537     _Tp operator()(const _Tp& __x) const
538         {return asin(__x);}
539 };
540
541 template <class _Tp>
542 struct __atan_expr : unary_function<_Tp, _Tp>
543 {
544     _LIBCPP_INLINE_VISIBILITY
545     _Tp operator()(const _Tp& __x) const
546         {return atan(__x);}
547 };
548
549 template <class _Tp>
550 struct __atan2_expr : binary_function<_Tp, _Tp, _Tp>
551 {
552     _LIBCPP_INLINE_VISIBILITY
553     _Tp operator()(const _Tp& __x, const _Tp& __y) const
554         {return atan2(__x, __y);}
555 };
556
557 template <class _Tp>
558 struct __cos_expr : unary_function<_Tp, _Tp>
559 {
560     _LIBCPP_INLINE_VISIBILITY
561     _Tp operator()(const _Tp& __x) const
562         {return cos(__x);}
563 };
564
565 template <class _Tp>
566 struct __cosh_expr : unary_function<_Tp, _Tp>
567 {
568     _LIBCPP_INLINE_VISIBILITY
569     _Tp operator()(const _Tp& __x) const
570         {return cosh(__x);}
571 };
572
573 template <class _Tp>
574 struct __exp_expr : unary_function<_Tp, _Tp>
575 {
576     _LIBCPP_INLINE_VISIBILITY
577     _Tp operator()(const _Tp& __x) const
578         {return exp(__x);}
579 };
580
581 template <class _Tp>
582 struct __log_expr : unary_function<_Tp, _Tp>
583 {
584     _LIBCPP_INLINE_VISIBILITY
585     _Tp operator()(const _Tp& __x) const
586         {return log(__x);}
587 };
588
589 template <class _Tp>
590 struct __log10_expr : unary_function<_Tp, _Tp>
591 {
592     _LIBCPP_INLINE_VISIBILITY
593     _Tp operator()(const _Tp& __x) const
594         {return log10(__x);}
595 };
596
597 template <class _Tp>
598 struct __pow_expr : binary_function<_Tp, _Tp, _Tp>
599 {
600     _LIBCPP_INLINE_VISIBILITY
601     _Tp operator()(const _Tp& __x, const _Tp& __y) const
602         {return pow(__x, __y);}
603 };
604
605 template <class _Tp>
606 struct __sin_expr : unary_function<_Tp, _Tp>
607 {
608     _LIBCPP_INLINE_VISIBILITY
609     _Tp operator()(const _Tp& __x) const
610         {return sin(__x);}
611 };
612
613 template <class _Tp>
614 struct __sinh_expr : unary_function<_Tp, _Tp>
615 {
616     _LIBCPP_INLINE_VISIBILITY
617     _Tp operator()(const _Tp& __x) const
618         {return sinh(__x);}
619 };
620
621 template <class _Tp>
622 struct __sqrt_expr : unary_function<_Tp, _Tp>
623 {
624     _LIBCPP_INLINE_VISIBILITY
625     _Tp operator()(const _Tp& __x) const
626         {return sqrt(__x);}
627 };
628
629 template <class _Tp>
630 struct __tan_expr : unary_function<_Tp, _Tp>
631 {
632     _LIBCPP_INLINE_VISIBILITY
633     _Tp operator()(const _Tp& __x) const
634         {return tan(__x);}
635 };
636
637 template <class _Tp>
638 struct __tanh_expr : unary_function<_Tp, _Tp>
639 {
640     _LIBCPP_INLINE_VISIBILITY
641     _Tp operator()(const _Tp& __x) const
642         {return tanh(__x);}
643 };
644
645 template <class _ValExpr>
646 class __slice_expr
647 {
648     typedef typename remove_reference<_ValExpr>::type  _RmExpr;
649 public:
650     typedef typename _RmExpr::value_type value_type;
651     typedef value_type result_type;
652
653 private:
654     _ValExpr __expr_;
655     size_t __start_;
656     size_t __size_;
657     size_t __stride_;
658
659     _LIBCPP_INLINE_VISIBILITY
660     __slice_expr(const slice& __sl, const _RmExpr& __e)
661         : __expr_(__e),
662           __start_(__sl.start()),
663           __size_(__sl.size()),
664           __stride_(__sl.stride())
665         {}
666 public:
667
668     _LIBCPP_INLINE_VISIBILITY
669     result_type operator[](size_t __i) const
670         {return __expr_[__start_ + __i * __stride_];}
671
672     _LIBCPP_INLINE_VISIBILITY
673     size_t size() const {return __size_;}
674
675     template <class> friend class _LIBCPP_TYPE_VIS_ONLY valarray;
676 };
677
678 template <class _ValExpr>
679 class __mask_expr;
680
681 template <class _ValExpr>
682 class __indirect_expr;
683
684 template <class _ValExpr>
685 class __shift_expr
686 {
687     typedef typename remove_reference<_ValExpr>::type  _RmExpr;
688 public:
689     typedef typename _RmExpr::value_type value_type;
690     typedef value_type result_type;
691
692 private:
693     _ValExpr __expr_;
694     size_t __size_;
695     ptrdiff_t __ul_;
696     ptrdiff_t __sn_;
697     ptrdiff_t __n_;
698     static const ptrdiff_t _Np = static_cast<ptrdiff_t>(
699                                     sizeof(ptrdiff_t) * __CHAR_BIT__ - 1);
700
701     _LIBCPP_INLINE_VISIBILITY
702     __shift_expr(int __n, const _RmExpr& __e)
703         : __expr_(__e),
704           __size_(__e.size()),
705           __n_(__n)
706         {
707             ptrdiff_t __neg_n = static_cast<ptrdiff_t>(__n_ >> _Np);
708             __sn_ = __neg_n | static_cast<ptrdiff_t>(static_cast<size_t>(-__n_) >> _Np);
709             __ul_ = ((__size_ - __n_) & ~__neg_n) | ((__n_ + 1) & __neg_n);
710         }
711 public:
712
713     _LIBCPP_INLINE_VISIBILITY
714     result_type operator[](size_t __j) const
715         {
716             ptrdiff_t __i = static_cast<ptrdiff_t>(__j);
717             ptrdiff_t __m = (__sn_ * __i - __ul_) >> _Np;
718             return (__expr_[(__i + __n_) & __m] & __m) | (value_type() & ~__m);
719         }
720
721     _LIBCPP_INLINE_VISIBILITY
722     size_t size() const {return __size_;}
723
724     template <class> friend class __val_expr;
725 };
726
727 template <class _ValExpr>
728 class __cshift_expr
729 {
730     typedef typename remove_reference<_ValExpr>::type  _RmExpr;
731 public:
732     typedef typename _RmExpr::value_type value_type;
733     typedef value_type result_type;
734
735 private:
736     _ValExpr __expr_;
737     size_t __size_;
738     size_t __m_;
739     size_t __o1_;
740     size_t __o2_;
741
742     _LIBCPP_INLINE_VISIBILITY
743     __cshift_expr(int __n, const _RmExpr& __e)
744         : __expr_(__e),
745           __size_(__e.size())
746         {
747             __n %= static_cast<int>(__size_);
748             if (__n >= 0)
749             {
750                 __m_ = __size_ - __n;
751                 __o1_ = __n;
752                 __o2_ = __n - __size_;
753             }
754             else
755             {
756                 __m_ = -__n;
757                 __o1_ = __n + __size_;
758                 __o2_ = __n;
759             }
760         }
761 public:
762
763     _LIBCPP_INLINE_VISIBILITY
764     result_type operator[](size_t __i) const
765         {
766             if (__i < __m_)
767                 return __expr_[__i + __o1_];
768             return __expr_[__i + __o2_];
769         }
770
771     _LIBCPP_INLINE_VISIBILITY
772     size_t size() const {return __size_;}
773
774     template <class> friend class __val_expr;
775 };
776
777 template<class _ValExpr>
778 class __val_expr;
779
780 template<class _ValExpr>
781 struct __is_val_expr : false_type {};
782
783 template<class _ValExpr>
784 struct __is_val_expr<__val_expr<_ValExpr> > : true_type {};
785
786 template<class _Tp>
787 struct __is_val_expr<valarray<_Tp> > : true_type {};
788
789 template<class _Tp>
790 class _LIBCPP_TYPE_VIS_ONLY valarray
791 {
792 public:
793     typedef _Tp value_type;
794     typedef _Tp result_type;
795
796 private:
797     value_type* __begin_;
798     value_type* __end_;
799
800 public:
801     // construct/destroy:
802     _LIBCPP_INLINE_VISIBILITY
803     valarray() : __begin_(0), __end_(0) {}
804     explicit valarray(size_t __n);
805     valarray(const value_type& __x, size_t __n);
806     valarray(const value_type* __p, size_t __n);
807     valarray(const valarray& __v);
808 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
809     valarray(valarray&& __v) _NOEXCEPT;
810 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
811 #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
812     valarray(initializer_list<value_type> __il);
813 #endif  // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
814     valarray(const slice_array<value_type>& __sa);
815     valarray(const gslice_array<value_type>& __ga);
816     valarray(const mask_array<value_type>& __ma);
817     valarray(const indirect_array<value_type>& __ia);
818     ~valarray();
819
820     // assignment:
821     valarray& operator=(const valarray& __v);
822 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
823     valarray& operator=(valarray&& __v) _NOEXCEPT;
824 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
825 #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
826     valarray& operator=(initializer_list<value_type>);
827 #endif  // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
828     valarray& operator=(const value_type& __x);
829     valarray& operator=(const slice_array<value_type>& __sa);
830     valarray& operator=(const gslice_array<value_type>& __ga);
831     valarray& operator=(const mask_array<value_type>& __ma);
832     valarray& operator=(const indirect_array<value_type>& __ia);
833     template <class _ValExpr>
834         valarray& operator=(const __val_expr<_ValExpr>& __v);
835
836     // element access:
837     _LIBCPP_INLINE_VISIBILITY
838     const value_type& operator[](size_t __i) const {return __begin_[__i];}
839
840     _LIBCPP_INLINE_VISIBILITY
841     value_type&       operator[](size_t __i)       {return __begin_[__i];}
842
843     // subset operations:
844     __val_expr<__slice_expr<const valarray&> >    operator[](slice __s) const;
845     slice_array<value_type>                       operator[](slice __s);
846     __val_expr<__indirect_expr<const valarray&> > operator[](const gslice& __gs) const;
847     gslice_array<value_type>   operator[](const gslice& __gs);
848 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
849     __val_expr<__indirect_expr<const valarray&> > operator[](gslice&& __gs) const;
850     gslice_array<value_type>                      operator[](gslice&& __gs);
851 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
852     __val_expr<__mask_expr<const valarray&> >     operator[](const valarray<bool>& __vb) const;
853     mask_array<value_type>                        operator[](const valarray<bool>& __vb);
854 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
855     __val_expr<__mask_expr<const valarray&> >     operator[](valarray<bool>&& __vb) const;
856     mask_array<value_type>                        operator[](valarray<bool>&& __vb);
857 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
858     __val_expr<__indirect_expr<const valarray&> > operator[](const valarray<size_t>& __vs) const;
859     indirect_array<value_type>                    operator[](const valarray<size_t>& __vs);
860 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
861     __val_expr<__indirect_expr<const valarray&> > operator[](valarray<size_t>&& __vs) const;
862     indirect_array<value_type>                    operator[](valarray<size_t>&& __vs);
863 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
864
865     // unary operators:
866     valarray       operator+() const;
867     valarray       operator-() const;
868     valarray       operator~() const;
869     valarray<bool> operator!() const;
870
871     // computed assignment:
872     valarray& operator*= (const value_type& __x);
873     valarray& operator/= (const value_type& __x);
874     valarray& operator%= (const value_type& __x);
875     valarray& operator+= (const value_type& __x);
876     valarray& operator-= (const value_type& __x);
877     valarray& operator^= (const value_type& __x);
878     valarray& operator&= (const value_type& __x);
879     valarray& operator|= (const value_type& __x);
880     valarray& operator<<=(const value_type& __x);
881     valarray& operator>>=(const value_type& __x);
882
883     template <class _Expr>
884     typename enable_if
885     <
886         __is_val_expr<_Expr>::value,
887         valarray&
888     >::type
889     operator*= (const _Expr& __v);
890
891     template <class _Expr>
892     typename enable_if
893     <
894         __is_val_expr<_Expr>::value,
895         valarray&
896     >::type
897     operator/= (const _Expr& __v);
898
899     template <class _Expr>
900     typename enable_if
901     <
902         __is_val_expr<_Expr>::value,
903         valarray&
904     >::type
905     operator%= (const _Expr& __v);
906
907     template <class _Expr>
908     typename enable_if
909     <
910         __is_val_expr<_Expr>::value,
911         valarray&
912     >::type
913     operator+= (const _Expr& __v);
914
915     template <class _Expr>
916     typename enable_if
917     <
918         __is_val_expr<_Expr>::value,
919         valarray&
920     >::type
921     operator-= (const _Expr& __v);
922
923     template <class _Expr>
924     typename enable_if
925     <
926         __is_val_expr<_Expr>::value,
927         valarray&
928     >::type
929     operator^= (const _Expr& __v);
930
931     template <class _Expr>
932     typename enable_if
933     <
934         __is_val_expr<_Expr>::value,
935         valarray&
936     >::type
937     operator|= (const _Expr& __v);
938
939     template <class _Expr>
940     typename enable_if
941     <
942         __is_val_expr<_Expr>::value,
943         valarray&
944     >::type
945     operator&= (const _Expr& __v);
946
947     template <class _Expr>
948     typename enable_if
949     <
950         __is_val_expr<_Expr>::value,
951         valarray&
952     >::type
953     operator<<= (const _Expr& __v);
954
955     template <class _Expr>
956     typename enable_if
957     <
958         __is_val_expr<_Expr>::value,
959         valarray&
960     >::type
961     operator>>= (const _Expr& __v);
962
963     // member functions:
964     void swap(valarray& __v) _NOEXCEPT;
965
966     _LIBCPP_INLINE_VISIBILITY
967     size_t size() const {return static_cast<size_t>(__end_ - __begin_);}
968
969     value_type sum() const;
970     value_type min() const;
971     value_type max() const;
972
973     valarray shift (int __i) const;
974     valarray cshift(int __i) const;
975     valarray apply(value_type __f(value_type)) const;
976     valarray apply(value_type __f(const value_type&)) const;
977     void     resize(size_t __n, value_type __x = value_type());
978
979 private:
980     template <class> friend class _LIBCPP_TYPE_VIS_ONLY valarray;
981     template <class> friend class _LIBCPP_TYPE_VIS_ONLY slice_array;
982     template <class> friend class _LIBCPP_TYPE_VIS_ONLY gslice_array;
983     template <class> friend class _LIBCPP_TYPE_VIS_ONLY mask_array;
984     template <class> friend class __mask_expr;
985     template <class> friend class _LIBCPP_TYPE_VIS_ONLY indirect_array;
986     template <class> friend class __indirect_expr;
987     template <class> friend class __val_expr;
988
989     template <class _Up>
990     friend
991     _Up*
992     begin(valarray<_Up>& __v);
993
994     template <class _Up>
995     friend
996     const _Up*
997     begin(const valarray<_Up>& __v);
998
999     template <class _Up>
1000     friend
1001     _Up*
1002     end(valarray<_Up>& __v);
1003
1004     template <class _Up>
1005     friend
1006     const _Up*
1007     end(const valarray<_Up>& __v);
1008 };
1009
1010 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS valarray<size_t>::valarray(size_t))
1011 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS valarray<size_t>::~valarray())
1012 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void valarray<size_t>::resize(size_t, size_t))
1013
1014 template <class _Op, class _Tp>
1015 struct _UnaryOp<_Op, valarray<_Tp> >
1016 {
1017     typedef typename _Op::result_type result_type;
1018     typedef _Tp value_type;
1019
1020     _Op __op_;
1021     const valarray<_Tp>& __a0_;
1022
1023     _LIBCPP_INLINE_VISIBILITY
1024     _UnaryOp(const _Op& __op, const valarray<_Tp>& __a0) : __op_(__op), __a0_(__a0) {}
1025
1026     _LIBCPP_INLINE_VISIBILITY
1027     result_type operator[](size_t __i) const {return __op_(__a0_[__i]);}
1028
1029     _LIBCPP_INLINE_VISIBILITY
1030     size_t size() const {return __a0_.size();}
1031 };
1032
1033 template <class _Op, class _Tp, class _A1>
1034 struct _BinaryOp<_Op, valarray<_Tp>, _A1>
1035 {
1036     typedef typename _Op::result_type result_type;
1037     typedef _Tp value_type;
1038
1039     _Op __op_;
1040     const valarray<_Tp>& __a0_;
1041     _A1 __a1_;
1042
1043     _LIBCPP_INLINE_VISIBILITY
1044     _BinaryOp(const _Op& __op, const valarray<_Tp>& __a0, const _A1& __a1)
1045         : __op_(__op), __a0_(__a0), __a1_(__a1) {}
1046
1047     _LIBCPP_INLINE_VISIBILITY
1048     value_type operator[](size_t __i) const {return __op_(__a0_[__i], __a1_[__i]);}
1049
1050     _LIBCPP_INLINE_VISIBILITY
1051     size_t size() const {return __a0_.size();}
1052 };
1053
1054 template <class _Op, class _A0, class _Tp>
1055 struct _BinaryOp<_Op, _A0, valarray<_Tp> >
1056 {
1057     typedef typename _Op::result_type result_type;
1058     typedef _Tp value_type;
1059
1060     _Op __op_;
1061     _A0 __a0_;
1062     const valarray<_Tp>& __a1_;
1063
1064     _LIBCPP_INLINE_VISIBILITY
1065     _BinaryOp(const _Op& __op, const _A0& __a0, const valarray<_Tp>& __a1)
1066         : __op_(__op), __a0_(__a0), __a1_(__a1) {}
1067
1068     _LIBCPP_INLINE_VISIBILITY
1069     value_type operator[](size_t __i) const {return __op_(__a0_[__i], __a1_[__i]);}
1070
1071     _LIBCPP_INLINE_VISIBILITY
1072     size_t size() const {return __a0_.size();}
1073 };
1074
1075 template <class _Op, class _Tp>
1076 struct _BinaryOp<_Op, valarray<_Tp>, valarray<_Tp> >
1077 {
1078     typedef typename _Op::result_type result_type;
1079     typedef _Tp value_type;
1080
1081     _Op __op_;
1082     const valarray<_Tp>& __a0_;
1083     const valarray<_Tp>& __a1_;
1084
1085     _LIBCPP_INLINE_VISIBILITY
1086     _BinaryOp(const _Op& __op, const valarray<_Tp>& __a0, const valarray<_Tp>& __a1)
1087         : __op_(__op), __a0_(__a0), __a1_(__a1) {}
1088
1089     _LIBCPP_INLINE_VISIBILITY
1090     value_type operator[](size_t __i) const {return __op_(__a0_[__i], __a1_[__i]);}
1091
1092     _LIBCPP_INLINE_VISIBILITY
1093     size_t size() const {return __a0_.size();}
1094 };
1095
1096 // slice_array
1097
1098 template <class _Tp>
1099 class _LIBCPP_TYPE_VIS_ONLY slice_array
1100 {
1101 public:
1102     typedef _Tp value_type;
1103
1104 private:
1105     value_type* __vp_;
1106     size_t __size_;
1107     size_t __stride_;
1108
1109 public:
1110     template <class _Expr>
1111     typename enable_if
1112     <
1113         __is_val_expr<_Expr>::value,
1114         void
1115     >::type
1116     operator=(const _Expr& __v) const;
1117
1118     template <class _Expr>
1119     typename enable_if
1120     <
1121         __is_val_expr<_Expr>::value,
1122         void
1123     >::type
1124     operator*=(const _Expr& __v) const;
1125
1126     template <class _Expr>
1127     typename enable_if
1128     <
1129         __is_val_expr<_Expr>::value,
1130         void
1131     >::type
1132     operator/=(const _Expr& __v) const;
1133
1134     template <class _Expr>
1135     typename enable_if
1136     <
1137         __is_val_expr<_Expr>::value,
1138         void
1139     >::type
1140     operator%=(const _Expr& __v) const;
1141
1142     template <class _Expr>
1143     typename enable_if
1144     <
1145         __is_val_expr<_Expr>::value,
1146         void
1147     >::type
1148     operator+=(const _Expr& __v) const;
1149
1150     template <class _Expr>
1151     typename enable_if
1152     <
1153         __is_val_expr<_Expr>::value,
1154         void
1155     >::type
1156     operator-=(const _Expr& __v) const;
1157
1158     template <class _Expr>
1159     typename enable_if
1160     <
1161         __is_val_expr<_Expr>::value,
1162         void
1163     >::type
1164     operator^=(const _Expr& __v) const;
1165
1166     template <class _Expr>
1167     typename enable_if
1168     <
1169         __is_val_expr<_Expr>::value,
1170         void
1171     >::type
1172     operator&=(const _Expr& __v) const;
1173
1174     template <class _Expr>
1175     typename enable_if
1176     <
1177         __is_val_expr<_Expr>::value,
1178         void
1179     >::type
1180     operator|=(const _Expr& __v) const;
1181
1182     template <class _Expr>
1183     typename enable_if
1184     <
1185         __is_val_expr<_Expr>::value,
1186         void
1187     >::type
1188     operator<<=(const _Expr& __v) const;
1189
1190     template <class _Expr>
1191     typename enable_if
1192     <
1193         __is_val_expr<_Expr>::value,
1194         void
1195     >::type
1196     operator>>=(const _Expr& __v) const;
1197
1198     const slice_array& operator=(const slice_array& __sa) const;
1199
1200     void operator=(const value_type& __x) const;
1201
1202 private:
1203     _LIBCPP_INLINE_VISIBILITY
1204     slice_array(const slice& __sl, const valarray<value_type>& __v)
1205         : __vp_(const_cast<value_type*>(__v.__begin_ + __sl.start())),
1206           __size_(__sl.size()),
1207           __stride_(__sl.stride())
1208         {}
1209
1210     template <class> friend class valarray;
1211     template <class> friend class sliceExpr;
1212 };
1213
1214 template <class _Tp>
1215 inline _LIBCPP_INLINE_VISIBILITY
1216 const slice_array<_Tp>&
1217 slice_array<_Tp>::operator=(const slice_array& __sa) const
1218 {
1219     value_type* __t = __vp_;
1220     const value_type* __s = __sa.__vp_;
1221     for (size_t __n = __size_; __n; --__n, __t += __stride_, __s += __sa.__stride_)
1222         *__t = *__s;
1223     return *this;
1224 }
1225
1226 template <class _Tp>
1227 template <class _Expr>
1228 inline _LIBCPP_INLINE_VISIBILITY
1229 typename enable_if
1230 <
1231     __is_val_expr<_Expr>::value,
1232     void
1233 >::type
1234 slice_array<_Tp>::operator=(const _Expr& __v) const
1235 {
1236     value_type* __t = __vp_;
1237     for (size_t __i = 0; __i < __size_; ++__i, __t += __stride_)
1238         *__t = __v[__i];
1239 }
1240
1241 template <class _Tp>
1242 template <class _Expr>
1243 inline _LIBCPP_INLINE_VISIBILITY
1244 typename enable_if
1245 <
1246     __is_val_expr<_Expr>::value,
1247     void
1248 >::type
1249 slice_array<_Tp>::operator*=(const _Expr& __v) const
1250 {
1251     value_type* __t = __vp_;
1252     for (size_t __i = 0; __i < __size_; ++__i, __t += __stride_)
1253         *__t *= __v[__i];
1254 }
1255
1256 template <class _Tp>
1257 template <class _Expr>
1258 inline _LIBCPP_INLINE_VISIBILITY
1259 typename enable_if
1260 <
1261     __is_val_expr<_Expr>::value,
1262     void
1263 >::type
1264 slice_array<_Tp>::operator/=(const _Expr& __v) const
1265 {
1266     value_type* __t = __vp_;
1267     for (size_t __i = 0; __i < __size_; ++__i, __t += __stride_)
1268         *__t /= __v[__i];
1269 }
1270
1271 template <class _Tp>
1272 template <class _Expr>
1273 inline _LIBCPP_INLINE_VISIBILITY
1274 typename enable_if
1275 <
1276     __is_val_expr<_Expr>::value,
1277     void
1278 >::type
1279 slice_array<_Tp>::operator%=(const _Expr& __v) const
1280 {
1281     value_type* __t = __vp_;
1282     for (size_t __i = 0; __i < __size_; ++__i, __t += __stride_)
1283         *__t %= __v[__i];
1284 }
1285
1286 template <class _Tp>
1287 template <class _Expr>
1288 inline _LIBCPP_INLINE_VISIBILITY
1289 typename enable_if
1290 <
1291     __is_val_expr<_Expr>::value,
1292     void
1293 >::type
1294 slice_array<_Tp>::operator+=(const _Expr& __v) const
1295 {
1296     value_type* __t = __vp_;
1297     for (size_t __i = 0; __i < __size_; ++__i, __t += __stride_)
1298         *__t += __v[__i];
1299 }
1300
1301 template <class _Tp>
1302 template <class _Expr>
1303 inline _LIBCPP_INLINE_VISIBILITY
1304 typename enable_if
1305 <
1306     __is_val_expr<_Expr>::value,
1307     void
1308 >::type
1309 slice_array<_Tp>::operator-=(const _Expr& __v) const
1310 {
1311     value_type* __t = __vp_;
1312     for (size_t __i = 0; __i < __size_; ++__i, __t += __stride_)
1313         *__t -= __v[__i];
1314 }
1315
1316 template <class _Tp>
1317 template <class _Expr>
1318 inline _LIBCPP_INLINE_VISIBILITY
1319 typename enable_if
1320 <
1321     __is_val_expr<_Expr>::value,
1322     void
1323 >::type
1324 slice_array<_Tp>::operator^=(const _Expr& __v) const
1325 {
1326     value_type* __t = __vp_;
1327     for (size_t __i = 0; __i < __size_; ++__i, __t += __stride_)
1328         *__t ^= __v[__i];
1329 }
1330
1331 template <class _Tp>
1332 template <class _Expr>
1333 inline _LIBCPP_INLINE_VISIBILITY
1334 typename enable_if
1335 <
1336     __is_val_expr<_Expr>::value,
1337     void
1338 >::type
1339 slice_array<_Tp>::operator&=(const _Expr& __v) const
1340 {
1341     value_type* __t = __vp_;
1342     for (size_t __i = 0; __i < __size_; ++__i, __t += __stride_)
1343         *__t &= __v[__i];
1344 }
1345
1346 template <class _Tp>
1347 template <class _Expr>
1348 inline _LIBCPP_INLINE_VISIBILITY
1349 typename enable_if
1350 <
1351     __is_val_expr<_Expr>::value,
1352     void
1353 >::type
1354 slice_array<_Tp>::operator|=(const _Expr& __v) const
1355 {
1356     value_type* __t = __vp_;
1357     for (size_t __i = 0; __i < __size_; ++__i, __t += __stride_)
1358         *__t |= __v[__i];
1359 }
1360
1361 template <class _Tp>
1362 template <class _Expr>
1363 inline _LIBCPP_INLINE_VISIBILITY
1364 typename enable_if
1365 <
1366     __is_val_expr<_Expr>::value,
1367     void
1368 >::type
1369 slice_array<_Tp>::operator<<=(const _Expr& __v) const
1370 {
1371     value_type* __t = __vp_;
1372     for (size_t __i = 0; __i < __size_; ++__i, __t += __stride_)
1373         *__t <<= __v[__i];
1374 }
1375
1376 template <class _Tp>
1377 template <class _Expr>
1378 inline _LIBCPP_INLINE_VISIBILITY
1379 typename enable_if
1380 <
1381     __is_val_expr<_Expr>::value,
1382     void
1383 >::type
1384 slice_array<_Tp>::operator>>=(const _Expr& __v) const
1385 {
1386     value_type* __t = __vp_;
1387     for (size_t __i = 0; __i < __size_; ++__i, __t += __stride_)
1388         *__t >>= __v[__i];
1389 }
1390
1391 template <class _Tp>
1392 inline _LIBCPP_INLINE_VISIBILITY
1393 void
1394 slice_array<_Tp>::operator=(const value_type& __x) const
1395 {
1396     value_type* __t = __vp_;
1397     for (size_t __n = __size_; __n; --__n, __t += __stride_)
1398         *__t = __x;
1399 }
1400
1401 // gslice
1402
1403 class _LIBCPP_TYPE_VIS gslice
1404 {
1405     valarray<size_t> __size_;
1406     valarray<size_t> __stride_;
1407     valarray<size_t> __1d_;
1408
1409 public:
1410     _LIBCPP_INLINE_VISIBILITY
1411     gslice() {}
1412
1413     _LIBCPP_INLINE_VISIBILITY
1414     gslice(size_t __start, const valarray<size_t>& __size,
1415                            const valarray<size_t>& __stride)
1416         : __size_(__size),
1417           __stride_(__stride)
1418         {__init(__start);}
1419
1420 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1421
1422     _LIBCPP_INLINE_VISIBILITY
1423     gslice(size_t __start, const valarray<size_t>&  __size,
1424                                  valarray<size_t>&& __stride)
1425         : __size_(__size),
1426           __stride_(move(__stride))
1427         {__init(__start);}
1428
1429     _LIBCPP_INLINE_VISIBILITY
1430     gslice(size_t __start,       valarray<size_t>&& __size,
1431                            const valarray<size_t>&  __stride)
1432         : __size_(move(__size)),
1433           __stride_(__stride)
1434         {__init(__start);}
1435
1436     _LIBCPP_INLINE_VISIBILITY
1437     gslice(size_t __start,       valarray<size_t>&& __size,
1438                                  valarray<size_t>&& __stride)
1439         : __size_(move(__size)),
1440           __stride_(move(__stride))
1441         {__init(__start);}
1442
1443 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1444
1445 //  gslice(const gslice&)            = default;
1446 //  gslice(gslice&&)                 = default;
1447 //  gslice& operator=(const gslice&) = default;
1448 //  gslice& operator=(gslice&&)      = default;
1449
1450     _LIBCPP_INLINE_VISIBILITY
1451     size_t           start()  const {return __1d_.size() ? __1d_[0] : 0;}
1452
1453     _LIBCPP_INLINE_VISIBILITY
1454     valarray<size_t> size()   const {return __size_;}
1455
1456     _LIBCPP_INLINE_VISIBILITY
1457     valarray<size_t> stride() const {return __stride_;}
1458
1459 private:
1460     void __init(size_t __start);
1461
1462     template <class> friend class gslice_array;
1463     template <class> friend class valarray;
1464     template <class> friend class __val_expr;
1465 };
1466
1467 // gslice_array
1468
1469 template <class _Tp>
1470 class _LIBCPP_TYPE_VIS_ONLY gslice_array
1471 {
1472 public:
1473     typedef _Tp value_type;
1474
1475 private:
1476     value_type*      __vp_;
1477     valarray<size_t> __1d_;
1478
1479 public:
1480     template <class _Expr>
1481     typename enable_if
1482     <
1483         __is_val_expr<_Expr>::value,
1484         void
1485     >::type
1486     operator=(const _Expr& __v) const;
1487
1488     template <class _Expr>
1489     typename enable_if
1490     <
1491         __is_val_expr<_Expr>::value,
1492         void
1493     >::type
1494     operator*=(const _Expr& __v) const;
1495
1496     template <class _Expr>
1497     typename enable_if
1498     <
1499         __is_val_expr<_Expr>::value,
1500         void
1501     >::type
1502     operator/=(const _Expr& __v) const;
1503
1504     template <class _Expr>
1505     typename enable_if
1506     <
1507         __is_val_expr<_Expr>::value,
1508         void
1509     >::type
1510     operator%=(const _Expr& __v) const;
1511
1512     template <class _Expr>
1513     typename enable_if
1514     <
1515         __is_val_expr<_Expr>::value,
1516         void
1517     >::type
1518     operator+=(const _Expr& __v) const;
1519
1520     template <class _Expr>
1521     typename enable_if
1522     <
1523         __is_val_expr<_Expr>::value,
1524         void
1525     >::type
1526     operator-=(const _Expr& __v) const;
1527
1528     template <class _Expr>
1529     typename enable_if
1530     <
1531         __is_val_expr<_Expr>::value,
1532         void
1533     >::type
1534     operator^=(const _Expr& __v) const;
1535
1536     template <class _Expr>
1537     typename enable_if
1538     <
1539         __is_val_expr<_Expr>::value,
1540         void
1541     >::type
1542     operator&=(const _Expr& __v) const;
1543
1544     template <class _Expr>
1545     typename enable_if
1546     <
1547         __is_val_expr<_Expr>::value,
1548         void
1549     >::type
1550     operator|=(const _Expr& __v) const;
1551
1552     template <class _Expr>
1553     typename enable_if
1554     <
1555         __is_val_expr<_Expr>::value,
1556         void
1557     >::type
1558     operator<<=(const _Expr& __v) const;
1559
1560     template <class _Expr>
1561     typename enable_if
1562     <
1563         __is_val_expr<_Expr>::value,
1564         void
1565     >::type
1566     operator>>=(const _Expr& __v) const;
1567
1568     const gslice_array& operator=(const gslice_array& __ga) const;
1569
1570     void operator=(const value_type& __x) const;
1571
1572 //  gslice_array(const gslice_array&)            = default;
1573 //  gslice_array(gslice_array&&)                 = default;
1574 //  gslice_array& operator=(const gslice_array&) = default;
1575 //  gslice_array& operator=(gslice_array&&)      = default;
1576
1577 private:
1578     _LIBCPP_INLINE_VISIBILITY
1579     gslice_array(const gslice& __gs, const valarray<value_type>& __v)
1580         : __vp_(const_cast<value_type*>(__v.__begin_)),
1581           __1d_(__gs.__1d_)
1582         {}
1583
1584 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1585
1586     _LIBCPP_INLINE_VISIBILITY
1587     gslice_array(gslice&& __gs, const valarray<value_type>& __v)
1588         : __vp_(const_cast<value_type*>(__v.__begin_)),
1589           __1d_(move(__gs.__1d_))
1590         {}
1591
1592 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1593
1594     template <class> friend class valarray;
1595 };
1596
1597 template <class _Tp>
1598 template <class _Expr>
1599 inline _LIBCPP_INLINE_VISIBILITY
1600 typename enable_if
1601 <
1602     __is_val_expr<_Expr>::value,
1603     void
1604 >::type
1605 gslice_array<_Tp>::operator=(const _Expr& __v) const
1606 {
1607     typedef const size_t* _Ip;
1608     size_t __j = 0;
1609     for (_Ip __i = __1d_.__begin_, __e = __1d_.__end_; __i != __e; ++__i, ++__j)
1610         __vp_[*__i] = __v[__j];
1611 }
1612
1613 template <class _Tp>
1614 template <class _Expr>
1615 inline _LIBCPP_INLINE_VISIBILITY
1616 typename enable_if
1617 <
1618     __is_val_expr<_Expr>::value,
1619     void
1620 >::type
1621 gslice_array<_Tp>::operator*=(const _Expr& __v) const
1622 {
1623     typedef const size_t* _Ip;
1624     size_t __j = 0;
1625     for (_Ip __i = __1d_.__begin_, __e = __1d_.__end_; __i != __e; ++__i, ++__j)
1626         __vp_[*__i] *= __v[__j];
1627 }
1628
1629 template <class _Tp>
1630 template <class _Expr>
1631 inline _LIBCPP_INLINE_VISIBILITY
1632 typename enable_if
1633 <
1634     __is_val_expr<_Expr>::value,
1635     void
1636 >::type
1637 gslice_array<_Tp>::operator/=(const _Expr& __v) const
1638 {
1639     typedef const size_t* _Ip;
1640     size_t __j = 0;
1641     for (_Ip __i = __1d_.__begin_, __e = __1d_.__end_; __i != __e; ++__i, ++__j)
1642         __vp_[*__i] /= __v[__j];
1643 }
1644
1645 template <class _Tp>
1646 template <class _Expr>
1647 inline _LIBCPP_INLINE_VISIBILITY
1648 typename enable_if
1649 <
1650     __is_val_expr<_Expr>::value,
1651     void
1652 >::type
1653 gslice_array<_Tp>::operator%=(const _Expr& __v) const
1654 {
1655     typedef const size_t* _Ip;
1656     size_t __j = 0;
1657     for (_Ip __i = __1d_.__begin_, __e = __1d_.__end_; __i != __e; ++__i, ++__j)
1658         __vp_[*__i] %= __v[__j];
1659 }
1660
1661 template <class _Tp>
1662 template <class _Expr>
1663 inline _LIBCPP_INLINE_VISIBILITY
1664 typename enable_if
1665 <
1666     __is_val_expr<_Expr>::value,
1667     void
1668 >::type
1669 gslice_array<_Tp>::operator+=(const _Expr& __v) const
1670 {
1671     typedef const size_t* _Ip;
1672     size_t __j = 0;
1673     for (_Ip __i = __1d_.__begin_, __e = __1d_.__end_; __i != __e; ++__i, ++__j)
1674         __vp_[*__i] += __v[__j];
1675 }
1676
1677 template <class _Tp>
1678 template <class _Expr>
1679 inline _LIBCPP_INLINE_VISIBILITY
1680 typename enable_if
1681 <
1682     __is_val_expr<_Expr>::value,
1683     void
1684 >::type
1685 gslice_array<_Tp>::operator-=(const _Expr& __v) const
1686 {
1687     typedef const size_t* _Ip;
1688     size_t __j = 0;
1689     for (_Ip __i = __1d_.__begin_, __e = __1d_.__end_; __i != __e; ++__i, ++__j)
1690         __vp_[*__i] -= __v[__j];
1691 }
1692
1693 template <class _Tp>
1694 template <class _Expr>
1695 inline _LIBCPP_INLINE_VISIBILITY
1696 typename enable_if
1697 <
1698     __is_val_expr<_Expr>::value,
1699     void
1700 >::type
1701 gslice_array<_Tp>::operator^=(const _Expr& __v) const
1702 {
1703     typedef const size_t* _Ip;
1704     size_t __j = 0;
1705     for (_Ip __i = __1d_.__begin_, __e = __1d_.__end_; __i != __e; ++__i, ++__j)
1706         __vp_[*__i] ^= __v[__j];
1707 }
1708
1709 template <class _Tp>
1710 template <class _Expr>
1711 inline _LIBCPP_INLINE_VISIBILITY
1712 typename enable_if
1713 <
1714     __is_val_expr<_Expr>::value,
1715     void
1716 >::type
1717 gslice_array<_Tp>::operator&=(const _Expr& __v) const
1718 {
1719     typedef const size_t* _Ip;
1720     size_t __j = 0;
1721     for (_Ip __i = __1d_.__begin_, __e = __1d_.__end_; __i != __e; ++__i, ++__j)
1722         __vp_[*__i] &= __v[__j];
1723 }
1724
1725 template <class _Tp>
1726 template <class _Expr>
1727 inline _LIBCPP_INLINE_VISIBILITY
1728 typename enable_if
1729 <
1730     __is_val_expr<_Expr>::value,
1731     void
1732 >::type
1733 gslice_array<_Tp>::operator|=(const _Expr& __v) const
1734 {
1735     typedef const size_t* _Ip;
1736     size_t __j = 0;
1737     for (_Ip __i = __1d_.__begin_, __e = __1d_.__end_; __i != __e; ++__i, ++__j)
1738         __vp_[*__i] |= __v[__j];
1739 }
1740
1741 template <class _Tp>
1742 template <class _Expr>
1743 inline _LIBCPP_INLINE_VISIBILITY
1744 typename enable_if
1745 <
1746     __is_val_expr<_Expr>::value,
1747     void
1748 >::type
1749 gslice_array<_Tp>::operator<<=(const _Expr& __v) const
1750 {
1751     typedef const size_t* _Ip;
1752     size_t __j = 0;
1753     for (_Ip __i = __1d_.__begin_, __e = __1d_.__end_; __i != __e; ++__i, ++__j)
1754         __vp_[*__i] <<= __v[__j];
1755 }
1756
1757 template <class _Tp>
1758 template <class _Expr>
1759 inline _LIBCPP_INLINE_VISIBILITY
1760 typename enable_if
1761 <
1762     __is_val_expr<_Expr>::value,
1763     void
1764 >::type
1765 gslice_array<_Tp>::operator>>=(const _Expr& __v) const
1766 {
1767     typedef const size_t* _Ip;
1768     size_t __j = 0;
1769     for (_Ip __i = __1d_.__begin_, __e = __1d_.__end_; __i != __e; ++__i, ++__j)
1770         __vp_[*__i] >>= __v[__j];
1771 }
1772
1773 template <class _Tp>
1774 inline _LIBCPP_INLINE_VISIBILITY
1775 const gslice_array<_Tp>&
1776 gslice_array<_Tp>::operator=(const gslice_array& __ga) const
1777 {
1778     typedef const size_t* _Ip;
1779     const value_type* __s = __ga.__vp_;
1780     for (_Ip __i = __1d_.__begin_, __e = __1d_.__end_, __j = __ga.__1d_.__begin_;
1781             __i != __e; ++__i, ++__j)
1782         __vp_[*__i] = __s[*__j];
1783     return *this;
1784 }
1785
1786 template <class _Tp>
1787 inline _LIBCPP_INLINE_VISIBILITY
1788 void
1789 gslice_array<_Tp>::operator=(const value_type& __x) const
1790 {
1791     typedef const size_t* _Ip;
1792     for (_Ip __i = __1d_.__begin_, __e = __1d_.__end_; __i != __e; ++__i)
1793         __vp_[*__i] = __x;
1794 }
1795
1796 // mask_array
1797
1798 template <class _Tp>
1799 class _LIBCPP_TYPE_VIS_ONLY mask_array
1800 {
1801 public:
1802     typedef _Tp value_type;
1803
1804 private:
1805     value_type*      __vp_;
1806     valarray<size_t> __1d_;
1807
1808 public:
1809     template <class _Expr>
1810     typename enable_if
1811     <
1812         __is_val_expr<_Expr>::value,
1813         void
1814     >::type
1815     operator=(const _Expr& __v) const;
1816
1817     template <class _Expr>
1818     typename enable_if
1819     <
1820         __is_val_expr<_Expr>::value,
1821         void
1822     >::type
1823     operator*=(const _Expr& __v) const;
1824
1825     template <class _Expr>
1826     typename enable_if
1827     <
1828         __is_val_expr<_Expr>::value,
1829         void
1830     >::type
1831     operator/=(const _Expr& __v) const;
1832
1833     template <class _Expr>
1834     typename enable_if
1835     <
1836         __is_val_expr<_Expr>::value,
1837         void
1838     >::type
1839     operator%=(const _Expr& __v) const;
1840
1841     template <class _Expr>
1842     typename enable_if
1843     <
1844         __is_val_expr<_Expr>::value,
1845         void
1846     >::type
1847     operator+=(const _Expr& __v) const;
1848
1849     template <class _Expr>
1850     typename enable_if
1851     <
1852         __is_val_expr<_Expr>::value,
1853         void
1854     >::type
1855     operator-=(const _Expr& __v) const;
1856
1857     template <class _Expr>
1858     typename enable_if
1859     <
1860         __is_val_expr<_Expr>::value,
1861         void
1862     >::type
1863     operator^=(const _Expr& __v) const;
1864
1865     template <class _Expr>
1866     typename enable_if
1867     <
1868         __is_val_expr<_Expr>::value,
1869         void
1870     >::type
1871     operator&=(const _Expr& __v) const;
1872
1873     template <class _Expr>
1874     typename enable_if
1875     <
1876         __is_val_expr<_Expr>::value,
1877         void
1878     >::type
1879     operator|=(const _Expr& __v) const;
1880
1881     template <class _Expr>
1882     typename enable_if
1883     <
1884         __is_val_expr<_Expr>::value,
1885         void
1886     >::type
1887     operator<<=(const _Expr& __v) const;
1888
1889     template <class _Expr>
1890     typename enable_if
1891     <
1892         __is_val_expr<_Expr>::value,
1893         void
1894     >::type
1895     operator>>=(const _Expr& __v) const;
1896
1897     const mask_array& operator=(const mask_array& __ma) const;
1898
1899     void operator=(const value_type& __x) const;
1900
1901 //  mask_array(const mask_array&)            = default;
1902 //  mask_array(mask_array&&)                 = default;
1903 //  mask_array& operator=(const mask_array&) = default;
1904 //  mask_array& operator=(mask_array&&)      = default;
1905
1906 private:
1907     _LIBCPP_INLINE_VISIBILITY
1908     mask_array(const valarray<bool>& __vb, const valarray<value_type>& __v)
1909         : __vp_(const_cast<value_type*>(__v.__begin_)),
1910           __1d_(static_cast<size_t>(count(__vb.__begin_, __vb.__end_, true)))
1911           {
1912               size_t __j = 0;
1913               for (size_t __i = 0; __i < __vb.size(); ++__i)
1914                   if (__vb[__i])
1915                       __1d_[__j++] = __i;
1916           }
1917
1918     template <class> friend class valarray;
1919 };
1920
1921 template <class _Tp>
1922 template <class _Expr>
1923 inline _LIBCPP_INLINE_VISIBILITY
1924 typename enable_if
1925 <
1926     __is_val_expr<_Expr>::value,
1927     void
1928 >::type
1929 mask_array<_Tp>::operator=(const _Expr& __v) const
1930 {
1931     size_t __n = __1d_.size();
1932     for (size_t __i = 0; __i < __n; ++__i)
1933         __vp_[__1d_[__i]] = __v[__i];
1934 }
1935
1936 template <class _Tp>
1937 template <class _Expr>
1938 inline _LIBCPP_INLINE_VISIBILITY
1939 typename enable_if
1940 <
1941     __is_val_expr<_Expr>::value,
1942     void
1943 >::type
1944 mask_array<_Tp>::operator*=(const _Expr& __v) const
1945 {
1946     size_t __n = __1d_.size();
1947     for (size_t __i = 0; __i < __n; ++__i)
1948         __vp_[__1d_[__i]] *= __v[__i];
1949 }
1950
1951 template <class _Tp>
1952 template <class _Expr>
1953 inline _LIBCPP_INLINE_VISIBILITY
1954 typename enable_if
1955 <
1956     __is_val_expr<_Expr>::value,
1957     void
1958 >::type
1959 mask_array<_Tp>::operator/=(const _Expr& __v) const
1960 {
1961     size_t __n = __1d_.size();
1962     for (size_t __i = 0; __i < __n; ++__i)
1963         __vp_[__1d_[__i]] /= __v[__i];
1964 }
1965
1966 template <class _Tp>
1967 template <class _Expr>
1968 inline _LIBCPP_INLINE_VISIBILITY
1969 typename enable_if
1970 <
1971     __is_val_expr<_Expr>::value,
1972     void
1973 >::type
1974 mask_array<_Tp>::operator%=(const _Expr& __v) const
1975 {
1976     size_t __n = __1d_.size();
1977     for (size_t __i = 0; __i < __n; ++__i)
1978         __vp_[__1d_[__i]] %= __v[__i];
1979 }
1980
1981 template <class _Tp>
1982 template <class _Expr>
1983 inline _LIBCPP_INLINE_VISIBILITY
1984 typename enable_if
1985 <
1986     __is_val_expr<_Expr>::value,
1987     void
1988 >::type
1989 mask_array<_Tp>::operator+=(const _Expr& __v) const
1990 {
1991     size_t __n = __1d_.size();
1992     for (size_t __i = 0; __i < __n; ++__i)
1993         __vp_[__1d_[__i]] += __v[__i];
1994 }
1995
1996 template <class _Tp>
1997 template <class _Expr>
1998 inline _LIBCPP_INLINE_VISIBILITY
1999 typename enable_if
2000 <
2001     __is_val_expr<_Expr>::value,
2002     void
2003 >::type
2004 mask_array<_Tp>::operator-=(const _Expr& __v) const
2005 {
2006     size_t __n = __1d_.size();
2007     for (size_t __i = 0; __i < __n; ++__i)
2008         __vp_[__1d_[__i]] -= __v[__i];
2009 }
2010
2011 template <class _Tp>
2012 template <class _Expr>
2013 inline _LIBCPP_INLINE_VISIBILITY
2014 typename enable_if
2015 <
2016     __is_val_expr<_Expr>::value,
2017     void
2018 >::type
2019 mask_array<_Tp>::operator^=(const _Expr& __v) const
2020 {
2021     size_t __n = __1d_.size();
2022     for (size_t __i = 0; __i < __n; ++__i)
2023         __vp_[__1d_[__i]] ^= __v[__i];
2024 }
2025
2026 template <class _Tp>
2027 template <class _Expr>
2028 inline _LIBCPP_INLINE_VISIBILITY
2029 typename enable_if
2030 <
2031     __is_val_expr<_Expr>::value,
2032     void
2033 >::type
2034 mask_array<_Tp>::operator&=(const _Expr& __v) const
2035 {
2036     size_t __n = __1d_.size();
2037     for (size_t __i = 0; __i < __n; ++__i)
2038         __vp_[__1d_[__i]] &= __v[__i];
2039 }
2040
2041 template <class _Tp>
2042 template <class _Expr>
2043 inline _LIBCPP_INLINE_VISIBILITY
2044 typename enable_if
2045 <
2046     __is_val_expr<_Expr>::value,
2047     void
2048 >::type
2049 mask_array<_Tp>::operator|=(const _Expr& __v) const
2050 {
2051     size_t __n = __1d_.size();
2052     for (size_t __i = 0; __i < __n; ++__i)
2053         __vp_[__1d_[__i]] |= __v[__i];
2054 }
2055
2056 template <class _Tp>
2057 template <class _Expr>
2058 inline _LIBCPP_INLINE_VISIBILITY
2059 typename enable_if
2060 <
2061     __is_val_expr<_Expr>::value,
2062     void
2063 >::type
2064 mask_array<_Tp>::operator<<=(const _Expr& __v) const
2065 {
2066     size_t __n = __1d_.size();
2067     for (size_t __i = 0; __i < __n; ++__i)
2068         __vp_[__1d_[__i]] <<= __v[__i];
2069 }
2070
2071 template <class _Tp>
2072 template <class _Expr>
2073 inline _LIBCPP_INLINE_VISIBILITY
2074 typename enable_if
2075 <
2076     __is_val_expr<_Expr>::value,
2077     void
2078 >::type
2079 mask_array<_Tp>::operator>>=(const _Expr& __v) const
2080 {
2081     size_t __n = __1d_.size();
2082     for (size_t __i = 0; __i < __n; ++__i)
2083         __vp_[__1d_[__i]] >>= __v[__i];
2084 }
2085
2086 template <class _Tp>
2087 inline _LIBCPP_INLINE_VISIBILITY
2088 const mask_array<_Tp>&
2089 mask_array<_Tp>::operator=(const mask_array& __ma) const
2090 {
2091     size_t __n = __1d_.size();
2092     for (size_t __i = 0; __i < __n; ++__i)
2093         __vp_[__1d_[__i]] = __ma.__vp_[__1d_[__i]];
2094     return *this;
2095 }
2096
2097 template <class _Tp>
2098 inline _LIBCPP_INLINE_VISIBILITY
2099 void
2100 mask_array<_Tp>::operator=(const value_type& __x) const
2101 {
2102     size_t __n = __1d_.size();
2103     for (size_t __i = 0; __i < __n; ++__i)
2104         __vp_[__1d_[__i]] = __x;
2105 }
2106
2107 template <class _ValExpr>
2108 class __mask_expr
2109 {
2110     typedef typename remove_reference<_ValExpr>::type  _RmExpr;
2111 public:
2112     typedef typename _RmExpr::value_type value_type;
2113     typedef value_type result_type;
2114
2115 private:
2116     _ValExpr __expr_;
2117     valarray<size_t> __1d_;
2118
2119     _LIBCPP_INLINE_VISIBILITY
2120     __mask_expr(const valarray<bool>& __vb, const _RmExpr& __e)
2121         : __expr_(__e),
2122           __1d_(static_cast<size_t>(count(__vb.__begin_, __vb.__end_, true)))
2123           {
2124               size_t __j = 0;
2125               for (size_t __i = 0; __i < __vb.size(); ++__i)
2126                   if (__vb[__i])
2127                       __1d_[__j++] = __i;
2128           }
2129
2130 public:
2131     _LIBCPP_INLINE_VISIBILITY
2132     result_type operator[](size_t __i) const
2133         {return __expr_[__1d_[__i]];}
2134
2135     _LIBCPP_INLINE_VISIBILITY
2136     size_t size() const {return __1d_.size();}
2137
2138     template <class> friend class valarray;
2139 };
2140
2141 // indirect_array
2142
2143 template <class _Tp>
2144 class _LIBCPP_TYPE_VIS_ONLY indirect_array
2145 {
2146 public:
2147     typedef _Tp value_type;
2148
2149 private:
2150     value_type*      __vp_;
2151     valarray<size_t> __1d_;
2152
2153 public:
2154     template <class _Expr>
2155     typename enable_if
2156     <
2157         __is_val_expr<_Expr>::value,
2158         void
2159     >::type
2160     operator=(const _Expr& __v) const;
2161
2162     template <class _Expr>
2163     typename enable_if
2164     <
2165         __is_val_expr<_Expr>::value,
2166         void
2167     >::type
2168     operator*=(const _Expr& __v) const;
2169
2170     template <class _Expr>
2171     typename enable_if
2172     <
2173         __is_val_expr<_Expr>::value,
2174         void
2175     >::type
2176     operator/=(const _Expr& __v) const;
2177
2178     template <class _Expr>
2179     typename enable_if
2180     <
2181         __is_val_expr<_Expr>::value,
2182         void
2183     >::type
2184     operator%=(const _Expr& __v) const;
2185
2186     template <class _Expr>
2187     typename enable_if
2188     <
2189         __is_val_expr<_Expr>::value,
2190         void
2191     >::type
2192     operator+=(const _Expr& __v) const;
2193
2194     template <class _Expr>
2195     typename enable_if
2196     <
2197         __is_val_expr<_Expr>::value,
2198         void
2199     >::type
2200     operator-=(const _Expr& __v) const;
2201
2202     template <class _Expr>
2203     typename enable_if
2204     <
2205         __is_val_expr<_Expr>::value,
2206         void
2207     >::type
2208     operator^=(const _Expr& __v) const;
2209
2210     template <class _Expr>
2211     typename enable_if
2212     <
2213         __is_val_expr<_Expr>::value,
2214         void
2215     >::type
2216     operator&=(const _Expr& __v) const;
2217
2218     template <class _Expr>
2219     typename enable_if
2220     <
2221         __is_val_expr<_Expr>::value,
2222         void
2223     >::type
2224     operator|=(const _Expr& __v) const;
2225
2226     template <class _Expr>
2227     typename enable_if
2228     <
2229         __is_val_expr<_Expr>::value,
2230         void
2231     >::type
2232     operator<<=(const _Expr& __v) const;
2233
2234     template <class _Expr>
2235     typename enable_if
2236     <
2237         __is_val_expr<_Expr>::value,
2238         void
2239     >::type
2240     operator>>=(const _Expr& __v) const;
2241
2242     const indirect_array& operator=(const indirect_array& __ia) const;
2243
2244     void operator=(const value_type& __x) const;
2245
2246 //  indirect_array(const indirect_array&)            = default;
2247 //  indirect_array(indirect_array&&)                 = default;
2248 //  indirect_array& operator=(const indirect_array&) = default;
2249 //  indirect_array& operator=(indirect_array&&)      = default;
2250
2251 private:
2252      _LIBCPP_INLINE_VISIBILITY
2253    indirect_array(const valarray<size_t>& __ia, const valarray<value_type>& __v)
2254         : __vp_(const_cast<value_type*>(__v.__begin_)),
2255           __1d_(__ia)
2256         {}
2257
2258 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2259
2260     _LIBCPP_INLINE_VISIBILITY
2261     indirect_array(valarray<size_t>&& __ia, const valarray<value_type>& __v)
2262         : __vp_(const_cast<value_type*>(__v.__begin_)),
2263           __1d_(move(__ia))
2264         {}
2265
2266 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2267
2268     template <class> friend class valarray;
2269 };
2270
2271 template <class _Tp>
2272 template <class _Expr>
2273 inline _LIBCPP_INLINE_VISIBILITY
2274 typename enable_if
2275 <
2276     __is_val_expr<_Expr>::value,
2277     void
2278 >::type
2279 indirect_array<_Tp>::operator=(const _Expr& __v) const
2280 {
2281     size_t __n = __1d_.size();
2282     for (size_t __i = 0; __i < __n; ++__i)
2283         __vp_[__1d_[__i]] = __v[__i];
2284 }
2285
2286 template <class _Tp>
2287 template <class _Expr>
2288 inline _LIBCPP_INLINE_VISIBILITY
2289 typename enable_if
2290 <
2291     __is_val_expr<_Expr>::value,
2292     void
2293 >::type
2294 indirect_array<_Tp>::operator*=(const _Expr& __v) const
2295 {
2296     size_t __n = __1d_.size();
2297     for (size_t __i = 0; __i < __n; ++__i)
2298         __vp_[__1d_[__i]] *= __v[__i];
2299 }
2300
2301 template <class _Tp>
2302 template <class _Expr>
2303 inline _LIBCPP_INLINE_VISIBILITY
2304 typename enable_if
2305 <
2306     __is_val_expr<_Expr>::value,
2307     void
2308 >::type
2309 indirect_array<_Tp>::operator/=(const _Expr& __v) const
2310 {
2311     size_t __n = __1d_.size();
2312     for (size_t __i = 0; __i < __n; ++__i)
2313         __vp_[__1d_[__i]] /= __v[__i];
2314 }
2315
2316 template <class _Tp>
2317 template <class _Expr>
2318 inline _LIBCPP_INLINE_VISIBILITY
2319 typename enable_if
2320 <
2321     __is_val_expr<_Expr>::value,
2322     void
2323 >::type
2324 indirect_array<_Tp>::operator%=(const _Expr& __v) const
2325 {
2326     size_t __n = __1d_.size();
2327     for (size_t __i = 0; __i < __n; ++__i)
2328         __vp_[__1d_[__i]] %= __v[__i];
2329 }
2330
2331 template <class _Tp>
2332 template <class _Expr>
2333 inline _LIBCPP_INLINE_VISIBILITY
2334 typename enable_if
2335 <
2336     __is_val_expr<_Expr>::value,
2337     void
2338 >::type
2339 indirect_array<_Tp>::operator+=(const _Expr& __v) const
2340 {
2341     size_t __n = __1d_.size();
2342     for (size_t __i = 0; __i < __n; ++__i)
2343         __vp_[__1d_[__i]] += __v[__i];
2344 }
2345
2346 template <class _Tp>
2347 template <class _Expr>
2348 inline _LIBCPP_INLINE_VISIBILITY
2349 typename enable_if
2350 <
2351     __is_val_expr<_Expr>::value,
2352     void
2353 >::type
2354 indirect_array<_Tp>::operator-=(const _Expr& __v) const
2355 {
2356     size_t __n = __1d_.size();
2357     for (size_t __i = 0; __i < __n; ++__i)
2358         __vp_[__1d_[__i]] -= __v[__i];
2359 }
2360
2361 template <class _Tp>
2362 template <class _Expr>
2363 inline _LIBCPP_INLINE_VISIBILITY
2364 typename enable_if
2365 <
2366     __is_val_expr<_Expr>::value,
2367     void
2368 >::type
2369 indirect_array<_Tp>::operator^=(const _Expr& __v) const
2370 {
2371     size_t __n = __1d_.size();
2372     for (size_t __i = 0; __i < __n; ++__i)
2373         __vp_[__1d_[__i]] ^= __v[__i];
2374 }
2375
2376 template <class _Tp>
2377 template <class _Expr>
2378 inline _LIBCPP_INLINE_VISIBILITY
2379 typename enable_if
2380 <
2381     __is_val_expr<_Expr>::value,
2382     void
2383 >::type
2384 indirect_array<_Tp>::operator&=(const _Expr& __v) const
2385 {
2386     size_t __n = __1d_.size();
2387     for (size_t __i = 0; __i < __n; ++__i)
2388         __vp_[__1d_[__i]] &= __v[__i];
2389 }
2390
2391 template <class _Tp>
2392 template <class _Expr>
2393 inline _LIBCPP_INLINE_VISIBILITY
2394 typename enable_if
2395 <
2396     __is_val_expr<_Expr>::value,
2397     void
2398 >::type
2399 indirect_array<_Tp>::operator|=(const _Expr& __v) const
2400 {
2401     size_t __n = __1d_.size();
2402     for (size_t __i = 0; __i < __n; ++__i)
2403         __vp_[__1d_[__i]] |= __v[__i];
2404 }
2405
2406 template <class _Tp>
2407 template <class _Expr>
2408 inline _LIBCPP_INLINE_VISIBILITY
2409 typename enable_if
2410 <
2411     __is_val_expr<_Expr>::value,
2412     void
2413 >::type
2414 indirect_array<_Tp>::operator<<=(const _Expr& __v) const
2415 {
2416     size_t __n = __1d_.size();
2417     for (size_t __i = 0; __i < __n; ++__i)
2418         __vp_[__1d_[__i]] <<= __v[__i];
2419 }
2420
2421 template <class _Tp>
2422 template <class _Expr>
2423 inline _LIBCPP_INLINE_VISIBILITY
2424 typename enable_if
2425 <
2426     __is_val_expr<_Expr>::value,
2427     void
2428 >::type
2429 indirect_array<_Tp>::operator>>=(const _Expr& __v) const
2430 {
2431     size_t __n = __1d_.size();
2432     for (size_t __i = 0; __i < __n; ++__i)
2433         __vp_[__1d_[__i]] >>= __v[__i];
2434 }
2435
2436 template <class _Tp>
2437 inline _LIBCPP_INLINE_VISIBILITY
2438 const indirect_array<_Tp>&
2439 indirect_array<_Tp>::operator=(const indirect_array& __ia) const
2440 {
2441     typedef const size_t* _Ip;
2442     const value_type* __s = __ia.__vp_;
2443     for (_Ip __i = __1d_.__begin_, __e = __1d_.__end_, __j = __ia.__1d_.__begin_;
2444             __i != __e; ++__i, ++__j)
2445         __vp_[*__i] = __s[*__j];
2446     return *this;
2447 }
2448
2449 template <class _Tp>
2450 inline _LIBCPP_INLINE_VISIBILITY
2451 void
2452 indirect_array<_Tp>::operator=(const value_type& __x) const
2453 {
2454     typedef const size_t* _Ip;
2455     for (_Ip __i = __1d_.__begin_, __e = __1d_.__end_; __i != __e; ++__i)
2456         __vp_[*__i] = __x;
2457 }
2458
2459 template <class _ValExpr>
2460 class __indirect_expr
2461 {
2462     typedef typename remove_reference<_ValExpr>::type  _RmExpr;
2463 public:
2464     typedef typename _RmExpr::value_type value_type;
2465     typedef value_type result_type;
2466
2467 private:
2468     _ValExpr __expr_;
2469     valarray<size_t> __1d_;
2470
2471     _LIBCPP_INLINE_VISIBILITY
2472     __indirect_expr(const valarray<size_t>& __ia, const _RmExpr& __e)
2473         : __expr_(__e),
2474           __1d_(__ia)
2475           {}
2476
2477 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2478
2479     _LIBCPP_INLINE_VISIBILITY
2480     __indirect_expr(valarray<size_t>&& __ia, const _RmExpr& __e)
2481         : __expr_(__e),
2482           __1d_(move(__ia))
2483           {}
2484
2485 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2486
2487 public:
2488     _LIBCPP_INLINE_VISIBILITY
2489     result_type operator[](size_t __i) const
2490         {return __expr_[__1d_[__i]];}
2491
2492     _LIBCPP_INLINE_VISIBILITY
2493     size_t size() const {return __1d_.size();}
2494
2495     template <class> friend class _LIBCPP_TYPE_VIS_ONLY valarray;
2496 };
2497
2498 template<class _ValExpr>
2499 class __val_expr
2500 {
2501     typedef typename remove_reference<_ValExpr>::type  _RmExpr;
2502
2503     _ValExpr __expr_;
2504 public:
2505     typedef typename _RmExpr::value_type value_type;
2506     typedef typename _RmExpr::result_type result_type;
2507
2508     _LIBCPP_INLINE_VISIBILITY
2509     explicit __val_expr(const _RmExpr& __e) : __expr_(__e) {}
2510
2511     _LIBCPP_INLINE_VISIBILITY
2512     result_type operator[](size_t __i) const
2513         {return __expr_[__i];}
2514
2515     _LIBCPP_INLINE_VISIBILITY
2516     __val_expr<__slice_expr<_ValExpr> > operator[](slice __s) const
2517         {return __val_expr<__slice_expr<_ValExpr> >(__expr_, __s);}
2518
2519     _LIBCPP_INLINE_VISIBILITY
2520     __val_expr<__indirect_expr<_ValExpr> > operator[](const gslice& __gs) const
2521         {return __val_expr<__indirect_expr<_ValExpr> >(__expr_, __gs.__1d_);}
2522
2523     _LIBCPP_INLINE_VISIBILITY
2524     __val_expr<__mask_expr<_ValExpr> > operator[](const valarray<bool>& __vb) const
2525         {return __val_expr<__mask_expr<_ValExpr> >(__expr_, __vb);}
2526
2527     _LIBCPP_INLINE_VISIBILITY
2528     __val_expr<__indirect_expr<_ValExpr> > operator[](const valarray<size_t>& __vs) const
2529         {return __val_expr<__indirect_expr<_ValExpr> >(__expr_, __vs);}
2530
2531     _LIBCPP_INLINE_VISIBILITY
2532     __val_expr<_UnaryOp<__unary_plus<value_type>, _ValExpr> >
2533     operator+() const
2534     {
2535         typedef _UnaryOp<__unary_plus<value_type>, _ValExpr> _NewExpr;
2536         return __val_expr<_NewExpr>(_NewExpr(__unary_plus<value_type>(), __expr_));
2537     }
2538
2539     _LIBCPP_INLINE_VISIBILITY
2540     __val_expr<_UnaryOp<negate<value_type>, _ValExpr> >
2541     operator-() const
2542     {
2543         typedef _UnaryOp<negate<value_type>, _ValExpr> _NewExpr;
2544         return __val_expr<_NewExpr>(_NewExpr(negate<value_type>(), __expr_));
2545     }
2546
2547     _LIBCPP_INLINE_VISIBILITY
2548     __val_expr<_UnaryOp<__bit_not<value_type>, _ValExpr> >
2549     operator~() const
2550     {
2551         typedef _UnaryOp<__bit_not<value_type>, _ValExpr> _NewExpr;
2552         return __val_expr<_NewExpr>(_NewExpr(__bit_not<value_type>(), __expr_));
2553     }
2554
2555     _LIBCPP_INLINE_VISIBILITY
2556     __val_expr<_UnaryOp<logical_not<value_type>, _ValExpr> >
2557     operator!() const
2558     {
2559         typedef _UnaryOp<logical_not<value_type>, _ValExpr> _NewExpr;
2560         return __val_expr<_NewExpr>(_NewExpr(logical_not<value_type>(), __expr_));
2561     }
2562
2563     operator valarray<result_type>() const;
2564
2565     _LIBCPP_INLINE_VISIBILITY
2566     size_t size() const {return __expr_.size();}
2567
2568     _LIBCPP_INLINE_VISIBILITY
2569     result_type sum() const
2570     {
2571         size_t __n = __expr_.size();
2572         result_type __r = __n ? __expr_[0] : result_type();
2573         for (size_t __i = 1; __i < __n; ++__i)
2574             __r += __expr_[__i];
2575         return __r;
2576     }
2577
2578     _LIBCPP_INLINE_VISIBILITY
2579     result_type min() const
2580     {
2581         size_t __n = size();
2582         result_type __r = __n ? (*this)[0] : result_type();
2583         for (size_t __i = 1; __i < __n; ++__i)
2584         {
2585             result_type __x = __expr_[__i];
2586             if (__x < __r)
2587                 __r = __x;
2588         }
2589         return __r;
2590     }
2591
2592     _LIBCPP_INLINE_VISIBILITY
2593     result_type max() const
2594     {
2595         size_t __n = size();
2596         result_type __r = __n ? (*this)[0] : result_type();
2597         for (size_t __i = 1; __i < __n; ++__i)
2598         {
2599             result_type __x = __expr_[__i];
2600             if (__r < __x)
2601                 __r = __x;
2602         }
2603         return __r;
2604     }
2605
2606     _LIBCPP_INLINE_VISIBILITY
2607     __val_expr<__shift_expr<_ValExpr> > shift (int __i) const
2608         {return __val_expr<__shift_expr<_ValExpr> >(__shift_expr<_ValExpr>(__i, __expr_));}
2609
2610     _LIBCPP_INLINE_VISIBILITY
2611     __val_expr<__cshift_expr<_ValExpr> > cshift(int __i) const
2612         {return __val_expr<__cshift_expr<_ValExpr> >(__cshift_expr<_ValExpr>(__i, __expr_));}
2613
2614     _LIBCPP_INLINE_VISIBILITY
2615     __val_expr<_UnaryOp<__apply_expr<value_type, value_type(*)(value_type)>, _ValExpr> >
2616     apply(value_type __f(value_type)) const
2617     {
2618         typedef __apply_expr<value_type, value_type(*)(value_type)> _Op;
2619         typedef _UnaryOp<_Op, _ValExpr> _NewExpr;
2620         return __val_expr<_NewExpr>(_NewExpr(_Op(__f), __expr_));
2621     }
2622
2623     _LIBCPP_INLINE_VISIBILITY
2624     __val_expr<_UnaryOp<__apply_expr<value_type, value_type(*)(const value_type&)>, _ValExpr> >
2625     apply(value_type __f(const value_type&)) const
2626     {
2627         typedef __apply_expr<value_type, value_type(*)(const value_type&)> _Op;
2628         typedef _UnaryOp<_Op, _ValExpr> _NewExpr;
2629         return __val_expr<_NewExpr>(_NewExpr(_Op(__f), __expr_));
2630     }
2631 };
2632
2633 template<class _ValExpr>
2634 __val_expr<_ValExpr>::operator valarray<__val_expr::result_type>() const
2635 {
2636     valarray<result_type> __r;
2637     size_t __n = __expr_.size();
2638     if (__n)
2639     {
2640         __r.__begin_ =
2641             __r.__end_ =
2642                 static_cast<result_type*>(_VSTD::__allocate(__n * sizeof(result_type)));
2643         for (size_t __i = 0; __i != __n; ++__r.__end_, ++__i)
2644             ::new (__r.__end_) result_type(__expr_[__i]);
2645     }
2646     return __r;
2647 }
2648
2649 // valarray
2650
2651 template <class _Tp>
2652 inline _LIBCPP_INLINE_VISIBILITY
2653 valarray<_Tp>::valarray(size_t __n)
2654     : __begin_(0),
2655       __end_(0)
2656 {
2657     resize(__n);
2658 }
2659
2660 template <class _Tp>
2661 inline _LIBCPP_INLINE_VISIBILITY
2662 valarray<_Tp>::valarray(const value_type& __x, size_t __n)
2663     : __begin_(0),
2664       __end_(0)
2665 {
2666     resize(__n, __x);
2667 }
2668
2669 template <class _Tp>
2670 valarray<_Tp>::valarray(const value_type* __p, size_t __n)
2671     : __begin_(0),
2672       __end_(0)
2673 {
2674     if (__n)
2675     {
2676         __begin_ = __end_ = static_cast<value_type*>(_VSTD::__allocate(__n * sizeof(value_type)));
2677 #ifndef _LIBCPP_NO_EXCEPTIONS
2678         try
2679         {
2680 #endif  // _LIBCPP_NO_EXCEPTIONS
2681             for (; __n; ++__end_, ++__p, --__n)
2682                 ::new (__end_) value_type(*__p);
2683 #ifndef _LIBCPP_NO_EXCEPTIONS
2684         }
2685         catch (...)
2686         {
2687             resize(0);
2688             throw;
2689         }
2690 #endif  // _LIBCPP_NO_EXCEPTIONS
2691     }
2692 }
2693
2694 template <class _Tp>
2695 valarray<_Tp>::valarray(const valarray& __v)
2696     : __begin_(0),
2697       __end_(0)
2698 {
2699     if (__v.size())
2700     {
2701         __begin_ = __end_ = static_cast<value_type*>(_VSTD::__allocate(__v.size() * sizeof(value_type)));
2702 #ifndef _LIBCPP_NO_EXCEPTIONS
2703         try
2704         {
2705 #endif  // _LIBCPP_NO_EXCEPTIONS
2706             for (value_type* __p = __v.__begin_; __p != __v.__end_; ++__end_, ++__p)
2707                 ::new (__end_) value_type(*__p);
2708 #ifndef _LIBCPP_NO_EXCEPTIONS
2709         }
2710         catch (...)
2711         {
2712             resize(0);
2713             throw;
2714         }
2715 #endif  // _LIBCPP_NO_EXCEPTIONS
2716     }
2717 }
2718
2719 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2720
2721 template <class _Tp>
2722 inline _LIBCPP_INLINE_VISIBILITY
2723 valarray<_Tp>::valarray(valarray&& __v) _NOEXCEPT
2724     : __begin_(__v.__begin_),
2725       __end_(__v.__end_)
2726 {
2727     __v.__begin_ = __v.__end_ = nullptr;
2728 }
2729
2730 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2731
2732 #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2733
2734 template <class _Tp>
2735 valarray<_Tp>::valarray(initializer_list<value_type> __il)
2736     : __begin_(0),
2737       __end_(0)
2738 {
2739     size_t __n = __il.size();
2740     if (__n)
2741     {
2742         __begin_ = __end_ = static_cast<value_type*>(_VSTD::__allocate(__n * sizeof(value_type)));
2743 #ifndef _LIBCPP_NO_EXCEPTIONS
2744         try
2745         {
2746 #endif  // _LIBCPP_NO_EXCEPTIONS
2747             for (const value_type* __p = __il.begin(); __n; ++__end_, ++__p, --__n)
2748                 ::new (__end_) value_type(*__p);
2749 #ifndef _LIBCPP_NO_EXCEPTIONS
2750         }
2751         catch (...)
2752         {
2753             resize(0);
2754             throw;
2755         }
2756 #endif  // _LIBCPP_NO_EXCEPTIONS
2757     }
2758 }
2759
2760 #endif  // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2761
2762 template <class _Tp>
2763 valarray<_Tp>::valarray(const slice_array<value_type>& __sa)
2764     : __begin_(0),
2765       __end_(0)
2766 {
2767     size_t __n = __sa.__size_;
2768     if (__n)
2769     {
2770         __begin_ = __end_ = static_cast<value_type*>(_VSTD::__allocate(__n * sizeof(value_type)));
2771 #ifndef _LIBCPP_NO_EXCEPTIONS
2772         try
2773         {
2774 #endif  // _LIBCPP_NO_EXCEPTIONS
2775             for (const value_type* __p = __sa.__vp_; __n; ++__end_, __p += __sa.__stride_, --__n)
2776                 ::new (__end_) value_type(*__p);
2777 #ifndef _LIBCPP_NO_EXCEPTIONS
2778         }
2779         catch (...)
2780         {
2781             resize(0);
2782             throw;
2783         }
2784 #endif  // _LIBCPP_NO_EXCEPTIONS
2785     }
2786 }
2787
2788 template <class _Tp>
2789 valarray<_Tp>::valarray(const gslice_array<value_type>& __ga)
2790     : __begin_(0),
2791       __end_(0)
2792 {
2793     size_t __n = __ga.__1d_.size();
2794     if (__n)
2795     {
2796         __begin_ = __end_ = static_cast<value_type*>(_VSTD::__allocate(__n * sizeof(value_type)));
2797 #ifndef _LIBCPP_NO_EXCEPTIONS
2798         try
2799         {
2800 #endif  // _LIBCPP_NO_EXCEPTIONS
2801             typedef const size_t* _Ip;
2802             const value_type* __s = __ga.__vp_;
2803             for (_Ip __i = __ga.__1d_.__begin_, __e = __ga.__1d_.__end_;
2804                     __i != __e; ++__i, ++__end_)
2805                 ::new (__end_) value_type(__s[*__i]);
2806 #ifndef _LIBCPP_NO_EXCEPTIONS
2807         }
2808         catch (...)
2809         {
2810             resize(0);
2811             throw;
2812         }
2813 #endif  // _LIBCPP_NO_EXCEPTIONS
2814     }
2815 }
2816
2817 template <class _Tp>
2818 valarray<_Tp>::valarray(const mask_array<value_type>& __ma)
2819     : __begin_(0),
2820       __end_(0)
2821 {
2822     size_t __n = __ma.__1d_.size();
2823     if (__n)
2824     {
2825         __begin_ = __end_ = static_cast<value_type*>(_VSTD::__allocate(__n * sizeof(value_type)));
2826 #ifndef _LIBCPP_NO_EXCEPTIONS
2827         try
2828         {
2829 #endif  // _LIBCPP_NO_EXCEPTIONS
2830             typedef const size_t* _Ip;
2831             const value_type* __s = __ma.__vp_;
2832             for (_Ip __i = __ma.__1d_.__begin_, __e = __ma.__1d_.__end_;
2833                     __i != __e; ++__i, ++__end_)
2834                 ::new (__end_) value_type(__s[*__i]);
2835 #ifndef _LIBCPP_NO_EXCEPTIONS
2836         }
2837         catch (...)
2838         {
2839             resize(0);
2840             throw;
2841         }
2842 #endif  // _LIBCPP_NO_EXCEPTIONS
2843     }
2844 }
2845
2846 template <class _Tp>
2847 valarray<_Tp>::valarray(const indirect_array<value_type>& __ia)
2848     : __begin_(0),
2849       __end_(0)
2850 {
2851     size_t __n = __ia.__1d_.size();
2852     if (__n)
2853     {
2854         __begin_ = __end_ = static_cast<value_type*>(_VSTD::__allocate(__n * sizeof(value_type)));
2855 #ifndef _LIBCPP_NO_EXCEPTIONS
2856         try
2857         {
2858 #endif  // _LIBCPP_NO_EXCEPTIONS
2859             typedef const size_t* _Ip;
2860             const value_type* __s = __ia.__vp_;
2861             for (_Ip __i = __ia.__1d_.__begin_, __e = __ia.__1d_.__end_;
2862                     __i != __e; ++__i, ++__end_)
2863                 ::new (__end_) value_type(__s[*__i]);
2864 #ifndef _LIBCPP_NO_EXCEPTIONS
2865         }
2866         catch (...)
2867         {
2868             resize(0);
2869             throw;
2870         }
2871 #endif  // _LIBCPP_NO_EXCEPTIONS
2872     }
2873 }
2874
2875 template <class _Tp>
2876 inline _LIBCPP_INLINE_VISIBILITY
2877 valarray<_Tp>::~valarray()
2878 {
2879     resize(0);
2880 }
2881
2882 template <class _Tp>
2883 valarray<_Tp>&
2884 valarray<_Tp>::operator=(const valarray& __v)
2885 {
2886     if (this != &__v)
2887     {
2888         if (size() != __v.size())
2889             resize(__v.size());
2890         _VSTD::copy(__v.__begin_, __v.__end_, __begin_);
2891     }
2892     return *this;
2893 }
2894
2895 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2896
2897 template <class _Tp>
2898 inline _LIBCPP_INLINE_VISIBILITY
2899 valarray<_Tp>&
2900 valarray<_Tp>::operator=(valarray&& __v) _NOEXCEPT
2901 {
2902     resize(0);
2903     __begin_ = __v.__begin_;
2904     __end_ = __v.__end_;
2905     __v.__begin_ = nullptr;
2906     __v.__end_ = nullptr;
2907     return *this;
2908 }
2909
2910 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2911
2912 #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2913
2914 template <class _Tp>
2915 inline _LIBCPP_INLINE_VISIBILITY
2916 valarray<_Tp>&
2917 valarray<_Tp>::operator=(initializer_list<value_type> __il)
2918 {
2919     if (size() != __il.size())
2920         resize(__il.size());
2921     _VSTD::copy(__il.begin(), __il.end(), __begin_);
2922     return *this;
2923 }
2924
2925 #endif  // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2926
2927 template <class _Tp>
2928 inline _LIBCPP_INLINE_VISIBILITY
2929 valarray<_Tp>&
2930 valarray<_Tp>::operator=(const value_type& __x)
2931 {
2932     _VSTD::fill(__begin_, __end_, __x);
2933     return *this;
2934 }
2935
2936 template <class _Tp>
2937 inline _LIBCPP_INLINE_VISIBILITY
2938 valarray<_Tp>&
2939 valarray<_Tp>::operator=(const slice_array<value_type>& __sa)
2940 {
2941     value_type* __t = __begin_;
2942     const value_type* __s = __sa.__vp_;
2943     for (size_t __n = __sa.__size_; __n; --__n, __s += __sa.__stride_, ++__t)
2944         *__t = *__s;
2945     return *this;
2946 }
2947
2948 template <class _Tp>
2949 inline _LIBCPP_INLINE_VISIBILITY
2950 valarray<_Tp>&
2951 valarray<_Tp>::operator=(const gslice_array<value_type>& __ga)
2952 {
2953     typedef const size_t* _Ip;
2954     value_type* __t = __begin_;
2955     const value_type* __s = __ga.__vp_;
2956     for (_Ip __i = __ga.__1d_.__begin_, __e = __ga.__1d_.__end_;
2957                     __i != __e; ++__i, ++__t)
2958         *__t = __s[*__i];
2959     return *this;
2960 }
2961
2962 template <class _Tp>
2963 inline _LIBCPP_INLINE_VISIBILITY
2964 valarray<_Tp>&
2965 valarray<_Tp>::operator=(const mask_array<value_type>& __ma)
2966 {
2967     typedef const size_t* _Ip;
2968     value_type* __t = __begin_;
2969     const value_type* __s = __ma.__vp_;
2970     for (_Ip __i = __ma.__1d_.__begin_, __e = __ma.__1d_.__end_;
2971                     __i != __e; ++__i, ++__t)
2972         *__t = __s[*__i];
2973     return *this;
2974 }
2975
2976 template <class _Tp>
2977 inline _LIBCPP_INLINE_VISIBILITY
2978 valarray<_Tp>&
2979 valarray<_Tp>::operator=(const indirect_array<value_type>& __ia)
2980 {
2981     typedef const size_t* _Ip;
2982     value_type* __t = __begin_;
2983     const value_type* __s = __ia.__vp_;
2984     for (_Ip __i = __ia.__1d_.__begin_, __e = __ia.__1d_.__end_;
2985                     __i != __e; ++__i, ++__t)
2986         *__t = __s[*__i];
2987     return *this;
2988 }
2989
2990 template <class _Tp>
2991 template <class _ValExpr>
2992 inline _LIBCPP_INLINE_VISIBILITY
2993 valarray<_Tp>&
2994 valarray<_Tp>::operator=(const __val_expr<_ValExpr>& __v)
2995 {
2996     size_t __n = __v.size();
2997     if (size() != __n)
2998         resize(__n);
2999     value_type* __t = __begin_;
3000     for (size_t __i = 0; __i != __n; ++__t, ++__i)
3001         *__t = result_type(__v[__i]);
3002     return *this;
3003 }
3004
3005 template <class _Tp>
3006 inline _LIBCPP_INLINE_VISIBILITY
3007 __val_expr<__slice_expr<const valarray<_Tp>&> >
3008 valarray<_Tp>::operator[](slice __s) const
3009 {
3010     return __val_expr<__slice_expr<const valarray&> >(__slice_expr<const valarray&>(__s, *this));
3011 }
3012
3013 template <class _Tp>
3014 inline _LIBCPP_INLINE_VISIBILITY
3015 slice_array<_Tp>
3016 valarray<_Tp>::operator[](slice __s)
3017 {
3018     return slice_array<value_type>(__s, *this);
3019 }
3020
3021 template <class _Tp>
3022 inline _LIBCPP_INLINE_VISIBILITY
3023 __val_expr<__indirect_expr<const valarray<_Tp>&> >
3024 valarray<_Tp>::operator[](const gslice& __gs) const
3025 {
3026     return __val_expr<__indirect_expr<const valarray&> >(__indirect_expr<const valarray&>(__gs.__1d_, *this));
3027 }
3028
3029 template <class _Tp>
3030 inline _LIBCPP_INLINE_VISIBILITY
3031 gslice_array<_Tp>
3032 valarray<_Tp>::operator[](const gslice& __gs)
3033 {
3034     return gslice_array<value_type>(__gs, *this);
3035 }
3036
3037 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3038
3039 template <class _Tp>
3040 inline _LIBCPP_INLINE_VISIBILITY
3041 __val_expr<__indirect_expr<const valarray<_Tp>&> >
3042 valarray<_Tp>::operator[](gslice&& __gs) const
3043 {
3044     return __val_expr<__indirect_expr<const valarray&> >(__indirect_expr<const valarray&>(move(__gs.__1d_), *this));
3045 }
3046
3047 template <class _Tp>
3048 inline _LIBCPP_INLINE_VISIBILITY
3049 gslice_array<_Tp>
3050 valarray<_Tp>::operator[](gslice&& __gs)
3051 {
3052     return gslice_array<value_type>(move(__gs), *this);
3053 }
3054
3055 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
3056
3057 template <class _Tp>
3058 inline _LIBCPP_INLINE_VISIBILITY
3059 __val_expr<__mask_expr<const valarray<_Tp>&> >
3060 valarray<_Tp>::operator[](const valarray<bool>& __vb) const
3061 {
3062     return __val_expr<__mask_expr<const valarray&> >(__mask_expr<const valarray&>(__vb, *this));
3063 }
3064
3065 template <class _Tp>
3066 inline _LIBCPP_INLINE_VISIBILITY
3067 mask_array<_Tp>
3068 valarray<_Tp>::operator[](const valarray<bool>& __vb)
3069 {
3070     return mask_array<value_type>(__vb, *this);
3071 }
3072
3073 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3074
3075 template <class _Tp>
3076 inline _LIBCPP_INLINE_VISIBILITY
3077 __val_expr<__mask_expr<const valarray<_Tp>&> >
3078 valarray<_Tp>::operator[](valarray<bool>&& __vb) const
3079 {
3080     return __val_expr<__mask_expr<const valarray&> >(__mask_expr<const valarray&>(move(__vb), *this));
3081 }
3082
3083 template <class _Tp>
3084 inline _LIBCPP_INLINE_VISIBILITY
3085 mask_array<_Tp>
3086 valarray<_Tp>::operator[](valarray<bool>&& __vb)
3087 {
3088     return mask_array<value_type>(move(__vb), *this);
3089 }
3090
3091 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
3092
3093 template <class _Tp>
3094 inline _LIBCPP_INLINE_VISIBILITY
3095 __val_expr<__indirect_expr<const valarray<_Tp>&> >
3096 valarray<_Tp>::operator[](const valarray<size_t>& __vs) const
3097 {
3098     return __val_expr<__indirect_expr<const valarray&> >(__indirect_expr<const valarray&>(__vs, *this));
3099 }
3100
3101 template <class _Tp>
3102 inline _LIBCPP_INLINE_VISIBILITY
3103 indirect_array<_Tp>
3104 valarray<_Tp>::operator[](const valarray<size_t>& __vs)
3105 {
3106     return indirect_array<value_type>(__vs, *this);
3107 }
3108
3109 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3110
3111 template <class _Tp>
3112 inline _LIBCPP_INLINE_VISIBILITY
3113 __val_expr<__indirect_expr<const valarray<_Tp>&> >
3114 valarray<_Tp>::operator[](valarray<size_t>&& __vs) const
3115 {
3116     return __val_expr<__indirect_expr<const valarray&> >(__indirect_expr<const valarray&>(move(__vs), *this));
3117 }
3118
3119 template <class _Tp>
3120 inline _LIBCPP_INLINE_VISIBILITY
3121 indirect_array<_Tp>
3122 valarray<_Tp>::operator[](valarray<size_t>&& __vs)
3123 {
3124     return indirect_array<value_type>(move(__vs), *this);
3125 }
3126
3127 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
3128
3129 template <class _Tp>
3130 valarray<_Tp>
3131 valarray<_Tp>::operator+() const
3132 {
3133     valarray<value_type> __r;
3134     size_t __n = size();
3135     if (__n)
3136     {
3137         __r.__begin_ =
3138             __r.__end_ =
3139                 static_cast<value_type*>(_VSTD::__allocate(__n * sizeof(value_type)));
3140         for (const value_type* __p = __begin_; __n; ++__r.__end_, ++__p, --__n)
3141             ::new (__r.__end_) value_type(+*__p);
3142     }
3143     return __r;
3144 }
3145
3146 template <class _Tp>
3147 valarray<_Tp>
3148 valarray<_Tp>::operator-() const
3149 {
3150     valarray<value_type> __r;
3151     size_t __n = size();
3152     if (__n)
3153     {
3154         __r.__begin_ =
3155             __r.__end_ =
3156                 static_cast<value_type*>(_VSTD::__allocate(__n * sizeof(value_type)));
3157         for (const value_type* __p = __begin_; __n; ++__r.__end_, ++__p, --__n)
3158             ::new (__r.__end_) value_type(-*__p);
3159     }
3160     return __r;
3161 }
3162
3163 template <class _Tp>
3164 valarray<_Tp>
3165 valarray<_Tp>::operator~() const
3166 {
3167     valarray<value_type> __r;
3168     size_t __n = size();
3169     if (__n)
3170     {
3171         __r.__begin_ =
3172             __r.__end_ =
3173                 static_cast<value_type*>(_VSTD::__allocate(__n * sizeof(value_type)));
3174         for (const value_type* __p = __begin_; __n; ++__r.__end_, ++__p, --__n)
3175             ::new (__r.__end_) value_type(~*__p);
3176     }
3177     return __r;
3178 }
3179
3180 template <class _Tp>
3181 valarray<bool>
3182 valarray<_Tp>::operator!() const
3183 {
3184     valarray<bool> __r;
3185     size_t __n = size();
3186     if (__n)
3187     {
3188         __r.__begin_ =
3189             __r.__end_ =
3190                 static_cast<bool*>(_VSTD::__allocate(__n * sizeof(bool)));
3191         for (const value_type* __p = __begin_; __n; ++__r.__end_, ++__p, --__n)
3192             ::new (__r.__end_) bool(!*__p);
3193     }
3194     return __r;
3195 }
3196
3197 template <class _Tp>
3198 inline _LIBCPP_INLINE_VISIBILITY
3199 valarray<_Tp>&
3200 valarray<_Tp>::operator*=(const value_type& __x)
3201 {
3202     for (value_type* __p = __begin_; __p != __end_; ++__p)
3203         *__p *= __x;
3204     return *this;
3205 }
3206
3207 template <class _Tp>
3208 inline _LIBCPP_INLINE_VISIBILITY
3209 valarray<_Tp>&
3210 valarray<_Tp>::operator/=(const value_type& __x)
3211 {
3212     for (value_type* __p = __begin_; __p != __end_; ++__p)
3213         *__p /= __x;
3214     return *this;
3215 }
3216
3217 template <class _Tp>
3218 inline _LIBCPP_INLINE_VISIBILITY
3219 valarray<_Tp>&
3220 valarray<_Tp>::operator%=(const value_type& __x)
3221 {
3222     for (value_type* __p = __begin_; __p != __end_; ++__p)
3223         *__p %= __x;
3224     return *this;
3225 }
3226
3227 template <class _Tp>
3228 inline _LIBCPP_INLINE_VISIBILITY
3229 valarray<_Tp>&
3230 valarray<_Tp>::operator+=(const value_type& __x)
3231 {
3232     for (value_type* __p = __begin_; __p != __end_; ++__p)
3233         *__p += __x;
3234     return *this;
3235 }
3236
3237 template <class _Tp>
3238 inline _LIBCPP_INLINE_VISIBILITY
3239 valarray<_Tp>&
3240 valarray<_Tp>::operator-=(const value_type& __x)
3241 {
3242     for (value_type* __p = __begin_; __p != __end_; ++__p)
3243         *__p -= __x;
3244     return *this;
3245 }
3246
3247 template <class _Tp>
3248 inline _LIBCPP_INLINE_VISIBILITY
3249 valarray<_Tp>&
3250 valarray<_Tp>::operator^=(const value_type& __x)
3251 {
3252     for (value_type* __p = __begin_; __p != __end_; ++__p)
3253         *__p ^= __x;
3254     return *this;
3255 }
3256
3257 template <class _Tp>
3258 inline _LIBCPP_INLINE_VISIBILITY
3259 valarray<_Tp>&
3260 valarray<_Tp>::operator&=(const value_type& __x)
3261 {
3262     for (value_type* __p = __begin_; __p != __end_; ++__p)
3263         *__p &= __x;
3264     return *this;
3265 }
3266
3267 template <class _Tp>
3268 inline _LIBCPP_INLINE_VISIBILITY
3269 valarray<_Tp>&
3270 valarray<_Tp>::operator|=(const value_type& __x)
3271 {
3272     for (value_type* __p = __begin_; __p != __end_; ++__p)
3273         *__p |= __x;
3274     return *this;
3275 }
3276
3277 template <class _Tp>
3278 inline _LIBCPP_INLINE_VISIBILITY
3279 valarray<_Tp>&
3280 valarray<_Tp>::operator<<=(const value_type& __x)
3281 {
3282     for (value_type* __p = __begin_; __p != __end_; ++__p)
3283         *__p <<= __x;
3284     return *this;
3285 }
3286
3287 template <class _Tp>
3288 inline _LIBCPP_INLINE_VISIBILITY
3289 valarray<_Tp>&
3290 valarray<_Tp>::operator>>=(const value_type& __x)
3291 {
3292     for (value_type* __p = __begin_; __p != __end_; ++__p)
3293         *__p >>= __x;
3294     return *this;
3295 }
3296
3297 template <class _Tp>
3298 template <class _Expr>
3299 inline _LIBCPP_INLINE_VISIBILITY
3300 typename enable_if
3301 <
3302     __is_val_expr<_Expr>::value,
3303     valarray<_Tp>&
3304 >::type
3305 valarray<_Tp>::operator*=(const _Expr& __v)
3306 {
3307     size_t __i = 0;
3308     for (value_type* __t = __begin_; __t != __end_ ; ++__t, ++__i)
3309         *__t *= __v[__i];
3310     return *this;
3311 }
3312
3313 template <class _Tp>
3314 template <class _Expr>
3315 inline _LIBCPP_INLINE_VISIBILITY
3316 typename enable_if
3317 <
3318     __is_val_expr<_Expr>::value,
3319     valarray<_Tp>&
3320 >::type
3321 valarray<_Tp>::operator/=(const _Expr& __v)
3322 {
3323     size_t __i = 0;
3324     for (value_type* __t = __begin_; __t != __end_ ; ++__t, ++__i)
3325         *__t /= __v[__i];
3326     return *this;
3327 }
3328
3329 template <class _Tp>
3330 template <class _Expr>
3331 inline _LIBCPP_INLINE_VISIBILITY
3332 typename enable_if
3333 <
3334     __is_val_expr<_Expr>::value,
3335     valarray<_Tp>&
3336 >::type
3337 valarray<_Tp>::operator%=(const _Expr& __v)
3338 {
3339     size_t __i = 0;
3340     for (value_type* __t = __begin_; __t != __end_ ; ++__t, ++__i)
3341         *__t %= __v[__i];
3342     return *this;
3343 }
3344
3345 template <class _Tp>
3346 template <class _Expr>
3347 inline _LIBCPP_INLINE_VISIBILITY
3348 typename enable_if
3349 <
3350     __is_val_expr<_Expr>::value,
3351     valarray<_Tp>&
3352 >::type
3353 valarray<_Tp>::operator+=(const _Expr& __v)
3354 {
3355     size_t __i = 0;
3356     for (value_type* __t = __begin_; __t != __end_ ; ++__t, ++__i)
3357         *__t += __v[__i];
3358     return *this;
3359 }
3360
3361 template <class _Tp>
3362 template <class _Expr>
3363 inline _LIBCPP_INLINE_VISIBILITY
3364 typename enable_if
3365 <
3366     __is_val_expr<_Expr>::value,
3367     valarray<_Tp>&
3368 >::type
3369 valarray<_Tp>::operator-=(const _Expr& __v)
3370 {
3371     size_t __i = 0;
3372     for (value_type* __t = __begin_; __t != __end_ ; ++__t, ++__i)
3373         *__t -= __v[__i];
3374     return *this;
3375 }
3376
3377 template <class _Tp>
3378 template <class _Expr>
3379 inline _LIBCPP_INLINE_VISIBILITY
3380 typename enable_if
3381 <
3382     __is_val_expr<_Expr>::value,
3383     valarray<_Tp>&
3384 >::type
3385 valarray<_Tp>::operator^=(const _Expr& __v)
3386 {
3387     size_t __i = 0;
3388     for (value_type* __t = __begin_; __t != __end_ ; ++__t, ++__i)
3389         *__t ^= __v[__i];
3390     return *this;
3391 }
3392
3393 template <class _Tp>
3394 template <class _Expr>
3395 inline _LIBCPP_INLINE_VISIBILITY
3396 typename enable_if
3397 <
3398     __is_val_expr<_Expr>::value,
3399     valarray<_Tp>&
3400 >::type
3401 valarray<_Tp>::operator|=(const _Expr& __v)
3402 {
3403     size_t __i = 0;
3404     for (value_type* __t = __begin_; __t != __end_ ; ++__t, ++__i)
3405         *__t |= __v[__i];
3406     return *this;
3407 }
3408
3409 template <class _Tp>
3410 template <class _Expr>
3411 inline _LIBCPP_INLINE_VISIBILITY
3412 typename enable_if
3413 <
3414     __is_val_expr<_Expr>::value,
3415     valarray<_Tp>&
3416 >::type
3417 valarray<_Tp>::operator&=(const _Expr& __v)
3418 {
3419     size_t __i = 0;
3420     for (value_type* __t = __begin_; __t != __end_ ; ++__t, ++__i)
3421         *__t &= __v[__i];
3422     return *this;
3423 }
3424
3425 template <class _Tp>
3426 template <class _Expr>
3427 inline _LIBCPP_INLINE_VISIBILITY
3428 typename enable_if
3429 <
3430     __is_val_expr<_Expr>::value,
3431     valarray<_Tp>&
3432 >::type
3433 valarray<_Tp>::operator<<=(const _Expr& __v)
3434 {
3435     size_t __i = 0;
3436     for (value_type* __t = __begin_; __t != __end_ ; ++__t, ++__i)
3437         *__t <<= __v[__i];
3438     return *this;
3439 }
3440
3441 template <class _Tp>
3442 template <class _Expr>
3443 inline _LIBCPP_INLINE_VISIBILITY
3444 typename enable_if
3445 <
3446     __is_val_expr<_Expr>::value,
3447     valarray<_Tp>&
3448 >::type
3449 valarray<_Tp>::operator>>=(const _Expr& __v)
3450 {
3451     size_t __i = 0;
3452     for (value_type* __t = __begin_; __t != __end_ ; ++__t, ++__i)
3453         *__t >>= __v[__i];
3454     return *this;
3455 }
3456
3457 template <class _Tp>
3458 inline _LIBCPP_INLINE_VISIBILITY
3459 void
3460 valarray<_Tp>::swap(valarray& __v) _NOEXCEPT
3461 {
3462     _VSTD::swap(__begin_, __v.__begin_);
3463     _VSTD::swap(__end_, __v.__end_);
3464 }
3465
3466 template <class _Tp>
3467 inline _LIBCPP_INLINE_VISIBILITY
3468 _Tp
3469 valarray<_Tp>::sum() const
3470 {
3471     if (__begin_ == __end_)
3472         return value_type();
3473     const value_type* __p = __begin_;
3474     _Tp __r = *__p;
3475     for (++__p; __p != __end_; ++__p)
3476         __r += *__p;
3477     return __r;
3478 }
3479
3480 template <class _Tp>
3481 inline _LIBCPP_INLINE_VISIBILITY
3482 _Tp
3483 valarray<_Tp>::min() const
3484 {
3485     if (__begin_ == __end_)
3486         return value_type();
3487     return *_VSTD::min_element(__begin_, __end_);
3488 }
3489
3490 template <class _Tp>
3491 inline _LIBCPP_INLINE_VISIBILITY
3492 _Tp
3493 valarray<_Tp>::max() const
3494 {
3495     if (__begin_ == __end_)
3496         return value_type();
3497     return *_VSTD::max_element(__begin_, __end_);
3498 }
3499
3500 template <class _Tp>
3501 valarray<_Tp>
3502 valarray<_Tp>::shift(int __i) const
3503 {
3504     valarray<value_type> __r;
3505     size_t __n = size();
3506     if (__n)
3507     {
3508         __r.__begin_ =
3509             __r.__end_ =
3510                 static_cast<value_type*>(_VSTD::__allocate(__n * sizeof(value_type)));
3511         const value_type* __sb;
3512         value_type* __tb;
3513         value_type* __te;
3514         if (__i >= 0)
3515         {
3516             __i = _VSTD::min(__i, static_cast<int>(__n));
3517             __sb = __begin_ + __i;
3518             __tb = __r.__begin_;
3519             __te = __r.__begin_ + (__n - __i);
3520         }
3521         else
3522         {
3523             __i = _VSTD::min(-__i, static_cast<int>(__n));
3524             __sb = __begin_;
3525             __tb = __r.__begin_ + __i;
3526             __te = __r.__begin_ + __n;
3527         }
3528         for (; __r.__end_ != __tb; ++__r.__end_)
3529             ::new (__r.__end_) value_type();
3530         for (; __r.__end_ != __te; ++__r.__end_, ++__sb)
3531             ::new (__r.__end_) value_type(*__sb);
3532         for (__te = __r.__begin_ + __n; __r.__end_ != __te; ++__r.__end_)
3533             ::new (__r.__end_) value_type();
3534     }
3535     return __r;
3536 }
3537
3538 template <class _Tp>
3539 valarray<_Tp>
3540 valarray<_Tp>::cshift(int __i) const
3541 {
3542     valarray<value_type> __r;
3543     size_t __n = size();
3544     if (__n)
3545     {
3546         __r.__begin_ =
3547             __r.__end_ =
3548                 static_cast<value_type*>(_VSTD::__allocate(__n * sizeof(value_type)));
3549         __i %= static_cast<int>(__n);
3550         const value_type* __m = __i >= 0 ? __begin_ + __i : __end_ + __i;
3551         for (const value_type* __s = __m; __s != __end_; ++__r.__end_, ++__s)
3552             ::new (__r.__end_) value_type(*__s);
3553         for (const value_type* __s = __begin_; __s != __m; ++__r.__end_, ++__s)
3554             ::new (__r.__end_) value_type(*__s);
3555     }
3556     return __r;
3557 }
3558
3559 template <class _Tp>
3560 valarray<_Tp>
3561 valarray<_Tp>::apply(value_type __f(value_type)) const
3562 {
3563     valarray<value_type> __r;
3564     size_t __n = size();
3565     if (__n)
3566     {
3567         __r.__begin_ =
3568             __r.__end_ =
3569                 static_cast<value_type*>(_VSTD::__allocate(__n * sizeof(value_type)));
3570         for (const value_type* __p = __begin_; __n; ++__r.__end_, ++__p, --__n)
3571             ::new (__r.__end_) value_type(__f(*__p));
3572     }
3573     return __r;
3574 }
3575
3576 template <class _Tp>
3577 valarray<_Tp>
3578 valarray<_Tp>::apply(value_type __f(const value_type&)) const
3579 {
3580     valarray<value_type> __r;
3581     size_t __n = size();
3582     if (__n)
3583     {
3584         __r.__begin_ =
3585             __r.__end_ =
3586                 static_cast<value_type*>(_VSTD::__allocate(__n * sizeof(value_type)));
3587         for (const value_type* __p = __begin_; __n; ++__r.__end_, ++__p, --__n)
3588             ::new (__r.__end_) value_type(__f(*__p));
3589     }
3590     return __r;
3591 }
3592
3593 template <class _Tp>
3594 void
3595 valarray<_Tp>::resize(size_t __n, value_type __x)
3596 {
3597     if (__begin_ != nullptr)
3598     {
3599         while (__end_ != __begin_)
3600             (--__end_)->~value_type();
3601         _VSTD::__deallocate(__begin_);
3602         __begin_ = __end_ = nullptr;
3603     }
3604     if (__n)
3605     {
3606         __begin_ = __end_ = static_cast<value_type*>(_VSTD::__allocate(__n * sizeof(value_type)));
3607 #ifndef _LIBCPP_NO_EXCEPTIONS
3608         try
3609         {
3610 #endif  // _LIBCPP_NO_EXCEPTIONS
3611             for (; __n; --__n, ++__end_)
3612                 ::new (__end_) value_type(__x);
3613 #ifndef _LIBCPP_NO_EXCEPTIONS
3614         }
3615         catch (...)
3616         {
3617             resize(0);
3618             throw;
3619         }
3620 #endif  // _LIBCPP_NO_EXCEPTIONS
3621     }
3622 }
3623
3624 template<class _Tp>
3625 inline _LIBCPP_INLINE_VISIBILITY
3626 void
3627 swap(valarray<_Tp>& __x, valarray<_Tp>& __y) _NOEXCEPT
3628 {
3629     __x.swap(__y);
3630 }
3631
3632 template<class _Expr1, class _Expr2>
3633 inline _LIBCPP_INLINE_VISIBILITY
3634 typename enable_if
3635 <
3636     __is_val_expr<_Expr1>::value && __is_val_expr<_Expr2>::value,
3637     __val_expr<_BinaryOp<multiplies<typename _Expr1::value_type>, _Expr1, _Expr2> >
3638 >::type
3639 operator*(const _Expr1& __x, const _Expr2& __y)
3640 {
3641     typedef typename _Expr1::value_type value_type;
3642     typedef _BinaryOp<multiplies<value_type>, _Expr1, _Expr2> _Op;
3643     return __val_expr<_Op>(_Op(multiplies<value_type>(), __x, __y));
3644 }
3645
3646 template<class _Expr>
3647 inline _LIBCPP_INLINE_VISIBILITY
3648 typename enable_if
3649 <
3650     __is_val_expr<_Expr>::value,
3651     __val_expr<_BinaryOp<multiplies<typename _Expr::value_type>,
3652                _Expr, __scalar_expr<typename _Expr::value_type> > >
3653 >::type
3654 operator*(const _Expr& __x, const typename _Expr::value_type& __y)
3655 {
3656     typedef typename _Expr::value_type value_type;
3657     typedef _BinaryOp<multiplies<value_type>, _Expr, __scalar_expr<value_type> > _Op;
3658     return __val_expr<_Op>(_Op(multiplies<value_type>(),
3659                            __x, __scalar_expr<value_type>(__y, __x.size())));
3660 }
3661
3662 template<class _Expr>
3663 inline _LIBCPP_INLINE_VISIBILITY
3664 typename enable_if
3665 <
3666     __is_val_expr<_Expr>::value,
3667     __val_expr<_BinaryOp<multiplies<typename _Expr::value_type>,
3668                __scalar_expr<typename _Expr::value_type>, _Expr> >
3669 >::type
3670 operator*(const typename _Expr::value_type& __x, const _Expr& __y)
3671 {
3672     typedef typename _Expr::value_type value_type;
3673     typedef _BinaryOp<multiplies<value_type>, __scalar_expr<value_type>, _Expr> _Op;
3674     return __val_expr<_Op>(_Op(multiplies<value_type>(),
3675                            __scalar_expr<value_type>(__x, __y.size()), __y));
3676 }
3677
3678 template<class _Expr1, class _Expr2>
3679 inline _LIBCPP_INLINE_VISIBILITY
3680 typename enable_if
3681 <
3682     __is_val_expr<_Expr1>::value && __is_val_expr<_Expr2>::value,
3683     __val_expr<_BinaryOp<divides<typename _Expr1::value_type>, _Expr1, _Expr2> >
3684 >::type
3685 operator/(const _Expr1& __x, const _Expr2& __y)
3686 {
3687     typedef typename _Expr1::value_type value_type;
3688     typedef _BinaryOp<divides<value_type>, _Expr1, _Expr2> _Op;
3689     return __val_expr<_Op>(_Op(divides<value_type>(), __x, __y));
3690 }
3691
3692 template<class _Expr>
3693 inline _LIBCPP_INLINE_VISIBILITY
3694 typename enable_if
3695 <
3696     __is_val_expr<_Expr>::value,
3697     __val_expr<_BinaryOp<divides<typename _Expr::value_type>,
3698                _Expr, __scalar_expr<typename _Expr::value_type> > >
3699 >::type
3700 operator/(const _Expr& __x, const typename _Expr::value_type& __y)
3701 {
3702     typedef typename _Expr::value_type value_type;
3703     typedef _BinaryOp<divides<value_type>, _Expr, __scalar_expr<value_type> > _Op;
3704     return __val_expr<_Op>(_Op(divides<value_type>(),
3705                            __x, __scalar_expr<value_type>(__y, __x.size())));
3706 }
3707
3708 template<class _Expr>
3709 inline _LIBCPP_INLINE_VISIBILITY
3710 typename enable_if
3711 <
3712     __is_val_expr<_Expr>::value,
3713     __val_expr<_BinaryOp<divides<typename _Expr::value_type>,
3714                __scalar_expr<typename _Expr::value_type>, _Expr> >
3715 >::type
3716 operator/(const typename _Expr::value_type& __x, const _Expr& __y)
3717 {
3718     typedef typename _Expr::value_type value_type;
3719     typedef _BinaryOp<divides<value_type>, __scalar_expr<value_type>, _Expr> _Op;
3720     return __val_expr<_Op>(_Op(divides<value_type>(),
3721                            __scalar_expr<value_type>(__x, __y.size()), __y));
3722 }
3723
3724 template<class _Expr1, class _Expr2>
3725 inline _LIBCPP_INLINE_VISIBILITY
3726 typename enable_if
3727 <
3728     __is_val_expr<_Expr1>::value && __is_val_expr<_Expr2>::value,
3729     __val_expr<_BinaryOp<modulus<typename _Expr1::value_type>, _Expr1, _Expr2> >
3730 >::type
3731 operator%(const _Expr1& __x, const _Expr2& __y)
3732 {
3733     typedef typename _Expr1::value_type value_type;
3734     typedef _BinaryOp<modulus<value_type>, _Expr1, _Expr2> _Op;
3735     return __val_expr<_Op>(_Op(modulus<value_type>(), __x, __y));
3736 }
3737
3738 template<class _Expr>
3739 inline _LIBCPP_INLINE_VISIBILITY
3740 typename enable_if
3741 <
3742     __is_val_expr<_Expr>::value,
3743     __val_expr<_BinaryOp<modulus<typename _Expr::value_type>,
3744                _Expr, __scalar_expr<typename _Expr::value_type> > >
3745 >::type
3746 operator%(const _Expr& __x, const typename _Expr::value_type& __y)
3747 {
3748     typedef typename _Expr::value_type value_type;
3749     typedef _BinaryOp<modulus<value_type>, _Expr, __scalar_expr<value_type> > _Op;
3750     return __val_expr<_Op>(_Op(modulus<value_type>(),
3751                            __x, __scalar_expr<value_type>(__y, __x.size())));
3752 }
3753
3754 template<class _Expr>
3755 inline _LIBCPP_INLINE_VISIBILITY
3756 typename enable_if
3757 <
3758     __is_val_expr<_Expr>::value,
3759     __val_expr<_BinaryOp<modulus<typename _Expr::value_type>,
3760                __scalar_expr<typename _Expr::value_type>, _Expr> >
3761 >::type
3762 operator%(const typename _Expr::value_type& __x, const _Expr& __y)
3763 {
3764     typedef typename _Expr::value_type value_type;
3765     typedef _BinaryOp<modulus<value_type>, __scalar_expr<value_type>, _Expr> _Op;
3766     return __val_expr<_Op>(_Op(modulus<value_type>(),
3767                            __scalar_expr<value_type>(__x, __y.size()), __y));
3768 }
3769
3770 template<class _Expr1, class _Expr2>
3771 inline _LIBCPP_INLINE_VISIBILITY
3772 typename enable_if
3773 <
3774     __is_val_expr<_Expr1>::value && __is_val_expr<_Expr2>::value,
3775     __val_expr<_BinaryOp<plus<typename _Expr1::value_type>, _Expr1, _Expr2> >
3776 >::type
3777 operator+(const _Expr1& __x, const _Expr2& __y)
3778 {
3779     typedef typename _Expr1::value_type value_type;
3780     typedef _BinaryOp<plus<value_type>, _Expr1, _Expr2> _Op;
3781     return __val_expr<_Op>(_Op(plus<value_type>(), __x, __y));
3782 }
3783
3784 template<class _Expr>
3785 inline _LIBCPP_INLINE_VISIBILITY
3786 typename enable_if
3787 <
3788     __is_val_expr<_Expr>::value,
3789     __val_expr<_BinaryOp<plus<typename _Expr::value_type>,
3790                _Expr, __scalar_expr<typename _Expr::value_type> > >
3791 >::type
3792 operator+(const _Expr& __x, const typename _Expr::value_type& __y)
3793 {
3794     typedef typename _Expr::value_type value_type;
3795     typedef _BinaryOp<plus<value_type>, _Expr, __scalar_expr<value_type> > _Op;
3796     return __val_expr<_Op>(_Op(plus<value_type>(),
3797                            __x, __scalar_expr<value_type>(__y, __x.size())));
3798 }
3799
3800 template<class _Expr>
3801 inline _LIBCPP_INLINE_VISIBILITY
3802 typename enable_if
3803 <
3804     __is_val_expr<_Expr>::value,
3805     __val_expr<_BinaryOp<plus<typename _Expr::value_type>,
3806                __scalar_expr<typename _Expr::value_type>, _Expr> >
3807 >::type
3808 operator+(const typename _Expr::value_type& __x, const _Expr& __y)
3809 {
3810     typedef typename _Expr::value_type value_type;
3811     typedef _BinaryOp<plus<value_type>, __scalar_expr<value_type>, _Expr> _Op;
3812     return __val_expr<_Op>(_Op(plus<value_type>(),
3813                            __scalar_expr<value_type>(__x, __y.size()), __y));
3814 }
3815
3816 template<class _Expr1, class _Expr2>
3817 inline _LIBCPP_INLINE_VISIBILITY
3818 typename enable_if
3819 <
3820     __is_val_expr<_Expr1>::value && __is_val_expr<_Expr2>::value,
3821     __val_expr<_BinaryOp<minus<typename _Expr1::value_type>, _Expr1, _Expr2> >
3822 >::type
3823 operator-(const _Expr1& __x, const _Expr2& __y)
3824 {
3825     typedef typename _Expr1::value_type value_type;
3826     typedef _BinaryOp<minus<value_type>, _Expr1, _Expr2> _Op;
3827     return __val_expr<_Op>(_Op(minus<value_type>(), __x, __y));
3828 }
3829
3830 template<class _Expr>
3831 inline _LIBCPP_INLINE_VISIBILITY
3832 typename enable_if
3833 <
3834     __is_val_expr<_Expr>::value,
3835     __val_expr<_BinaryOp<minus<typename _Expr::value_type>,
3836                _Expr, __scalar_expr<typename _Expr::value_type> > >
3837 >::type
3838 operator-(const _Expr& __x, const typename _Expr::value_type& __y)
3839 {
3840     typedef typename _Expr::value_type value_type;
3841     typedef _BinaryOp<minus<value_type>, _Expr, __scalar_expr<value_type> > _Op;
3842     return __val_expr<_Op>(_Op(minus<value_type>(),
3843                            __x, __scalar_expr<value_type>(__y, __x.size())));
3844 }
3845
3846 template<class _Expr>
3847 inline _LIBCPP_INLINE_VISIBILITY
3848 typename enable_if
3849 <
3850     __is_val_expr<_Expr>::value,
3851     __val_expr<_BinaryOp<minus<typename _Expr::value_type>,
3852                __scalar_expr<typename _Expr::value_type>, _Expr> >
3853 >::type
3854 operator-(const typename _Expr::value_type& __x, const _Expr& __y)
3855 {
3856     typedef typename _Expr::value_type value_type;
3857     typedef _BinaryOp<minus<value_type>, __scalar_expr<value_type>, _Expr> _Op;
3858     return __val_expr<_Op>(_Op(minus<value_type>(),
3859                            __scalar_expr<value_type>(__x, __y.size()), __y));
3860 }
3861
3862 template<class _Expr1, class _Expr2>
3863 inline _LIBCPP_INLINE_VISIBILITY
3864 typename enable_if
3865 <
3866     __is_val_expr<_Expr1>::value && __is_val_expr<_Expr2>::value,
3867     __val_expr<_BinaryOp<bit_xor<typename _Expr1::value_type>, _Expr1, _Expr2> >
3868 >::type
3869 operator^(const _Expr1& __x, const _Expr2& __y)
3870 {
3871     typedef typename _Expr1::value_type value_type;
3872     typedef _BinaryOp<bit_xor<value_type>, _Expr1, _Expr2> _Op;
3873     return __val_expr<_Op>(_Op(bit_xor<value_type>(), __x, __y));
3874 }
3875
3876 template<class _Expr>
3877 inline _LIBCPP_INLINE_VISIBILITY
3878 typename enable_if
3879 <
3880     __is_val_expr<_Expr>::value,
3881     __val_expr<_BinaryOp<bit_xor<typename _Expr::value_type>,
3882                _Expr, __scalar_expr<typename _Expr::value_type> > >
3883 >::type
3884 operator^(const _Expr& __x, const typename _Expr::value_type& __y)
3885 {
3886     typedef typename _Expr::value_type value_type;
3887     typedef _BinaryOp<bit_xor<value_type>, _Expr, __scalar_expr<value_type> > _Op;
3888     return __val_expr<_Op>(_Op(bit_xor<value_type>(),
3889                            __x, __scalar_expr<value_type>(__y, __x.size())));
3890 }
3891
3892 template<class _Expr>
3893 inline _LIBCPP_INLINE_VISIBILITY
3894 typename enable_if
3895 <
3896     __is_val_expr<_Expr>::value,
3897     __val_expr<_BinaryOp<bit_xor<typename _Expr::value_type>,
3898                __scalar_expr<typename _Expr::value_type>, _Expr> >
3899 >::type
3900 operator^(const typename _Expr::value_type& __x, const _Expr& __y)
3901 {
3902     typedef typename _Expr::value_type value_type;
3903     typedef _BinaryOp<bit_xor<value_type>, __scalar_expr<value_type>, _Expr> _Op;
3904     return __val_expr<_Op>(_Op(bit_xor<value_type>(),
3905                            __scalar_expr<value_type>(__x, __y.size()), __y));
3906 }
3907
3908 template<class _Expr1, class _Expr2>
3909 inline _LIBCPP_INLINE_VISIBILITY
3910 typename enable_if
3911 <
3912     __is_val_expr<_Expr1>::value && __is_val_expr<_Expr2>::value,
3913     __val_expr<_BinaryOp<bit_and<typename _Expr1::value_type>, _Expr1, _Expr2> >
3914 >::type
3915 operator&(const _Expr1& __x, const _Expr2& __y)
3916 {
3917     typedef typename _Expr1::value_type value_type;
3918     typedef _BinaryOp<bit_and<value_type>, _Expr1, _Expr2> _Op;
3919     return __val_expr<_Op>(_Op(bit_and<value_type>(), __x, __y));
3920 }
3921
3922 template<class _Expr>
3923 inline _LIBCPP_INLINE_VISIBILITY
3924 typename enable_if
3925 <
3926     __is_val_expr<_Expr>::value,
3927     __val_expr<_BinaryOp<bit_and<typename _Expr::value_type>,
3928                _Expr, __scalar_expr<typename _Expr::value_type> > >
3929 >::type
3930 operator&(const _Expr& __x, const typename _Expr::value_type& __y)
3931 {
3932     typedef typename _Expr::value_type value_type;
3933     typedef _BinaryOp<bit_and<value_type>, _Expr, __scalar_expr<value_type> > _Op;
3934     return __val_expr<_Op>(_Op(bit_and<value_type>(),
3935                            __x, __scalar_expr<value_type>(__y, __x.size())));
3936 }
3937
3938 template<class _Expr>
3939 inline _LIBCPP_INLINE_VISIBILITY
3940 typename enable_if
3941 <
3942     __is_val_expr<_Expr>::value,
3943     __val_expr<_BinaryOp<bit_and<typename _Expr::value_type>,
3944                __scalar_expr<typename _Expr::value_type>, _Expr> >
3945 >::type
3946 operator&(const typename _Expr::value_type& __x, const _Expr& __y)
3947 {
3948     typedef typename _Expr::value_type value_type;
3949     typedef _BinaryOp<bit_and<value_type>, __scalar_expr<value_type>, _Expr> _Op;
3950     return __val_expr<_Op>(_Op(bit_and<value_type>(),
3951                            __scalar_expr<value_type>(__x, __y.size()), __y));
3952 }
3953
3954 template<class _Expr1, class _Expr2>
3955 inline _LIBCPP_INLINE_VISIBILITY
3956 typename enable_if
3957 <
3958     __is_val_expr<_Expr1>::value && __is_val_expr<_Expr2>::value,
3959     __val_expr<_BinaryOp<bit_or<typename _Expr1::value_type>, _Expr1, _Expr2> >
3960 >::type
3961 operator|(const _Expr1& __x, const _Expr2& __y)
3962 {
3963     typedef typename _Expr1::value_type value_type;
3964     typedef _BinaryOp<bit_or<value_type>, _Expr1, _Expr2> _Op;
3965     return __val_expr<_Op>(_Op(bit_or<value_type>(), __x, __y));
3966 }
3967
3968 template<class _Expr>
3969 inline _LIBCPP_INLINE_VISIBILITY
3970 typename enable_if
3971 <
3972     __is_val_expr<_Expr>::value,
3973     __val_expr<_BinaryOp<bit_or<typename _Expr::value_type>,
3974                _Expr, __scalar_expr<typename _Expr::value_type> > >
3975 >::type
3976 operator|(const _Expr& __x, const typename _Expr::value_type& __y)
3977 {
3978     typedef typename _Expr::value_type value_type;
3979     typedef _BinaryOp<bit_or<value_type>, _Expr, __scalar_expr<value_type> > _Op;
3980     return __val_expr<_Op>(_Op(bit_or<value_type>(),
3981                            __x, __scalar_expr<value_type>(__y, __x.size())));
3982 }
3983
3984 template<class _Expr>
3985 inline _LIBCPP_INLINE_VISIBILITY
3986 typename enable_if
3987 <
3988     __is_val_expr<_Expr>::value,
3989     __val_expr<_BinaryOp<bit_or<typename _Expr::value_type>,
3990                __scalar_expr<typename _Expr::value_type>, _Expr> >
3991 >::type
3992 operator|(const typename _Expr::value_type& __x, const _Expr& __y)
3993 {
3994     typedef typename _Expr::value_type value_type;
3995     typedef _BinaryOp<bit_or<value_type>, __scalar_expr<value_type>, _Expr> _Op;
3996     return __val_expr<_Op>(_Op(bit_or<value_type>(),
3997                            __scalar_expr<value_type>(__x, __y.size()), __y));
3998 }
3999
4000 template<class _Expr1, class _Expr2>
4001 inline _LIBCPP_INLINE_VISIBILITY
4002 typename enable_if
4003 <
4004     __is_val_expr<_Expr1>::value && __is_val_expr<_Expr2>::value,
4005     __val_expr<_BinaryOp<__bit_shift_left<typename _Expr1::value_type>, _Expr1, _Expr2> >
4006 >::type
4007 operator<<(const _Expr1& __x, const _Expr2& __y)
4008 {
4009     typedef typename _Expr1::value_type value_type;
4010     typedef _BinaryOp<__bit_shift_left<value_type>, _Expr1, _Expr2> _Op;
4011     return __val_expr<_Op>(_Op(__bit_shift_left<value_type>(), __x, __y));
4012 }
4013
4014 template<class _Expr>
4015 inline _LIBCPP_INLINE_VISIBILITY
4016 typename enable_if
4017 <
4018     __is_val_expr<_Expr>::value,
4019     __val_expr<_BinaryOp<__bit_shift_left<typename _Expr::value_type>,
4020                _Expr, __scalar_expr<typename _Expr::value_type> > >
4021 >::type
4022 operator<<(const _Expr& __x, const typename _Expr::value_type& __y)
4023 {
4024     typedef typename _Expr::value_type value_type;
4025     typedef _BinaryOp<__bit_shift_left<value_type>, _Expr, __scalar_expr<value_type> > _Op;
4026     return __val_expr<_Op>(_Op(__bit_shift_left<value_type>(),
4027                            __x, __scalar_expr<value_type>(__y, __x.size())));
4028 }
4029
4030 template<class _Expr>
4031 inline _LIBCPP_INLINE_VISIBILITY
4032 typename enable_if
4033 <
4034     __is_val_expr<_Expr>::value,
4035     __val_expr<_BinaryOp<__bit_shift_left<typename _Expr::value_type>,
4036                __scalar_expr<typename _Expr::value_type>, _Expr> >
4037 >::type
4038 operator<<(const typename _Expr::value_type& __x, const _Expr& __y)
4039 {
4040     typedef typename _Expr::value_type value_type;
4041     typedef _BinaryOp<__bit_shift_left<value_type>, __scalar_expr<value_type>, _Expr> _Op;
4042     return __val_expr<_Op>(_Op(__bit_shift_left<value_type>(),
4043                            __scalar_expr<value_type>(__x, __y.size()), __y));
4044 }
4045
4046 template<class _Expr1, class _Expr2>
4047 inline _LIBCPP_INLINE_VISIBILITY
4048 typename enable_if
4049 <
4050     __is_val_expr<_Expr1>::value && __is_val_expr<_Expr2>::value,
4051     __val_expr<_BinaryOp<__bit_shift_right<typename _Expr1::value_type>, _Expr1, _Expr2> >
4052 >::type
4053 operator>>(const _Expr1& __x, const _Expr2& __y)
4054 {
4055     typedef typename _Expr1::value_type value_type;
4056     typedef _BinaryOp<__bit_shift_right<value_type>, _Expr1, _Expr2> _Op;
4057     return __val_expr<_Op>(_Op(__bit_shift_right<value_type>(), __x, __y));
4058 }
4059
4060 template<class _Expr>
4061 inline _LIBCPP_INLINE_VISIBILITY
4062 typename enable_if
4063 <
4064     __is_val_expr<_Expr>::value,
4065     __val_expr<_BinaryOp<__bit_shift_right<typename _Expr::value_type>,
4066                _Expr, __scalar_expr<typename _Expr::value_type> > >
4067 >::type
4068 operator>>(const _Expr& __x, const typename _Expr::value_type& __y)
4069 {
4070     typedef typename _Expr::value_type value_type;
4071     typedef _BinaryOp<__bit_shift_right<value_type>, _Expr, __scalar_expr<value_type> > _Op;
4072     return __val_expr<_Op>(_Op(__bit_shift_right<value_type>(),
4073                            __x, __scalar_expr<value_type>(__y, __x.size())));
4074 }
4075
4076 template<class _Expr>
4077 inline _LIBCPP_INLINE_VISIBILITY
4078 typename enable_if
4079 <
4080     __is_val_expr<_Expr>::value,
4081     __val_expr<_BinaryOp<__bit_shift_right<typename _Expr::value_type>,
4082                __scalar_expr<typename _Expr::value_type>, _Expr> >
4083 >::type
4084 operator>>(const typename _Expr::value_type& __x, const _Expr& __y)
4085 {
4086     typedef typename _Expr::value_type value_type;
4087     typedef _BinaryOp<__bit_shift_right<value_type>, __scalar_expr<value_type>, _Expr> _Op;
4088     return __val_expr<_Op>(_Op(__bit_shift_right<value_type>(),
4089                            __scalar_expr<value_type>(__x, __y.size()), __y));
4090 }
4091
4092 template<class _Expr1, class _Expr2>
4093 inline _LIBCPP_INLINE_VISIBILITY
4094 typename enable_if
4095 <
4096     __is_val_expr<_Expr1>::value && __is_val_expr<_Expr2>::value,
4097     __val_expr<_BinaryOp<logical_and<typename _Expr1::value_type>, _Expr1, _Expr2> >
4098 >::type
4099 operator&&(const _Expr1& __x, const _Expr2& __y)
4100 {
4101     typedef typename _Expr1::value_type value_type;
4102     typedef _BinaryOp<logical_and<value_type>, _Expr1, _Expr2> _Op;
4103     return __val_expr<_Op>(_Op(logical_and<value_type>(), __x, __y));
4104 }
4105
4106 template<class _Expr>
4107 inline _LIBCPP_INLINE_VISIBILITY
4108 typename enable_if
4109 <
4110     __is_val_expr<_Expr>::value,
4111     __val_expr<_BinaryOp<logical_and<typename _Expr::value_type>,
4112                _Expr, __scalar_expr<typename _Expr::value_type> > >
4113 >::type
4114 operator&&(const _Expr& __x, const typename _Expr::value_type& __y)
4115 {
4116     typedef typename _Expr::value_type value_type;
4117     typedef _BinaryOp<logical_and<value_type>, _Expr, __scalar_expr<value_type> > _Op;
4118     return __val_expr<_Op>(_Op(logical_and<value_type>(),
4119                            __x, __scalar_expr<value_type>(__y, __x.size())));
4120 }
4121
4122 template<class _Expr>
4123 inline _LIBCPP_INLINE_VISIBILITY
4124 typename enable_if
4125 <
4126     __is_val_expr<_Expr>::value,
4127     __val_expr<_BinaryOp<logical_and<typename _Expr::value_type>,
4128                __scalar_expr<typename _Expr::value_type>, _Expr> >
4129 >::type
4130 operator&&(const typename _Expr::value_type& __x, const _Expr& __y)
4131 {
4132     typedef typename _Expr::value_type value_type;
4133     typedef _BinaryOp<logical_and<value_type>, __scalar_expr<value_type>, _Expr> _Op;
4134     return __val_expr<_Op>(_Op(logical_and<value_type>(),
4135                            __scalar_expr<value_type>(__x, __y.size()), __y));
4136 }
4137
4138 template<class _Expr1, class _Expr2>
4139 inline _LIBCPP_INLINE_VISIBILITY
4140 typename enable_if
4141 <
4142     __is_val_expr<_Expr1>::value && __is_val_expr<_Expr2>::value,
4143     __val_expr<_BinaryOp<logical_or<typename _Expr1::value_type>, _Expr1, _Expr2> >
4144 >::type
4145 operator||(const _Expr1& __x, const _Expr2& __y)
4146 {
4147     typedef typename _Expr1::value_type value_type;
4148     typedef _BinaryOp<logical_or<value_type>, _Expr1, _Expr2> _Op;
4149     return __val_expr<_Op>(_Op(logical_or<value_type>(), __x, __y));
4150 }
4151
4152 template<class _Expr>
4153 inline _LIBCPP_INLINE_VISIBILITY
4154 typename enable_if
4155 <
4156     __is_val_expr<_Expr>::value,
4157     __val_expr<_BinaryOp<logical_or<typename _Expr::value_type>,
4158                _Expr, __scalar_expr<typename _Expr::value_type> > >
4159 >::type
4160 operator||(const _Expr& __x, const typename _Expr::value_type& __y)
4161 {
4162     typedef typename _Expr::value_type value_type;
4163     typedef _BinaryOp<logical_or<value_type>, _Expr, __scalar_expr<value_type> > _Op;
4164     return __val_expr<_Op>(_Op(logical_or<value_type>(),
4165                            __x, __scalar_expr<value_type>(__y, __x.size())));
4166 }
4167
4168 template<class _Expr>
4169 inline _LIBCPP_INLINE_VISIBILITY
4170 typename enable_if
4171 <
4172     __is_val_expr<_Expr>::value,
4173     __val_expr<_BinaryOp<logical_or<typename _Expr::value_type>,
4174                __scalar_expr<typename _Expr::value_type>, _Expr> >
4175 >::type
4176 operator||(const typename _Expr::value_type& __x, const _Expr& __y)
4177 {
4178     typedef typename _Expr::value_type value_type;
4179     typedef _BinaryOp<logical_or<value_type>, __scalar_expr<value_type>, _Expr> _Op;
4180     return __val_expr<_Op>(_Op(logical_or<value_type>(),
4181                            __scalar_expr<value_type>(__x, __y.size()), __y));
4182 }
4183
4184 template<class _Expr1, class _Expr2>
4185 inline _LIBCPP_INLINE_VISIBILITY
4186 typename enable_if
4187 <
4188     __is_val_expr<_Expr1>::value && __is_val_expr<_Expr2>::value,
4189     __val_expr<_BinaryOp<equal_to<typename _Expr1::value_type>, _Expr1, _Expr2> >
4190 >::type
4191 operator==(const _Expr1& __x, const _Expr2& __y)
4192 {
4193     typedef typename _Expr1::value_type value_type;
4194     typedef _BinaryOp<equal_to<value_type>, _Expr1, _Expr2> _Op;
4195     return __val_expr<_Op>(_Op(equal_to<value_type>(), __x, __y));
4196 }
4197
4198 template<class _Expr>
4199 inline _LIBCPP_INLINE_VISIBILITY
4200 typename enable_if
4201 <
4202     __is_val_expr<_Expr>::value,
4203     __val_expr<_BinaryOp<equal_to<typename _Expr::value_type>,
4204                _Expr, __scalar_expr<typename _Expr::value_type> > >
4205 >::type
4206 operator==(const _Expr& __x, const typename _Expr::value_type& __y)
4207 {
4208     typedef typename _Expr::value_type value_type;
4209     typedef _BinaryOp<equal_to<value_type>, _Expr, __scalar_expr<value_type> > _Op;
4210     return __val_expr<_Op>(_Op(equal_to<value_type>(),
4211                            __x, __scalar_expr<value_type>(__y, __x.size())));
4212 }
4213
4214 template<class _Expr>
4215 inline _LIBCPP_INLINE_VISIBILITY
4216 typename enable_if
4217 <
4218     __is_val_expr<_Expr>::value,
4219     __val_expr<_BinaryOp<equal_to<typename _Expr::value_type>,
4220                __scalar_expr<typename _Expr::value_type>, _Expr> >
4221 >::type
4222 operator==(const typename _Expr::value_type& __x, const _Expr& __y)
4223 {
4224     typedef typename _Expr::value_type value_type;
4225     typedef _BinaryOp<equal_to<value_type>, __scalar_expr<value_type>, _Expr> _Op;
4226     return __val_expr<_Op>(_Op(equal_to<value_type>(),
4227                            __scalar_expr<value_type>(__x, __y.size()), __y));
4228 }
4229
4230 template<class _Expr1, class _Expr2>
4231 inline _LIBCPP_INLINE_VISIBILITY
4232 typename enable_if
4233 <
4234     __is_val_expr<_Expr1>::value && __is_val_expr<_Expr2>::value,
4235     __val_expr<_BinaryOp<not_equal_to<typename _Expr1::value_type>, _Expr1, _Expr2> >
4236 >::type
4237 operator!=(const _Expr1& __x, const _Expr2& __y)
4238 {
4239     typedef typename _Expr1::value_type value_type;
4240     typedef _BinaryOp<not_equal_to<value_type>, _Expr1, _Expr2> _Op;
4241     return __val_expr<_Op>(_Op(not_equal_to<value_type>(), __x, __y));
4242 }
4243
4244 template<class _Expr>
4245 inline _LIBCPP_INLINE_VISIBILITY
4246 typename enable_if
4247 <
4248     __is_val_expr<_Expr>::value,
4249     __val_expr<_BinaryOp<not_equal_to<typename _Expr::value_type>,
4250                _Expr, __scalar_expr<typename _Expr::value_type> > >
4251 >::type
4252 operator!=(const _Expr& __x, const typename _Expr::value_type& __y)
4253 {
4254     typedef typename _Expr::value_type value_type;
4255     typedef _BinaryOp<not_equal_to<value_type>, _Expr, __scalar_expr<value_type> > _Op;
4256     return __val_expr<_Op>(_Op(not_equal_to<value_type>(),
4257                            __x, __scalar_expr<value_type>(__y, __x.size())));
4258 }
4259
4260 template<class _Expr>
4261 inline _LIBCPP_INLINE_VISIBILITY
4262 typename enable_if
4263 <
4264     __is_val_expr<_Expr>::value,
4265     __val_expr<_BinaryOp<not_equal_to<typename _Expr::value_type>,
4266                __scalar_expr<typename _Expr::value_type>, _Expr> >
4267 >::type
4268 operator!=(const typename _Expr::value_type& __x, const _Expr& __y)
4269 {
4270     typedef typename _Expr::value_type value_type;
4271     typedef _BinaryOp<not_equal_to<value_type>, __scalar_expr<value_type>, _Expr> _Op;
4272     return __val_expr<_Op>(_Op(not_equal_to<value_type>(),
4273                            __scalar_expr<value_type>(__x, __y.size()), __y));
4274 }
4275
4276 template<class _Expr1, class _Expr2>
4277 inline _LIBCPP_INLINE_VISIBILITY
4278 typename enable_if
4279 <
4280     __is_val_expr<_Expr1>::value && __is_val_expr<_Expr2>::value,
4281     __val_expr<_BinaryOp<less<typename _Expr1::value_type>, _Expr1, _Expr2> >
4282 >::type
4283 operator<(const _Expr1& __x, const _Expr2& __y)
4284 {
4285     typedef typename _Expr1::value_type value_type;
4286     typedef _BinaryOp<less<value_type>, _Expr1, _Expr2> _Op;
4287     return __val_expr<_Op>(_Op(less<value_type>(), __x, __y));
4288 }
4289
4290 template<class _Expr>
4291 inline _LIBCPP_INLINE_VISIBILITY
4292 typename enable_if
4293 <
4294     __is_val_expr<_Expr>::value,
4295     __val_expr<_BinaryOp<less<typename _Expr::value_type>,
4296                _Expr, __scalar_expr<typename _Expr::value_type> > >
4297 >::type
4298 operator<(const _Expr& __x, const typename _Expr::value_type& __y)
4299 {
4300     typedef typename _Expr::value_type value_type;
4301     typedef _BinaryOp<less<value_type>, _Expr, __scalar_expr<value_type> > _Op;
4302     return __val_expr<_Op>(_Op(less<value_type>(),
4303                            __x, __scalar_expr<value_type>(__y, __x.size())));
4304 }
4305
4306 template<class _Expr>
4307 inline _LIBCPP_INLINE_VISIBILITY
4308 typename enable_if
4309 <
4310     __is_val_expr<_Expr>::value,
4311     __val_expr<_BinaryOp<less<typename _Expr::value_type>,
4312                __scalar_expr<typename _Expr::value_type>, _Expr> >
4313 >::type
4314 operator<(const typename _Expr::value_type& __x, const _Expr& __y)
4315 {
4316     typedef typename _Expr::value_type value_type;
4317     typedef _BinaryOp<less<value_type>, __scalar_expr<value_type>, _Expr> _Op;
4318     return __val_expr<_Op>(_Op(less<value_type>(),
4319                            __scalar_expr<value_type>(__x, __y.size()), __y));
4320 }
4321
4322 template<class _Expr1, class _Expr2>
4323 inline _LIBCPP_INLINE_VISIBILITY
4324 typename enable_if
4325 <
4326     __is_val_expr<_Expr1>::value && __is_val_expr<_Expr2>::value,
4327     __val_expr<_BinaryOp<greater<typename _Expr1::value_type>, _Expr1, _Expr2> >
4328 >::type
4329 operator>(const _Expr1& __x, const _Expr2& __y)
4330 {
4331     typedef typename _Expr1::value_type value_type;
4332     typedef _BinaryOp<greater<value_type>, _Expr1, _Expr2> _Op;
4333     return __val_expr<_Op>(_Op(greater<value_type>(), __x, __y));
4334 }
4335
4336 template<class _Expr>
4337 inline _LIBCPP_INLINE_VISIBILITY
4338 typename enable_if
4339 <
4340     __is_val_expr<_Expr>::value,
4341     __val_expr<_BinaryOp<greater<typename _Expr::value_type>,
4342                _Expr, __scalar_expr<typename _Expr::value_type> > >
4343 >::type
4344 operator>(const _Expr& __x, const typename _Expr::value_type& __y)
4345 {
4346     typedef typename _Expr::value_type value_type;
4347     typedef _BinaryOp<greater<value_type>, _Expr, __scalar_expr<value_type> > _Op;
4348     return __val_expr<_Op>(_Op(greater<value_type>(),
4349                            __x, __scalar_expr<value_type>(__y, __x.size())));
4350 }
4351
4352 template<class _Expr>
4353 inline _LIBCPP_INLINE_VISIBILITY
4354 typename enable_if
4355 <
4356     __is_val_expr<_Expr>::value,
4357     __val_expr<_BinaryOp<greater<typename _Expr::value_type>,
4358                __scalar_expr<typename _Expr::value_type>, _Expr> >
4359 >::type
4360 operator>(const typename _Expr::value_type& __x, const _Expr& __y)
4361 {
4362     typedef typename _Expr::value_type value_type;
4363     typedef _BinaryOp<greater<value_type>, __scalar_expr<value_type>, _Expr> _Op;
4364     return __val_expr<_Op>(_Op(greater<value_type>(),
4365                            __scalar_expr<value_type>(__x, __y.size()), __y));
4366 }
4367
4368 template<class _Expr1, class _Expr2>
4369 inline _LIBCPP_INLINE_VISIBILITY
4370 typename enable_if
4371 <
4372     __is_val_expr<_Expr1>::value && __is_val_expr<_Expr2>::value,
4373     __val_expr<_BinaryOp<less_equal<typename _Expr1::value_type>, _Expr1, _Expr2> >
4374 >::type
4375 operator<=(const _Expr1& __x, const _Expr2& __y)
4376 {
4377     typedef typename _Expr1::value_type value_type;
4378     typedef _BinaryOp<less_equal<value_type>, _Expr1, _Expr2> _Op;
4379     return __val_expr<_Op>(_Op(less_equal<value_type>(), __x, __y));
4380 }
4381
4382 template<class _Expr>
4383 inline _LIBCPP_INLINE_VISIBILITY
4384 typename enable_if
4385 <
4386     __is_val_expr<_Expr>::value,
4387     __val_expr<_BinaryOp<less_equal<typename _Expr::value_type>,
4388                _Expr, __scalar_expr<typename _Expr::value_type> > >
4389 >::type
4390 operator<=(const _Expr& __x, const typename _Expr::value_type& __y)
4391 {
4392     typedef typename _Expr::value_type value_type;
4393     typedef _BinaryOp<less_equal<value_type>, _Expr, __scalar_expr<value_type> > _Op;
4394     return __val_expr<_Op>(_Op(less_equal<value_type>(),
4395                            __x, __scalar_expr<value_type>(__y, __x.size())));
4396 }
4397
4398 template<class _Expr>
4399 inline _LIBCPP_INLINE_VISIBILITY
4400 typename enable_if
4401 <
4402     __is_val_expr<_Expr>::value,
4403     __val_expr<_BinaryOp<less_equal<typename _Expr::value_type>,
4404                __scalar_expr<typename _Expr::value_type>, _Expr> >
4405 >::type
4406 operator<=(const typename _Expr::value_type& __x, const _Expr& __y)
4407 {
4408     typedef typename _Expr::value_type value_type;
4409     typedef _BinaryOp<less_equal<value_type>, __scalar_expr<value_type>, _Expr> _Op;
4410     return __val_expr<_Op>(_Op(less_equal<value_type>(),
4411                            __scalar_expr<value_type>(__x, __y.size()), __y));
4412 }
4413
4414 template<class _Expr1, class _Expr2>
4415 inline _LIBCPP_INLINE_VISIBILITY
4416 typename enable_if
4417 <
4418     __is_val_expr<_Expr1>::value && __is_val_expr<_Expr2>::value,
4419     __val_expr<_BinaryOp<greater_equal<typename _Expr1::value_type>, _Expr1, _Expr2> >
4420 >::type
4421 operator>=(const _Expr1& __x, const _Expr2& __y)
4422 {
4423     typedef typename _Expr1::value_type value_type;
4424     typedef _BinaryOp<greater_equal<value_type>, _Expr1, _Expr2> _Op;
4425     return __val_expr<_Op>(_Op(greater_equal<value_type>(), __x, __y));
4426 }
4427
4428 template<class _Expr>
4429 inline _LIBCPP_INLINE_VISIBILITY
4430 typename enable_if
4431 <
4432     __is_val_expr<_Expr>::value,
4433     __val_expr<_BinaryOp<greater_equal<typename _Expr::value_type>,
4434                _Expr, __scalar_expr<typename _Expr::value_type> > >
4435 >::type
4436 operator>=(const _Expr& __x, const typename _Expr::value_type& __y)
4437 {
4438     typedef typename _Expr::value_type value_type;
4439     typedef _BinaryOp<greater_equal<value_type>, _Expr, __scalar_expr<value_type> > _Op;
4440     return __val_expr<_Op>(_Op(greater_equal<value_type>(),
4441                            __x, __scalar_expr<value_type>(__y, __x.size())));
4442 }
4443
4444 template<class _Expr>
4445 inline _LIBCPP_INLINE_VISIBILITY
4446 typename enable_if
4447 <
4448     __is_val_expr<_Expr>::value,
4449     __val_expr<_BinaryOp<greater_equal<typename _Expr::value_type>,
4450                __scalar_expr<typename _Expr::value_type>, _Expr> >
4451 >::type
4452 operator>=(const typename _Expr::value_type& __x, const _Expr& __y)
4453 {
4454     typedef typename _Expr::value_type value_type;
4455     typedef _BinaryOp<greater_equal<value_type>, __scalar_expr<value_type>, _Expr> _Op;
4456     return __val_expr<_Op>(_Op(greater_equal<value_type>(),
4457                            __scalar_expr<value_type>(__x, __y.size()), __y));
4458 }
4459
4460 template<class _Expr>
4461 inline _LIBCPP_INLINE_VISIBILITY
4462 typename enable_if
4463 <
4464     __is_val_expr<_Expr>::value,
4465     __val_expr<_UnaryOp<__abs_expr<typename _Expr::value_type>, _Expr> >
4466 >::type
4467 abs(const _Expr& __x)
4468 {
4469     typedef typename _Expr::value_type value_type;
4470     typedef _UnaryOp<__abs_expr<value_type>, _Expr> _Op;
4471     return __val_expr<_Op>(_Op(__abs_expr<value_type>(), __x));
4472 }
4473
4474 template<class _Expr>
4475 inline _LIBCPP_INLINE_VISIBILITY
4476 typename enable_if
4477 <
4478     __is_val_expr<_Expr>::value,
4479     __val_expr<_UnaryOp<__acos_expr<typename _Expr::value_type>, _Expr> >
4480 >::type
4481 acos(const _Expr& __x)
4482 {
4483     typedef typename _Expr::value_type value_type;
4484     typedef _UnaryOp<__acos_expr<value_type>, _Expr> _Op;
4485     return __val_expr<_Op>(_Op(__acos_expr<value_type>(), __x));
4486 }
4487
4488 template<class _Expr>
4489 inline _LIBCPP_INLINE_VISIBILITY
4490 typename enable_if
4491 <
4492     __is_val_expr<_Expr>::value,
4493     __val_expr<_UnaryOp<__asin_expr<typename _Expr::value_type>, _Expr> >
4494 >::type
4495 asin(const _Expr& __x)
4496 {
4497     typedef typename _Expr::value_type value_type;
4498     typedef _UnaryOp<__asin_expr<value_type>, _Expr> _Op;
4499     return __val_expr<_Op>(_Op(__asin_expr<value_type>(), __x));
4500 }
4501
4502 template<class _Expr>
4503 inline _LIBCPP_INLINE_VISIBILITY
4504 typename enable_if
4505 <
4506     __is_val_expr<_Expr>::value,
4507     __val_expr<_UnaryOp<__atan_expr<typename _Expr::value_type>, _Expr> >
4508 >::type
4509 atan(const _Expr& __x)
4510 {
4511     typedef typename _Expr::value_type value_type;
4512     typedef _UnaryOp<__atan_expr<value_type>, _Expr> _Op;
4513     return __val_expr<_Op>(_Op(__atan_expr<value_type>(), __x));
4514 }
4515
4516 template<class _Expr1, class _Expr2>
4517 inline _LIBCPP_INLINE_VISIBILITY
4518 typename enable_if
4519 <
4520     __is_val_expr<_Expr1>::value && __is_val_expr<_Expr2>::value,
4521     __val_expr<_BinaryOp<__atan2_expr<typename _Expr1::value_type>, _Expr1, _Expr2> >
4522 >::type
4523 atan2(const _Expr1& __x, const _Expr2& __y)
4524 {
4525     typedef typename _Expr1::value_type value_type;
4526     typedef _BinaryOp<__atan2_expr<value_type>, _Expr1, _Expr2> _Op;
4527     return __val_expr<_Op>(_Op(__atan2_expr<value_type>(), __x, __y));
4528 }
4529
4530 template<class _Expr>
4531 inline _LIBCPP_INLINE_VISIBILITY
4532 typename enable_if
4533 <
4534     __is_val_expr<_Expr>::value,
4535     __val_expr<_BinaryOp<__atan2_expr<typename _Expr::value_type>,
4536                _Expr, __scalar_expr<typename _Expr::value_type> > >
4537 >::type
4538 atan2(const _Expr& __x, const typename _Expr::value_type& __y)
4539 {
4540     typedef typename _Expr::value_type value_type;
4541     typedef _BinaryOp<__atan2_expr<value_type>, _Expr, __scalar_expr<value_type> > _Op;
4542     return __val_expr<_Op>(_Op(__atan2_expr<value_type>(),
4543                            __x, __scalar_expr<value_type>(__y, __x.size())));
4544 }
4545
4546 template<class _Expr>
4547 inline _LIBCPP_INLINE_VISIBILITY
4548 typename enable_if
4549 <
4550     __is_val_expr<_Expr>::value,
4551     __val_expr<_BinaryOp<__atan2_expr<typename _Expr::value_type>,
4552                __scalar_expr<typename _Expr::value_type>, _Expr> >
4553 >::type
4554 atan2(const typename _Expr::value_type& __x, const _Expr& __y)
4555 {
4556     typedef typename _Expr::value_type value_type;
4557     typedef _BinaryOp<__atan2_expr<value_type>, __scalar_expr<value_type>, _Expr> _Op;
4558     return __val_expr<_Op>(_Op(__atan2_expr<value_type>(),
4559                            __scalar_expr<value_type>(__x, __y.size()), __y));
4560 }
4561
4562 template<class _Expr>
4563 inline _LIBCPP_INLINE_VISIBILITY
4564 typename enable_if
4565 <
4566     __is_val_expr<_Expr>::value,
4567     __val_expr<_UnaryOp<__cos_expr<typename _Expr::value_type>, _Expr> >
4568 >::type
4569 cos(const _Expr& __x)
4570 {
4571     typedef typename _Expr::value_type value_type;
4572     typedef _UnaryOp<__cos_expr<value_type>, _Expr> _Op;
4573     return __val_expr<_Op>(_Op(__cos_expr<value_type>(), __x));
4574 }
4575
4576 template<class _Expr>
4577 inline _LIBCPP_INLINE_VISIBILITY
4578 typename enable_if
4579 <
4580     __is_val_expr<_Expr>::value,
4581     __val_expr<_UnaryOp<__cosh_expr<typename _Expr::value_type>, _Expr> >
4582 >::type
4583 cosh(const _Expr& __x)
4584 {
4585     typedef typename _Expr::value_type value_type;
4586     typedef _UnaryOp<__cosh_expr<value_type>, _Expr> _Op;
4587     return __val_expr<_Op>(_Op(__cosh_expr<value_type>(), __x));
4588 }
4589
4590 template<class _Expr>
4591 inline _LIBCPP_INLINE_VISIBILITY
4592 typename enable_if
4593 <
4594     __is_val_expr<_Expr>::value,
4595     __val_expr<_UnaryOp<__exp_expr<typename _Expr::value_type>, _Expr> >
4596 >::type
4597 exp(const _Expr& __x)
4598 {
4599     typedef typename _Expr::value_type value_type;
4600     typedef _UnaryOp<__exp_expr<value_type>, _Expr> _Op;
4601     return __val_expr<_Op>(_Op(__exp_expr<value_type>(), __x));
4602 }
4603
4604 template<class _Expr>
4605 inline _LIBCPP_INLINE_VISIBILITY
4606 typename enable_if
4607 <
4608     __is_val_expr<_Expr>::value,
4609     __val_expr<_UnaryOp<__log_expr<typename _Expr::value_type>, _Expr> >
4610 >::type
4611 log(const _Expr& __x)
4612 {
4613     typedef typename _Expr::value_type value_type;
4614     typedef _UnaryOp<__log_expr<value_type>, _Expr> _Op;
4615     return __val_expr<_Op>(_Op(__log_expr<value_type>(), __x));
4616 }
4617
4618 template<class _Expr>
4619 inline _LIBCPP_INLINE_VISIBILITY
4620 typename enable_if
4621 <
4622     __is_val_expr<_Expr>::value,
4623     __val_expr<_UnaryOp<__log10_expr<typename _Expr::value_type>, _Expr> >
4624 >::type
4625 log10(const _Expr& __x)
4626 {
4627     typedef typename _Expr::value_type value_type;
4628     typedef _UnaryOp<__log10_expr<value_type>, _Expr> _Op;
4629     return __val_expr<_Op>(_Op(__log10_expr<value_type>(), __x));
4630 }
4631
4632 template<class _Expr1, class _Expr2>
4633 inline _LIBCPP_INLINE_VISIBILITY
4634 typename enable_if
4635 <
4636     __is_val_expr<_Expr1>::value && __is_val_expr<_Expr2>::value,
4637     __val_expr<_BinaryOp<__pow_expr<typename _Expr1::value_type>, _Expr1, _Expr2> >
4638 >::type
4639 pow(const _Expr1& __x, const _Expr2& __y)
4640 {
4641     typedef typename _Expr1::value_type value_type;
4642     typedef _BinaryOp<__pow_expr<value_type>, _Expr1, _Expr2> _Op;
4643     return __val_expr<_Op>(_Op(__pow_expr<value_type>(), __x, __y));
4644 }
4645
4646 template<class _Expr>
4647 inline _LIBCPP_INLINE_VISIBILITY
4648 typename enable_if
4649 <
4650     __is_val_expr<_Expr>::value,
4651     __val_expr<_BinaryOp<__pow_expr<typename _Expr::value_type>,
4652                _Expr, __scalar_expr<typename _Expr::value_type> > >
4653 >::type
4654 pow(const _Expr& __x, const typename _Expr::value_type& __y)
4655 {
4656     typedef typename _Expr::value_type value_type;
4657     typedef _BinaryOp<__pow_expr<value_type>, _Expr, __scalar_expr<value_type> > _Op;
4658     return __val_expr<_Op>(_Op(__pow_expr<value_type>(),
4659                            __x, __scalar_expr<value_type>(__y, __x.size())));
4660 }
4661
4662 template<class _Expr>
4663 inline _LIBCPP_INLINE_VISIBILITY
4664 typename enable_if
4665 <
4666     __is_val_expr<_Expr>::value,
4667     __val_expr<_BinaryOp<__pow_expr<typename _Expr::value_type>,
4668                __scalar_expr<typename _Expr::value_type>, _Expr> >
4669 >::type
4670 pow(const typename _Expr::value_type& __x, const _Expr& __y)
4671 {
4672     typedef typename _Expr::value_type value_type;
4673     typedef _BinaryOp<__pow_expr<value_type>, __scalar_expr<value_type>, _Expr> _Op;
4674     return __val_expr<_Op>(_Op(__pow_expr<value_type>(),
4675                            __scalar_expr<value_type>(__x, __y.size()), __y));
4676 }
4677
4678 template<class _Expr>
4679 inline _LIBCPP_INLINE_VISIBILITY
4680 typename enable_if
4681 <
4682     __is_val_expr<_Expr>::value,
4683     __val_expr<_UnaryOp<__sin_expr<typename _Expr::value_type>, _Expr> >
4684 >::type
4685 sin(const _Expr& __x)
4686 {
4687     typedef typename _Expr::value_type value_type;
4688     typedef _UnaryOp<__sin_expr<value_type>, _Expr> _Op;
4689     return __val_expr<_Op>(_Op(__sin_expr<value_type>(), __x));
4690 }
4691
4692 template<class _Expr>
4693 inline _LIBCPP_INLINE_VISIBILITY
4694 typename enable_if
4695 <
4696     __is_val_expr<_Expr>::value,
4697     __val_expr<_UnaryOp<__sinh_expr<typename _Expr::value_type>, _Expr> >
4698 >::type
4699 sinh(const _Expr& __x)
4700 {
4701     typedef typename _Expr::value_type value_type;
4702     typedef _UnaryOp<__sinh_expr<value_type>, _Expr> _Op;
4703     return __val_expr<_Op>(_Op(__sinh_expr<value_type>(), __x));
4704 }
4705
4706 template<class _Expr>
4707 inline _LIBCPP_INLINE_VISIBILITY
4708 typename enable_if
4709 <
4710     __is_val_expr<_Expr>::value,
4711     __val_expr<_UnaryOp<__sqrt_expr<typename _Expr::value_type>, _Expr> >
4712 >::type
4713 sqrt(const _Expr& __x)
4714 {
4715     typedef typename _Expr::value_type value_type;
4716     typedef _UnaryOp<__sqrt_expr<value_type>, _Expr> _Op;
4717     return __val_expr<_Op>(_Op(__sqrt_expr<value_type>(), __x));
4718 }
4719
4720 template<class _Expr>
4721 inline _LIBCPP_INLINE_VISIBILITY
4722 typename enable_if
4723 <
4724     __is_val_expr<_Expr>::value,
4725     __val_expr<_UnaryOp<__tan_expr<typename _Expr::value_type>, _Expr> >
4726 >::type
4727 tan(const _Expr& __x)
4728 {
4729     typedef typename _Expr::value_type value_type;
4730     typedef _UnaryOp<__tan_expr<value_type>, _Expr> _Op;
4731     return __val_expr<_Op>(_Op(__tan_expr<value_type>(), __x));
4732 }
4733
4734 template<class _Expr>
4735 inline _LIBCPP_INLINE_VISIBILITY
4736 typename enable_if
4737 <
4738     __is_val_expr<_Expr>::value,
4739     __val_expr<_UnaryOp<__tanh_expr<typename _Expr::value_type>, _Expr> >
4740 >::type
4741 tanh(const _Expr& __x)
4742 {
4743     typedef typename _Expr::value_type value_type;
4744     typedef _UnaryOp<__tanh_expr<value_type>, _Expr> _Op;
4745     return __val_expr<_Op>(_Op(__tanh_expr<value_type>(), __x));
4746 }
4747
4748 template <class _Tp>
4749 inline _LIBCPP_INLINE_VISIBILITY
4750 _Tp*
4751 begin(valarray<_Tp>& __v)
4752 {
4753     return __v.__begin_;
4754 }
4755
4756 template <class _Tp>
4757 inline _LIBCPP_INLINE_VISIBILITY
4758 const _Tp*
4759 begin(const valarray<_Tp>& __v)
4760 {
4761     return __v.__begin_;
4762 }
4763
4764 template <class _Tp>
4765 inline _LIBCPP_INLINE_VISIBILITY
4766 _Tp*
4767 end(valarray<_Tp>& __v)
4768 {
4769     return __v.__end_;
4770 }
4771
4772 template <class _Tp>
4773 inline _LIBCPP_INLINE_VISIBILITY
4774 const _Tp*
4775 end(const valarray<_Tp>& __v)
4776 {
4777     return __v.__end_;
4778 }
4779
4780 _LIBCPP_END_NAMESPACE_STD
4781
4782 #endif  // _LIBCPP_VALARRAY