Skip to content

Instantly share code, notes, and snippets.

@amorphobia
Created May 29, 2016 13:22
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 amorphobia/637e07b0d325c1396db17d741d7bd959 to your computer and use it in GitHub Desktop.
Save amorphobia/637e07b0d325c1396db17d741d7bd959 to your computer and use it in GitHub Desktop.
Copied from hash_range in Boost.
#ifndef HASH_EXTENDED_H
#define HASH_EXTENDED_H
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
namespace std {
template<typename T> struct hash<vector<T>> {
inline size_t operator() (const vector<T>& vec) const {
hash<T> hasher;
size_t seed = 0;
for (auto& i : vec) {
seed ^= hasher(i) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
return seed;
}
};
template<typename T> struct hash<unordered_multiset<T>> {
inline size_t operator() (const unordered_multiset<T>& uset) const {
hash<T> hasher;
vector<size_t> hashes;
for (auto& i : uset) {
hashes.push_back(hasher(i));
}
sort(hashes.begin(), hashes.end());
hash<vector<size_t>> h;
return h(hashes);
}
};
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment