c++ - How to properly hash a pair of pointers? -
this question has answer here:
i have std::unordered_map
keys of type std::pair<t*, t*>
(i.e., pair of pointers).
the code base has following hash functor defined:
struct pair_hash { template<typename t> std::size_t operator()(std::pair<t, t> const &p) { return (std::hash<t>()(p.first) + 0x9e3779b9) ^ (std::hash<t>()(p.second) + 0x9e3779b9); } };
and used as:
std::unordered_map<std::pair<t*, t*>, u, pair_hash> mydictionary;
where u
arbitrary object.
now hash functor displayed above must have issues because in vc++ gives me out of bounds crash in std::unordered_map::find
.
q:
- could hash functor problematic or behaviour attributed vc++ itself?
- is there "proper/more proper" way hash pair of pointers?
return (std::hash<t>()(p.first) + 0x9e3779b9) ^ (std::hash<t>()(p.first) + 0x9e3779b9); // ^^^^^ // p.second! // ^^^^^^^^^^ // choose different magic constant prevent collisions between (p1, p2) , (p2, p1)
your function returning 0 every pair of pointers. therefore getting lots of collisions in unordered_map
.
Comments
Post a Comment