Home > database >  Tell std::unordered_map that the hash function is perfect
Tell std::unordered_map that the hash function is perfect

Time:10-19

Would it be possible to tell std::unordered_map that the hash function is perfect?

Thus, if two different keys yield the same hash code, they edit the same slot of memory.

CodePudding user response:

Hash function being perfect is not enough. It is possible that values that generate different hash-values are mapped to the same slot in the hash table. For example, the size of container might be 11, one of your hash values can be 12 and the other can be 23. Assuming the container uses modulus operator to map these values, they would both be mapped to to slot number 1. So, you need a way to differentiate these later on.

CodePudding user response:

Yeah, just make the operator== yield a comparison with the hashes instead of the attributes and std::unordered_map will assume they are equal.

CodePudding user response:

You might get away with setting KeyEqual template paratemer of std::unordered_map to [](const auto& l,const auto& r){return true;} as it should only be used when hashes compare equal.

  • Related