Skip to content

Commit e20413c

Browse files
authored
[libc++][NFC] Refactor __do_rehash a bit (#151543)
This refactors `__hash_table::__do_rehash` to use early returns and renames some of the variables.
1 parent 3e2fadf commit e20413c

File tree

1 file changed

+38
-34
lines changed

1 file changed

+38
-34
lines changed

libcxx/include/__hash_table

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,41 +1709,45 @@ void __hash_table<_Tp, _Hash, _Equal, _Alloc>::__rehash(size_type __n) _LIBCPP_D
17091709

17101710
template <class _Tp, class _Hash, class _Equal, class _Alloc>
17111711
template <bool _UniqueKeys>
1712-
void __hash_table<_Tp, _Hash, _Equal, _Alloc>::__do_rehash(size_type __nbc) {
1713-
__pointer_allocator& __npa = __bucket_list_.get_deleter().__alloc();
1714-
__bucket_list_.reset(__nbc > 0 ? __pointer_alloc_traits::allocate(__npa, __nbc) : nullptr);
1715-
__bucket_list_.get_deleter().size() = __nbc;
1716-
if (__nbc > 0) {
1717-
for (size_type __i = 0; __i < __nbc; ++__i)
1718-
__bucket_list_[__i] = nullptr;
1719-
__next_pointer __pp = __first_node_.__ptr();
1720-
__next_pointer __cp = __pp->__next_;
1721-
if (__cp != nullptr) {
1722-
size_type __chash = std::__constrain_hash(__cp->__hash(), __nbc);
1723-
__bucket_list_[__chash] = __pp;
1724-
size_type __phash = __chash;
1725-
for (__pp = __cp, void(), __cp = __cp->__next_; __cp != nullptr; __cp = __pp->__next_) {
1726-
__chash = std::__constrain_hash(__cp->__hash(), __nbc);
1727-
if (__chash == __phash)
1728-
__pp = __cp;
1729-
else {
1730-
if (__bucket_list_[__chash] == nullptr) {
1731-
__bucket_list_[__chash] = __pp;
1732-
__pp = __cp;
1733-
__phash = __chash;
1734-
} else {
1735-
__next_pointer __np = __cp;
1736-
if _LIBCPP_CONSTEXPR_SINCE_CXX17 (!_UniqueKeys) {
1737-
for (; __np->__next_ != nullptr &&
1738-
key_eq()(__cp->__upcast()->__get_value(), __np->__next_->__upcast()->__get_value());
1739-
__np = __np->__next_)
1740-
;
1741-
}
1742-
__pp->__next_ = __np->__next_;
1743-
__np->__next_ = __bucket_list_[__chash]->__next_;
1744-
__bucket_list_[__chash]->__next_ = __cp;
1745-
}
1712+
void __hash_table<_Tp, _Hash, _Equal, _Alloc>::__do_rehash(size_type __bucket_count) {
1713+
__pointer_allocator& __ptr_alloc = __bucket_list_.get_deleter().__alloc();
1714+
__bucket_list_.reset(__bucket_count > 0 ? __pointer_alloc_traits::allocate(__ptr_alloc, __bucket_count) : nullptr);
1715+
__bucket_list_.get_deleter().size() = __bucket_count;
1716+
1717+
if (__bucket_count == 0)
1718+
return;
1719+
1720+
for (size_type __i = 0; __i < __bucket_count; ++__i)
1721+
__bucket_list_[__i] = nullptr;
1722+
__next_pointer __pp = __first_node_.__ptr();
1723+
__next_pointer __cp = __pp->__next_;
1724+
1725+
if (!__cp)
1726+
return;
1727+
1728+
size_type __chash = std::__constrain_hash(__cp->__hash(), __bucket_count);
1729+
__bucket_list_[__chash] = __pp;
1730+
size_type __phash = __chash;
1731+
for (__pp = __cp, void(), __cp = __cp->__next_; __cp != nullptr; __cp = __pp->__next_) {
1732+
__chash = std::__constrain_hash(__cp->__hash(), __bucket_count);
1733+
if (__chash == __phash)
1734+
__pp = __cp;
1735+
else {
1736+
if (__bucket_list_[__chash] == nullptr) {
1737+
__bucket_list_[__chash] = __pp;
1738+
__pp = __cp;
1739+
__phash = __chash;
1740+
} else {
1741+
__next_pointer __np = __cp;
1742+
if _LIBCPP_CONSTEXPR (!_UniqueKeys) {
1743+
for (; __np->__next_ != nullptr &&
1744+
key_eq()(__cp->__upcast()->__get_value(), __np->__next_->__upcast()->__get_value());
1745+
__np = __np->__next_)
1746+
;
17461747
}
1748+
__pp->__next_ = __np->__next_;
1749+
__np->__next_ = __bucket_list_[__chash]->__next_;
1750+
__bucket_list_[__chash]->__next_ = __cp;
17471751
}
17481752
}
17491753
}

0 commit comments

Comments
 (0)