Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <tuple>
  3. #include <unordered_set>
  4.  
  5. using namespace std;
  6.  
  7. template <class T>
  8. inline size_t hash_combine(std::size_t seed, T const& v) {
  9. return seed ^ std::hash<T>()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
  10. }
  11. namespace std {
  12. template <typename... Ts>
  13. class hash<tuple<Ts...>> {
  14. public:
  15. size_t operator()(const tuple<Ts...>& t) const { return impl<0>(t); }
  16. template <size_t Index>
  17. typename enable_if<Index == tuple_size<tuple<Ts...>>::value, size_t>::type
  18. impl(const tuple<Ts...>& t) const {
  19. return 0;
  20. }
  21. template <size_t Index>
  22. typename enable_if<Index != tuple_size<tuple<Ts...>>::value, size_t>::type
  23. impl(const tuple<Ts...>& t) const {
  24. return hash_combine(impl<Index + 1>(t), get<Index>(t));
  25. }
  26. };
  27. }
  28.  
  29.  
  30. int main() {
  31. auto t = make_tuple(1, 2.0, string("abc"));
  32. cout << hash<decltype(t)>()(t) << endl;
  33. cout << hash_combine(hash_combine(hash_combine(0, string("abc")), 2.0), 1) << endl;
  34. return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement