hash.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright 2015 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <cstddef>
  6. #include <cstring>
  7. #include <utility>
  8. #include <boost/functional/hash.hpp>
  9. #include "common/cityhash.h"
  10. #include "common/common_types.h"
  11. namespace Common {
  12. /**
  13. * Computes a 64-bit hash over the specified block of data
  14. * @param data Block of data to compute hash over
  15. * @param len Length of data (in bytes) to compute hash over
  16. * @returns 64-bit hash value that was computed over the data block
  17. */
  18. static inline u64 ComputeHash64(const void* data, std::size_t len) {
  19. return CityHash64(static_cast<const char*>(data), len);
  20. }
  21. /**
  22. * Computes a 64-bit hash of a struct. In addition to being trivially copyable, it is also critical
  23. * that either the struct includes no padding, or that any padding is initialized to a known value
  24. * by memsetting the struct to 0 before filling it in.
  25. */
  26. template <typename T>
  27. static inline u64 ComputeStructHash64(const T& data) {
  28. static_assert(std::is_trivially_copyable_v<T>,
  29. "Type passed to ComputeStructHash64 must be trivially copyable");
  30. return ComputeHash64(&data, sizeof(data));
  31. }
  32. struct PairHash {
  33. template <class T1, class T2>
  34. std::size_t operator()(const std::pair<T1, T2>& pair) const noexcept {
  35. std::size_t seed = std::hash<T1>()(pair.first);
  36. boost::hash_combine(seed, std::hash<T2>()(pair.second));
  37. return seed;
  38. }
  39. };
  40. } // namespace Common