views:

476

answers:

2

I am trying to use boost::unordered_map to cache some values. I try to specify minimum number of buckets in the constructor:

#include <boost/unordered_map.hpp>
typedef boost::unordered_map<float, float> Mycache;
Mycache cache((std::size_t)25165843,
              boost::hash<float>(),
              std::equal_to<float>(),
              std::allocator<std::pair<float const, float> >());

But when I display information about my unordered_map at the end of program:

g++:

unordered_map.size(): 15861612
unordered_map.load_factor: 10.0845
unordered_map.bucket_count: 1572869
unordered_map.max_size: 1572868
unordered_map.max_load_factor: 1
unordered_map.max_bucket_count: 1572869

vc++:

unordered_map.size(): 13916119
unordered_map.load_factor: 8.8476
unordered_map.bucket_count: 1572869
unordered_map.max_size: 1572868
unordered_map.max_load_factor: 1
unordered_map.max_bucket_count: 1572869

How do I specify the minimum number of buckets ?

+1  A: 

boost::unordered_map::max_bucket_count() returns the implementation-dependent limit on the bucket count of an unordered map. You appear to have exceeded this limit with your constructor parameter. Note that while MSDN defines this to be the max buckets "currently" permitted (whatever that means), the C++0x spec defines it to be the most buckets the map can ever have.

I've never used the class, and I can't see anything in the draft C++0x spec to explain why the constructor is silently creating an object which doesn't do what you told it to.

I also don't know what the motivation might be behind the value 1572869, other than that it's a largeish prime.

Steve Jessop
+2  A: 

The other answer is correct about the standard, but the small max_bucket_count is actually a bug in Boost 1.38, any other version will let you use more buckets.

Daniel James