From 1d79168f3114367413e1d81eda4badf191745bdf Mon Sep 17 00:00:00 2001 From: achabense <60953653+achabense@users.noreply.github.com> Date: Fri, 8 Sep 2023 13:56:36 +0800 Subject: [PATCH 01/20] trivial relocations ctor 1 --- stl/inc/deque | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/stl/inc/deque b/stl/inc/deque index 2800f211443..d8462299b9f 100644 --- a/stl/inc/deque +++ b/stl/inc/deque @@ -643,21 +643,6 @@ public: _Proxy._Release(); } - deque(const deque& _Right) - : _Mypair(_One_then_variadic_args_t{}, _Alty_traits::select_on_container_copy_construction(_Right._Getal())) { - _Alproxy_ty _Alproxy(_Getal()); - _Container_proxy_ptr12<_Alproxy_ty> _Proxy(_Alproxy, _Get_data()); - _Construct(_Right._Unchecked_begin(), _Right._Unchecked_end()); - _Proxy._Release(); - } - - deque(const deque& _Right, const _Identity_t<_Alloc>& _Al) : _Mypair(_One_then_variadic_args_t{}, _Al) { - _Alproxy_ty _Alproxy(_Getal()); - _Container_proxy_ptr12<_Alproxy_ty> _Proxy(_Alproxy, _Get_data()); - _Construct(_Right._Unchecked_begin(), _Right._Unchecked_end()); - _Proxy._Release(); - } - template , int> = 0> deque(_Iter _First, _Iter _Last) : _Mypair(_Zero_then_variadic_args_t{}) { _Alproxy_ty _Alproxy(_Getal()); @@ -692,6 +677,21 @@ public: } #endif // _HAS_CXX23 && defined(__cpp_lib_concepts) + deque(const deque& _Right) + : _Mypair(_One_then_variadic_args_t{}, _Alty_traits::select_on_container_copy_construction(_Right._Getal())) { + _Alproxy_ty _Alproxy(_Getal()); + _Container_proxy_ptr12<_Alproxy_ty> _Proxy(_Alproxy, _Get_data()); + _Construct(_Right._Unchecked_begin(), _Right._Unchecked_end()); + _Proxy._Release(); + } + + deque(const deque& _Right, const _Identity_t<_Alloc>& _Al) : _Mypair(_One_then_variadic_args_t{}, _Al) { + _Alproxy_ty _Alproxy(_Getal()); + _Container_proxy_ptr12<_Alproxy_ty> _Proxy(_Alproxy, _Get_data()); + _Construct(_Right._Unchecked_begin(), _Right._Unchecked_end()); + _Proxy._Release(); + } + private: template void _Construct(_Iter _First, const _Sent _Last) { // initialize from input range [_First, _Last) From f5e058f9d3d2e7788f9bfae9ac2b88a5005bb962 Mon Sep 17 00:00:00 2001 From: achabense <60953653+achabense@users.noreply.github.com> Date: Fri, 8 Sep 2023 13:58:26 +0800 Subject: [PATCH 02/20] trivial relocations ctor 2 --- stl/inc/deque | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/stl/inc/deque b/stl/inc/deque index d8462299b9f..d6bbb117dfa 100644 --- a/stl/inc/deque +++ b/stl/inc/deque @@ -692,27 +692,6 @@ public: _Proxy._Release(); } -private: - template - void _Construct(_Iter _First, const _Sent _Last) { // initialize from input range [_First, _Last) - _Tidy_guard _Guard{this}; - for (; _First != _Last; ++_First) { - _Emplace_back_internal(*_First); - } - - _Guard._Target = nullptr; - } - - void _Construct_n(size_type _Count, const _Ty& _Val) { // construct from _Count * _Val - _Tidy_guard _Guard{this}; - for (; _Count > 0; --_Count) { - _Emplace_back_internal(_Val); - } - - _Guard._Target = nullptr; - } - -public: deque(deque&& _Right) : _Mypair(_One_then_variadic_args_t{}, _STD move(_Right._Getal())) { _Get_data()._Alloc_proxy(static_cast<_Alproxy_ty>(_Getal())); _Take_contents(_Right); @@ -734,6 +713,27 @@ public: _Take_contents(_Right); } +private: + template + void _Construct(_Iter _First, const _Sent _Last) { // initialize from input range [_First, _Last) + _Tidy_guard _Guard{this}; + for (; _First != _Last; ++_First) { + _Emplace_back_internal(*_First); + } + + _Guard._Target = nullptr; + } + + void _Construct_n(size_type _Count, const _Ty& _Val) { // construct from _Count * _Val + _Tidy_guard _Guard{this}; + for (; _Count > 0; --_Count) { + _Emplace_back_internal(_Val); + } + + _Guard._Target = nullptr; + } + +public: deque& operator=(deque&& _Right) noexcept(_Alty_traits::is_always_equal::value) { if (this == _STD addressof(_Right)) { return *this; From 5b31ef7dee5f9e296074716c8de34ba8f14d70e6 Mon Sep 17 00:00:00 2001 From: achabense <60953653+achabense@users.noreply.github.com> Date: Fri, 8 Sep 2023 13:59:21 +0800 Subject: [PATCH 03/20] trivial relocations ctor 3 --- stl/inc/deque | 51 +++++++++++++++++++++++++-------------------------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/stl/inc/deque b/stl/inc/deque index d6bbb117dfa..e3023e5f0fb 100644 --- a/stl/inc/deque +++ b/stl/inc/deque @@ -713,6 +713,14 @@ public: _Take_contents(_Right); } + deque(initializer_list<_Ty> _Ilist, const _Alloc& _Al = allocator_type()) + : _Mypair(_One_then_variadic_args_t{}, _Al) { + _Alproxy_ty _Alproxy(_Getal()); + _Container_proxy_ptr12<_Alproxy_ty> _Proxy(_Alproxy, _Get_data()); + _Construct(_Ilist.begin(), _Ilist.end()); + _Proxy._Release(); + } + private: template void _Construct(_Iter _First, const _Sent _Last) { // initialize from input range [_First, _Last) @@ -733,6 +741,23 @@ private: _Guard._Target = nullptr; } + void _Take_contents(deque& _Right) noexcept { + // move from _Right, stealing its contents + // pre: _Getal() == _Right._Getal() + auto& _My_data = _Get_data(); + auto& _Right_data = _Right._Get_data(); + _My_data._Swap_proxy_and_iterators(_Right_data); + _My_data._Map = _Right_data._Map; + _My_data._Mapsize = _Right_data._Mapsize; + _My_data._Myoff = _Right_data._Myoff; + _My_data._Mysize = _Right_data._Mysize; + + _Right_data._Map = nullptr; + _Right_data._Mapsize = 0; + _Right_data._Myoff = 0; + _Right_data._Mysize = 0; + } + public: deque& operator=(deque&& _Right) noexcept(_Alty_traits::is_always_equal::value) { if (this == _STD addressof(_Right)) { @@ -768,24 +793,6 @@ public: return *this; } -private: - void _Take_contents(deque& _Right) noexcept { - // move from _Right, stealing its contents - // pre: _Getal() == _Right._Getal() - auto& _My_data = _Get_data(); - auto& _Right_data = _Right._Get_data(); - _My_data._Swap_proxy_and_iterators(_Right_data); - _My_data._Map = _Right_data._Map; - _My_data._Mapsize = _Right_data._Mapsize; - _My_data._Myoff = _Right_data._Myoff; - _My_data._Mysize = _Right_data._Mysize; - - _Right_data._Map = nullptr; - _Right_data._Mapsize = 0; - _Right_data._Myoff = 0; - _Right_data._Mysize = 0; - } - public: void push_front(_Ty&& _Val) { emplace_front(_STD move(_Val)); @@ -881,14 +888,6 @@ public: return begin() + static_cast(_Off); } - deque(initializer_list<_Ty> _Ilist, const _Alloc& _Al = allocator_type()) - : _Mypair(_One_then_variadic_args_t{}, _Al) { - _Alproxy_ty _Alproxy(_Getal()); - _Container_proxy_ptr12<_Alproxy_ty> _Proxy(_Alproxy, _Get_data()); - _Construct(_Ilist.begin(), _Ilist.end()); - _Proxy._Release(); - } - deque& operator=(initializer_list<_Ty> _Ilist) { assign(_Ilist.begin(), _Ilist.end()); return *this; From 8ddc212cd0e297f5e9f42db42927bcbb11976014 Mon Sep 17 00:00:00 2001 From: achabense <60953653+achabense@users.noreply.github.com> Date: Fri, 8 Sep 2023 14:02:30 +0800 Subject: [PATCH 04/20] trivial relocations assign 1 --- stl/inc/deque | 52 +++++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/stl/inc/deque b/stl/inc/deque index e3023e5f0fb..729cdfb81b8 100644 --- a/stl/inc/deque +++ b/stl/inc/deque @@ -759,6 +759,32 @@ private: } public: + ~deque() noexcept { + _Tidy(); + _Alproxy_ty _Proxy_allocator(_Getal()); + _Delete_plain_internal(_Proxy_allocator, _STD exchange(_Get_data()._Myproxy, nullptr)); + } + + deque& operator=(const deque& _Right) { + if (this == _STD addressof(_Right)) { + return *this; + } + + auto& _Al = _Getal(); + auto& _Right_al = _Right._Getal(); + if constexpr (_Choose_pocca_v<_Alty>) { + if (_Al != _Right_al) { + _Tidy(); + _Get_data()._Reload_proxy(static_cast<_Alproxy_ty>(_Al), static_cast<_Alproxy_ty>(_Right_al)); + } + } + + _Pocca(_Al, _Right_al); + assign(_Right._Unchecked_begin(), _Right._Unchecked_end()); + + return *this; + } + deque& operator=(deque&& _Right) noexcept(_Alty_traits::is_always_equal::value) { if (this == _STD addressof(_Right)) { return *this; @@ -901,32 +927,6 @@ public: return insert(_Where, _Ilist.begin(), _Ilist.end()); } - ~deque() noexcept { - _Tidy(); - _Alproxy_ty _Proxy_allocator(_Getal()); - _Delete_plain_internal(_Proxy_allocator, _STD exchange(_Get_data()._Myproxy, nullptr)); - } - - deque& operator=(const deque& _Right) { - if (this == _STD addressof(_Right)) { - return *this; - } - - auto& _Al = _Getal(); - auto& _Right_al = _Right._Getal(); - if constexpr (_Choose_pocca_v<_Alty>) { - if (_Al != _Right_al) { - _Tidy(); - _Get_data()._Reload_proxy(static_cast<_Alproxy_ty>(_Al), static_cast<_Alproxy_ty>(_Right_al)); - } - } - - _Pocca(_Al, _Right_al); - assign(_Right._Unchecked_begin(), _Right._Unchecked_end()); - - return *this; - } - _NODISCARD iterator begin() noexcept { return iterator(_Myoff(), _STD addressof(_Get_data())); } From f2bd377ecc86010c183730a29f02264e9f1dacfb Mon Sep 17 00:00:00 2001 From: achabense <60953653+achabense@users.noreply.github.com> Date: Fri, 8 Sep 2023 14:12:21 +0800 Subject: [PATCH 05/20] trivial relocations assign 2 --- stl/inc/deque | 62 +++++++++++++++++++++++++-------------------------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/stl/inc/deque b/stl/inc/deque index 729cdfb81b8..6784b3bce30 100644 --- a/stl/inc/deque +++ b/stl/inc/deque @@ -819,6 +819,37 @@ public: return *this; } + template , int> = 0> + void assign(_Iter _First, _Iter _Last) { + _Adl_verify_range(_First, _Last); + _Assign_range(_Get_unwrapped(_First), _Get_unwrapped(_Last)); + } + +#if _HAS_CXX23 && defined(__cpp_lib_concepts) // TRANSITION, GH-395 + template <_Container_compatible_range<_Ty> _Rng> + void assign_range(_Rng&& _Range) { + _Assign_range(_RANGES _Ubegin(_Range), _RANGES _Uend(_Range)); + } +#endif // _HAS_CXX23 && defined(__cpp_lib_concepts) + + void assign(_CRT_GUARDOVERFLOW size_type _Count, const _Ty& _Val) { // assign _Count * _Val + _Orphan_all(); + auto _Myfirst = _Unchecked_begin(); + const auto _Oldsize = _Mysize(); + auto _Assign_count = (_STD min)(_Count, _Oldsize); + for (; _Assign_count > 0; --_Assign_count) { + *_Myfirst = _Val; + ++_Myfirst; + } + + const auto _Shrink_by = _Oldsize - _Assign_count; + auto _Extend_by = _Count - _Assign_count; + _Erase_last_n(_Shrink_by); + for (; _Extend_by > 0; --_Extend_by) { + _Emplace_back_internal(_Val); + } + } + public: void push_front(_Ty&& _Val) { emplace_front(_STD move(_Val)); @@ -1234,37 +1265,6 @@ private: } public: - template , int> = 0> - void assign(_Iter _First, _Iter _Last) { - _Adl_verify_range(_First, _Last); - _Assign_range(_Get_unwrapped(_First), _Get_unwrapped(_Last)); - } - -#if _HAS_CXX23 && defined(__cpp_lib_concepts) // TRANSITION, GH-395 - template <_Container_compatible_range<_Ty> _Rng> - void assign_range(_Rng&& _Range) { - _Assign_range(_RANGES _Ubegin(_Range), _RANGES _Uend(_Range)); - } -#endif // _HAS_CXX23 && defined(__cpp_lib_concepts) - - void assign(_CRT_GUARDOVERFLOW size_type _Count, const _Ty& _Val) { // assign _Count * _Val - _Orphan_all(); - auto _Myfirst = _Unchecked_begin(); - const auto _Oldsize = _Mysize(); - auto _Assign_count = (_STD min)(_Count, _Oldsize); - for (; _Assign_count > 0; --_Assign_count) { - *_Myfirst = _Val; - ++_Myfirst; - } - - const auto _Shrink_by = _Oldsize - _Assign_count; - auto _Extend_by = _Count - _Assign_count; - _Erase_last_n(_Shrink_by); - for (; _Extend_by > 0; --_Extend_by) { - _Emplace_back_internal(_Val); - } - } - iterator insert(const_iterator _Where, const _Ty& _Val) { size_type _Off = static_cast(_Where - begin()); From cb2e6edc95c6241564442e61b9daab4c1b4f556f Mon Sep 17 00:00:00 2001 From: achabense <60953653+achabense@users.noreply.github.com> Date: Fri, 8 Sep 2023 14:15:26 +0800 Subject: [PATCH 06/20] trivial relocations assign 3 --- stl/inc/deque | 70 +++++++++++++++++++++++++-------------------------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/stl/inc/deque b/stl/inc/deque index 6784b3bce30..1a0606f3d87 100644 --- a/stl/inc/deque +++ b/stl/inc/deque @@ -819,6 +819,11 @@ public: return *this; } + deque& operator=(initializer_list<_Ty> _Ilist) { + assign(_Ilist.begin(), _Ilist.end()); + return *this; + } + template , int> = 0> void assign(_Iter _First, _Iter _Last) { _Adl_verify_range(_First, _Last); @@ -850,6 +855,36 @@ public: } } + void assign(initializer_list<_Ty> _Ilist) { + assign(_Ilist.begin(), _Ilist.end()); + } + + _NODISCARD allocator_type get_allocator() const noexcept { + return static_cast(_Getal()); + } + +private: + template + void _Assign_range(_Iter _First, const _Sent _Last) { + _Orphan_all(); + auto _Myfirst = _Unchecked_begin(); + const auto _Mylast = _Unchecked_end(); + // Reuse existing elements + for (; _Myfirst != _Mylast; ++_Myfirst, (void) ++_First) { + if (_First == _Last) { + _Erase_last_n(static_cast(_Mylast - _Myfirst)); + return; + } + + *_Myfirst = *_First; + } + + // Allocate new elements for remaining tail of values + for (; _First != _Last; ++_First) { + _Emplace_back_internal(*_First); + } + } + public: void push_front(_Ty&& _Val) { emplace_front(_STD move(_Val)); @@ -945,15 +980,6 @@ public: return begin() + static_cast(_Off); } - deque& operator=(initializer_list<_Ty> _Ilist) { - assign(_Ilist.begin(), _Ilist.end()); - return *this; - } - - void assign(initializer_list<_Ty> _Ilist) { - assign(_Ilist.begin(), _Ilist.end()); - } - iterator insert(const_iterator _Where, initializer_list<_Ty> _Ilist) { return insert(_Where, _Ilist.begin(), _Ilist.end()); } @@ -1076,10 +1102,6 @@ public: return _Mysize() == 0; } - _NODISCARD allocator_type get_allocator() const noexcept { - return static_cast(_Getal()); - } - _NODISCARD const_reference at(size_type _Pos) const { if (_Mysize() <= _Pos) { _Xran(); @@ -1242,28 +1264,6 @@ public: #endif // ^^^ _ITERATOR_DEBUG_LEVEL < 2 ^^^ } -private: - template - void _Assign_range(_Iter _First, const _Sent _Last) { - _Orphan_all(); - auto _Myfirst = _Unchecked_begin(); - const auto _Mylast = _Unchecked_end(); - // Reuse existing elements - for (; _Myfirst != _Mylast; ++_Myfirst, (void) ++_First) { - if (_First == _Last) { - _Erase_last_n(static_cast(_Mylast - _Myfirst)); - return; - } - - *_Myfirst = *_First; - } - - // Allocate new elements for remaining tail of values - for (; _First != _Last; ++_First) { - _Emplace_back_internal(*_First); - } - } - public: iterator insert(const_iterator _Where, const _Ty& _Val) { size_type _Off = static_cast(_Where - begin()); From d74eb8740dde3b4be0fe28d928406e6cb0822d57 Mon Sep 17 00:00:00 2001 From: achabense <60953653+achabense@users.noreply.github.com> Date: Fri, 8 Sep 2023 14:20:09 +0800 Subject: [PATCH 07/20] trivial relocations iterators --- stl/inc/deque | 136 +++++++++++++++++++++++++------------------------- 1 file changed, 68 insertions(+), 68 deletions(-) diff --git a/stl/inc/deque b/stl/inc/deque index 1a0606f3d87..8e6e8ce3054 100644 --- a/stl/inc/deque +++ b/stl/inc/deque @@ -886,6 +886,74 @@ private: } public: + _NODISCARD iterator begin() noexcept { + return iterator(_Myoff(), _STD addressof(_Get_data())); + } + + _NODISCARD const_iterator begin() const noexcept { + return const_iterator(_Myoff(), _STD addressof(_Get_data())); + } + + _NODISCARD iterator end() noexcept { + return iterator(_Myoff() + _Mysize(), _STD addressof(_Get_data())); + } + + _NODISCARD const_iterator end() const noexcept { + return const_iterator(_Myoff() + _Mysize(), _STD addressof(_Get_data())); + } + + _Unchecked_iterator _Unchecked_begin() noexcept { + return _Unchecked_iterator(_Myoff(), _STD addressof(_Get_data())); + } + + _Unchecked_const_iterator _Unchecked_begin() const noexcept { + return _Unchecked_const_iterator(_Myoff(), _STD addressof(_Get_data())); + } + + _Unchecked_iterator _Unchecked_end() noexcept { + return _Unchecked_iterator(_Myoff() + _Mysize(), _STD addressof(_Get_data())); + } + + _Unchecked_const_iterator _Unchecked_end() const noexcept { + return _Unchecked_const_iterator(_Myoff() + _Mysize(), _STD addressof(_Get_data())); + } + + iterator _Make_iter(const_iterator _Where) const noexcept { + return iterator(_Where._Myoff, _STD addressof(_Get_data())); + } + + _NODISCARD reverse_iterator rbegin() noexcept { + return reverse_iterator(end()); + } + + _NODISCARD const_reverse_iterator rbegin() const noexcept { + return const_reverse_iterator(end()); + } + + _NODISCARD reverse_iterator rend() noexcept { + return reverse_iterator(begin()); + } + + _NODISCARD const_reverse_iterator rend() const noexcept { + return const_reverse_iterator(begin()); + } + + _NODISCARD const_iterator cbegin() const noexcept { + return begin(); + } + + _NODISCARD const_iterator cend() const noexcept { + return end(); + } + + _NODISCARD const_reverse_iterator crbegin() const noexcept { + return rbegin(); + } + + _NODISCARD const_reverse_iterator crend() const noexcept { + return rend(); + } + void push_front(_Ty&& _Val) { emplace_front(_STD move(_Val)); } @@ -984,74 +1052,6 @@ public: return insert(_Where, _Ilist.begin(), _Ilist.end()); } - _NODISCARD iterator begin() noexcept { - return iterator(_Myoff(), _STD addressof(_Get_data())); - } - - _NODISCARD const_iterator begin() const noexcept { - return const_iterator(_Myoff(), _STD addressof(_Get_data())); - } - - _NODISCARD iterator end() noexcept { - return iterator(_Myoff() + _Mysize(), _STD addressof(_Get_data())); - } - - _NODISCARD const_iterator end() const noexcept { - return const_iterator(_Myoff() + _Mysize(), _STD addressof(_Get_data())); - } - - _Unchecked_iterator _Unchecked_begin() noexcept { - return _Unchecked_iterator(_Myoff(), _STD addressof(_Get_data())); - } - - _Unchecked_const_iterator _Unchecked_begin() const noexcept { - return _Unchecked_const_iterator(_Myoff(), _STD addressof(_Get_data())); - } - - _Unchecked_iterator _Unchecked_end() noexcept { - return _Unchecked_iterator(_Myoff() + _Mysize(), _STD addressof(_Get_data())); - } - - _Unchecked_const_iterator _Unchecked_end() const noexcept { - return _Unchecked_const_iterator(_Myoff() + _Mysize(), _STD addressof(_Get_data())); - } - - iterator _Make_iter(const_iterator _Where) const noexcept { - return iterator(_Where._Myoff, _STD addressof(_Get_data())); - } - - _NODISCARD reverse_iterator rbegin() noexcept { - return reverse_iterator(end()); - } - - _NODISCARD const_reverse_iterator rbegin() const noexcept { - return const_reverse_iterator(end()); - } - - _NODISCARD reverse_iterator rend() noexcept { - return reverse_iterator(begin()); - } - - _NODISCARD const_reverse_iterator rend() const noexcept { - return const_reverse_iterator(begin()); - } - - _NODISCARD const_iterator cbegin() const noexcept { - return begin(); - } - - _NODISCARD const_iterator cend() const noexcept { - return end(); - } - - _NODISCARD const_reverse_iterator crbegin() const noexcept { - return rbegin(); - } - - _NODISCARD const_reverse_iterator crend() const noexcept { - return rend(); - } - void shrink_to_fit() { size_type _Oldcapacity = _Block_size * _Mapsize(); size_type _Newcapacity = _Oldcapacity / 2; From d3726fc07e3fc8a18ce7b8f471bb18d8559f065f Mon Sep 17 00:00:00 2001 From: achabense <60953653+achabense@users.noreply.github.com> Date: Fri, 8 Sep 2023 14:22:56 +0800 Subject: [PATCH 08/20] trivial relocations capacity --- stl/inc/deque | 100 +++++++++++++++++++++++++------------------------- 1 file changed, 50 insertions(+), 50 deletions(-) diff --git a/stl/inc/deque b/stl/inc/deque index 8e6e8ce3054..af013514acd 100644 --- a/stl/inc/deque +++ b/stl/inc/deque @@ -954,6 +954,56 @@ public: return rend(); } + _NODISCARD_EMPTY_MEMBER bool empty() const noexcept { + return _Mysize() == 0; + } + + _NODISCARD size_type size() const noexcept { + return _Mysize(); + } + + _NODISCARD size_type max_size() const noexcept { + return (_STD min)( + static_cast((numeric_limits::max)()), _Alty_traits::max_size(_Getal())); + } + + void resize(_CRT_GUARDOVERFLOW size_type _Newsize) { + _Orphan_all(); + while (_Mysize() < _Newsize) { + _Emplace_back_internal(); + } + + while (_Mysize() > _Newsize) { + pop_back(); + } + } + + void resize(_CRT_GUARDOVERFLOW size_type _Newsize, const _Ty& _Val) { + _Orphan_all(); + while (_Mysize() < _Newsize) { + _Emplace_back_internal(_Val); + } + + while (_Mysize() > _Newsize) { + pop_back(); + } + } + + void shrink_to_fit() { + size_type _Oldcapacity = _Block_size * _Mapsize(); + size_type _Newcapacity = _Oldcapacity / 2; + + if (_Newcapacity < _Block_size * _Minimum_map_size) { + _Newcapacity = _Block_size * _Minimum_map_size; + } + + if ((empty() && _Mapsize() > 0) + || (!empty() && size() <= _Newcapacity && _Newcapacity < _Oldcapacity)) { // worth shrinking, do it + deque _Tmp(_STD make_move_iterator(begin()), _STD make_move_iterator(end())); + swap(_Tmp); + } + } + void push_front(_Ty&& _Val) { emplace_front(_STD move(_Val)); } @@ -1052,56 +1102,6 @@ public: return insert(_Where, _Ilist.begin(), _Ilist.end()); } - void shrink_to_fit() { - size_type _Oldcapacity = _Block_size * _Mapsize(); - size_type _Newcapacity = _Oldcapacity / 2; - - if (_Newcapacity < _Block_size * _Minimum_map_size) { - _Newcapacity = _Block_size * _Minimum_map_size; - } - - if ((empty() && _Mapsize() > 0) - || (!empty() && size() <= _Newcapacity && _Newcapacity < _Oldcapacity)) { // worth shrinking, do it - deque _Tmp(_STD make_move_iterator(begin()), _STD make_move_iterator(end())); - swap(_Tmp); - } - } - - void resize(_CRT_GUARDOVERFLOW size_type _Newsize) { - _Orphan_all(); - while (_Mysize() < _Newsize) { - _Emplace_back_internal(); - } - - while (_Mysize() > _Newsize) { - pop_back(); - } - } - - void resize(_CRT_GUARDOVERFLOW size_type _Newsize, const _Ty& _Val) { - _Orphan_all(); - while (_Mysize() < _Newsize) { - _Emplace_back_internal(_Val); - } - - while (_Mysize() > _Newsize) { - pop_back(); - } - } - - _NODISCARD size_type size() const noexcept { - return _Mysize(); - } - - _NODISCARD size_type max_size() const noexcept { - return (_STD min)( - static_cast((numeric_limits::max)()), _Alty_traits::max_size(_Getal())); - } - - _NODISCARD_EMPTY_MEMBER bool empty() const noexcept { - return _Mysize() == 0; - } - _NODISCARD const_reference at(size_type _Pos) const { if (_Mysize() <= _Pos) { _Xran(); From 4a1ee357fdeb13fe11082508ab70be184cbcea78 Mon Sep 17 00:00:00 2001 From: achabense <60953653+achabense@users.noreply.github.com> Date: Fri, 8 Sep 2023 14:26:49 +0800 Subject: [PATCH 09/20] trivial relocations [] 1 --- stl/inc/deque | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/stl/inc/deque b/stl/inc/deque index af013514acd..55d9942ffa2 100644 --- a/stl/inc/deque +++ b/stl/inc/deque @@ -1102,22 +1102,6 @@ public: return insert(_Where, _Ilist.begin(), _Ilist.end()); } - _NODISCARD const_reference at(size_type _Pos) const { - if (_Mysize() <= _Pos) { - _Xran(); - } - - return *(begin() + static_cast(_Pos)); - } - - _NODISCARD reference at(size_type _Pos) { - if (_Mysize() <= _Pos) { - _Xran(); - } - - return *(begin() + static_cast(_Pos)); - } - _NODISCARD const_reference operator[](size_type _Pos) const noexcept /* strengthened */ { #if _CONTAINER_DEBUG_LEVEL > 0 _STL_VERIFY(_Pos < _Mysize(), "deque subscript out of range"); @@ -1134,6 +1118,22 @@ public: return *(_Unchecked_begin() + static_cast(_Pos)); } + _NODISCARD const_reference at(size_type _Pos) const { + if (_Mysize() <= _Pos) { + _Xran(); + } + + return *(begin() + static_cast(_Pos)); + } + + _NODISCARD reference at(size_type _Pos) { + if (_Mysize() <= _Pos) { + _Xran(); + } + + return *(begin() + static_cast(_Pos)); + } + _NODISCARD reference front() noexcept /* strengthened */ { #if _CONTAINER_DEBUG_LEVEL > 0 _STL_VERIFY(!empty(), "front() called on empty deque"); From 3da7dbafbb4ef28ab2109716bfcdac05c0b92d42 Mon Sep 17 00:00:00 2001 From: achabense <60953653+achabense@users.noreply.github.com> Date: Fri, 8 Sep 2023 14:28:49 +0800 Subject: [PATCH 10/20] trivial relocations [] 2 --- stl/inc/deque | 128 +++++++++++++++++++++++++------------------------- 1 file changed, 64 insertions(+), 64 deletions(-) diff --git a/stl/inc/deque b/stl/inc/deque index 55d9942ffa2..9e27a131184 100644 --- a/stl/inc/deque +++ b/stl/inc/deque @@ -1004,6 +1004,70 @@ public: } } + _NODISCARD const_reference operator[](size_type _Pos) const noexcept /* strengthened */ { +#if _CONTAINER_DEBUG_LEVEL > 0 + _STL_VERIFY(_Pos < _Mysize(), "deque subscript out of range"); +#endif // _CONTAINER_DEBUG_LEVEL > 0 + + return *(_Unchecked_begin() + static_cast(_Pos)); + } + + _NODISCARD reference operator[](size_type _Pos) noexcept /* strengthened */ { +#if _CONTAINER_DEBUG_LEVEL > 0 + _STL_VERIFY(_Pos < _Mysize(), "deque subscript out of range"); +#endif // _CONTAINER_DEBUG_LEVEL > 0 + + return *(_Unchecked_begin() + static_cast(_Pos)); + } + + _NODISCARD const_reference at(size_type _Pos) const { + if (_Mysize() <= _Pos) { + _Xran(); + } + + return *(begin() + static_cast(_Pos)); + } + + _NODISCARD reference at(size_type _Pos) { + if (_Mysize() <= _Pos) { + _Xran(); + } + + return *(begin() + static_cast(_Pos)); + } + + _NODISCARD reference front() noexcept /* strengthened */ { +#if _CONTAINER_DEBUG_LEVEL > 0 + _STL_VERIFY(!empty(), "front() called on empty deque"); +#endif // _CONTAINER_DEBUG_LEVEL > 0 + + return *_Unchecked_begin(); + } + + _NODISCARD const_reference front() const noexcept /* strengthened */ { +#if _CONTAINER_DEBUG_LEVEL > 0 + _STL_VERIFY(!empty(), "front() called on empty deque"); +#endif // _CONTAINER_DEBUG_LEVEL > 0 + + return *_Unchecked_begin(); + } + + _NODISCARD reference back() noexcept /* strengthened */ { +#if _CONTAINER_DEBUG_LEVEL > 0 + _STL_VERIFY(!empty(), "back() called on empty deque"); +#endif // _CONTAINER_DEBUG_LEVEL > 0 + + return *_Prev_iter(_Unchecked_end()); + } + + _NODISCARD const_reference back() const noexcept /* strengthened */ { +#if _CONTAINER_DEBUG_LEVEL > 0 + _STL_VERIFY(!empty(), "back() called on empty deque"); +#endif // _CONTAINER_DEBUG_LEVEL > 0 + + return *_Prev_iter(_Unchecked_end()); + } + void push_front(_Ty&& _Val) { emplace_front(_STD move(_Val)); } @@ -1102,70 +1166,6 @@ public: return insert(_Where, _Ilist.begin(), _Ilist.end()); } - _NODISCARD const_reference operator[](size_type _Pos) const noexcept /* strengthened */ { -#if _CONTAINER_DEBUG_LEVEL > 0 - _STL_VERIFY(_Pos < _Mysize(), "deque subscript out of range"); -#endif // _CONTAINER_DEBUG_LEVEL > 0 - - return *(_Unchecked_begin() + static_cast(_Pos)); - } - - _NODISCARD reference operator[](size_type _Pos) noexcept /* strengthened */ { -#if _CONTAINER_DEBUG_LEVEL > 0 - _STL_VERIFY(_Pos < _Mysize(), "deque subscript out of range"); -#endif // _CONTAINER_DEBUG_LEVEL > 0 - - return *(_Unchecked_begin() + static_cast(_Pos)); - } - - _NODISCARD const_reference at(size_type _Pos) const { - if (_Mysize() <= _Pos) { - _Xran(); - } - - return *(begin() + static_cast(_Pos)); - } - - _NODISCARD reference at(size_type _Pos) { - if (_Mysize() <= _Pos) { - _Xran(); - } - - return *(begin() + static_cast(_Pos)); - } - - _NODISCARD reference front() noexcept /* strengthened */ { -#if _CONTAINER_DEBUG_LEVEL > 0 - _STL_VERIFY(!empty(), "front() called on empty deque"); -#endif // _CONTAINER_DEBUG_LEVEL > 0 - - return *_Unchecked_begin(); - } - - _NODISCARD const_reference front() const noexcept /* strengthened */ { -#if _CONTAINER_DEBUG_LEVEL > 0 - _STL_VERIFY(!empty(), "front() called on empty deque"); -#endif // _CONTAINER_DEBUG_LEVEL > 0 - - return *_Unchecked_begin(); - } - - _NODISCARD reference back() noexcept /* strengthened */ { -#if _CONTAINER_DEBUG_LEVEL > 0 - _STL_VERIFY(!empty(), "back() called on empty deque"); -#endif // _CONTAINER_DEBUG_LEVEL > 0 - - return *_Prev_iter(_Unchecked_end()); - } - - _NODISCARD const_reference back() const noexcept /* strengthened */ { -#if _CONTAINER_DEBUG_LEVEL > 0 - _STL_VERIFY(!empty(), "back() called on empty deque"); -#endif // _CONTAINER_DEBUG_LEVEL > 0 - - return *_Prev_iter(_Unchecked_end()); - } - void push_front(const _Ty& _Val) { emplace_front(_Val); } From 8206847801c3692e7d101068568539c79218257d Mon Sep 17 00:00:00 2001 From: achabense <60953653+achabense@users.noreply.github.com> Date: Fri, 8 Sep 2023 14:32:34 +0800 Subject: [PATCH 11/20] trivial relocations emplace 1 --- stl/inc/deque | 78 +++++++++++++++++++++++++-------------------------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/stl/inc/deque b/stl/inc/deque index 9e27a131184..b2d64eacad6 100644 --- a/stl/inc/deque +++ b/stl/inc/deque @@ -1068,6 +1068,45 @@ public: return *_Prev_iter(_Unchecked_end()); } +private: + template + void _Emplace_back_internal(_Tys&&... _Vals) { + if ((_Myoff() + _Mysize()) % _Block_size == 0 && _Mapsize() <= (_Mysize() + _Block_size) / _Block_size) { + _Growmap(1); + } + _Myoff() &= _Mapsize() * _Block_size - 1; + size_type _Newoff = _Myoff() + _Mysize(); + size_type _Block = _Getblock(_Newoff); + if (_Map()[_Block] == nullptr) { + _Map()[_Block] = _Getal().allocate(_Block_size); + } + + _Alty_traits::construct( + _Getal(), _Unfancy(_Map()[_Block] + _Newoff % _Block_size), _STD forward<_Tys>(_Vals)...); + + ++_Mysize(); + } + + template + void _Emplace_front_internal(_Tys&&... _Vals) { + if (_Myoff() % _Block_size == 0 && _Mapsize() <= (_Mysize() + _Block_size) / _Block_size) { + _Growmap(1); + } + _Myoff() &= _Mapsize() * _Block_size - 1; + size_type _Newoff = _Myoff() != 0 ? _Myoff() : _Mapsize() * _Block_size; + const size_type _Block = _Getblock(--_Newoff); + if (_Map()[_Block] == nullptr) { + _Map()[_Block] = _Getal().allocate(_Block_size); + } + + _Alty_traits::construct( + _Getal(), _Unfancy(_Map()[_Block] + _Newoff % _Block_size), _STD forward<_Tys>(_Vals)...); + + _Myoff() = _Newoff; + ++_Mysize(); + } + +public: void push_front(_Ty&& _Val) { emplace_front(_STD move(_Val)); } @@ -1196,45 +1235,6 @@ public: #endif // ^^^ _ITERATOR_DEBUG_LEVEL < 2 ^^^ } -private: - template - void _Emplace_back_internal(_Tys&&... _Vals) { - if ((_Myoff() + _Mysize()) % _Block_size == 0 && _Mapsize() <= (_Mysize() + _Block_size) / _Block_size) { - _Growmap(1); - } - _Myoff() &= _Mapsize() * _Block_size - 1; - size_type _Newoff = _Myoff() + _Mysize(); - size_type _Block = _Getblock(_Newoff); - if (_Map()[_Block] == nullptr) { - _Map()[_Block] = _Getal().allocate(_Block_size); - } - - _Alty_traits::construct( - _Getal(), _Unfancy(_Map()[_Block] + _Newoff % _Block_size), _STD forward<_Tys>(_Vals)...); - - ++_Mysize(); - } - - template - void _Emplace_front_internal(_Tys&&... _Vals) { - if (_Myoff() % _Block_size == 0 && _Mapsize() <= (_Mysize() + _Block_size) / _Block_size) { - _Growmap(1); - } - _Myoff() &= _Mapsize() * _Block_size - 1; - size_type _Newoff = _Myoff() != 0 ? _Myoff() : _Mapsize() * _Block_size; - const size_type _Block = _Getblock(--_Newoff); - if (_Map()[_Block] == nullptr) { - _Map()[_Block] = _Getal().allocate(_Block_size); - } - - _Alty_traits::construct( - _Getal(), _Unfancy(_Map()[_Block] + _Newoff % _Block_size), _STD forward<_Tys>(_Vals)...); - - _Myoff() = _Newoff; - ++_Mysize(); - } - -public: void push_back(const _Ty& _Val) { _Orphan_all(); _Emplace_back_internal(_Val); From 53f02194c76da1ad60a22739533b672bacff639d Mon Sep 17 00:00:00 2001 From: achabense <60953653+achabense@users.noreply.github.com> Date: Fri, 8 Sep 2023 14:34:58 +0800 Subject: [PATCH 12/20] trivial relocations emplace 2 --- stl/inc/deque | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/stl/inc/deque b/stl/inc/deque index b2d64eacad6..5362c72ced4 100644 --- a/stl/inc/deque +++ b/stl/inc/deque @@ -1107,6 +1107,10 @@ private: } public: + void push_front(const _Ty& _Val) { + emplace_front(_Val); + } + void push_front(_Ty&& _Val) { emplace_front(_STD move(_Val)); } @@ -1138,6 +1142,11 @@ public: } #endif // _HAS_CXX23 && defined(__cpp_lib_concepts) + void push_back(const _Ty& _Val) { + _Orphan_all(); + _Emplace_back_internal(_Val); + } + void push_back(_Ty&& _Val) { _Orphan_all(); _Emplace_back_internal(_STD move(_Val)); @@ -1205,10 +1214,6 @@ public: return insert(_Where, _Ilist.begin(), _Ilist.end()); } - void push_front(const _Ty& _Val) { - emplace_front(_Val); - } - void pop_front() noexcept /* strengthened */ { #if _ITERATOR_DEBUG_LEVEL == 2 if (empty()) { @@ -1235,11 +1240,6 @@ public: #endif // ^^^ _ITERATOR_DEBUG_LEVEL < 2 ^^^ } - void push_back(const _Ty& _Val) { - _Orphan_all(); - _Emplace_back_internal(_Val); - } - void pop_back() noexcept /* strengthened */ { #if _ITERATOR_DEBUG_LEVEL == 2 if (empty()) { From dc2dc9acab1de921152db1fcd44951da7eac476a Mon Sep 17 00:00:00 2001 From: achabense <60953653+achabense@users.noreply.github.com> Date: Fri, 8 Sep 2023 14:35:46 +0800 Subject: [PATCH 13/20] trivial relocations emplace 3 --- stl/inc/deque | 74 +++++++++++++++++++++++++-------------------------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/stl/inc/deque b/stl/inc/deque index 5362c72ced4..879f4023a34 100644 --- a/stl/inc/deque +++ b/stl/inc/deque @@ -1107,6 +1107,43 @@ private: } public: + template + decltype(auto) emplace_front(_Valty&&... _Val) { + _Orphan_all(); + _Emplace_front_internal(_STD forward<_Valty>(_Val)...); +#if _HAS_CXX17 + return front(); +#endif // _HAS_CXX17 + } + + template + decltype(auto) emplace_back(_Valty&&... _Val) { + _Orphan_all(); + _Emplace_back_internal(_STD forward<_Valty>(_Val)...); + +#if _HAS_CXX17 + return back(); +#endif // _HAS_CXX17 + } + + template + iterator emplace(const_iterator _Where, _Valty&&... _Val) { + const auto _Off = static_cast(_Where - begin()); + +#if _ITERATOR_DEBUG_LEVEL == 2 + _STL_VERIFY(_Off <= _Mysize(), "deque emplace iterator outside range"); +#endif // _ITERATOR_DEBUG_LEVEL == 2 + + if (_Off <= _Mysize() / 2) { // closer to front, push to front then rotate + emplace_front(_STD forward<_Valty>(_Val)...); + _STD rotate(begin(), _Next_iter(begin()), begin() + static_cast(1 + _Off)); + } else { // closer to back, push to back then rotate + emplace_back(_STD forward<_Valty>(_Val)...); + _STD rotate(begin() + static_cast(_Off), _Prev_iter(end()), end()); + } + return begin() + static_cast(_Off); + } + void push_front(const _Ty& _Val) { emplace_front(_Val); } @@ -1173,43 +1210,6 @@ public: return emplace(_Where, _STD move(_Val)); } - template - decltype(auto) emplace_front(_Valty&&... _Val) { - _Orphan_all(); - _Emplace_front_internal(_STD forward<_Valty>(_Val)...); -#if _HAS_CXX17 - return front(); -#endif // _HAS_CXX17 - } - - template - decltype(auto) emplace_back(_Valty&&... _Val) { - _Orphan_all(); - _Emplace_back_internal(_STD forward<_Valty>(_Val)...); - -#if _HAS_CXX17 - return back(); -#endif // _HAS_CXX17 - } - - template - iterator emplace(const_iterator _Where, _Valty&&... _Val) { - const auto _Off = static_cast(_Where - begin()); - -#if _ITERATOR_DEBUG_LEVEL == 2 - _STL_VERIFY(_Off <= _Mysize(), "deque emplace iterator outside range"); -#endif // _ITERATOR_DEBUG_LEVEL == 2 - - if (_Off <= _Mysize() / 2) { // closer to front, push to front then rotate - emplace_front(_STD forward<_Valty>(_Val)...); - _STD rotate(begin(), _Next_iter(begin()), begin() + static_cast(1 + _Off)); - } else { // closer to back, push to back then rotate - emplace_back(_STD forward<_Valty>(_Val)...); - _STD rotate(begin() + static_cast(_Off), _Prev_iter(end()), end()); - } - return begin() + static_cast(_Off); - } - iterator insert(const_iterator _Where, initializer_list<_Ty> _Ilist) { return insert(_Where, _Ilist.begin(), _Ilist.end()); } From 1d1f2666c77da835fbba0b9fcef90b706f649e47 Mon Sep 17 00:00:00 2001 From: achabense <60953653+achabense@users.noreply.github.com> Date: Fri, 8 Sep 2023 14:40:56 +0800 Subject: [PATCH 14/20] trivial relocations emplace 4 --- stl/inc/deque | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/stl/inc/deque b/stl/inc/deque index 879f4023a34..c7ed1b39c94 100644 --- a/stl/inc/deque +++ b/stl/inc/deque @@ -1152,6 +1152,16 @@ public: emplace_front(_STD move(_Val)); } + void push_back(const _Ty& _Val) { + _Orphan_all(); + _Emplace_back_internal(_Val); + } + + void push_back(_Ty&& _Val) { + _Orphan_all(); + _Emplace_back_internal(_STD move(_Val)); + } + #if _HAS_CXX23 && defined(__cpp_lib_concepts) // TRANSITION, GH-395 template <_Container_compatible_range<_Ty> _Rng> void prepend_range(_Rng&& _Range) { @@ -1177,19 +1187,7 @@ public: } _Guard._Container = nullptr; } -#endif // _HAS_CXX23 && defined(__cpp_lib_concepts) - - void push_back(const _Ty& _Val) { - _Orphan_all(); - _Emplace_back_internal(_Val); - } - void push_back(_Ty&& _Val) { - _Orphan_all(); - _Emplace_back_internal(_STD move(_Val)); - } - -#if _HAS_CXX23 && defined(__cpp_lib_concepts) // TRANSITION, GH-395 template <_Container_compatible_range<_Ty> _Rng> void append_range(_Rng&& _Range) { _Orphan_all(); From 3f70198e9918e995243fcdde82fa8b124fecbdad Mon Sep 17 00:00:00 2001 From: achabense <60953653+achabense@users.noreply.github.com> Date: Fri, 8 Sep 2023 14:49:38 +0800 Subject: [PATCH 15/20] trivial relocations emplace 5 --- stl/inc/deque | 51 +++++++++++++++++++++++++-------------------------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/stl/inc/deque b/stl/inc/deque index c7ed1b39c94..344ae574293 100644 --- a/stl/inc/deque +++ b/stl/inc/deque @@ -1204,10 +1204,35 @@ public: } #endif // _HAS_CXX23 && defined(__cpp_lib_concepts) + iterator insert(const_iterator _Where, const _Ty& _Val) { + size_type _Off = static_cast(_Where - begin()); + +#if _ITERATOR_DEBUG_LEVEL == 2 + _STL_VERIFY(_Off <= _Mysize(), "deque insert iterator outside range"); +#endif // _ITERATOR_DEBUG_LEVEL == 2 + + if (_Off <= _Mysize() / 2) { // closer to front, push to front then copy + push_front(_Val); + _STD rotate(begin(), _Next_iter(begin()), begin() + static_cast(1 + _Off)); + } else { // closer to back, push to back then copy + push_back(_Val); + _STD rotate(begin() + static_cast(_Off), _Prev_iter(end()), end()); + } + + return begin() + static_cast(_Off); + } + iterator insert(const_iterator _Where, _Ty&& _Val) { return emplace(_Where, _STD move(_Val)); } + iterator insert(const_iterator _Where, _CRT_GUARDOVERFLOW size_type _Count, const _Ty& _Val) { + // insert _Count * _Val at _Where + size_type _Off = static_cast(_Where - begin()); + _Insert_n(_Where, _Count, _Val); + return begin() + static_cast(_Off); + } + iterator insert(const_iterator _Where, initializer_list<_Ty> _Ilist) { return insert(_Where, _Ilist.begin(), _Ilist.end()); } @@ -1262,32 +1287,6 @@ public: #endif // ^^^ _ITERATOR_DEBUG_LEVEL < 2 ^^^ } -public: - iterator insert(const_iterator _Where, const _Ty& _Val) { - size_type _Off = static_cast(_Where - begin()); - -#if _ITERATOR_DEBUG_LEVEL == 2 - _STL_VERIFY(_Off <= _Mysize(), "deque insert iterator outside range"); -#endif // _ITERATOR_DEBUG_LEVEL == 2 - - if (_Off <= _Mysize() / 2) { // closer to front, push to front then copy - push_front(_Val); - _STD rotate(begin(), _Next_iter(begin()), begin() + static_cast(1 + _Off)); - } else { // closer to back, push to back then copy - push_back(_Val); - _STD rotate(begin() + static_cast(_Off), _Prev_iter(end()), end()); - } - - return begin() + static_cast(_Off); - } - - iterator insert(const_iterator _Where, _CRT_GUARDOVERFLOW size_type _Count, const _Ty& _Val) { - // insert _Count * _Val at _Where - size_type _Off = static_cast(_Where - begin()); - _Insert_n(_Where, _Count, _Val); - return begin() + static_cast(_Off); - } - private: enum class _Pop_direction : bool { _Front, From 13e04900b250e52f584e76037d92a03d9a4faa01 Mon Sep 17 00:00:00 2001 From: achabense <60953653+achabense@users.noreply.github.com> Date: Fri, 8 Sep 2023 14:51:56 +0800 Subject: [PATCH 16/20] trivial relocations emplace 6 --- stl/inc/deque | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/stl/inc/deque b/stl/inc/deque index 344ae574293..0a5d4bcdd55 100644 --- a/stl/inc/deque +++ b/stl/inc/deque @@ -1233,6 +1233,29 @@ public: return begin() + static_cast(_Off); } + template , int> = 0> + iterator insert(const_iterator _Where, _Iter _First, _Iter _Last) { + // insert [_First, _Last) at _Where + _Adl_verify_range(_First, _Last); + const size_type _Off = static_cast(_Where - begin()); + return _Insert_range(_Is_cpp17_bidi_iter_v<_Iter>)>( + _Off, _Get_unwrapped(_First), _Get_unwrapped(_Last)); + } + +#if _HAS_CXX23 && defined(__cpp_lib_concepts) // TRANSITION, GH-395 + template <_Container_compatible_range<_Ty> _Rng> + iterator insert_range(const_iterator _Where, _Rng&& _Range) { + const size_type _Off = static_cast(_Where - begin()); + + if constexpr (_RANGES bidirectional_range<_Rng>) { + return _Insert_range<_Is_bidi::_Yes>( + _Off, _RANGES _Ubegin(_Range), _RANGES _Get_final_iterator_unwrapped(_Range)); + } else { + return _Insert_range<_Is_bidi::_Nope>(_Off, _RANGES _Ubegin(_Range), _RANGES _Uend(_Range)); + } + } +#endif // _HAS_CXX23 && defined(__cpp_lib_concepts) + iterator insert(const_iterator _Where, initializer_list<_Ty> _Ilist) { return insert(_Where, _Ilist.begin(), _Ilist.end()); } @@ -1369,29 +1392,6 @@ private: } public: - template , int> = 0> - iterator insert(const_iterator _Where, _Iter _First, _Iter _Last) { - // insert [_First, _Last) at _Where - _Adl_verify_range(_First, _Last); - const size_type _Off = static_cast(_Where - begin()); - return _Insert_range(_Is_cpp17_bidi_iter_v<_Iter>)>( - _Off, _Get_unwrapped(_First), _Get_unwrapped(_Last)); - } - -#if _HAS_CXX23 && defined(__cpp_lib_concepts) // TRANSITION, GH-395 - template <_Container_compatible_range<_Ty> _Rng> - iterator insert_range(const_iterator _Where, _Rng&& _Range) { - const size_type _Off = static_cast(_Where - begin()); - - if constexpr (_RANGES bidirectional_range<_Rng>) { - return _Insert_range<_Is_bidi::_Yes>( - _Off, _RANGES _Ubegin(_Range), _RANGES _Get_final_iterator_unwrapped(_Range)); - } else { - return _Insert_range<_Is_bidi::_Nope>(_Off, _RANGES _Ubegin(_Range), _RANGES _Uend(_Range)); - } - } -#endif // _HAS_CXX23 && defined(__cpp_lib_concepts) - iterator erase(const_iterator _Where) noexcept(is_nothrow_move_assignable_v) /* strengthened */ { return erase(_Where, _Next_iter(_Where)); } From ef1936aa85c5c33467397cc1c160346e2650a393 Mon Sep 17 00:00:00 2001 From: achabense <60953653+achabense@users.noreply.github.com> Date: Fri, 8 Sep 2023 15:27:09 +0800 Subject: [PATCH 17/20] trivial relocations emplace 7 --- stl/inc/deque | 130 +++++++++++++++++++++++++------------------------- 1 file changed, 65 insertions(+), 65 deletions(-) diff --git a/stl/inc/deque b/stl/inc/deque index 0a5d4bcdd55..4c07052469b 100644 --- a/stl/inc/deque +++ b/stl/inc/deque @@ -1391,6 +1391,71 @@ private: return begin() + static_cast(_Off); } + void _Insert_n(const_iterator _Where, size_type _Count, const _Ty& _Val) { // insert _Count * _Val at _Where + iterator _Mid; + size_type _Num; + size_type _Off = static_cast(_Where - begin()); + size_type _Oldsize = _Mysize(); + size_type _Rem = _Oldsize - _Off; + +#if _ITERATOR_DEBUG_LEVEL == 2 + _STL_VERIFY(_Off <= _Oldsize, "deque insert iterator outside range"); +#endif // _ITERATOR_DEBUG_LEVEL == 2 + + if (_Off < _Rem) { // closer to front + _Restore_old_size_guard<_Pop_direction::_Front> _Guard{this, _Oldsize}; + if (_Off < _Count) { // insert longer than prefix + for (_Num = _Count - _Off; _Num > 0; --_Num) { + push_front(_Val); // push excess values + } + for (_Num = _Off; _Num > 0; --_Num) { + push_front(begin()[static_cast(_Count - 1)]); // push prefix + } + + _Mid = begin() + static_cast(_Count); + _STD fill_n(_Mid, _Off, _Val); // fill in rest of values + } else { // insert not longer than prefix + for (_Num = _Count; _Num > 0; --_Num) { + push_front(begin()[static_cast(_Count - 1)]); // push part of prefix + } + + _Mid = begin() + static_cast(_Count); + _Alloc_temporary2<_Alty> _Tmp(_Getal(), _Val); // in case _Val is in sequence + _STD move(_Mid + static_cast(_Count), _Mid + static_cast(_Off), + _Mid); // copy rest of prefix + _STD fill(begin() + static_cast(_Off), _Mid + static_cast(_Off), + _Tmp._Get_value()); // fill in values + } + _Guard._Container = nullptr; + } else { // closer to back + _Restore_old_size_guard<_Pop_direction::_Back> _Guard{this, _Oldsize}; + if (_Rem < _Count) { // insert longer than suffix + _Orphan_all(); + for (_Num = _Count - _Rem; _Num > 0; --_Num) { + _Emplace_back_internal(_Val); // push excess values + } + for (_Num = 0; _Num < _Rem; ++_Num) { + _Emplace_back_internal(begin()[static_cast(_Off + _Num)]); // push suffix + } + + _Mid = begin() + static_cast(_Off); + _STD fill_n(_Mid, _Rem, _Val); // fill in rest of values + } else { // insert not longer than prefix + for (_Num = 0; _Num < _Count; ++_Num) { + _Emplace_back_internal( + begin()[static_cast(_Off + _Rem - _Count + _Num)]); // push part of prefix + } + + _Mid = begin() + static_cast(_Off); + _Alloc_temporary2<_Alty> _Tmp(_Getal(), _Val); // in case _Val is in sequence + _STD move_backward(_Mid, _Mid + static_cast(_Rem - _Count), + _Mid + static_cast(_Rem)); // copy rest of prefix + _STD fill_n(_Mid, _Count, _Tmp._Get_value()); // fill in values + } + _Guard._Container = nullptr; + } + } + public: iterator erase(const_iterator _Where) noexcept(is_nothrow_move_assignable_v) /* strengthened */ { return erase(_Where, _Next_iter(_Where)); @@ -1466,71 +1531,6 @@ public: } private: - void _Insert_n(const_iterator _Where, size_type _Count, const _Ty& _Val) { // insert _Count * _Val at _Where - iterator _Mid; - size_type _Num; - size_type _Off = static_cast(_Where - begin()); - size_type _Oldsize = _Mysize(); - size_type _Rem = _Oldsize - _Off; - -#if _ITERATOR_DEBUG_LEVEL == 2 - _STL_VERIFY(_Off <= _Oldsize, "deque insert iterator outside range"); -#endif // _ITERATOR_DEBUG_LEVEL == 2 - - if (_Off < _Rem) { // closer to front - _Restore_old_size_guard<_Pop_direction::_Front> _Guard{this, _Oldsize}; - if (_Off < _Count) { // insert longer than prefix - for (_Num = _Count - _Off; _Num > 0; --_Num) { - push_front(_Val); // push excess values - } - for (_Num = _Off; _Num > 0; --_Num) { - push_front(begin()[static_cast(_Count - 1)]); // push prefix - } - - _Mid = begin() + static_cast(_Count); - _STD fill_n(_Mid, _Off, _Val); // fill in rest of values - } else { // insert not longer than prefix - for (_Num = _Count; _Num > 0; --_Num) { - push_front(begin()[static_cast(_Count - 1)]); // push part of prefix - } - - _Mid = begin() + static_cast(_Count); - _Alloc_temporary2<_Alty> _Tmp(_Getal(), _Val); // in case _Val is in sequence - _STD move(_Mid + static_cast(_Count), _Mid + static_cast(_Off), - _Mid); // copy rest of prefix - _STD fill(begin() + static_cast(_Off), _Mid + static_cast(_Off), - _Tmp._Get_value()); // fill in values - } - _Guard._Container = nullptr; - } else { // closer to back - _Restore_old_size_guard<_Pop_direction::_Back> _Guard{this, _Oldsize}; - if (_Rem < _Count) { // insert longer than suffix - _Orphan_all(); - for (_Num = _Count - _Rem; _Num > 0; --_Num) { - _Emplace_back_internal(_Val); // push excess values - } - for (_Num = 0; _Num < _Rem; ++_Num) { - _Emplace_back_internal(begin()[static_cast(_Off + _Num)]); // push suffix - } - - _Mid = begin() + static_cast(_Off); - _STD fill_n(_Mid, _Rem, _Val); // fill in rest of values - } else { // insert not longer than prefix - for (_Num = 0; _Num < _Count; ++_Num) { - _Emplace_back_internal( - begin()[static_cast(_Off + _Rem - _Count + _Num)]); // push part of prefix - } - - _Mid = begin() + static_cast(_Off); - _Alloc_temporary2<_Alty> _Tmp(_Getal(), _Val); // in case _Val is in sequence - _STD move_backward(_Mid, _Mid + static_cast(_Rem - _Count), - _Mid + static_cast(_Rem)); // copy rest of prefix - _STD fill_n(_Mid, _Count, _Tmp._Get_value()); // fill in values - } - _Guard._Container = nullptr; - } - } - [[noreturn]] static void _Xlen() { _Xlength_error("deque too long"); } From 4a91d8dcb3ccccbfc323cbe3214bcc1a63a4ff65 Mon Sep 17 00:00:00 2001 From: achabense <60953653+achabense@users.noreply.github.com> Date: Fri, 8 Sep 2023 14:53:52 +0800 Subject: [PATCH 18/20] trivial relocations erase --- stl/inc/deque | 102 +++++++++++++++++++++++++------------------------- 1 file changed, 51 insertions(+), 51 deletions(-) diff --git a/stl/inc/deque b/stl/inc/deque index 4c07052469b..878e49dabce 100644 --- a/stl/inc/deque +++ b/stl/inc/deque @@ -1233,7 +1233,7 @@ public: return begin() + static_cast(_Off); } - template , int> = 0> + template , int> = 0> iterator insert(const_iterator _Where, _Iter _First, _Iter _Last) { // insert [_First, _Last) at _Where _Adl_verify_range(_First, _Last); @@ -1260,56 +1260,6 @@ public: return insert(_Where, _Ilist.begin(), _Ilist.end()); } - void pop_front() noexcept /* strengthened */ { -#if _ITERATOR_DEBUG_LEVEL == 2 - if (empty()) { - _STL_REPORT_ERROR("deque empty before pop"); - } else { // something to erase, do it - _Orphan_off(_Myoff()); - size_type _Block = _Getblock(_Myoff()); - _Alty_traits::destroy(_Getal(), _Unfancy(_Map()[_Block] + _Myoff() % _Block_size)); - if (--_Mysize() == 0) { - _Myoff() = 0; - } else { - ++_Myoff(); - } - } - -#else // ^^^ _ITERATOR_DEBUG_LEVEL == 2 / _ITERATOR_DEBUG_LEVEL < 2 vvv - size_type _Block = _Getblock(_Myoff()); - _Alty_traits::destroy(_Getal(), _Unfancy(_Map()[_Block] + _Myoff() % _Block_size)); - if (--_Mysize() == 0) { - _Myoff() = 0; - } else { - ++_Myoff(); - } -#endif // ^^^ _ITERATOR_DEBUG_LEVEL < 2 ^^^ - } - - void pop_back() noexcept /* strengthened */ { -#if _ITERATOR_DEBUG_LEVEL == 2 - if (empty()) { - _STL_REPORT_ERROR("deque empty before pop"); - } else { // something to erase, do it - size_type _Newoff = _Myoff() + _Mysize() - 1; - _Orphan_off(_Newoff); - size_type _Block = _Getblock(_Newoff); - _Alty_traits::destroy(_Getal(), _Unfancy(_Map()[_Block] + _Newoff % _Block_size)); - if (--_Mysize() == 0) { - _Myoff() = 0; - } - } - -#else // ^^^ _ITERATOR_DEBUG_LEVEL == 2 / _ITERATOR_DEBUG_LEVEL < 2 vvv - size_type _Newoff = _Myoff() + _Mysize() - 1; - size_type _Block = _Getblock(_Newoff); - _Alty_traits::destroy(_Getal(), _Unfancy(_Map()[_Block] + _Newoff % _Block_size)); - if (--_Mysize() == 0) { - _Myoff() = 0; - } -#endif // ^^^ _ITERATOR_DEBUG_LEVEL < 2 ^^^ - } - private: enum class _Pop_direction : bool { _Front, @@ -1457,6 +1407,56 @@ private: } public: + void pop_front() noexcept /* strengthened */ { +#if _ITERATOR_DEBUG_LEVEL == 2 + if (empty()) { + _STL_REPORT_ERROR("deque empty before pop"); + } else { // something to erase, do it + _Orphan_off(_Myoff()); + size_type _Block = _Getblock(_Myoff()); + _Alty_traits::destroy(_Getal(), _Unfancy(_Map()[_Block] + _Myoff() % _Block_size)); + if (--_Mysize() == 0) { + _Myoff() = 0; + } else { + ++_Myoff(); + } + } + +#else // ^^^ _ITERATOR_DEBUG_LEVEL == 2 / _ITERATOR_DEBUG_LEVEL < 2 vvv + size_type _Block = _Getblock(_Myoff()); + _Alty_traits::destroy(_Getal(), _Unfancy(_Map()[_Block] + _Myoff() % _Block_size)); + if (--_Mysize() == 0) { + _Myoff() = 0; + } else { + ++_Myoff(); + } +#endif // ^^^ _ITERATOR_DEBUG_LEVEL < 2 ^^^ + } + + void pop_back() noexcept /* strengthened */ { +#if _ITERATOR_DEBUG_LEVEL == 2 + if (empty()) { + _STL_REPORT_ERROR("deque empty before pop"); + } else { // something to erase, do it + size_type _Newoff = _Myoff() + _Mysize() - 1; + _Orphan_off(_Newoff); + size_type _Block = _Getblock(_Newoff); + _Alty_traits::destroy(_Getal(), _Unfancy(_Map()[_Block] + _Newoff % _Block_size)); + if (--_Mysize() == 0) { + _Myoff() = 0; + } + } + +#else // ^^^ _ITERATOR_DEBUG_LEVEL == 2 / _ITERATOR_DEBUG_LEVEL < 2 vvv + size_type _Newoff = _Myoff() + _Mysize() - 1; + size_type _Block = _Getblock(_Newoff); + _Alty_traits::destroy(_Getal(), _Unfancy(_Map()[_Block] + _Newoff % _Block_size)); + if (--_Mysize() == 0) { + _Myoff() = 0; + } +#endif // ^^^ _ITERATOR_DEBUG_LEVEL < 2 ^^^ + } + iterator erase(const_iterator _Where) noexcept(is_nothrow_move_assignable_v) /* strengthened */ { return erase(_Where, _Next_iter(_Where)); } From 30ec6d0432669c9f995ad4f6d3a0bfde1092049c Mon Sep 17 00:00:00 2001 From: achabense <60953653+achabense@users.noreply.github.com> Date: Fri, 8 Sep 2023 15:00:48 +0800 Subject: [PATCH 19/20] A. like `push_back`, `push_front` should use `_Emplace_front_internal` B. like `insert(where,T&&)`, `insert(where,const&)` should use `emplace` --- stl/inc/deque | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/stl/inc/deque b/stl/inc/deque index 878e49dabce..3c735384306 100644 --- a/stl/inc/deque +++ b/stl/inc/deque @@ -1145,11 +1145,13 @@ public: } void push_front(const _Ty& _Val) { - emplace_front(_Val); + _Orphan_all(); + _Emplace_front_internal(_Val); } void push_front(_Ty&& _Val) { - emplace_front(_STD move(_Val)); + _Orphan_all(); + _Emplace_front_internal(_STD move(_Val)); } void push_back(const _Ty& _Val) { @@ -1205,21 +1207,7 @@ public: #endif // _HAS_CXX23 && defined(__cpp_lib_concepts) iterator insert(const_iterator _Where, const _Ty& _Val) { - size_type _Off = static_cast(_Where - begin()); - -#if _ITERATOR_DEBUG_LEVEL == 2 - _STL_VERIFY(_Off <= _Mysize(), "deque insert iterator outside range"); -#endif // _ITERATOR_DEBUG_LEVEL == 2 - - if (_Off <= _Mysize() / 2) { // closer to front, push to front then copy - push_front(_Val); - _STD rotate(begin(), _Next_iter(begin()), begin() + static_cast(1 + _Off)); - } else { // closer to back, push to back then copy - push_back(_Val); - _STD rotate(begin() + static_cast(_Off), _Prev_iter(end()), end()); - } - - return begin() + static_cast(_Off); + return emplace(_Where, _Val); } iterator insert(const_iterator _Where, _Ty&& _Val) { From 8e1812767608712c1dc596509dc8ecb922dbafe9 Mon Sep 17 00:00:00 2001 From: achabense <60953653+achabense@users.noreply.github.com> Date: Fri, 8 Sep 2023 17:31:52 +0800 Subject: [PATCH 20/20] C. let `_Construct[_n]` do proxy allocation --- stl/inc/deque | 40 +++++++++------------------------------- 1 file changed, 9 insertions(+), 31 deletions(-) diff --git a/stl/inc/deque b/stl/inc/deque index 3c735384306..2b24d91236f 100644 --- a/stl/inc/deque +++ b/stl/inc/deque @@ -629,67 +629,43 @@ public: } deque(_CRT_GUARDOVERFLOW size_type _Count, const _Ty& _Val) : _Mypair(_Zero_then_variadic_args_t{}) { - _Alproxy_ty _Alproxy(_Getal()); - _Container_proxy_ptr12<_Alproxy_ty> _Proxy(_Alproxy, _Get_data()); _Construct_n(_Count, _Val); - _Proxy._Release(); } deque(_CRT_GUARDOVERFLOW size_type _Count, const _Ty& _Val, const _Alloc& _Al) : _Mypair(_One_then_variadic_args_t{}, _Al) { - _Alproxy_ty _Alproxy(_Getal()); - _Container_proxy_ptr12<_Alproxy_ty> _Proxy(_Alproxy, _Get_data()); _Construct_n(_Count, _Val); - _Proxy._Release(); } template , int> = 0> deque(_Iter _First, _Iter _Last) : _Mypair(_Zero_then_variadic_args_t{}) { - _Alproxy_ty _Alproxy(_Getal()); - _Container_proxy_ptr12<_Alproxy_ty> _Proxy(_Alproxy, _Get_data()); _Construct(_First, _Last); - _Proxy._Release(); } template , int> = 0> deque(_Iter _First, _Iter _Last, const _Alloc& _Al) : _Mypair(_One_then_variadic_args_t{}, _Al) { - _Alproxy_ty _Alproxy(_Getal()); - _Container_proxy_ptr12<_Alproxy_ty> _Proxy(_Alproxy, _Get_data()); _Construct(_First, _Last); - _Proxy._Release(); } #if _HAS_CXX23 && defined(__cpp_lib_concepts) // TRANSITION, GH-395 template <_Container_compatible_range<_Ty> _Rng> deque(from_range_t, _Rng&& _Range) : _Mypair(_Zero_then_variadic_args_t{}) { - _Alproxy_ty _Alproxy(_Getal()); - _Container_proxy_ptr12<_Alproxy_ty> _Proxy(_Alproxy, _Get_data()); _Construct(_RANGES _Ubegin(_Range), _RANGES _Uend(_Range)); - _Proxy._Release(); } template <_Container_compatible_range<_Ty> _Rng> deque(from_range_t, _Rng&& _Range, const _Alloc& _Al) : _Mypair(_One_then_variadic_args_t{}, _Al) { - _Alproxy_ty _Alproxy(_Getal()); - _Container_proxy_ptr12<_Alproxy_ty> _Proxy(_Alproxy, _Get_data()); _Construct(_RANGES _Ubegin(_Range), _RANGES _Uend(_Range)); - _Proxy._Release(); } #endif // _HAS_CXX23 && defined(__cpp_lib_concepts) deque(const deque& _Right) : _Mypair(_One_then_variadic_args_t{}, _Alty_traits::select_on_container_copy_construction(_Right._Getal())) { - _Alproxy_ty _Alproxy(_Getal()); - _Container_proxy_ptr12<_Alproxy_ty> _Proxy(_Alproxy, _Get_data()); _Construct(_Right._Unchecked_begin(), _Right._Unchecked_end()); - _Proxy._Release(); } deque(const deque& _Right, const _Identity_t<_Alloc>& _Al) : _Mypair(_One_then_variadic_args_t{}, _Al) { - _Alproxy_ty _Alproxy(_Getal()); - _Container_proxy_ptr12<_Alproxy_ty> _Proxy(_Alproxy, _Get_data()); _Construct(_Right._Unchecked_begin(), _Right._Unchecked_end()); - _Proxy._Release(); } deque(deque&& _Right) : _Mypair(_One_then_variadic_args_t{}, _STD move(_Right._Getal())) { @@ -698,47 +674,49 @@ public: } deque(deque&& _Right, const _Identity_t<_Alloc>& _Al) : _Mypair(_One_then_variadic_args_t{}, _Al) { - _Alproxy_ty _Alproxy(_Getal()); if constexpr (!_Alty_traits::is_always_equal::value) { if (_Getal() != _Right._Getal()) { - _Container_proxy_ptr12<_Alproxy_ty> _Proxy(_Alproxy, _Get_data()); _Construct(_STD make_move_iterator(_Right._Unchecked_begin()), _STD make_move_iterator(_Right._Unchecked_end())); - _Proxy._Release(); return; } } - _Get_data()._Alloc_proxy(_Alproxy); + _Get_data()._Alloc_proxy(static_cast<_Alproxy_ty>(_Getal())); _Take_contents(_Right); } deque(initializer_list<_Ty> _Ilist, const _Alloc& _Al = allocator_type()) : _Mypair(_One_then_variadic_args_t{}, _Al) { - _Alproxy_ty _Alproxy(_Getal()); - _Container_proxy_ptr12<_Alproxy_ty> _Proxy(_Alproxy, _Get_data()); _Construct(_Ilist.begin(), _Ilist.end()); - _Proxy._Release(); } private: template void _Construct(_Iter _First, const _Sent _Last) { // initialize from input range [_First, _Last) + _Alproxy_ty _Alproxy(_Getal()); + _Container_proxy_ptr12<_Alproxy_ty> _Proxy(_Alproxy, _Get_data()); + _Tidy_guard _Guard{this}; for (; _First != _Last; ++_First) { _Emplace_back_internal(*_First); } _Guard._Target = nullptr; + _Proxy._Release(); } void _Construct_n(size_type _Count, const _Ty& _Val) { // construct from _Count * _Val + _Alproxy_ty _Alproxy(_Getal()); + _Container_proxy_ptr12<_Alproxy_ty> _Proxy(_Alproxy, _Get_data()); + _Tidy_guard _Guard{this}; for (; _Count > 0; --_Count) { _Emplace_back_internal(_Val); } _Guard._Target = nullptr; + _Proxy._Release(); } void _Take_contents(deque& _Right) noexcept {