Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why don't unordered containers provide an interface for defining minimum load factor?

Tags:

c++

c++11

I was trying to understand why hash table (unordered containers such as unordered_map or unordered_set) don't provide an interface for querying or setting minimum load factor.

Say c is a unordered_set, I can use

c.max_load_factor()

for querying

and

c.max_load_factor(val)

for setting.

Why doesn't C++11 provide an interface for querying min_load_factor? Are there implementation details, which would explain?

Also, C++ STL by Josuttis, mentions that:

The minimum load factor, which is used to force rehashing when the number of elements in the container shrinks cannot be influenced.

like image 675
Lokesh A. R. Avatar asked Aug 16 '13 14:08

Lokesh A. R.


1 Answers

The load factor on an unordered_map affects the probability of collision in the hash table. For example,the probability of two elements being located in the same bucket. The container uses the value of max_load_factor as the threshold that forces an increase in the number of buckets and thus causing a rehash.

There is no such thing as a user-controllable minimum load factor since it should respect the number of elements already in the container.

like image 172
Sergey K. Avatar answered Nov 02 '22 18:11

Sergey K.