Skip to content

Instantly share code, notes, and snippets.

@davidalbertonogueira
Last active November 8, 2016 13:08
Show Gist options
  • Save davidalbertonogueira/9a8fc9934aa52ec4dcbd4462ec05d20c to your computer and use it in GitHub Desktop.
Save davidalbertonogueira/9a8fc9934aa52ec4dcbd4462ec05d20c to your computer and use it in GitHub Desktop.
Hashing utils
#include <stdint.h>
#include <functional>
template <typename TFirst, typename TSecond>
struct HashablePair : public std::pair<TFirst, TSecond> {
typedef std::pair<TFirst, TSecond> Base;
public:
HashablePair() : Base() {};
HashablePair(const Base& a) : Base(a) {};
HashablePair(Base&& a) : Base(std::move(a)) {};
size_t operator()(const HashablePair& p) const {
size_t seed = std::hash<TFirst>()(p.first);
HashCombine<size_t>(std::hash<TSecond>()(p.second), &seed);
return seed;
};
bool operator()(const HashablePair &lhs, const HashablePair &rhs) const {
return lhs.first == rhs.first && lhs.second == rhs.second;
};
private:
template <typename TSeed>
inline void HashCombine(TSeed value, TSeed *seed) const {
*seed ^= value + 0x9e3779b9 + (*seed << 6) + (*seed >> 2);
};
};
template<class THash>
typename THash::iterator IncrementHashTableCount(const typename THash::key_type &key,
THash *hash) {
static_assert(std::is_integral<typename THash::mapped_type>::value,
"Only value integral types are support");
auto it = hash->find(key);
if (it == hash->end())
it = hash->emplace(key, 1).first;
else
++(it->second);
return it;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment