Skip to content

Commit faae1ed

Browse files
committed
Refactor __split_buffer to eliminate code duplication
1 parent 8b55162 commit faae1ed

File tree

1 file changed

+12
-55
lines changed

1 file changed

+12
-55
lines changed

libcxx/include/__split_buffer

Lines changed: 12 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ public:
152152
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void push_front(value_type&& __x);
153153
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void push_back(value_type&& __x);
154154

155+
template <class... _Args>
156+
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void emplace_front(_Args&&... __args);
155157
template <class... _Args>
156158
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void emplace_back(_Args&&... __args);
157159

@@ -456,28 +458,17 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::shrink_to_fi
456458

457459
template <class _Tp, class _Allocator>
458460
_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::push_front(const_reference __x) {
459-
if (__begin_ == __first_) {
460-
if (__end_ < __end_cap()) {
461-
difference_type __d = __end_cap() - __end_;
462-
__d = (__d + 1) / 2;
463-
__begin_ = std::move_backward(__begin_, __end_, __end_ + __d);
464-
__end_ += __d;
465-
} else {
466-
size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
467-
__split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc());
468-
__t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
469-
std::swap(__first_, __t.__first_);
470-
std::swap(__begin_, __t.__begin_);
471-
std::swap(__end_, __t.__end_);
472-
std::swap(__end_cap(), __t.__end_cap());
473-
}
474-
}
475-
__alloc_traits::construct(__alloc(), std::__to_address(__begin_ - 1), __x);
476-
--__begin_;
461+
emplace_front(__x);
477462
}
478463

479464
template <class _Tp, class _Allocator>
480465
_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::push_front(value_type&& __x) {
466+
emplace_front(std::move(__x));
467+
}
468+
469+
template <class _Tp, class _Allocator>
470+
template <class... _Args>
471+
_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::emplace_front(_Args&&... __args) {
481472
if (__begin_ == __first_) {
482473
if (__end_ < __end_cap()) {
483474
difference_type __d = __end_cap() - __end_;
@@ -494,53 +485,19 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::push_front(v
494485
std::swap(__end_cap(), __t.__end_cap());
495486
}
496487
}
497-
__alloc_traits::construct(__alloc(), std::__to_address(__begin_ - 1), std::move(__x));
488+
__alloc_traits::construct(__alloc(), std::__to_address(__begin_ - 1), std::forward<_Args>(__args)...);
498489
--__begin_;
499490
}
500491

501492
template <class _Tp, class _Allocator>
502493
_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI void
503494
__split_buffer<_Tp, _Allocator>::push_back(const_reference __x) {
504-
if (__end_ == __end_cap()) {
505-
if (__begin_ > __first_) {
506-
difference_type __d = __begin_ - __first_;
507-
__d = (__d + 1) / 2;
508-
__end_ = std::move(__begin_, __end_, __begin_ - __d);
509-
__begin_ -= __d;
510-
} else {
511-
size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
512-
__split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
513-
__t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
514-
std::swap(__first_, __t.__first_);
515-
std::swap(__begin_, __t.__begin_);
516-
std::swap(__end_, __t.__end_);
517-
std::swap(__end_cap(), __t.__end_cap());
518-
}
519-
}
520-
__alloc_traits::construct(__alloc(), std::__to_address(__end_), __x);
521-
++__end_;
495+
emplace_back(__x);
522496
}
523497

524498
template <class _Tp, class _Allocator>
525499
_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::push_back(value_type&& __x) {
526-
if (__end_ == __end_cap()) {
527-
if (__begin_ > __first_) {
528-
difference_type __d = __begin_ - __first_;
529-
__d = (__d + 1) / 2;
530-
__end_ = std::move(__begin_, __end_, __begin_ - __d);
531-
__begin_ -= __d;
532-
} else {
533-
size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
534-
__split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
535-
__t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
536-
std::swap(__first_, __t.__first_);
537-
std::swap(__begin_, __t.__begin_);
538-
std::swap(__end_, __t.__end_);
539-
std::swap(__end_cap(), __t.__end_cap());
540-
}
541-
}
542-
__alloc_traits::construct(__alloc(), std::__to_address(__end_), std::move(__x));
543-
++__end_;
500+
emplace_back(std::move(__x));
544501
}
545502

546503
template <class _Tp, class _Allocator>

0 commit comments

Comments
 (0)