diff options
Diffstat (limited to 'lib/libcxx/include/valarray')
-rw-r--r-- | lib/libcxx/include/valarray | 133 |
1 files changed, 94 insertions, 39 deletions
diff --git a/lib/libcxx/include/valarray b/lib/libcxx/include/valarray index ee61238a932..8d3892ad35d 100644 --- a/lib/libcxx/include/valarray +++ b/lib/libcxx/include/valarray @@ -1053,6 +1053,9 @@ private: friend const _Up* end(const valarray<_Up>& __v); + + void __clear(); + valarray& __assign_range(const value_type* __f, const value_type* __l); }; _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS valarray<size_t>::valarray(size_t)) @@ -2735,7 +2738,8 @@ __val_expr<_ValExpr>::operator valarray<__val_expr::result_type>() const { __r.__begin_ = __r.__end_ = - static_cast<result_type*>(_VSTD::__allocate(__n * sizeof(result_type))); + static_cast<result_type*>( + _VSTD::__libcpp_allocate(__n * sizeof(result_type), __alignof(result_type))); for (size_t __i = 0; __i != __n; ++__r.__end_, ++__i) ::new (__r.__end_) result_type(__expr_[__i]); } @@ -2750,7 +2754,25 @@ valarray<_Tp>::valarray(size_t __n) : __begin_(0), __end_(0) { - resize(__n); + if (__n) + { + __begin_ = __end_ = static_cast<value_type*>( + _VSTD::__libcpp_allocate(__n * sizeof(value_type), __alignof(value_type))); +#ifndef _LIBCPP_NO_EXCEPTIONS + try + { +#endif // _LIBCPP_NO_EXCEPTIONS + for (; __n; --__n, ++__end_) + ::new (__end_) value_type(); +#ifndef _LIBCPP_NO_EXCEPTIONS + } + catch (...) + { + __clear(); + throw; + } +#endif // _LIBCPP_NO_EXCEPTIONS + } } template <class _Tp> @@ -2769,7 +2791,8 @@ valarray<_Tp>::valarray(const value_type* __p, size_t __n) { if (__n) { - __begin_ = __end_ = static_cast<value_type*>(_VSTD::__allocate(__n * sizeof(value_type))); + __begin_ = __end_ = static_cast<value_type*>( + _VSTD::__libcpp_allocate(__n * sizeof(value_type), __alignof(value_type))); #ifndef _LIBCPP_NO_EXCEPTIONS try { @@ -2780,7 +2803,7 @@ valarray<_Tp>::valarray(const value_type* __p, size_t __n) } catch (...) { - resize(0); + __clear(); throw; } #endif // _LIBCPP_NO_EXCEPTIONS @@ -2794,7 +2817,8 @@ valarray<_Tp>::valarray(const valarray& __v) { if (__v.size()) { - __begin_ = __end_ = static_cast<value_type*>(_VSTD::__allocate(__v.size() * sizeof(value_type))); + __begin_ = __end_ = static_cast<value_type*>( + _VSTD::__libcpp_allocate(__v.size() * sizeof(value_type), __alignof(value_type))); #ifndef _LIBCPP_NO_EXCEPTIONS try { @@ -2805,7 +2829,7 @@ valarray<_Tp>::valarray(const valarray& __v) } catch (...) { - resize(0); + __clear(); throw; } #endif // _LIBCPP_NO_EXCEPTIONS @@ -2831,7 +2855,8 @@ valarray<_Tp>::valarray(initializer_list<value_type> __il) size_t __n = __il.size(); if (__n) { - __begin_ = __end_ = static_cast<value_type*>(_VSTD::__allocate(__n * sizeof(value_type))); + __begin_ = __end_ = static_cast<value_type*>( +_VSTD::__libcpp_allocate(__n * sizeof(value_type), __alignof(value_type))); #ifndef _LIBCPP_NO_EXCEPTIONS try { @@ -2842,7 +2867,7 @@ valarray<_Tp>::valarray(initializer_list<value_type> __il) } catch (...) { - resize(0); + __clear(); throw; } #endif // _LIBCPP_NO_EXCEPTIONS @@ -2859,7 +2884,8 @@ valarray<_Tp>::valarray(const slice_array<value_type>& __sa) size_t __n = __sa.__size_; if (__n) { - __begin_ = __end_ = static_cast<value_type*>(_VSTD::__allocate(__n * sizeof(value_type))); + __begin_ = __end_ = static_cast<value_type*>( + _VSTD::__libcpp_allocate(__n * sizeof(value_type), __alignof(value_type))); #ifndef _LIBCPP_NO_EXCEPTIONS try { @@ -2870,7 +2896,7 @@ valarray<_Tp>::valarray(const slice_array<value_type>& __sa) } catch (...) { - resize(0); + __clear(); throw; } #endif // _LIBCPP_NO_EXCEPTIONS @@ -2885,7 +2911,8 @@ valarray<_Tp>::valarray(const gslice_array<value_type>& __ga) size_t __n = __ga.__1d_.size(); if (__n) { - __begin_ = __end_ = static_cast<value_type*>(_VSTD::__allocate(__n * sizeof(value_type))); + __begin_ = __end_ = static_cast<value_type*>( + _VSTD::__libcpp_allocate(__n * sizeof(value_type), __alignof(value_type))); #ifndef _LIBCPP_NO_EXCEPTIONS try { @@ -2899,7 +2926,7 @@ valarray<_Tp>::valarray(const gslice_array<value_type>& __ga) } catch (...) { - resize(0); + __clear(); throw; } #endif // _LIBCPP_NO_EXCEPTIONS @@ -2914,7 +2941,8 @@ valarray<_Tp>::valarray(const mask_array<value_type>& __ma) size_t __n = __ma.__1d_.size(); if (__n) { - __begin_ = __end_ = static_cast<value_type*>(_VSTD::__allocate(__n * sizeof(value_type))); + __begin_ = __end_ = static_cast<value_type*>( + _VSTD::__libcpp_allocate(__n * sizeof(value_type), __alignof(value_type))); #ifndef _LIBCPP_NO_EXCEPTIONS try { @@ -2928,7 +2956,7 @@ valarray<_Tp>::valarray(const mask_array<value_type>& __ma) } catch (...) { - resize(0); + __clear(); throw; } #endif // _LIBCPP_NO_EXCEPTIONS @@ -2943,7 +2971,8 @@ valarray<_Tp>::valarray(const indirect_array<value_type>& __ia) size_t __n = __ia.__1d_.size(); if (__n) { - __begin_ = __end_ = static_cast<value_type*>(_VSTD::__allocate(__n * sizeof(value_type))); + __begin_ = __end_ = static_cast<value_type*>( + _VSTD::__libcpp_allocate(__n * sizeof(value_type), __alignof(value_type))); #ifndef _LIBCPP_NO_EXCEPTIONS try { @@ -2957,7 +2986,7 @@ valarray<_Tp>::valarray(const indirect_array<value_type>& __ia) } catch (...) { - resize(0); + __clear(); throw; } #endif // _LIBCPP_NO_EXCEPTIONS @@ -2968,22 +2997,36 @@ template <class _Tp> inline valarray<_Tp>::~valarray() { - resize(0); + __clear(); } template <class _Tp> valarray<_Tp>& -valarray<_Tp>::operator=(const valarray& __v) +valarray<_Tp>::__assign_range(const value_type* __f, const value_type* __l) { - if (this != &__v) + size_t __n = __l - __f; + if (size() != __n) { - if (size() != __v.size()) - resize(__v.size()); - _VSTD::copy(__v.__begin_, __v.__end_, __begin_); + __clear(); + __begin_ = static_cast<value_type*>( + _VSTD::__libcpp_allocate(__n * sizeof(value_type), __alignof(value_type))); + __end_ = __begin_ + __n; + _VSTD::uninitialized_copy(__f, __l, __begin_); + } else { + _VSTD::copy(__f, __l, __begin_); } return *this; } +template <class _Tp> +valarray<_Tp>& +valarray<_Tp>::operator=(const valarray& __v) +{ + if (this != &__v) + return __assign_range(__v.__begin_, __v.__end_); + return *this; +} + #ifndef _LIBCPP_CXX03_LANG template <class _Tp> @@ -2991,7 +3034,7 @@ inline valarray<_Tp>& valarray<_Tp>::operator=(valarray&& __v) _NOEXCEPT { - resize(0); + __clear(); __begin_ = __v.__begin_; __end_ = __v.__end_; __v.__begin_ = nullptr; @@ -3004,10 +3047,7 @@ inline valarray<_Tp>& valarray<_Tp>::operator=(initializer_list<value_type> __il) { - if (size() != __il.size()) - resize(__il.size()); - _VSTD::copy(__il.begin(), __il.end(), __begin_); - return *this; + return __assign_range(__il.begin(), __il.end()); } #endif // _LIBCPP_CXX03_LANG @@ -3224,7 +3264,8 @@ valarray<_Tp>::operator+() const { __r.__begin_ = __r.__end_ = - static_cast<value_type*>(_VSTD::__allocate(__n * sizeof(value_type))); + static_cast<value_type*>( + _VSTD::__libcpp_allocate(__n * sizeof(value_type), __alignof(value_type))); for (const value_type* __p = __begin_; __n; ++__r.__end_, ++__p, --__n) ::new (__r.__end_) value_type(+*__p); } @@ -3241,7 +3282,8 @@ valarray<_Tp>::operator-() const { __r.__begin_ = __r.__end_ = - static_cast<value_type*>(_VSTD::__allocate(__n * sizeof(value_type))); + static_cast<value_type*>( + _VSTD::__libcpp_allocate(__n * sizeof(value_type), __alignof(value_type))); for (const value_type* __p = __begin_; __n; ++__r.__end_, ++__p, --__n) ::new (__r.__end_) value_type(-*__p); } @@ -3258,7 +3300,8 @@ valarray<_Tp>::operator~() const { __r.__begin_ = __r.__end_ = - static_cast<value_type*>(_VSTD::__allocate(__n * sizeof(value_type))); + static_cast<value_type*>( + _VSTD::__libcpp_allocate(__n * sizeof(value_type), __alignof(value_type))); for (const value_type* __p = __begin_; __n; ++__r.__end_, ++__p, --__n) ::new (__r.__end_) value_type(~*__p); } @@ -3275,7 +3318,7 @@ valarray<_Tp>::operator!() const { __r.__begin_ = __r.__end_ = - static_cast<bool*>(_VSTD::__allocate(__n * sizeof(bool))); + static_cast<bool*>(_VSTD::__libcpp_allocate(__n * sizeof(bool), __alignof(bool))); for (const value_type* __p = __begin_; __n; ++__r.__end_, ++__p, --__n) ::new (__r.__end_) bool(!*__p); } @@ -3595,7 +3638,8 @@ valarray<_Tp>::shift(int __i) const { __r.__begin_ = __r.__end_ = - static_cast<value_type*>(_VSTD::__allocate(__n * sizeof(value_type))); + static_cast<value_type*>( + _VSTD::__libcpp_allocate(__n * sizeof(value_type), __alignof(value_type))); const value_type* __sb; value_type* __tb; value_type* __te; @@ -3633,7 +3677,8 @@ valarray<_Tp>::cshift(int __i) const { __r.__begin_ = __r.__end_ = - static_cast<value_type*>(_VSTD::__allocate(__n * sizeof(value_type))); + static_cast<value_type*>( + _VSTD::__libcpp_allocate(__n * sizeof(value_type), __alignof(value_type))); __i %= static_cast<int>(__n); const value_type* __m = __i >= 0 ? __begin_ + __i : __end_ + __i; for (const value_type* __s = __m; __s != __end_; ++__r.__end_, ++__s) @@ -3654,7 +3699,8 @@ valarray<_Tp>::apply(value_type __f(value_type)) const { __r.__begin_ = __r.__end_ = - static_cast<value_type*>(_VSTD::__allocate(__n * sizeof(value_type))); + static_cast<value_type*>( + _VSTD::__libcpp_allocate(__n * sizeof(value_type), __alignof(value_type))); for (const value_type* __p = __begin_; __n; ++__r.__end_, ++__p, --__n) ::new (__r.__end_) value_type(__f(*__p)); } @@ -3671,7 +3717,8 @@ valarray<_Tp>::apply(value_type __f(const value_type&)) const { __r.__begin_ = __r.__end_ = - static_cast<value_type*>(_VSTD::__allocate(__n * sizeof(value_type))); + static_cast<value_type*>( + _VSTD::__libcpp_allocate(__n * sizeof(value_type), __alignof(value_type))); for (const value_type* __p = __begin_; __n; ++__r.__end_, ++__p, --__n) ::new (__r.__end_) value_type(__f(*__p)); } @@ -3680,18 +3727,26 @@ valarray<_Tp>::apply(value_type __f(const value_type&)) const template <class _Tp> void -valarray<_Tp>::resize(size_t __n, value_type __x) +valarray<_Tp>::__clear() { if (__begin_ != nullptr) { while (__end_ != __begin_) (--__end_)->~value_type(); - _VSTD::__libcpp_deallocate(__begin_); + _VSTD::__libcpp_deallocate(__begin_, __alignof(value_type)); __begin_ = __end_ = nullptr; } +} + +template <class _Tp> +void +valarray<_Tp>::resize(size_t __n, value_type __x) +{ + __clear(); if (__n) { - __begin_ = __end_ = static_cast<value_type*>(_VSTD::__allocate(__n * sizeof(value_type))); + __begin_ = __end_ = static_cast<value_type*>( + _VSTD::__libcpp_allocate(__n * sizeof(value_type), __alignof(value_type))); #ifndef _LIBCPP_NO_EXCEPTIONS try { @@ -3702,7 +3757,7 @@ valarray<_Tp>::resize(size_t __n, value_type __x) } catch (...) { - resize(0); + __clear(); throw; } #endif // _LIBCPP_NO_EXCEPTIONS |