在多暗中搜索一维数组。排列

时间:2014-01-14 13:19:26

标签: arrays delphi

请问在多暗调中搜索 FaDirection 数组的最快(最“优雅”)方式是什么。 FdVSubs 数组?

TaDirection = array[0..7] of TSubRect; //TSubRect = class(TObject)

Multi dim array:
FdVSubs: array[0..15] of TaDirection;

Array:
FaDirection : TaDirection;

如果FaDirection已经存储在FdVSubs中,我需要解决。

谢谢。

1 个答案:

答案 0 :(得分:0)

首先也是最重要的,您需要确定将两个数组定义为相等的内容。然后,您必须调整我的代码以匹配该定义。对于答案,我假设数组是相等的,当且仅当它们在数组中的每个相应位置具有相同的对象引用时。

function DirectionsAreEqual(const ADirection1, ADirection2: TaDirection): Boolean;
var
  LoopI: Integer;
begin
  Result := True;
  for LoopI := Low(TaDirection) to High(TaDirection) do
  begin
    if (ADirection1[LoopI] <> ADirection2[LoopI]) then
    begin
      Result := False;
      Exit;
    end;
  end;
end;

function DirectionExistsIn(const ADirection: TaDirection;
    const ADirectionList: array of TaDirection): Boolean;
var
  LoopI: Integer;
begin
  Result := False; //Caters for empty DirectionList
  for LoopI := Low(ADirectionList) to High(ADirectionList) do
  begin
    Result := DirectionsAreEqual(ADirection, ADirectionList[LoopI]);
    if (Result) then
    begin
      Exit;
    end;
  end;
end;

//Then you can simply do the test as follows:
if DirectionExistsIn(FaDirection, FdVSubs) then ...

备注

  • DirectionExistsIn使用Open Array,因此为FdVSubs数组定义了多少元素无关紧要。
  • 对DirectionExistsIn的一个小改动可以让你返回副本的位置。