Skip to content

Commit

Permalink
Fix no-op shrink_to_fit in vector<bool>
Browse files Browse the repository at this point in the history
  • Loading branch information
winner245 committed Jan 10, 2025
1 parent a6aa936 commit a3d32ec
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
6 changes: 4 additions & 2 deletions libcxx/include/__vector/vector_bool.h
Original file line number Diff line number Diff line change
Expand Up @@ -859,11 +859,13 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<bool, _Allocator>::reserve(size_type _

template <class _Allocator>
_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT {
if (__external_cap_to_internal(size()) > __cap_) {
if (__external_cap_to_internal(size()) < __cap_) {
#if _LIBCPP_HAS_EXCEPTIONS
try {
#endif // _LIBCPP_HAS_EXCEPTIONS
vector(*this, allocator_type(__alloc_)).swap(*this);
vector __v(*this, allocator_type(__alloc_));
if (__v.__cap_ < __cap_)
__v.swap(*this);
#if _LIBCPP_HAS_EXCEPTIONS
} catch (...) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

// void shrink_to_fit();

// XFAIL: FROZEN-CXX03-HEADERS-FIXME

#include <cassert>
#include <climits>
#include <vector>

#include "increasing_allocator.h"
Expand All @@ -20,21 +23,48 @@

TEST_CONSTEXPR_CXX20 bool tests() {
{
std::vector<bool> v(100);
using C = std::vector<bool>;
C v(100);
v.push_back(1);
v.clear();
v.shrink_to_fit();
assert(v.capacity() >= 101);
assert(v.size() >= 101);
assert(v.capacity() == 0);
assert(v.size() == 0);
}
#if TEST_STD_VER >= 11
{
std::vector<bool, min_allocator<bool>> v(100);
using C = std::vector<bool, min_allocator<bool> >;
C v(100);
v.push_back(1);
C::size_type before_cap = v.capacity();
v.shrink_to_fit();
assert(v.capacity() >= 101);
assert(v.size() >= 101);
assert(v.capacity() <= before_cap);
assert(v.size() == 101);
}
{
using C = std::vector<bool>;
unsigned bits_per_word = static_cast<unsigned>(sizeof(C::__storage_type) * CHAR_BIT);
C v(bits_per_word);
v.push_back(1);
assert(v.capacity() == bits_per_word * 2);
assert(v.size() == bits_per_word + 1);
v.pop_back();
v.shrink_to_fit();
assert(v.capacity() == bits_per_word);
assert(v.size() == bits_per_word);
}
{
using C = std::vector<bool>;
unsigned bits_per_word = static_cast<unsigned>(sizeof(C::__storage_type) * CHAR_BIT);
C v;
v.reserve(bits_per_word * 2);
v.push_back(1);
assert(v.capacity() == bits_per_word * 2);
assert(v.size() == 1);
v.shrink_to_fit();
assert(v.capacity() == bits_per_word);
assert(v.size() == 1);
}
#endif

return true;
}
Expand Down

0 comments on commit a3d32ec

Please sign in to comment.