[libcxx] Add `__split_buffer::__swap_layouts` (#180102) This commit simplifies the cumbersome process of swapping the respective layout members for `__split_buffer` and `vector`. NOKEYCHECK=True GitOrigin-RevId: 65f39a16abf651008185839459fb330258800a62
diff --git a/include/__split_buffer b/include/__split_buffer index 89c398e..27ec8b7 100644 --- a/include/__split_buffer +++ b/include/__split_buffer
@@ -188,6 +188,14 @@ __back_cap_ = __other.__back_cap_; } + _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void + __swap_layouts(pointer& __begin, pointer& __end, pointer& __back_capacity) { + using std::swap; + swap(__begin_, __begin); + swap(__end_, __end); + swap(__back_cap_, __back_capacity); + } + private: pointer __front_cap_ = nullptr; pointer __begin_ = nullptr; @@ -336,6 +344,14 @@ __size_ = __other.__size_; } + _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void + __swap_layouts(pointer& __begin, size_type& __size, size_type& __capacity) { + using std::swap; + swap(__begin_, __begin); + swap(__size_, __size); + swap(__cap_, __capacity); + } + private: pointer __front_cap_ = nullptr; pointer __begin_ = nullptr; @@ -447,6 +463,7 @@ using __base_type::__set_data; using __base_type::__set_sentinel; using __base_type::__set_valid_range; + using __base_type::__swap_layouts; using typename __base_type::__alloc_traits; using typename __base_type::allocator_type;
diff --git a/include/__vector/vector.h b/include/__vector/vector.h index 181230d..22b4f70 100644 --- a/include/__vector/vector.h +++ b/include/__vector/vector.h
@@ -820,22 +820,9 @@ return __p; } - _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __swap_layouts(_SplitBuffer& __sb) { - auto __vector_begin = __begin_; - auto __vector_sentinel = __end_; - auto __vector_cap = __cap_; - - auto __sb_begin = __sb.begin(); - auto __sb_sentinel = __sb.__raw_sentinel(); - auto __sb_cap = __sb.__raw_capacity(); - - // TODO: replace with __set_valid_range and __set_capacity when vector supports it. - __begin_ = __sb_begin; - __end_ = __sb_sentinel; - __cap_ = __sb_cap; - - __sb.__set_valid_range(__vector_begin, __vector_sentinel); - __sb.__set_capacity(__vector_cap); + _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void + __swap_layouts(__split_buffer<_Tp, allocator_type>& __sb) { + __sb.__swap_layouts(__begin_, __end_, __cap_); } };