From c2e7221bf9c93643b6e16c2c605ad365ba69020e Mon Sep 17 00:00:00 2001 From: Daniel James Date: Mon, 17 Sep 2012 18:57:58 +0000 Subject: [PATCH] Unordered: Set `max_load_` to 0 when there are no buckets. [SVN r80559] --- include/boost/unordered/detail/table.hpp | 17 ++++++++++------- include/boost/unordered/detail/unique.hpp | 22 +++------------------- 2 files changed, 13 insertions(+), 26 deletions(-) diff --git a/include/boost/unordered/detail/table.hpp b/include/boost/unordered/detail/table.hpp index 70a46572..5ce391d6 100644 --- a/include/boost/unordered/detail/table.hpp +++ b/include/boost/unordered/detail/table.hpp @@ -187,7 +187,7 @@ namespace boost { namespace unordered { namespace detail { std::size_t bucket_count_; std::size_t size_; float mlf_; - std::size_t max_load_; // Only use if buckets_. + std::size_t max_load_; bucket_pointer buckets_; //////////////////////////////////////////////////////////////////////// @@ -292,10 +292,10 @@ namespace boost { namespace unordered { namespace detail { // From 6.3.1/13: // Only resize when size >= mlf_ * count - max_load_ = boost::unordered::detail::double_to_size(ceil( + max_load_ = buckets_ ? boost::unordered::detail::double_to_size(ceil( static_cast(mlf_) * static_cast(bucket_count_) - )); + )) : 0; } @@ -303,7 +303,7 @@ namespace boost { namespace unordered { namespace detail { { BOOST_ASSERT(z > 0); mlf_ = (std::max)(z, minimum_max_load_factor); - if (buckets_) recalculate_max_load(); + recalculate_max_load(); } std::size_t min_buckets_for_size(std::size_t size) const @@ -362,6 +362,7 @@ namespace boost { namespace unordered { namespace detail { { x.buckets_ = bucket_pointer(); x.size_ = 0; + x.max_load_ = 0; } table(table& x, node_allocator const& a, @@ -487,6 +488,7 @@ namespace boost { namespace unordered { namespace detail { size_ = other.size_; other.buckets_ = bucket_pointer(); other.size_ = 0; + other.max_load_ = 0; } //////////////////////////////////////////////////////////////////////// @@ -536,6 +538,7 @@ namespace boost { namespace unordered { namespace detail { destroy_buckets(); buckets_ = bucket_pointer(); + max_load_ = 0; } BOOST_ASSERT(!size_); @@ -680,7 +683,7 @@ namespace boost { namespace unordered { namespace detail { if (!size_ && !x.size_) return; - if (!buckets_ || x.size_ >= max_load_) { + if (x.size_ >= max_load_) { create_buckets(min_buckets_for_size(x.size_)); } else { @@ -760,7 +763,7 @@ namespace boost { namespace unordered { namespace detail { if (!size_ && !x.size_) return; - if (!buckets_ || x.size_ >= max_load_) { + if (x.size_ >= max_load_) { create_buckets(min_buckets_for_size(x.size_)); } else { @@ -784,9 +787,9 @@ namespace boost { namespace unordered { namespace detail { boost::unordered::detail::set_hash_functions new_func_this(*this, x); // No throw from here. - move_buckets_from(x); mlf_ = x.mlf_; max_load_ = x.max_load_; + move_buckets_from(x); new_func_this.commit(); } diff --git a/include/boost/unordered/detail/unique.hpp b/include/boost/unordered/detail/unique.hpp index bd3c63cd..59e44663 100644 --- a/include/boost/unordered/detail/unique.hpp +++ b/include/boost/unordered/detail/unique.hpp @@ -466,14 +466,9 @@ namespace boost { namespace unordered { namespace detail { { node_constructor a(this->node_alloc()); - // Special case for empty buckets so that we can use - // max_load_ (which isn't valid when buckets_ is null). - if (!this->buckets_) { - insert_range_empty(a, k, i, j); - if (++i == j) return; - } + insert_range_impl2(a, k, i, j); - do { + while(++i != j) { // Note: can't use get_key as '*i' might not be value_type - it // could be a pair with first_types as key_type without const or // a different second_type. @@ -483,18 +478,7 @@ namespace boost { namespace unordered { namespace detail { // be less efficient if copying the full value_type is // expensive. insert_range_impl2(a, extractor::extract(*i), i, j); - } while(++i != j); - } - - template - void insert_range_empty(node_constructor& a, key_type const& k, - InputIt i, InputIt j) - { - std::size_t key_hash = this->hash(k); - a.construct_with_value2(*i); - this->reserve_for_insert(this->size_ + - boost::unordered::detail::insert_size(i, j)); - this->add_node(a, key_hash); + } } template