From fe3d612fe0fe4d3e2c2ba3876d19ec2ec537b2bc Mon Sep 17 00:00:00 2001 From: Daniel James Date: Sun, 4 Oct 2009 10:37:36 +0000 Subject: [PATCH] Clean up some unordered TODOs. [SVN r56570] --- include/boost/unordered/detail/buckets.hpp | 3 +- include/boost/unordered/detail/equivalent.hpp | 1 - include/boost/unordered/detail/table.hpp | 17 +++----- test/unordered/rehash_tests.cpp | 43 +++++++++++++++++++ 4 files changed, 50 insertions(+), 14 deletions(-) diff --git a/include/boost/unordered/detail/buckets.hpp b/include/boost/unordered/detail/buckets.hpp index 8a2163c6..332acf2d 100644 --- a/include/boost/unordered/detail/buckets.hpp +++ b/include/boost/unordered/detail/buckets.hpp @@ -15,7 +15,6 @@ namespace boost { namespace unordered_detail { //////////////////////////////////////////////////////////////////////////// // Buckets - // TODO: Are these needed? template inline BOOST_DEDUCED_TYPENAME hash_buckets::bucket_ptr @@ -32,7 +31,7 @@ namespace boost { namespace unordered_detail { } template - inline std::size_t hash_buckets::bucket_size(std::size_t index) const + std::size_t hash_buckets::bucket_size(std::size_t index) const { if(!buckets_) return 0; bucket_ptr ptr = get_bucket(index)->next_; diff --git a/include/boost/unordered/detail/equivalent.hpp b/include/boost/unordered/detail/equivalent.hpp index fa05019d..5f250d62 100644 --- a/include/boost/unordered/detail/equivalent.hpp +++ b/include/boost/unordered/detail/equivalent.hpp @@ -257,7 +257,6 @@ namespace boost { namespace unordered_detail { // if hash function throws, or inserting > 1 element, basic exception safety // strong otherwise - // TODO: Should I special case an empty container? template template void hash_equivalent_table::insert_range(I i, I j) diff --git a/include/boost/unordered/detail/table.hpp b/include/boost/unordered/detail/table.hpp index 4e7a5811..90c333f8 100644 --- a/include/boost/unordered/detail/table.hpp +++ b/include/boost/unordered/detail/table.hpp @@ -431,23 +431,20 @@ namespace boost { namespace unordered_detail { // if hash function throws, basic exception safety // strong otherwise. - // TODO: Should this always create buckets? + template inline void hash_table::rehash(std::size_t min_buckets) { using namespace std; - if(!this->buckets_) { + if(!this->size_) { + if(this->buckets_) this->delete_buckets(); this->bucket_count_ = next_prime(min_buckets); - this->create_buckets(); - this->init_buckets(); } else { // no throw: - // TODO: Needlessly calling next_prime twice. - min_buckets = (std::max)( - next_prime(min_buckets), - this->min_buckets_for_size(this->size_)); + min_buckets = next_prime((std::max)(min_buckets, + double_to_size_t(floor(this->size_ / (double) mlf_)) + 1)); if(min_buckets != this->bucket_count_) rehash_impl(min_buckets); } } @@ -619,7 +616,6 @@ namespace boost { namespace unordered_detail { template void hash_table::clear() { - // TODO: Is this check needed when called internally? if(!this->size_) return; bucket_ptr end = this->get_bucket(this->bucket_count_); @@ -645,8 +641,7 @@ namespace boost { namespace unordered_detail { } template - std::size_t hash_table - ::erase_key(key_type const& k) + std::size_t hash_table::erase_key(key_type const& k) { if(!this->size_) return 0; diff --git a/test/unordered/rehash_tests.cpp b/test/unordered/rehash_tests.cpp index 64608cb9..0f1d7820 100644 --- a/test/unordered/rehash_tests.cpp +++ b/test/unordered/rehash_tests.cpp @@ -32,6 +32,43 @@ void rehash_empty_test1(X* = 0) BOOST_TEST(postcondition(x, 0)); } +template +void rehash_empty_test2(X* = 0, test::random_generator generator = test::default_generator) +{ + test::random_values v(1000, generator); + test::ordered tracker; + + X x; + + x.rehash(10000); + BOOST_TEST(postcondition(x, 10000)); + + tracker.insert_range(v.begin(), v.end()); + x.insert(v.begin(), v.end()); + tracker.compare(x); + + BOOST_TEST(postcondition(x, 10000)); +} + +template +void rehash_empty_test3(X* = 0, test::random_generator generator = test::default_generator) +{ + test::random_values v(1000, generator); + test::ordered tracker; + + X x; + + x.rehash(0); + BOOST_TEST(postcondition(x, 0)); + + tracker.insert_range(v.begin(), v.end()); + x.insert(v.begin(), v.end()); + tracker.compare(x); + + BOOST_TEST(postcondition(x, 0)); +} + + template void rehash_test1(X* = 0, test::random_generator generator = test::default_generator) { @@ -63,6 +100,12 @@ boost::unordered_multimap* int_multimap_ptr; UNORDERED_TEST(rehash_empty_test1, ((int_set_ptr)(int_multiset_ptr)(int_map_ptr)(int_multimap_ptr)) ) +UNORDERED_TEST(rehash_empty_test2, + ((int_set_ptr)(int_multiset_ptr)(int_map_ptr)(int_multimap_ptr)) +) +UNORDERED_TEST(rehash_empty_test3, + ((int_set_ptr)(int_multiset_ptr)(int_map_ptr)(int_multimap_ptr)) +) UNORDERED_TEST(rehash_test1, ((int_set_ptr)(int_multiset_ptr)(int_map_ptr)(int_multimap_ptr)) )