From f65f8c25e853075ca66d82da4d52662deb6e6846 Mon Sep 17 00:00:00 2001 From: Peng Liu Date: Tue, 29 Oct 2024 16:28:33 -0400 Subject: [PATCH] Refactor __split_buffer to eliminate code duplication --- libcxx/include/__split_buffer | 67 +++++++---------------------------- 1 file changed, 12 insertions(+), 55 deletions(-) diff --git a/libcxx/include/__split_buffer b/libcxx/include/__split_buffer index c4817601039f3b..99fe4059a0e15d 100644 --- a/libcxx/include/__split_buffer +++ b/libcxx/include/__split_buffer @@ -152,6 +152,8 @@ public: _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void push_front(value_type&& __x); _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void push_back(value_type&& __x); + template + _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void emplace_front(_Args&&... __args); template _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void emplace_back(_Args&&... __args); @@ -456,28 +458,17 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::shrink_to_fi template _LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::push_front(const_reference __x) { - if (__begin_ == __first_) { - if (__end_ < __end_cap()) { - difference_type __d = __end_cap() - __end_; - __d = (__d + 1) / 2; - __begin_ = std::move_backward(__begin_, __end_, __end_ + __d); - __end_ += __d; - } else { - size_type __c = std::max(2 * static_cast(__end_cap() - __first_), 1); - __split_buffer __t(__c, (__c + 3) / 4, __alloc()); - __t.__construct_at_end(move_iterator(__begin_), move_iterator(__end_)); - std::swap(__first_, __t.__first_); - std::swap(__begin_, __t.__begin_); - std::swap(__end_, __t.__end_); - std::swap(__end_cap(), __t.__end_cap()); - } - } - __alloc_traits::construct(__alloc(), std::__to_address(__begin_ - 1), __x); - --__begin_; + emplace_front(__x); } template _LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::push_front(value_type&& __x) { + emplace_front(std::move(__x)); +} + +template +template +_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::emplace_front(_Args&&... __args) { if (__begin_ == __first_) { if (__end_ < __end_cap()) { difference_type __d = __end_cap() - __end_; @@ -494,53 +485,19 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::push_front(v std::swap(__end_cap(), __t.__end_cap()); } } - __alloc_traits::construct(__alloc(), std::__to_address(__begin_ - 1), std::move(__x)); + __alloc_traits::construct(__alloc(), std::__to_address(__begin_ - 1), std::forward<_Args>(__args)...); --__begin_; } template _LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI void __split_buffer<_Tp, _Allocator>::push_back(const_reference __x) { - if (__end_ == __end_cap()) { - if (__begin_ > __first_) { - difference_type __d = __begin_ - __first_; - __d = (__d + 1) / 2; - __end_ = std::move(__begin_, __end_, __begin_ - __d); - __begin_ -= __d; - } else { - size_type __c = std::max(2 * static_cast(__end_cap() - __first_), 1); - __split_buffer __t(__c, __c / 4, __alloc()); - __t.__construct_at_end(move_iterator(__begin_), move_iterator(__end_)); - std::swap(__first_, __t.__first_); - std::swap(__begin_, __t.__begin_); - std::swap(__end_, __t.__end_); - std::swap(__end_cap(), __t.__end_cap()); - } - } - __alloc_traits::construct(__alloc(), std::__to_address(__end_), __x); - ++__end_; + emplace_back(__x); } template _LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::push_back(value_type&& __x) { - if (__end_ == __end_cap()) { - if (__begin_ > __first_) { - difference_type __d = __begin_ - __first_; - __d = (__d + 1) / 2; - __end_ = std::move(__begin_, __end_, __begin_ - __d); - __begin_ -= __d; - } else { - size_type __c = std::max(2 * static_cast(__end_cap() - __first_), 1); - __split_buffer __t(__c, __c / 4, __alloc()); - __t.__construct_at_end(move_iterator(__begin_), move_iterator(__end_)); - std::swap(__first_, __t.__first_); - std::swap(__begin_, __t.__begin_); - std::swap(__end_, __t.__end_); - std::swap(__end_cap(), __t.__end_cap()); - } - } - __alloc_traits::construct(__alloc(), std::__to_address(__end_), std::move(__x)); - ++__end_; + emplace_back(std::move(__x)); } template