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