unit hashfunc_djb2;

{$MODE FPC}
{$MODESWITCH DEFAULTPARAMETERS}
{$MODESWITCH OUT}
{$MODESWITCH RESULT}

interface

type
THashFuncDjb2 = object
public type
  THashValue = UInt32;
  TInitializer = record end;
public const
  DefaultInitializer: TInitializer = ();
public
  procedure Init(Initializer: TInitializer); static; inline;
  procedure Done; static; inline;
  function GetHash(const S: AnsiString): THashValue; static; inline;
end;

implementation

procedure THashFuncDjb2.Init(Initializer: TInitializer); static inline;
begin
end;

procedure THashFuncDjb2.Done;
begin
end;

function _djb2(Key, KeyEnd: PAnsiChar; Hash: UInt32): UInt32;
begin
  while Key < KeyEnd do begin
    Hash := ((Hash shl 5) + Hash) + Ord(Key^); // hash * 33 + c
    Inc(Key);
  end;
  Exit(Hash);
end;

function THashFuncDjb2.GetHash(const S: AnsiString): THashValue;
begin
  Exit(_djb2(@S[1], @S[1] + Length(S), 5381));
end;

end.
