Skip to content

Commit 723f953

Browse files
committed
Simplify vector<bool>::__construct_at_end
1 parent f53abc2 commit 723f953

File tree

1 file changed

+10
-17
lines changed

1 file changed

+10
-17
lines changed

libcxx/include/__vector/vector_bool.h

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define _LIBCPP___VECTOR_VECTOR_BOOL_H
1111

1212
#include <__algorithm/copy.h>
13+
#include <__algorithm/fill.h>
1314
#include <__algorithm/fill_n.h>
1415
#include <__algorithm/iterator_operations.h>
1516
#include <__algorithm/max.h>
@@ -536,30 +537,20 @@ vector<bool, _Allocator>::__recommend(size_type __new_size) const {
536537
template <class _Allocator>
537538
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
538539
vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x) {
539-
size_type __old_size = this->__size_;
540+
iterator __old_end = end();
540541
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);
542+
this->__begin_[(this->__size_ - 1) / __bits_per_word] = __storage_type(0);
543+
std::fill_n(__old_end, __n, __x);
548544
}
549545

550546
template <class _Allocator>
551547
template <class _InputIterator, class _Sentinel>
552548
_LIBCPP_CONSTEXPR_SINCE_CXX20 void
553549
vector<bool, _Allocator>::__construct_at_end(_InputIterator __first, _Sentinel __last, size_type __n) {
554-
size_type __old_size = this->__size_;
550+
iterator __old_end = end();
555551
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));
552+
this->__begin_[(this->__size_ - 1) / __bits_per_word] = __storage_type(0);
553+
std::__copy(__first, __last, __old_end);
563554
}
564555

565556
template <class _Allocator>
@@ -833,7 +824,9 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<bool, _Allocator>::reserve(size_type _
833824
this->__throw_length_error();
834825
vector __v(this->get_allocator());
835826
__v.__vallocate(__n);
836-
__v.__construct_at_end(this->begin(), this->end(), this->size());
827+
// Ensure that the call to __construct_at_end(first, last, n) meets the precondition of n > 0
828+
if (this->size() > 0)
829+
__v.__construct_at_end(this->begin(), this->end(), this->size());
837830
swap(__v);
838831
}
839832
}

0 commit comments

Comments
 (0)