Skip to content

Commit fc5b4d4

Browse files
authored
[libc++] Rename __construct_one_at_end to __emplace_back_assume_capacity (#132276)
This makes it clear that the end of the vector is updated when calling the function.
1 parent e16e93a commit fc5b4d4

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

libcxx/include/__vector/vector.h

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,15 @@ class _LIBCPP_TEMPLATE_VIS vector {
461461
emplace_back(_Args&&... __args);
462462
#endif
463463

464+
template <class... _Args>
465+
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __emplace_back_assume_capacity(_Args&&... __args) {
466+
_LIBCPP_ASSERT_INTERNAL(
467+
size() < capacity(), "We assume that we have enough space to insert an element at the end of the vector");
468+
_ConstructTransaction __tx(*this, 1);
469+
__alloc_traits::construct(this->__alloc_, std::__to_address(__tx.__pos_), std::forward<_Args>(__args)...);
470+
++__tx.__pos_;
471+
}
472+
464473
#if _LIBCPP_STD_VER >= 23
465474
template <_ContainerCompatibleRange<_Tp> _Range>
466475
_LIBCPP_HIDE_FROM_ABI constexpr void append_range(_Range&& __range) {
@@ -758,13 +767,6 @@ class _LIBCPP_TEMPLATE_VIS vector {
758767
_ConstructTransaction& operator=(_ConstructTransaction const&) = delete;
759768
};
760769

761-
template <class... _Args>
762-
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __construct_one_at_end(_Args&&... __args) {
763-
_ConstructTransaction __tx(*this, 1);
764-
__alloc_traits::construct(this->__alloc_, std::__to_address(__tx.__pos_), std::forward<_Args>(__args)...);
765-
++__tx.__pos_;
766-
}
767-
768770
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __base_destruct_at_end(pointer __new_last) _NOEXCEPT {
769771
pointer __soon_to_be_end = this->__end_;
770772
while (__new_last != __soon_to_be_end)
@@ -1152,7 +1154,7 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 inline
11521154
vector<_Tp, _Allocator>::emplace_back(_Args&&... __args) {
11531155
pointer __end = this->__end_;
11541156
if (__end < this->__cap_) {
1155-
__construct_one_at_end(std::forward<_Args>(__args)...);
1157+
__emplace_back_assume_capacity(std::forward<_Args>(__args)...);
11561158
++__end;
11571159
} else {
11581160
__end = __emplace_back_slow_path(std::forward<_Args>(__args)...);
@@ -1206,7 +1208,7 @@ vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
12061208
pointer __p = this->__begin_ + (__position - begin());
12071209
if (this->__end_ < this->__cap_) {
12081210
if (__p == this->__end_) {
1209-
__construct_one_at_end(__x);
1211+
__emplace_back_assume_capacity(__x);
12101212
} else {
12111213
__move_range(__p, this->__end_, __p + 1);
12121214
const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
@@ -1228,7 +1230,7 @@ vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x) {
12281230
pointer __p = this->__begin_ + (__position - begin());
12291231
if (this->__end_ < this->__cap_) {
12301232
if (__p == this->__end_) {
1231-
__construct_one_at_end(std::move(__x));
1233+
__emplace_back_assume_capacity(std::move(__x));
12321234
} else {
12331235
__move_range(__p, this->__end_, __p + 1);
12341236
*__p = std::move(__x);
@@ -1248,7 +1250,7 @@ vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args) {
12481250
pointer __p = this->__begin_ + (__position - begin());
12491251
if (this->__end_ < this->__cap_) {
12501252
if (__p == this->__end_) {
1251-
__construct_one_at_end(std::forward<_Args>(__args)...);
1253+
__emplace_back_assume_capacity(std::forward<_Args>(__args)...);
12521254
} else {
12531255
__temp_value<value_type, _Allocator> __tmp(this->__alloc_, std::forward<_Args>(__args)...);
12541256
__move_range(__p, this->__end_, __p + 1);
@@ -1300,7 +1302,7 @@ vector<_Tp, _Allocator>::__insert_with_sentinel(const_iterator __position, _Inpu
13001302
pointer __p = this->__begin_ + __off;
13011303
pointer __old_last = this->__end_;
13021304
for (; this->__end_ != this->__cap_ && __first != __last; ++__first)
1303-
__construct_one_at_end(*__first);
1305+
__emplace_back_assume_capacity(*__first);
13041306

13051307
if (__first == __last)
13061308
(void)std::rotate(__p, __old_last, this->__end_);

0 commit comments

Comments
 (0)