Skip to content

Commit 224797d

Browse files
committed
Simplfy emplace_back, __move_range, and __construct_at_end for vector
1 parent 145ddf7 commit 224797d

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

libcxx/include/__vector/vector.h

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -879,9 +879,8 @@ vector<_Tp, _Allocator>::__recommend(size_type __new_size) const {
879879
template <class _Tp, class _Allocator>
880880
_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::__construct_at_end(size_type __n) {
881881
_ConstructTransaction __tx(*this, __n);
882-
const_pointer __new_end = __tx.__new_end_;
883-
for (pointer __pos = __tx.__pos_; __pos != __new_end; __tx.__pos_ = ++__pos) {
884-
__alloc_traits::construct(this->__alloc_, std::__to_address(__pos));
882+
for (; __tx.__pos_ != __tx.__new_end_; ++__tx.__pos_) {
883+
__alloc_traits::construct(this->__alloc_, std::__to_address(__tx.__pos_));
885884
}
886885
}
887886

@@ -895,9 +894,8 @@ template <class _Tp, class _Allocator>
895894
_LIBCPP_CONSTEXPR_SINCE_CXX20 inline void
896895
vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x) {
897896
_ConstructTransaction __tx(*this, __n);
898-
const_pointer __new_end = __tx.__new_end_;
899-
for (pointer __pos = __tx.__pos_; __pos != __new_end; __tx.__pos_ = ++__pos) {
900-
__alloc_traits::construct(this->__alloc_, std::__to_address(__pos), __x);
897+
for (; __tx.__pos_ != __tx.__new_end_; ++__tx.__pos_) {
898+
__alloc_traits::construct(this->__alloc_, std::__to_address(__tx.__pos_), __x);
901899
}
902900
}
903901

@@ -1101,16 +1099,12 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 inline
11011099
void
11021100
#endif
11031101
vector<_Tp, _Allocator>::emplace_back(_Args&&... __args) {
1104-
pointer __end = this->__end_;
1105-
if (__end < this->__cap_) {
1102+
if (this->__end_ < this->__cap_)
11061103
__construct_one_at_end(std::forward<_Args>(__args)...);
1107-
++__end;
1108-
} else {
1109-
__end = __emplace_back_slow_path(std::forward<_Args>(__args)...);
1110-
}
1111-
this->__end_ = __end;
1104+
else
1105+
(void)__emplace_back_slow_path(std::forward<_Args>(__args)...);
11121106
#if _LIBCPP_STD_VER >= 17
1113-
return *(__end - 1);
1107+
return *(this->__end_ - 1);
11141108
#endif
11151109
}
11161110

@@ -1144,8 +1138,8 @@ vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointe
11441138
{
11451139
pointer __i = __from_s + __n;
11461140
_ConstructTransaction __tx(*this, __from_e - __i);
1147-
for (pointer __pos = __tx.__pos_; __i < __from_e; ++__i, (void)++__pos, __tx.__pos_ = __pos) {
1148-
__alloc_traits::construct(this->__alloc_, std::__to_address(__pos), std::move(*__i));
1141+
for (; __i < __from_e; ++__i, (void)++__tx.__pos_) {
1142+
__alloc_traits::construct(this->__alloc_, std::__to_address(__tx.__pos_), std::move(*__i));
11491143
}
11501144
}
11511145
std::move_backward(__from_s, __from_s + __n, __old_last);

0 commit comments

Comments
 (0)