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?
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?
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;
}
}
if (map.bucket_count() < map.size())
can be done to quickly check if there are any collisions to avoid the for loop in many cases –
Gibeon © 2022 - 2024 — McMap. All rights reserved.