1
0
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:
GAMMACASE
2024-03-18 18:09:38 +03:00
parent cce0b8346d
commit 973e91c238
2 changed files with 72 additions and 68 deletions

View File

@ -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 );
@ -108,8 +110,12 @@ template<> inline unsigned HashItem<char *>(char * const &pszKey )
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 );
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 ); uint64 MurmurHash64( const void *key, int len, uint32 seed );

View File

@ -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.
@ -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 );