Skip to content

Instantly share code, notes, and snippets.

@fpopic
Last active January 14, 2017 11:00
Show Gist options
  • Select an option

  • Save fpopic/924e752f6db720081b7cde8c2e3a7d5d to your computer and use it in GitHub Desktop.

Select an option

Save fpopic/924e752f6db720081b7cde8c2e3a7d5d to your computer and use it in GitHub Desktop.
// to enable O(1) lookup for unordered containers
// XOR (^) and SHIFT (<<,>>) operations will make (5,3) != (3,5)
namespace std {
template<typename T, typename U>
class hash<pair<T, U>> {
public :
size_t operator()(const pair<T, U>& p) const {
size_t seed = 0;
seed ^= p.first + 0x9e3779b9 + (seed << 6) + (seed >> 2);
seed ^= p.second + 0x9e3779b9 + (seed << 6) + (seed >> 2);
return seed;
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment