OSDN Git Service

2012-07-26 Fran├žois Dumont <fdumont@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / hashtable.h
index af645a6..929f0bb 100644 (file)
@@ -760,11 +760,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        _M_element_count(0),
        _M_rehash_policy()
       {
-       _M_bucket_count = std::max(_M_rehash_policy._M_next_bkt(__bucket_hint),
-                                  _M_rehash_policy.
-                                  _M_bkt_for_elements(__detail::
-                                                      __distance_fw(__f,
-                                                                    __l)));
+       _M_bucket_count =
+         _M_rehash_policy._M_bkt_for_elements(__detail::__distance_fw(__f,
+                                                                      __l));
+       if (_M_bucket_count <= __bucket_hint)
+         _M_bucket_count = _M_rehash_policy._M_next_bkt(__bucket_hint);
+
         // We don't want the rehash policy to ask for the hashtable to shrink
         // on the first insertion so we need to reset its previous resize
        // level.
@@ -1582,10 +1583,20 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     rehash(size_type __n)
     {
       const _RehashPolicyState& __saved_state = _M_rehash_policy._M_state();
-      _M_rehash(std::max(_M_rehash_policy._M_next_bkt(__n),
-                        _M_rehash_policy._M_bkt_for_elements(_M_element_count
-                                                             + 1)),
-               __saved_state);
+      std::size_t __buckets
+       = _M_rehash_policy._M_bkt_for_elements(_M_element_count + 1);
+      if (__buckets <= __n)
+       __buckets = _M_rehash_policy._M_next_bkt(__n);
+
+      if (__buckets != _M_bucket_count)
+       {
+         _M_rehash(__buckets, __saved_state);
+         
+         // We don't want the rehash policy to ask for the hashtable to shrink
+         // on the next insertion so we need to reset its previous resize
+         // level.
+         _M_rehash_policy._M_prev_resize = 0;
+       }
     }
 
   template<typename _Key, typename _Value,