MS这个很有需求嘛~~~~~~~~偶从C语言翻译了下,放这里好了~~~~~C语言源程序by 李羽修
program hash_of_string;
var
s1:string;
// RS Hash Function
function RSHash(s1:string):dword; //qword used;
var
a:qword; //unsigned int a = 63689;
b:dword;
hash:qword; //unsigned int hash = 0;
i:integer;
begin
a:=63689;
b:=378551;
hash:=0;
for i:=1 to length(s1) do
begin
hash:=(hash*a+ord(s1[i])) and $FFFFFFFF;
a:=(a*b) and $FFFFFFFF;
end;
RSHash:=hash and $7FFFFFFF;
end;
// JS Hash Function
function JSHash(s1:string):dword; //qword used;
var
i:integer;
hash:qword;
begin
hash:=1315423911;
for i:=1 to length(s1) do
hash:=(hash xor ((hash shl 5)+ord(s1[i])+(hash shr 2))) and $FFFFFFFF;
JSHash:=hash and $7FFFFFFF;
end;
// P. J. Weinberger Hash Function
function PJWHash(s1:string):dword;
var
i:integer;
BitsInUnigned,ThreeQuarters,OneEighth,HighBits,test:dword;
begin
BitsInUnigned:=32; //unsigned int BitsInUnignedInt = (unsigned int)(sizeof(unsigned int) * 8);
ThreeQuarters:=(BitsInUnigned*3) shr 2; //unsigned int ThreeQuarters = (unsigned int)((BitsInUnignedInt * 3) / 4);
OneEighth:=BitsInUnigned shr 3; //unsigned int OneEighth = (unsigned int)(BitsInUnignedInt / 8);
HighBits:=$FFFFFFFF shl (BitsInUnigned-OneEighth);
test:=0;
PJWHash:=0;
for i:=1 to length(s1) do
begin
PJWHash:=(PJWHash shl OneEighth)+ord(s1[i]);
test:=PJWHash and HighBits;
if test<>0 then
PJWHash:=(PJWHash xor (test shr ThreeQuarters)) and (not HighBits);
end;
PJWHash:=PJWHash and $7FFFFFFF;
end;
// ELF Hash Function
function ELFHash(s1:string):dword;
var
i:integer;
x:dword;
begin
ELFHash:=0;
x:=0;
for i:=1 to length(s1) do
begin
ELFHash:=(ELFHash shl 4)+ord(s1[i]);
x:=ELFHash and $F0000000; //x = hash & 0xF0000000L
if x<>0 then
begin
ELFHash:=ELFHash xor (x shr 24);
ELFHash:=ELFHash and not x;
end;
end;
ELFHash:=ELFHash and $7FFFFFFF;
end;
// BKDR Hash Function
function BKDRHash(s1:string):dword; //qword used;
var
i:integer;
seed:dword;
hash:qword;
begin
seed:=131; // 31 131 1313 13131 131313 etc..
hash:=0;
for i:=1 to length(s1) do
hash:=(hash*seed+ord(s1[i])) and $FFFFFFFF;
BKDRHash:=hash and $7FFFFFFF;
end;
// SDBM Hash Function
function SDBMHash(s1:string):dword; //qword used;
var
i:integer;
hash:qword;
begin
hash:=0;
for i:=1 to length(s1) do
hash:=(ord(s1[i])+(hash shl 6)+(hash shl 16)-hash) and $FFFFFFFF;
SDBMHash:=hash and $7FFFFFFF;
end;
// DJB Hash Function
function DJBHash(s1:string):dword; //qword used;
var
i:integer;
hash:qword;
begin
hash:=5381;
for i:=1 to length(s1) do
hash:=(hash+(hash shl 5)+ord(s1[i])) and $FFFFFFFF;
DJBHash:=hash and $7FFFFFFF;
end;
// AP Hash Function
function APHash(s1:string):dword;
var
i:integer;
begin
APHash:=0;
for i:=1 to length(s1) do
if i and 1=1 then
APHash:=APHash xor ((APHash shl 7) xor ord(s1[i]) xor (APHash shr 3))
else
APHash:=APHash xor (not ((APHash shl 11) xor ord(s1[i]) xor (APHash shr 5)));
APHash:=APHash and $7FFFFFFF;
end;
begin
assign(input,'strhash.in');
assign(output,'strhash.out');
reset(input);
rewrite(output);
read(s1);
writeln(RSHash(s1)); //qword used;
writeln(JSHash(s1)); //qword used;
writeln(PJWHash(s1));
writeln(ELFHash(s1));
writeln(BKDRHash(s1)); //qword used;
writeln(SDBMHash(s1)); //qword used;
writeln(DJBHash(s1)); //qword used;
writeln(APHash(s1));
close(input);
close(output);
end.
翻译log:
1、PJWHash几乎无法翻译~~~~原因是采用了sizeof()运算符,pascal米有相关运算符~~~~采取了其他翻译方法,结果应该一致~~~~~~
2、凡非按源程序翻译之处均作了注释~~~
3、由于RSHash、JSHash、BKDRHash、SDBMHash、DJBHash均使用了C语言滴一个特殊特性——溢出部分自动舍去,即对溢出滴变量采取and $FFFFFFFF(& 0xFFFFFFFF)处理,故这些函数使用了qword,以保证程序运行~~~~
4、根据第三条log,这五个函数在时间效率上会下降滴更多~~~~~
C语言源程序:
// RS 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);
}
ps.转载本篇请标注版权,注明地址好么?~~~~~
转自:http://hi.baidu.com/satily/blog/item/5f5ac3fcdaae09fbfc037fcb.html