[libcxx-commits] [libcxx] [libc++] Simplify vector<bool>::flip() and add new tests (PR #119607)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Tue Dec 17 09:29:17 PST 2024


================
@@ -1049,18 +1049,11 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<bool, _Allocator>::resize(size_type __
 
 template <class _Allocator>
 _LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<bool, _Allocator>::flip() _NOEXCEPT {
-  // do middle whole words
-  size_type __n         = __size_;
+  // Flip each storage word entirely, including the last potentially partial word.
+  // The unused bits in the last word are safe to flip as they won't be accessed.
   __storage_pointer __p = __begin_;
-  for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
+  for (size_type __n = __external_cap_to_internal(size()); __n; ++__p, --__n)
----------------
philnik777 wrote:

```suggestion
  for (size_type __n = __external_cap_to_internal(size()); __n != 0; ++__p, --__n)
```

https://github.com/llvm/llvm-project/pull/119607


More information about the libcxx-commits mailing list