hashfunctions.pas

来自「delphi的范型代码库」· PAS 代码 · 共 522 行 · 第 1/2 页

PAS
522
字号
  begin
    for i:=0 to Length-1 do
    begin
      tmpWChar:=PWordArray(Spos)[i];
      if not ( (PWordArray(CaseSub)[i]=tmpWChar) or ( (tmpWChar<=MaxChar) and
          (PWordArray(CaseSub)[i]=CharCaseInsensitive[tmpWChar]))  ) then
      begin
        result:=false;
        exit;
      end;
    end;
    result:=true;
  end;
  
var
  i,LS,LSub : integer;
  FdChar : WideChar;
  CaseSub  : WideString;
  tmpW : WideChar;
begin
  result:=0;
  LSub:=length(Substr);
  if LSub<=0 then exit;
  LS:=length(S);
  CaseSub:=AnsiUpperCase(Substr);
  FdChar:=CaseSub[1];
  for i:=Index to LS-LSub+1 do
  begin
    tmpW:=S[i];
    if (tmpW=FdChar) or ((ord(tmpW)<=MaxChar) and (CharCaseInsensitive[ord(tmpW)]=ord(FdChar))) then
    begin
      if _Case_IsEq(@CaseSub[2],@S[i+1],LSub-1) then
      begin
        result:=i;
        exit;
      end;
    end;
  end;
end;


function HashValue_WideStr(const Key :WideString):Cardinal; // 区分大小写
var
  i,Len    : integer;
  pStrChar : PWideChar;
begin
  result:=0;
  pStrChar:=PWideChar(Key);
  Len:= length(Key);
  for i:=1 to Len do
  begin
    result:=result*5;
    inc(result,Ord(pStrChar^)*37);
    inc(pStrChar);
  end;
end;

function HashValue_WideStrCaseInsensitive(const Key :WideString):Cardinal; //不区分大小写
var
  i,Len    : integer;
  pStrChar : PWideChar;
  wChar    : WideChar;
begin
  result:=0;
  pStrChar:=PWideChar(Key);
  Len:= length(Key);
  for i:=1 to Len do
  begin
    result:=result*5;
    wChar:=pStrChar^;
    if Ord(wChar)<=MaxChar then
      inc(result,CharCaseInsensitive[Ord(wChar)]*37)
    else
      inc(result,Ord(wChar)*37);
    inc(pStrChar);
  end;
end;

function IsEqual_WideStrCaseInsensitive(const Left,Right:WideString):boolean;  //不区分大小写
var
  i,LLen,RLen    : integer;
  pLStrChar      : PWideChar;
  pRStrChar      : PWideChar;
  LChar      : Cardinal;
  RChar      : Cardinal;
begin
  LLen:= length(Left);
  RLen:= length(Right);
  if (LLen<>RLen) then
  begin
    result:=false;
    exit;
  end;

  pLStrChar:=PWideChar(Left);
  pRStrChar:=PWideChar(Right);
  for i:=1 to LLen do
  begin
    LChar:=Ord(pLStrChar^);
    RChar:=Ord(pRStrChar^);
    if LChar<>RChar then
    begin
      if (LChar<=MaxChar) and (RChar<=MaxChar)
      and (CharCaseInsensitive[LChar]=CharCaseInsensitive[RChar]) then
        Continue
      else
      begin
        result:=false;
        exit;
      end;
    end;
    inc(pLStrChar);
    inc(pRStrChar);
  end;
  result:=true;
end;

function IsLess_WideStrCaseInsensitive(const Left,Right:WideString):boolean;  //不区分大小写的小于比较
var
  i,LLen,RLen    : integer;
  pLStrChar      : PWideChar;
  pRStrChar      : PWideChar;
  LChar      : Cardinal;
  RChar      : Cardinal;
begin
  LLen:= length(Left);
  RLen:= length(Right);

  pLStrChar:=PWideChar(Left);
  pRStrChar:=PWideChar(Right);
  for i:=1 to min(LLen,RLen) do
  begin
    LChar:=Ord(pLStrChar^);
    RChar:=Ord(pRStrChar^);
    if (LChar=RChar) then
    begin
      inc(pLStrChar);
      inc(pRStrChar);
    end
    else
    begin
      if (LChar<=MaxChar) and (RChar<=MaxChar)
      and (CharCaseInsensitive[LChar]=CharCaseInsensitive[RChar]) then
      begin
        inc(pLStrChar);
        inc(pRStrChar);
      end
      else
      begin
        result:=LChar<RChar;
        exit;
      end;
    end;
  end;
  result:=LLen<RLen;
end;

function HashValue_Pointer(const Key :Pointer):Cardinal;
begin
  result:=Cardinal(Key)*37;
end;

function IsEqual_Pointer(const Left,Right :Pointer):boolean;
begin
  result:=(Left=Right);
end;

function IsLess_Pointer(const Left,Right :Pointer):boolean;
begin
  result:=Cardinal(Left)<Cardinal(Right);
end;

function HashValue_IInterface(const Key :IInterface):Cardinal;
begin
  result:=Cardinal(Key)*37;
end;

function IsEqual_IInterface(const Left,Right :IInterface):boolean;
begin
  result:=(Left=Right);
end;

function IsLess_IInterface(const Left,Right :IInterface):boolean;
begin
  result:=Cardinal(Left)<Cardinal(Right);
end;

////

function HashValue_Char(const Key :Char):Cardinal;
begin
  result:=Cardinal(Key)*37;
end;
function HashValue_WideChar(const Key :WideChar):Cardinal;
begin
  result:=Cardinal(Key)*37;
end;
function HashValue_ShortInt(const Key :ShortInt):Cardinal;
begin
  result:=Cardinal(Key)*37;
end;
function HashValue_Smallint(const Key :Smallint ):Cardinal;
begin
  result:=Cardinal(Key)*37;
end;
function HashValue_Integer(const Key :integer):Cardinal;
begin
  result:=Cardinal(Key)*37;
end;
function HashValue_Int64(const Key :Int64):Cardinal;
begin
  result:=Cardinal(Key)*37+Cardinal(Key shr 32)*5;
end;
function HashValue_Byte(const Key :Byte):Cardinal;
begin
  result:=Cardinal(Key)*37;
end;
function HashValue_Word(const Key :Word):Cardinal;
begin
  result:=Cardinal(Key)*37;
end;
function HashValue_Cardinal(const Key :Cardinal):Cardinal;
begin
  result:=Cardinal(Key)*37;
end;

////

function HashValue_Real(const Key :Real):Cardinal;
begin
  result:=HashValue_Data(@Key,sizeof(Key));
end;

function HashValue_Real48(const Key :Real48):Cardinal;
begin
  result:=HashValue_Data(@Key,sizeof(Key));
end;
function HashValue_Single(const Key :Single):Cardinal;
begin
  result:=HashValue_Data(@Key,sizeof(Key));
end;
function HashValue_Double(const Key :double):Cardinal;
begin
  result:=HashValue_Data(@Key,sizeof(Key));
end;
function HashValue_Extended(const Key :Extended):Cardinal;
begin
  result:=HashValue_Data(@Key,sizeof(Key));
end;
function HashValue_Comp(const Key :Comp):Cardinal;
begin
  result:=HashValue_Data(@Key,sizeof(Key));
end;
function HashValue_Currency(const Key :Currency):Cardinal;
begin
  result:=HashValue_Data(@Key,sizeof(Key));
end;


end.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?