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