Skip to content

Commit 83451c6

Browse files
committed
Simplify vector<bool>::__construct_at_end
1 parent f4230b4 commit 83451c6

File tree

1 file changed

+9
-17
lines changed

1 file changed

+9
-17
lines changed

libcxx/include/__vector/vector_bool.h

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -536,30 +536,20 @@ vector<bool, _Allocator>::__recommend(size_type __new_size) const {
536536
template <class _Allocator>
537537
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
538538
vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x) {
539-
size_type __old_size = this->__size_;
539+
iterator __old_end = end();
540540
this->__size_ += __n;
541-
if (__old_size == 0 || ((__old_size - 1) / __bits_per_word) != ((this->__size_ - 1) / __bits_per_word)) {
542-
if (this->__size_ <= __bits_per_word)
543-
this->__begin_[0] = __storage_type(0);
544-
else
545-
this->__begin_[(this->__size_ - 1) / __bits_per_word] = __storage_type(0);
546-
}
547-
std::fill_n(__make_iter(__old_size), __n, __x);
541+
this->__begin_[(this->__size_ - 1) / __bits_per_word] = __storage_type(0);
542+
std::fill_n(__old_end, __n, __x);
548543
}
549544

550545
template <class _Allocator>
551546
template <class _InputIterator, class _Sentinel>
552547
_LIBCPP_CONSTEXPR_SINCE_CXX20 void
553548
vector<bool, _Allocator>::__construct_at_end(_InputIterator __first, _Sentinel __last, size_type __n) {
554-
size_type __old_size = this->__size_;
549+
iterator __old_end = end();
555550
this->__size_ += __n;
556-
if (__old_size == 0 || ((__old_size - 1) / __bits_per_word) != ((this->__size_ - 1) / __bits_per_word)) {
557-
if (this->__size_ <= __bits_per_word)
558-
this->__begin_[0] = __storage_type(0);
559-
else
560-
this->__begin_[(this->__size_ - 1) / __bits_per_word] = __storage_type(0);
561-
}
562-
std::__copy(__first, __last, __make_iter(__old_size));
551+
this->__begin_[(this->__size_ - 1) / __bits_per_word] = __storage_type(0);
552+
std::__copy(__first, __last, __old_end);
563553
}
564554

565555
template <class _Allocator>
@@ -833,7 +823,9 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<bool, _Allocator>::reserve(size_type _
833823
this->__throw_length_error();
834824
vector __v(this->get_allocator());
835825
__v.__vallocate(__n);
836-
__v.__construct_at_end(this->begin(), this->end(), this->size());
826+
// Ensure that the call to __construct_at_end(first, last, n) meets the precondition of n > 0
827+
if (this->size() > 0)
828+
__v.__construct_at_end(this->begin(), this->end(), this->size());
837829
swap(__v);
838830
}
839831
}

0 commit comments

Comments
 (0)