一个好用的hash函数(C语言)

时间:2024-11-08 09:04:56
 typedef unsigned int  DWORD;
typedef unsigned char BYTE; /**********************************************************************
* 函数名称: mk_hash_index
* 功能描述: 由Key值产生hash索引值
* 输入参数:
pstHashKey: 指向KEY的内容(要强转成DWORD*)
dwHashCount: hash表中结点的数目
dwLen : KEY的长度
* 返 回 值: dwHashVal : hash索引值
*作者/时间: 曹国平2014/02/27
***********************************************************************/
inline DWORD mk_hash_index(DWORD* pstHashKey, DWORD dwHashCount, DWORD dwLen)
{
DWORD dwi ;
BYTE* pKeyEnd ;
DWORD* pKey ;
DWORD dwKeyLen;
DWORD dwHashVal = dwHashCount ; dwKeyLen = dwLen;
pKey = (DWORD*)(pstHashKey); for(dwi = dwKeyLen/sizeof(DWORD); dwi>; dwi--)
{
dwHashVal = ((dwHashVal << ) + dwHashVal) + *pKey ;
pKey++ ;
}
dwi = sizeof(DWORD) - (dwKeyLen%sizeof(DWORD)) ;
pKeyEnd = (BYTE*)pKey+ ;
while(dwi-- > )
{
*pKeyEnd-- = ;
}
dwHashVal = ((dwHashVal << ) + dwHashVal) + *pKey ;
dwHashVal = dwHashVal % dwHashCount ;
return dwHashVal ;
}

相关文章