Skip to content

Commit f4112b8

Browse files
committed
Refactor vector<bool> assign functions
1 parent ab15976 commit f4112b8

File tree

1 file changed

+9
-23
lines changed

1 file changed

+9
-23
lines changed

libcxx/include/__vector/vector_bool.h

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -699,14 +699,7 @@ template <class _Allocator>
699699
_LIBCPP_CONSTEXPR_SINCE_CXX20 vector<bool, _Allocator>& vector<bool, _Allocator>::operator=(const vector& __v) {
700700
if (this != std::addressof(__v)) {
701701
__copy_assign_alloc(__v);
702-
if (__v.__size_) {
703-
if (__v.__size_ > capacity()) {
704-
__vdeallocate();
705-
__vallocate(__v.__size_);
706-
}
707-
std::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
708-
}
709-
__size_ = __v.__size_;
702+
__assign_with_size(__v.begin(), __v.end(), __v.size());
710703
}
711704
return *this;
712705
}
@@ -754,7 +747,7 @@ vector<bool, _Allocator>::operator=(vector&& __v)
754747
template <class _Allocator>
755748
_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<bool, _Allocator>::__move_assign(vector& __c, false_type) {
756749
if (__alloc_ != __c.__alloc_)
757-
assign(__c.begin(), __c.end());
750+
__assign_with_size(__c.begin(), __c.end(), __c.size());
758751
else
759752
__move_assign(__c, true_type());
760753
}
@@ -773,19 +766,14 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<bool, _Allocator>::__move_assign(vecto
773766

774767
template <class _Allocator>
775768
_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<bool, _Allocator>::assign(size_type __n, const value_type& __x) {
776-
__size_ = 0;
777769
if (__n > 0) {
778-
size_type __c = capacity();
779-
if (__n <= __c)
780-
__size_ = __n;
781-
else {
782-
vector __v(get_allocator());
783-
__v.reserve(__recommend(__n));
784-
__v.__size_ = __n;
785-
swap(__v);
770+
if (__n > capacity()) {
771+
__vdeallocate();
772+
__vallocate(__n);
786773
}
787774
std::fill_n(begin(), __n, __x);
788775
}
776+
this->__size_ = __n;
789777
}
790778

791779
template <class _Allocator>
@@ -814,17 +802,15 @@ template <class _ForwardIterator, class _Sentinel>
814802
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
815803
vector<bool, _Allocator>::__assign_with_size(_ForwardIterator __first, _Sentinel __last, difference_type __ns) {
816804
_LIBCPP_ASSERT_VALID_INPUT_RANGE(__ns >= 0, "invalid range specified");
817-
818-
clear();
819-
820-
const size_t __n = static_cast<size_type>(__ns);
805+
const size_type __n = static_cast<size_type>(__ns);
821806
if (__n) {
822807
if (__n > capacity()) {
823808
__vdeallocate();
824809
__vallocate(__n);
825810
}
826-
__construct_at_end(__first, __last, __n);
811+
std::__copy(__first, __last, this->begin());
827812
}
813+
this->__size_ = __n;
828814
}
829815

830816
template <class _Allocator>

0 commit comments

Comments
 (0)