39 lines
		
	
	
		
			924 B
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			39 lines
		
	
	
		
			924 B
		
	
	
	
		
			C++
		
	
	
	
	
	
| 
 | |
| 
 | |
| #pragma once
 | |
| 
 | |
| #if !defined(MIJIN_UTIL_HASH_HPP_INCLUDED)
 | |
| #define MIJIN_UTIL_HASH_HPP_INCLUDED 1
 | |
| 
 | |
| #include <functional>
 | |
| #include <tuple>
 | |
| #include <utility>
 | |
| 
 | |
| namespace mijin
 | |
| {
 | |
| template<typename T, typename THasher = std::hash<T>>
 | |
| inline void hashCombine(std::size_t& seed, const T& value, const THasher& hasher = {})
 | |
| {
 | |
|     seed ^= hasher(value) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
 | |
| }
 | |
| }
 | |
| 
 | |
| template<typename... T>
 | |
| struct std::hash<std::tuple<T...>>
 | |
| {
 | |
|     std::size_t operator()(const std::tuple<T...>& tuple) const noexcept
 | |
|     {
 | |
|         return hashImpl(tuple, std::index_sequence_for<T...>());
 | |
|     }
 | |
| 
 | |
|     template<std::size_t... indices>
 | |
|     std::size_t hashImpl(const std::tuple<T...>& tuple, std::index_sequence<indices...>) const noexcept
 | |
|     {
 | |
|         std::size_t result = 0;
 | |
|         (mijin::hashCombine(result, std::get<indices>(tuple)), ...);
 | |
|         return result;
 | |
|     }
 | |
| };
 | |
| 
 | |
| #endif // MIJIN_UTIL_HASH_HPP_INCLUDED
 |