Pull some common code into a function.

This commit is contained in:
Daniel James
2016-08-14 20:55:40 +01:00
parent 88612a8be4
commit 078c562b6c

View File

@ -347,21 +347,21 @@ namespace boost { namespace unordered { namespace detail {
return iterator(n); return iterator(n);
} }
inline iterator resize_and_add_node(node_pointer n, std::size_t key_hash)
{
node_tmp b(n, this->node_alloc());
this->reserve_for_insert(this->size_ + 1);
return this->add_node(b, key_hash);
}
value_type& operator[](key_type const& k) value_type& operator[](key_type const& k)
{ {
std::size_t key_hash = this->hash(k); std::size_t key_hash = this->hash(k);
iterator pos = this->find_node(key_hash, k); iterator pos = this->find_node(key_hash, k);
if (pos.node_) return *pos; if (pos.node_) return *pos;
return *this->resize_and_add_node(
// Create the node before rehashing in case it throws an
// exception (need strong safety in such a case).
node_tmp b(
boost::unordered::detail::func::construct_pair(this->node_alloc(), k), boost::unordered::detail::func::construct_pair(this->node_alloc(), k),
this->node_alloc()); key_hash);
this->reserve_for_insert(this->size_ + 1);
return *add_node(b, key_hash);
} }
#if defined(BOOST_NO_CXX11_RVALUE_REFERENCES) #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
@ -411,20 +411,12 @@ namespace boost { namespace unordered { namespace detail {
{ {
std::size_t key_hash = this->hash(k); std::size_t key_hash = this->hash(k);
iterator pos = this->find_node(key_hash, k); iterator pos = this->find_node(key_hash, k);
if (pos.node_) return emplace_return(pos, false); if (pos.node_) return emplace_return(pos, false);
pos = this->resize_and_add_node(
// Create the node before rehashing in case it throws an
// exception (need strong safety in such a case).
node_tmp b(
boost::unordered::detail::func::construct_value_generic( boost::unordered::detail::func::construct_value_generic(
this->node_alloc(), BOOST_UNORDERED_EMPLACE_FORWARD), this->node_alloc(), BOOST_UNORDERED_EMPLACE_FORWARD),
this->node_alloc()); key_hash);
return emplace_return(pos, true);
// reserve has basic exception safety if the hash function
// throws, strong otherwise.
this->reserve_for_insert(this->size_ + 1);
return emplace_return(this->add_node(b, key_hash), true);
} }
template <BOOST_UNORDERED_EMPLACE_TEMPLATE> template <BOOST_UNORDERED_EMPLACE_TEMPLATE>