1 typedef unsigned int DWORD; 2 typedef unsigned char BYTE; 3 4 /********************************************************************** 5 * 函数名称: mk_hash_index 6 * 功能描述: 由Key值产生hash索引值 7 * 输入参数: 8 pstHashKey: 指向KEY的内容(要强转成DWORD*) 9 dwHashCount: hash表中结点的数目 10 dwLen : KEY的长度 11 * 返 回 值: dwHashVal : hash索引值 12 *作者/时间: 曹国平2014/02/27 13 ***********************************************************************/ 14 inline DWORD mk_hash_index(DWORD* pstHashKey, DWORD dwHashCount, DWORD dwLen) 15 { 16 DWORD dwi ; 17 BYTE* pKeyEnd ; 18 DWORD* pKey ; 19 DWORD dwKeyLen; 20 DWORD dwHashVal = dwHashCount ; 21 22 dwKeyLen = dwLen; 23 pKey = (DWORD*)(pstHashKey); 24 25 for(dwi = dwKeyLen/sizeof(DWORD); dwi>0; dwi--) 26 { 27 dwHashVal = ((dwHashVal << 5) + dwHashVal) + *pKey ; 28 pKey++ ; 29 } 30 dwi = sizeof(DWORD) - (dwKeyLen%sizeof(DWORD)) ; 31 pKeyEnd = (BYTE*)pKey+3 ; 32 while(dwi-- > 0) 33 { 34 *pKeyEnd-- = 0 ; 35 } 36 dwHashVal = ((dwHashVal << 5) + dwHashVal) + *pKey ; 37 dwHashVal = dwHashVal % dwHashCount ; 38 return dwHashVal ; 39 }