std::unordered_map<> bucket_count() after default rehash

Viewed 148

If i keep adding values to an unordered_map then every time the number of elements exceeds the bucket_count() (assuming max_load_factor = 1) rehashing takes place.

What I am very confused about is the bucket size after rehashing.

#include <iostream>
#include <unordered_map>
int main() {
   std::unordered_map<size_t, size_t> mp;

   for (size_t i = 0; i < 1000; ++i) {
      mp[i] = i;
      std::cout << " count: " << mp.bucket_count() << std::endl;
   }
}

This outputs 3 7 17 37 79 167 337 709 1493

I have noticed that the bucket size is prime and approximately doubles. However it is not the closest prime to the next power of 2 either.

What is the methodology behind this bucket size increase. I was surprised or stupid enough that I couldn't find anything about it in standard references such as cplusplus.com

1 Answers
Related