mirror of
https://github.com/alliedmodders/hl2sdk.git
synced 2025-09-19 20:16:10 +08:00
Update generichash.cpp/.h
This commit is contained in:
@ -1,4 +1,4 @@
|
|||||||
//======= Copyright <20> 2005, , Valve Corporation, All rights reserved. =========
|
//======= Copyright <20> 2005, , Valve Corporation, All rights reserved. =========
|
||||||
//
|
//
|
||||||
// Purpose: Variant Pearson Hash general purpose hashing algorithm described
|
// Purpose: Variant Pearson Hash general purpose hashing algorithm described
|
||||||
// by Cargill in C++ Report 1994. Generates a 16-bit result.
|
// by Cargill in C++ Report 1994. Generates a 16-bit result.
|
||||||
@ -12,11 +12,13 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include "platform.h"
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
unsigned FASTCALL HashString( const char *pszKey );
|
unsigned FASTCALL HashString( const char *pszKey );
|
||||||
unsigned FASTCALL HashStringCaseless( const char *pszKey );
|
unsigned FASTCALL HashStringCaseless( const char *pszKey );
|
||||||
unsigned FASTCALL HashStringCaselessConventional( const char *pszKey );
|
unsigned FASTCALL HashStringFastCaselessConventional( const char *pszKey );
|
||||||
unsigned FASTCALL Hash4( const void *pKey );
|
unsigned FASTCALL Hash4( const void *pKey );
|
||||||
unsigned FASTCALL Hash8( const void *pKey );
|
unsigned FASTCALL Hash8( const void *pKey );
|
||||||
unsigned FASTCALL Hash12( const void *pKey );
|
unsigned FASTCALL Hash12( const void *pKey );
|
||||||
@ -105,12 +107,16 @@ template<> inline unsigned HashItem<char *>(char * const &pszKey )
|
|||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
// Murmur hash
|
// Murmur hash
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
uint32 MurmurHash2( const void * key, int len, uint32 seed );
|
uint32 MurmurHash2( const void *key, int len, uint32 seed );
|
||||||
|
|
||||||
// return murmurhash2 of a downcased string
|
// return murmurhash2 of a downcased string
|
||||||
|
uint32 MurmurHash2LowerCase( char const *pString, uint32 nSeed );
|
||||||
uint32 MurmurHash2LowerCase( char const *pString, int nLength, uint32 nSeed );
|
uint32 MurmurHash2LowerCase( char const *pString, int nLength, uint32 nSeed );
|
||||||
|
|
||||||
uint64 MurmurHash64( const void * key, int len, uint32 seed );
|
PLATFORM_INTERFACE uint32 MurmurHash3_32( void const *key, size_t len, uint32 seed, bool bCaselessStringVariant = false );
|
||||||
|
|
||||||
|
// MurmurHash2, 64-bit version
|
||||||
|
uint64 MurmurHash64( const void *key, int len, uint32 seed );
|
||||||
|
|
||||||
|
|
||||||
#endif /* !GENERICHASH_H */
|
#endif /* !GENERICHASH_H */
|
||||||
|
@ -78,21 +78,7 @@ static unsigned g_nRandomValues[256] =
|
|||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
unsigned FASTCALL HashString( const char *pszKey )
|
unsigned FASTCALL HashString( const char *pszKey )
|
||||||
{
|
{
|
||||||
const uint8 *k = (const uint8 *)pszKey;
|
return MurmurHash2( pszKey, strlen( pszKey ), 0x3501A674 );
|
||||||
unsigned even = 0,
|
|
||||||
odd = 0,
|
|
||||||
n;
|
|
||||||
|
|
||||||
while ((n = *k++) != 0)
|
|
||||||
{
|
|
||||||
even = g_nRandomValues[odd ^ n];
|
|
||||||
if ((n = *k++) != 0)
|
|
||||||
odd = g_nRandomValues[even ^ n];
|
|
||||||
else
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (even << 8) | odd ;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -101,27 +87,13 @@ unsigned FASTCALL HashString( const char *pszKey )
|
|||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
unsigned FASTCALL HashStringCaseless( const char *pszKey )
|
unsigned FASTCALL HashStringCaseless( const char *pszKey )
|
||||||
{
|
{
|
||||||
const uint8 *k = (const uint8 *) pszKey;
|
return MurmurHash2LowerCase( pszKey, 0x3501A674 );
|
||||||
unsigned even = 0,
|
|
||||||
odd = 0,
|
|
||||||
n;
|
|
||||||
|
|
||||||
while ((n = toupper(*k++)) != 0)
|
|
||||||
{
|
|
||||||
even = g_nRandomValues[odd ^ n];
|
|
||||||
if ((n = toupper(*k++)) != 0)
|
|
||||||
odd = g_nRandomValues[even ^ n];
|
|
||||||
else
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (even << 8) | odd;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
// 32 bit conventional case-insensitive string
|
// 32 bit conventional case-insensitive string
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
unsigned FASTCALL HashStringCaselessConventional( const char *pszKey )
|
unsigned FASTCALL HashStringFastCaselessConventional( const char *pszKey )
|
||||||
{
|
{
|
||||||
unsigned hash = 0xAAAAAAAA; // Alternating 1's and 0's to maximize the effect of the later multiply and add
|
unsigned hash = 0xAAAAAAAA; // Alternating 1's and 0's to maximize the effect of the later multiply and add
|
||||||
|
|
||||||
@ -133,6 +105,21 @@ unsigned FASTCALL HashStringCaselessConventional( const char *pszKey )
|
|||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// 32 bit conventional case-sensitive string
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
unsigned FASTCALL HashStringConventional( const char *pszKey )
|
||||||
|
{
|
||||||
|
unsigned hash = 0xAAAAAAAA; // Alternating 1's and 0's to maximize the effect of the later multiply and add
|
||||||
|
|
||||||
|
for(; *pszKey; pszKey++)
|
||||||
|
{
|
||||||
|
hash = ((hash << 5) + hash) + (uint8)*pszKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
return hash;
|
||||||
|
}
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
// int hash
|
// int hash
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
@ -302,6 +289,9 @@ unsigned FASTCALL HashBlock( const void *pKey, unsigned size )
|
|||||||
return (even << 8) | odd;
|
return (even << 8) | odd;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// MurmurHash variants
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
uint32 MurmurHash2( const void *key, int len, uint32 seed )
|
uint32 MurmurHash2( const void *key, int len, uint32 seed )
|
||||||
{
|
{
|
||||||
// 'm' and 'r' are mixing constants generated offline.
|
// 'm' and 'r' are mixing constants generated offline.
|
||||||
@ -316,7 +306,7 @@ uint32 MurmurHash2( const void *key, int len, uint32 seed )
|
|||||||
|
|
||||||
// Mix 4 bytes at a time into the hash
|
// Mix 4 bytes at a time into the hash
|
||||||
|
|
||||||
const unsigned char * data = (const unsigned char *)key;
|
const unsigned char *data = (const unsigned char *)key;
|
||||||
|
|
||||||
while(len >= 4)
|
while(len >= 4)
|
||||||
{
|
{
|
||||||
@ -354,6 +344,14 @@ uint32 MurmurHash2( const void *key, int len, uint32 seed )
|
|||||||
}
|
}
|
||||||
|
|
||||||
// return murmurhash2 of a downcased string
|
// return murmurhash2 of a downcased string
|
||||||
|
uint32 MurmurHash2LowerCase( char const *pString, uint32 nSeed )
|
||||||
|
{
|
||||||
|
CUtlString buf( pString );
|
||||||
|
buf.ToLowerFast();
|
||||||
|
|
||||||
|
return MurmurHash2( buf.Get(), buf.Length(), nSeed );
|
||||||
|
}
|
||||||
|
|
||||||
uint32 MurmurHash2LowerCase( char const *pString, int nLength, uint32 nSeed )
|
uint32 MurmurHash2LowerCase( char const *pString, int nLength, uint32 nSeed )
|
||||||
{
|
{
|
||||||
CUtlString buf( pString );
|
CUtlString buf( pString );
|
||||||
|
Reference in New Issue
Block a user