查找字符串的hash算法
2007-06-18 14:39
253 查看
查找字符串的hash算法
unsigned int RSHash( char * str)
{
unsigned int b = 378551 ;
unsigned int a = 63689 ;
unsigned int hash = 0 ;
while ( * str)
{
hash = hash * a + ( * str ++ );
a *= b;
}
return (hash & 0x7FFFFFFF );
}
// JS Hash Function
unsigned int JSHash( char * str)
{
unsigned int hash = 1315423911 ;
while ( * str)
{
hash ^= ((hash << 5 ) + ( * str ++ ) + (hash >> 2 ));
}
return (hash & 0x7FFFFFFF );
}
// P. J. Weinberger Hash Function
unsigned int PJWHash( char * str)
{
unsigned int BitsInUnignedInt = (unsigned int )( sizeof (unsigned int ) *
8 );
unsigned int ThreeQuarters = (unsigned int )((BitsInUnignedInt * 3 )
/ 4 );
unsigned int OneEighth = (unsigned int )(BitsInUnignedInt / 8 );
unsigned int HighBits = (unsigned int )( 0xFFFFFFFF ) << (BitsInUnignedInt - OneEighth);
unsigned int hash = 0 ;
unsigned int test = 0 ;
while ( * str)
{
hash = (hash << OneEighth) + ( * str ++ );
if ((test = hash & HighBits) != 0 )
{
hash = ((hash ^ (test >> ThreeQuarters)) & ( ~ HighBits));
}
}
return (hash & 0x7FFFFFFF );
}
// ELF Hash Function
unsigned int ELFHash( char * str)
{
unsigned int hash = 0 ;
unsigned int x = 0 ;
while ( * str)
{
hash = (hash << 4 ) + ( * str ++ );
if ((x = hash & 0xF0000000L ) != 0 )
{
hash ^= (x >> 24 );
hash &= ~ x;
}
}
return (hash & 0x7FFFFFFF );
}
// BKDR Hash Function
unsigned int BKDRHash( char * str)
{
unsigned int seed = 131 ; // 31 131 1313 13131 131313 etc..
unsigned int hash = 0 ;
while ( * str)
{
hash = hash * seed + ( * str ++ );
}
return (hash & 0x7FFFFFFF );
}
// SDBM Hash Function
unsigned int SDBMHash( char * str)
{
unsigned int hash = 0 ;
while ( * str)
{
hash = ( * str ++ ) + (hash << 6 ) + (hash << 16 ) - hash;
}
return (hash & 0x7FFFFFFF );
}
// DJB Hash Function
unsigned int DJBHash( char * str)
{
unsigned int hash = 5381 ;
while ( * str)
{
hash += (hash << 5 ) + ( * str ++ );
}
return (hash & 0x7FFFFFFF );
}
// AP Hash Function
unsigned int APHash( char * str)
{
unsigned int hash = 0 ;
int i;
for (i = 0 ; * str; i ++ )
{
if ((i & 1 ) == 0 )
{
hash ^= ((hash << 7 ) ^ ( * str ++ ) ^ (hash >> 3 ));
}
else
{
hash ^= ( ~ ((hash << 11 ) ^ ( * str ++ ) ^ (hash >> 5 )));
}
}
return (hash & 0x7FFFFFFF );
}
struct hash_map_func
{
enum
{ // parameters for hash table
bucket_size = 2, // 0 < bucket_size
min_buckets = 8
}; // min_buckets = 2 ^^ N, 0 < N
size_t operator()(const string& str)const
{
const char* p =str.c_str();
unsigned long hash = 5381;
int c;
while (c = *p++)
hash = ((hash << 5) + hash) + c; /**//* hash * 33 + c */
return hash;
}
bool operator()(const string& _Keyval1, const string& _Keyval2) const
{ // test if _Keyval1 ordered before _Keyval2
return (gt(_Keyval1, _Keyval2));
}
greater<string> gt;
};
比较经典的字符串hash就这些了吧,"ELF Hash Function" <-这个比较常用..
unsigned int RSHash( char * str)
{
unsigned int b = 378551 ;
unsigned int a = 63689 ;
unsigned int hash = 0 ;
while ( * str)
{
hash = hash * a + ( * str ++ );
a *= b;
}
return (hash & 0x7FFFFFFF );
}
// JS Hash Function
unsigned int JSHash( char * str)
{
unsigned int hash = 1315423911 ;
while ( * str)
{
hash ^= ((hash << 5 ) + ( * str ++ ) + (hash >> 2 ));
}
return (hash & 0x7FFFFFFF );
}
// P. J. Weinberger Hash Function
unsigned int PJWHash( char * str)
{
unsigned int BitsInUnignedInt = (unsigned int )( sizeof (unsigned int ) *
8 );
unsigned int ThreeQuarters = (unsigned int )((BitsInUnignedInt * 3 )
/ 4 );
unsigned int OneEighth = (unsigned int )(BitsInUnignedInt / 8 );
unsigned int HighBits = (unsigned int )( 0xFFFFFFFF ) << (BitsInUnignedInt - OneEighth);
unsigned int hash = 0 ;
unsigned int test = 0 ;
while ( * str)
{
hash = (hash << OneEighth) + ( * str ++ );
if ((test = hash & HighBits) != 0 )
{
hash = ((hash ^ (test >> ThreeQuarters)) & ( ~ HighBits));
}
}
return (hash & 0x7FFFFFFF );
}
// ELF Hash Function
unsigned int ELFHash( char * str)
{
unsigned int hash = 0 ;
unsigned int x = 0 ;
while ( * str)
{
hash = (hash << 4 ) + ( * str ++ );
if ((x = hash & 0xF0000000L ) != 0 )
{
hash ^= (x >> 24 );
hash &= ~ x;
}
}
return (hash & 0x7FFFFFFF );
}
// BKDR Hash Function
unsigned int BKDRHash( char * str)
{
unsigned int seed = 131 ; // 31 131 1313 13131 131313 etc..
unsigned int hash = 0 ;
while ( * str)
{
hash = hash * seed + ( * str ++ );
}
return (hash & 0x7FFFFFFF );
}
// SDBM Hash Function
unsigned int SDBMHash( char * str)
{
unsigned int hash = 0 ;
while ( * str)
{
hash = ( * str ++ ) + (hash << 6 ) + (hash << 16 ) - hash;
}
return (hash & 0x7FFFFFFF );
}
// DJB Hash Function
unsigned int DJBHash( char * str)
{
unsigned int hash = 5381 ;
while ( * str)
{
hash += (hash << 5 ) + ( * str ++ );
}
return (hash & 0x7FFFFFFF );
}
// AP Hash Function
unsigned int APHash( char * str)
{
unsigned int hash = 0 ;
int i;
for (i = 0 ; * str; i ++ )
{
if ((i & 1 ) == 0 )
{
hash ^= ((hash << 7 ) ^ ( * str ++ ) ^ (hash >> 3 ));
}
else
{
hash ^= ( ~ ((hash << 11 ) ^ ( * str ++ ) ^ (hash >> 5 )));
}
}
return (hash & 0x7FFFFFFF );
}
struct hash_map_func
{
enum
{ // parameters for hash table
bucket_size = 2, // 0 < bucket_size
min_buckets = 8
}; // min_buckets = 2 ^^ N, 0 < N
size_t operator()(const string& str)const
{
const char* p =str.c_str();
unsigned long hash = 5381;
int c;
while (c = *p++)
hash = ((hash << 5) + hash) + c; /**//* hash * 33 + c */
return hash;
}
bool operator()(const string& _Keyval1, const string& _Keyval2) const
{ // test if _Keyval1 ordered before _Keyval2
return (gt(_Keyval1, _Keyval2));
}
greater<string> gt;
};
比较经典的字符串hash就这些了吧,"ELF Hash Function" <-这个比较常用..
相关文章推荐
- 查找字符串的hash算法
- C# 数据16进制转10进制,richtextbox中查找字符串
- 题目1168:字符串的查找删除
- 在文件夹中 的指定类型文件中 查找字符串(CodeBlocks+GCC编译,控制台程序,仅能在Windows上运行)
- 黑马程序员-.NET-C#查找字符串的所有排列组合
- Windows下查找纯文本文件中的字符串 - 命令findstr简介(转)
- 在指定字符串中查找第一次找到所查找的字符和最后一次查找的到的字符
- linux查找目录下的所有文件中是否含有某个字符串
- PowerShell查找并返回匹配正则表达式的字符串
- 查找字符串中出现最多的字符
- VC 中字符串比较和查找
- 查找不包含"old"字符串的zip文件, unzip解压到自定目录
- c#读取输入字符串,从数据源中查找以该字符串开头的所有字符串(使用正则表达式)
- find 查找包含指定字符串的文件,仅列出文件名
- Python----使用正则re查找文本中特定中文字符串
- 查找一个字符串中最长的字符及个数
- 给一个字符串S和一个字符串数组T(T中的字符串要比S短许多),设计一个算法, 在字符串S中查找T中的字符串
- Linux查找含有某字符串的所有文件
- linux shell 字符串操作(长度,查找,替换)详解
- 使用grep 在源码文件查找特定字符串