[libcxx-commits] [libcxx] 67752f6 - [libc++] Simplify vector<bool>::__construct_at_end (#119632)
via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Jan 29 08:47:02 PST 2025
Author: Peng Liu
Date: 2025-01-29T11:46:57-05:00
New Revision: 67752f61917994e4daa6f5d30e39e237c26a73de
URL: https://github.com/llvm/llvm-project/commit/67752f61917994e4daa6f5d30e39e237c26a73de
DIFF: https://github.com/llvm/llvm-project/commit/67752f61917994e4daa6f5d30e39e237c26a73de.diff
LOG: [libc++] Simplify vector<bool>::__construct_at_end (#119632)
This patch simplifies the implementation of `__construct_at_end` in
`vector<bool>`, which currently contains duplicate initialization logic
across its two overloads.
Added:
Modified:
libcxx/include/__vector/vector_bool.h
Removed:
################################################################################
diff --git a/libcxx/include/__vector/vector_bool.h b/libcxx/include/__vector/vector_bool.h
index 4f1c442ce0be8d..8d9257eddfcd2d 100644
--- a/libcxx/include/__vector/vector_bool.h
+++ b/libcxx/include/__vector/vector_bool.h
@@ -552,36 +552,29 @@ vector<bool, _Allocator>::__recommend(size_type __new_size) const {
}
// Default constructs __n objects starting at __end_
-// Precondition: __n > 0
// Precondition: size() + __n <= capacity()
// Postcondition: size() == size() + __n
template <class _Allocator>
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x) {
- size_type __old_size = this->__size_;
+ _LIBCPP_ASSERT_INTERNAL(
+ capacity() >= size() + __n, "vector<bool>::__construct_at_end called with insufficient capacity");
+ std::fill_n(end(), __n, __x);
this->__size_ += __n;
- if (__old_size == 0 || ((__old_size - 1) / __bits_per_word) != ((this->__size_ - 1) / __bits_per_word)) {
- if (this->__size_ <= __bits_per_word)
- this->__begin_[0] = __storage_type(0);
- else
- this->__begin_[(this->__size_ - 1) / __bits_per_word] = __storage_type(0);
- }
- std::fill_n(__make_iter(__old_size), __n, __x);
+ if (end().__ctz_ != 0) // Ensure uninitialized leading bits in the last word are set to zero
+ std::fill_n(end(), __bits_per_word - end().__ctz_, 0);
}
template <class _Allocator>
template <class _InputIterator, class _Sentinel>
_LIBCPP_CONSTEXPR_SINCE_CXX20 void
vector<bool, _Allocator>::__construct_at_end(_InputIterator __first, _Sentinel __last, size_type __n) {
- size_type __old_size = this->__size_;
+ _LIBCPP_ASSERT_INTERNAL(
+ capacity() >= size() + __n, "vector<bool>::__construct_at_end called with insufficient capacity");
+ std::__copy(std::move(__first), std::move(__last), end());
this->__size_ += __n;
- if (__old_size == 0 || ((__old_size - 1) / __bits_per_word) != ((this->__size_ - 1) / __bits_per_word)) {
- if (this->__size_ <= __bits_per_word)
- this->__begin_[0] = __storage_type(0);
- else
- this->__begin_[(this->__size_ - 1) / __bits_per_word] = __storage_type(0);
- }
- std::__copy(std::move(__first), std::move(__last), __make_iter(__old_size));
+ if (end().__ctz_ != 0) // Ensure uninitialized leading bits in the last word are set to zero
+ std::fill_n(end(), __bits_per_word - end().__ctz_, 0);
}
template <class _Allocator>
More information about the libcxx-commits
mailing list