在 Delphi 中比较数组

问题描述 投票:0回答:3

我有3个数组,例如:

const
  A: Array[0..9] of Byte = ($00, $01, $AA, $A1, $BB, $B1, $B2, $B3, $B4, $FF);
  B: Array[0..2] of Byte = ($A1, $BB, $B1);
  C: Array[0..2] of Byte = ($00, $BB, $FF);

有没有一种方法可以比较并获得正确的索引,而不是一一检查每个字节?例如:

function GetArrayIndex(Source, Value: Array of Byte): Integer;
begin
..
end;

GetArrayIndex(A, B); // results 3
GetArrayIndex(A, C); // results -1

提前谢谢您。

arrays delphi indexing compare
3个回答
13
投票
function ByteArrayPos(const SearchArr : array of byte; const CompArr : array of byte) : integer;
//  result=Position or -1 if not found
var
  Comp,Search : AnsiString;
begin
  SetString(Comp, PAnsiChar(@CompArr[0]), Length(CompArr));
  SetString(Search, PAnsiChar(@SearchArr[0]), Length(SearchArr));
  Result := Pos(Search,Comp) - 1;
end;

7
投票

这是安德烈亚斯答案的修改版本这里

function BytePos(const Pattern: array of byte; const Buffer : array of byte): Integer;
var
  PatternLength,BufLength: cardinal;
  i,j: cardinal;
  OK: boolean;
begin
  Result := -1;
  PatternLength := Length(Pattern);
  BufLength := Length(Buffer);
  if (PatternLength > BufLength) then
    Exit;
  if (PatternLength = 0) then
    Exit;
  for i := 0 to BufLength - PatternLength do
    if Buffer[i] = Pattern[0] then
    begin
      OK := true;
      for j := 1 to PatternLength - 1 do
        if Buffer[i + j] <> Pattern[j] then
        begin
          OK := false;
          Break;
        end;
      if OK then
        Exit(i);
    end;
end;

begin
  WriteLn(BytePos(B,A)); // 3
  WriteLn(BytePos(C,A)); // -1
  ReadLn;
end.

不过,布米斯的答案是更喜欢。好多了。


只是评论中指出的一句话。

对于小型数据集

BytePos
优于
ByteArrayPos
,而对于大型数据集(10000 个项目),性能则相反。

这是针对 32 位模式的,其中汇编器优化的

Pos()
系统功能对于大型数据集发挥了最佳作用。

但在 64 位模式下,没有汇编器优化的 Pos() 函数。 在我的基准测试中,对于所有类型的数据集大小,

BytePos
ByteArrayPos
快 4-6 倍。


更新

基准测试是用XE3进行的。

在测试过程中,我发现 System.pas 函数中存在有缺陷的

purepascal
循环
Pos()

添加了改进请求,QC111103,建议的功能速度提高了约 3 倍。

我还对上面的

BytePos
进行了一些优化,并在下面将其呈现为
ByteposEx()

function BytePosEx(const Pattern,Buffer : array of byte; offset : Integer = 0): Integer;
var
  LoopMax    : Integer;
  OK         : Boolean;
  patternP   : PByte;
  patStart   : Byte;
  i,j        : NativeUInt;
begin
  LoopMax := High(Buffer) - High(Pattern);
  if (offset <= LoopMax) and
     (High(Pattern) >= 0) and
     (offset >= 0) then
  begin
    patternP := @Pattern[0];
    patStart := patternP^;
    for i := NativeUInt(@Buffer[offset]) to NativeUInt(@Buffer[LoopMax]) do
    begin
      if (PByte(i)^ = patStart) then
      begin
        OK := true;
        for j := 1 to High(Pattern) do
          if (PByte(i+j)^ <> patternP[j]) then
          begin
            OK := false;
            Break;
          end;
        if OK then
          Exit(i-NativeUInt(@Buffer[0]));
      end;
    end;
  end;
  Result := -1;
end;

0
投票

受到 .net SequenceEqual 的启发,可以执行以下操作:

function ArraysEqual<T>(const Arr1, Arr2: TArray<T>): Boolean;
var
  I: Integer;
  Comparer: IComparer<T>;
begin
  Result := Length(Arr1) = Length(Arr2);
  if Result then
  begin
    Comparer := TComparer<T>.Default;
    for I := Low(Arr1) to High(Arr1) do
    begin
      if Comparer.Compare(Arr1[I], Arr2[I]) <> 0 then
        Exit(False);
    end;
  end;
end;
© www.soinside.com 2019 - 2024. All rights reserved.