Simple string hashing function

前端 未结 6 1281
情话喂你
情话喂你 2020-12-14 11:28

I\'m trying to hash a string into an integer for placing it in an array. However I do not know all too much about hashing functions, and that\'s why my current method is jus

6条回答
  •  半阙折子戏
    2020-12-14 11:44

    I've tried many fast hash functions and chosen this one:

    function StrHash(const st:string):cardinal; 
     var
      i:integer;
     begin
      result:=0;
      for i:=1 to length(st) do
       result:=result*$20844 xor byte(st[i]);
     end;
    

    It is as fast as K&R function (actually even faster) but makes better (more even) distribution.

提交回复
热议问题