Skip to content

Instantly share code, notes, and snippets.

@reddragon
Created December 22, 2016 03:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reddragon/e6363719eba507cedd10a475915b8317 to your computer and use it in GitHub Desktop.
Save reddragon/e6363719eba507cedd10a475915b8317 to your computer and use it in GitHub Desktop.
Hash Function for unordered_map
struct Slope {
int n, d;
Slope(int nn, int dd) {
int g = gcd(abs(nn), abs(dd));
// cout << "gcd of " << nn << ", " << dd << " is: " << g << endl;
n = (g > 0 ? nn/g : nn);
d = (g > 0 ? dd/g : dd);
}
};
struct Cmp {
size_t operator()(const Slope& k) const {
return (k.n<<3)^k.d;
}
size_t operator()(const Slope& k1, const Slope& k2) const {
return k1.n == k2.n && k1.d == k2.d;
}
};
vector<unordered_map<Slope, vector<int>, Cmp, Cmp>> v;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment