Delphi:两个高效的哈希函数

function BKDRHash(buf: Pointer; count: Integer) : Cardinal; assembler; asm PUSH EBX; XOR EBX, EBX MOV ECX, EAX XOR EAX, EAX @LOOP: TEST EDX, EDX JZ @EXIT MOV BL, [ECX] IMUL EAX, 131 ADD EAX, EBX INC ECX DEC EDX JMP @LOOP @EXIT: POP EBX //---------------------------- // Pascal: // // Result := 0; // while count > 0 do // begin // Result := (Result * 131) + PByte(buf)^; // Inc(PByte(buf)); // Dec(count); // end; end; function DJBHash(buf: Pointer; count: Integer) : Cardinal; assembler; asm PUSH EDI PUSH EBX XOR EBX, EBX MOV ECX, EAX MOV EAX, 5381 @LOOP: TEST EDX, EDX JZ @EXIT MOV EDI, EAX SHL EDI, 5 ADD EAX, EDI MOV BL, [ECX] ADD EAX, EBX INC ECX DEC EDX JMP @LOOP @EXIT: POP EBX POP EDI //---------------------------- // Pascal: // // Result := 5381; // while count > 0 do // begin // Result := ((Result shl 5) + Result) + PByte(buf)^; // Inc(PByte(buf)); // Dec(count); // end; end; 

 

各种哈希函数算法请参考:

 

http://www.partow.net/programming/hashfunctions/

你可能感兴趣的:(算法,function,Integer,Delphi,pascal)