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