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