From acb8d34cc77603dc9ee90c70e26d131fd8db045e Mon Sep 17 00:00:00 2001 From: "A. Jiang" Date: Wed, 19 Jul 2023 07:46:37 +0800 Subject: [PATCH] Restore refactoring for `valarray` --- stl/inc/valarray | 43 ++++++++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/stl/inc/valarray b/stl/inc/valarray index 26fff41e33..2598ebaab0 100644 --- a/stl/inc/valarray +++ b/stl/inc/valarray @@ -74,54 +74,71 @@ public: using value_type = _Ty; - valarray() = default; // construct empty valarray + valarray() noexcept /* strengthened */ { // construct empty valarray + _Tidy_init(); + } explicit valarray(size_t _Count) { // construct with _Count * _Ty() + _Tidy_init(); _Grow(_Count); } valarray(const _Ty& _Val, size_t _Count) { // construct with _Count * _Val + _Tidy_init(); _Grow(_Count, &_Val); } valarray(const _Ty* _Ptr, size_t _Count) { // construct with [_Ptr, _Ptr + _Count) + _Tidy_init(); _Grow(_Count, _Ptr, 1); } valarray(const valarray& _Right) { + _Tidy_init(); _Grow(_Right.size(), _Right._Myptr, 1); } valarray(const slice_array<_Ty>& _Slicearr) { + _Tidy_init(); *this = _Slicearr; } valarray(const gslice_array<_Ty>& _Gslicearr) { + _Tidy_init(); *this = _Gslicearr; } valarray(const mask_array<_Ty>& _Maskarr) { + _Tidy_init(); *this = _Maskarr; } valarray(const indirect_array<_Ty>& _Indarr) { + _Tidy_init(); *this = _Indarr; } - valarray(valarray&& _Right) noexcept - : _Myptr(_STD exchange(_Right._Myptr, static_cast<_Ty*>(nullptr))), - _Mysize(_STD exchange(_Right._Mysize, size_t{0})) {} + valarray(valarray&& _Right) noexcept { + _Tidy_init(); + _Assign_rv(_STD move(_Right)); + } valarray& operator=(valarray&& _Right) noexcept { + _Assign_rv(_STD move(_Right)); + return *this; + } + + void _Assign_rv(valarray&& _Right) noexcept { if (this != _STD addressof(_Right)) { // clear this and steal from _Right _Tidy_deallocate(); - _Myptr = _STD exchange(_Right._Myptr, static_cast<_Ty*>(nullptr)); - _Mysize = _STD exchange(_Right._Mysize, size_t{0}); + _Myptr = _Right._Myptr; + _Mysize = _Right._Mysize; + _Right._Tidy_init(); } - return *this; } valarray(initializer_list<_Ty> _Ilist) { + _Tidy_init(); _Grow(_Ilist.size(), _Ilist.begin(), 1); } @@ -532,6 +549,11 @@ private: } } + void _Tidy_init() noexcept { + _Mysize = 0; + _Myptr = nullptr; + } + void _Tidy_deallocate() noexcept { if (_Myptr) { // destroy elements for (size_t _Idx = 0; _Idx < _Mysize; ++_Idx) { @@ -549,8 +571,7 @@ private: } } - _Mysize = 0; - _Myptr = nullptr; + _Tidy_init(); } void _Assign(size_t _Newsize, const _Ty* _Ptr) { @@ -565,8 +586,8 @@ private: } } - _Ty* _Myptr = nullptr; // current storage reserved for array - size_t _Mysize = 0; // current length of sequence + _Ty* _Myptr; // current storage reserved for array + size_t _Mysize; // current length of sequence }; #if _HAS_CXX17