Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to identify whether or not std::unordered_map has experienced hash collisions?

How to identify whether or not the keys in a std::unordered_map have experienced hash collisions?

That is, how to identify if any collision chaining is present?

like image 505
Greg Avatar asked Sep 10 '17 06:09

Greg


People also ask

Is std :: unordered_map a hash table?

Unordered_map IS a hash table actually. You may want to use a better example as, as is the second insert will fail since it has the same key.

Is unordered_map a hash map?

Internally unordered_map is implemented using Hash Table, the key provided to map is hashed into indices of a hash table which is why the performance of data structure depends on the hash function a lot but on average, the cost of search, insert, and delete from the hash table is O(1).

What is the difference between std :: map and std :: unordered_map?

map is used to store elements as key,value pairs in sorted order. unordered_map is used to store elements as key,value pairs in non-sorted order.

Does unordered_map use chaining?

Summary: all practical implementations of std::unordered_set (or unordered_map ) undoubtedly use collision chaining.


1 Answers

You can use the bucket interface and its bucket_size method.

std::unordered_map<int, int> map;
bool has_collision = false;

for(size_t bucket = 0; bucket < map.bucket_count(); bucket++) {
    if(map.bucket_size(bucket) > 1) {
        has_collision = true;
        break;
    }
}
like image 192
Aerle Avatar answered Sep 22 '22 10:09

Aerle