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