Skip to content

Commit

Permalink
Restore refactoring for valarray
Browse files Browse the repository at this point in the history
  • Loading branch information
frederick-vs-ja committed Jul 18, 2023
1 parent ee11e62 commit acb8d34
Showing 1 changed file with 32 additions and 11 deletions.
43 changes: 32 additions & 11 deletions stl/inc/valarray
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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) {
Expand All @@ -549,8 +571,7 @@ private:
}
}

_Mysize = 0;
_Myptr = nullptr;
_Tidy_init();
}

void _Assign(size_t _Newsize, const _Ty* _Ptr) {
Expand All @@ -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
Expand Down

0 comments on commit acb8d34

Please sign in to comment.