Utilities - Hash combining helper

This commit is contained in:
Emmanuel BENOîT 2020-09-26 10:37:13 +02:00
parent 90cf63c592
commit 3889efde67
3 changed files with 35 additions and 0 deletions
include/ebcl

View file

@ -466,6 +466,11 @@ struct T_HashFunction
template< typename T >
uint32_t ComputeHash( T const& item );
// CombineHash( &hv , T ) - Helper that consolidates multiple hashes into a
// single value.
template< typename T >
void CombineHash( uint32_t& hashValue , T const& item );
// Helper macros to declare and define specialised hash functions.
#define M_DECLARE_HASH( Type ) \
template< > \

View file

@ -315,6 +315,12 @@ inline uint32_t ComputeHash( T const& item )
return T_HashFunction< T >::hash( item );
}
template< typename T >
inline void CombineHash( uint32_t& hashValue , T const& item )
{
hashValue = ComputeHash( item ) + 0x9e3779b9 + ( hashValue << 6 ) + ( hashValue >> 2 );
}
/*----------------------------------------------------------------------------*/
// Hash function specializations for data sizes 1, 2, 4 and 8.
@ -358,6 +364,18 @@ struct T_HashFunction< T , 8 >
}
};
/*----------------------------------------------------------------------------*/
// Hash functions for pairs and tuples
template< typename T1 , typename T2 >
inline uint32_t ComputeHash( std::pair< T1 , T2 > const& item )
{
uint32_t hv{ 0 };
CombineHash( hv , item.first );
CombineHash( hv , item.second );
return hv;
}
/*= PRIVATE IMPLEMENTATION BASE ==============================================*/