Понадобилось мне хешировать строки. Нашёл на просторах интернета. Погонял тесты. Работает. Вот делюсь:
{$OverflowChecks Off}
function SuperFastHash(AData:pointer; ADataLength: integer):longword;
// Pascal translation of the SuperFastHash function by Paul Hsieh
// more info: http://www.azillionmonkeys.com/qed/hash.html
// Translation by: Davy Landman
// No warranties, but have fun :)
var
TempPart: longword;
RemainingBytes: integer;
begin
if not Assigned(AData) or (ADataLength <= 0) then
begin
Result := 0;
Exit;
end;
Result := ADataLength;
RemainingBytes := ADataLength and 3;
ADataLength := ADataLength shr 2; // div 4, so var name is not correct anymore..
// main loop
while ADataLength > 0 do
begin
inc(Result, PWord(AData)^);
TempPart := (PWord(Pointer(Cardinal(AData)+2))^ shl 11) xor Result;
Result := (Result shl 16) xor TempPart;
AData := Pointer(Cardinal(AData) + 4);
inc(Result, Result shr 11);
dec(ADataLength);
end;
// end case
if RemainingBytes = 3 then
begin
inc(Result, PWord(AData)^);
Result := Result xor (Result shl 16);
Result := Result xor (PByte(Pointer(Cardinal(AData)+2))^ shl 18);
inc(Result, Result shr 11);
end
else if RemainingBytes = 2 then
begin
inc(Result, PWord(AData)^);
Result := Result xor (Result shl 11);
inc(Result, Result shr 17);
end
else if RemainingBytes = 1 then
begin
inc(Result, PByte(AData)^);
Result := Result xor (Result shl 10);
inc(Result, Result shr 1);
end;
// avalance
Result := Result xor (Result shl 3);
inc(Result, Result shr 5);
Result := Result xor (Result shl 4);
inc(Result, Result shr 17);
Result := Result xor (Result shl 25);
inc(Result, Result shr 6);
end;
{$OverflowChecks On}
{$OverflowChecks Off}
function SuperFastHash(AData:pointer; ADataLength: integer):longword;
// Pascal translation of the SuperFastHash function by Paul Hsieh
// more info: http://www.azillionmonkeys.com/qed/hash.html
// Translation by: Davy Landman
// No warranties, but have fun :)
var
TempPart: longword;
RemainingBytes: integer;
begin
if not Assigned(AData) or (ADataLength <= 0) then
begin
Result := 0;
Exit;
end;
Result := ADataLength;
RemainingBytes := ADataLength and 3;
ADataLength := ADataLength shr 2; // div 4, so var name is not correct anymore..
// main loop
while ADataLength > 0 do
begin
inc(Result, PWord(AData)^);
TempPart := (PWord(Pointer(Cardinal(AData)+2))^ shl 11) xor Result;
Result := (Result shl 16) xor TempPart;
AData := Pointer(Cardinal(AData) + 4);
inc(Result, Result shr 11);
dec(ADataLength);
end;
// end case
if RemainingBytes = 3 then
begin
inc(Result, PWord(AData)^);
Result := Result xor (Result shl 16);
Result := Result xor (PByte(Pointer(Cardinal(AData)+2))^ shl 18);
inc(Result, Result shr 11);
end
else if RemainingBytes = 2 then
begin
inc(Result, PWord(AData)^);
Result := Result xor (Result shl 11);
inc(Result, Result shr 17);
end
else if RemainingBytes = 1 then
begin
inc(Result, PByte(AData)^);
Result := Result xor (Result shl 10);
inc(Result, Result shr 1);
end;
// avalance
Result := Result xor (Result shl 3);
inc(Result, Result shr 5);
Result := Result xor (Result shl 4);
inc(Result, Result shr 17);
Result := Result xor (Result shl 25);
inc(Result, Result shr 6);
end;
{$OverflowChecks On}
Комментариев нет:
Отправить комментарий