Utilities - Tuple hash computations

This commit is contained in:
Emmanuel BENOîT 2020-09-26 11:14:14 +02:00
parent 3889efde67
commit 8a19374d3f
2 changed files with 77 additions and 3 deletions
include/ebcl/inline

View file

@ -376,6 +376,42 @@ inline uint32_t ComputeHash( std::pair< T1 , T2 > const& item )
return hv;
}
// Recursive template that will hash each tuple element starting from
// the last one.
template< typename Tuple , size_t Index = std::tuple_size< Tuple >::value - 1 >
struct T_TupleHashHelper_
{
static void apply( uint32_t& hashValue , Tuple const& t )
{
T_TupleHashHelper_< Tuple , Index - 1 >::apply( hashValue , t );
CombineHash( hashValue , std::get< Index >( t ) );
}
};
// Specialize for the first element, as it must not recurse.
template< typename Tuple >
struct T_TupleHashHelper_< Tuple , 0 >
{
static void apply( uint32_t& hashValue , Tuple const& t )
{
CombineHash( hashValue , std::get< 0 >( t ) );
}
};
template< typename... Types >
inline uint32_t ComputeHash( std::tuple< Types... > const& item )
{
uint32_t hv = 0;
T_TupleHashHelper_< std::tuple< Types... > >::apply( hv , item );
return hv;
}
// Specialize the function for empty tuples
template< >
inline uint32_t ComputeHash( std::tuple< > const& )
{
return 0;
}
/*= PRIVATE IMPLEMENTATION BASE ==============================================*/