Reduce chance for wxPoint hash collision

Hash collisions reduce the speed of unordered maps by increasing the
comparison time of underlying values.  This implements the inverse
golden ration offset into the x value to ensure that consecutive points
are far from each other in the map.
This commit is contained in:
Seth Hillbrand 2019-04-09 20:40:04 -07:00
parent 541c2fdb29
commit 2048114512
1 changed files with 6 additions and 2 deletions

View File

@ -594,8 +594,12 @@ size_t std::hash<wxString>::operator()( const wxString& s ) const
#ifdef USE_KICAD_WXPOINT_LESS_AND_HASH
size_t std::hash<wxPoint>::operator() ( const wxPoint& k ) const
{
return ( ( std::hash<int>()( k.x )
^ ( std::hash<int>()( k.y ) << 1 ) ) >> 1 );
auto xhash = std::hash<int>()( k.x );
// 0x9e3779b9 is 2^33 / ( 1 + sqrt(5) )
// Adding this value ensures that consecutive bits of y will not be close to each other
// decreasing the likelihood of hash collision in similar values of x and y
return xhash ^ ( std::hash<int>()( k.y ) + 0x9e3779b9 + ( xhash << 6 ) + ( xhash >> 2 ) );
}
bool std::less<wxPoint>::operator()( const wxPoint& aA, const wxPoint& aB ) const