Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does rehashing occur for unordered associative containers?

I found this in the standard as the post-condition for the rehash function in unordered associative containers :

Post: a.bucket_count() > a.size() / a.max_load_factor() and a.bucket_count() >= n. (n being the number of buckets in the container)

Can I take the above to mean that an automatic rehashing is triggered when either of the above conditions is met for all implementations? Or, are implementations free to decide when to rehash and the above only pertains to the rehash function?

like image 813
Jesse Good Avatar asked Mar 26 '12 23:03

Jesse Good


1 Answers

The implementation shall keep the load_factor() <= max_load_factor() and load_factor() == size() / bucket_count(). So automatic rehashing can occur during an insert to keep the load factor invariant.

Although the load_factor() can not exceed max_load_factor(), I don't think there is a guarantee that no rehashing will be done during an insert even if you can prove that this invariant will not be violated.

Definition for max_load_factor is:

Returns a positive number that the container attempts to keep the load factor less than or equal to. The container automatically increases the number of buckets as necessary to keep the load factor below this number.

like image 156
Howard Hinnant Avatar answered Sep 24 '22 00:09

Howard Hinnant