[libc++][NFC] Refactor __do_rehash a bit (#151543)

This refactors `__hash_table::__do_rehash` to use early returns and
renames some of the variables.
This commit is contained in:
Nikolas Klauser 2025-08-01 08:15:09 +02:00 committed by GitHub
parent 3e2fadf3be
commit e20413c045
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1709,41 +1709,45 @@ void __hash_table<_Tp, _Hash, _Equal, _Alloc>::__rehash(size_type __n) _LIBCPP_D
template <class _Tp, class _Hash, class _Equal, class _Alloc> template <class _Tp, class _Hash, class _Equal, class _Alloc>
template <bool _UniqueKeys> template <bool _UniqueKeys>
void __hash_table<_Tp, _Hash, _Equal, _Alloc>::__do_rehash(size_type __nbc) { void __hash_table<_Tp, _Hash, _Equal, _Alloc>::__do_rehash(size_type __bucket_count) {
__pointer_allocator& __npa = __bucket_list_.get_deleter().__alloc(); __pointer_allocator& __ptr_alloc = __bucket_list_.get_deleter().__alloc();
__bucket_list_.reset(__nbc > 0 ? __pointer_alloc_traits::allocate(__npa, __nbc) : nullptr); __bucket_list_.reset(__bucket_count > 0 ? __pointer_alloc_traits::allocate(__ptr_alloc, __bucket_count) : nullptr);
__bucket_list_.get_deleter().size() = __nbc; __bucket_list_.get_deleter().size() = __bucket_count;
if (__nbc > 0) {
for (size_type __i = 0; __i < __nbc; ++__i) if (__bucket_count == 0)
__bucket_list_[__i] = nullptr; return;
__next_pointer __pp = __first_node_.__ptr();
__next_pointer __cp = __pp->__next_; for (size_type __i = 0; __i < __bucket_count; ++__i)
if (__cp != nullptr) { __bucket_list_[__i] = nullptr;
size_type __chash = std::__constrain_hash(__cp->__hash(), __nbc); __next_pointer __pp = __first_node_.__ptr();
__bucket_list_[__chash] = __pp; __next_pointer __cp = __pp->__next_;
size_type __phash = __chash;
for (__pp = __cp, void(), __cp = __cp->__next_; __cp != nullptr; __cp = __pp->__next_) { if (!__cp)
__chash = std::__constrain_hash(__cp->__hash(), __nbc); return;
if (__chash == __phash)
__pp = __cp; size_type __chash = std::__constrain_hash(__cp->__hash(), __bucket_count);
else { __bucket_list_[__chash] = __pp;
if (__bucket_list_[__chash] == nullptr) { size_type __phash = __chash;
__bucket_list_[__chash] = __pp; for (__pp = __cp, void(), __cp = __cp->__next_; __cp != nullptr; __cp = __pp->__next_) {
__pp = __cp; __chash = std::__constrain_hash(__cp->__hash(), __bucket_count);
__phash = __chash; if (__chash == __phash)
} else { __pp = __cp;
__next_pointer __np = __cp; else {
if _LIBCPP_CONSTEXPR_SINCE_CXX17 (!_UniqueKeys) { if (__bucket_list_[__chash] == nullptr) {
for (; __np->__next_ != nullptr && __bucket_list_[__chash] = __pp;
key_eq()(__cp->__upcast()->__get_value(), __np->__next_->__upcast()->__get_value()); __pp = __cp;
__np = __np->__next_) __phash = __chash;
; } else {
} __next_pointer __np = __cp;
__pp->__next_ = __np->__next_; if _LIBCPP_CONSTEXPR (!_UniqueKeys) {
__np->__next_ = __bucket_list_[__chash]->__next_; for (; __np->__next_ != nullptr &&
__bucket_list_[__chash]->__next_ = __cp; key_eq()(__cp->__upcast()->__get_value(), __np->__next_->__upcast()->__get_value());
} __np = __np->__next_)
;
} }
__pp->__next_ = __np->__next_;
__np->__next_ = __bucket_list_[__chash]->__next_;
__bucket_list_[__chash]->__next_ = __cp;
} }
} }
} }