简体   繁体   English

在多维中搜索一维数组。 数组

[英]Searching for a one dimensional array in multi dim. array

Please what would be the fastest (and most 'elegant') way to search for FaDirection array in the multi dim. 请问在多点暗中搜索FaDirection数组最快(也是最“优雅”)的方法。 FdVSubs array? FdVSubs数组?

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

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

Array:
FaDirection : TaDirection;

I need to resolve if FaDirection is already stored in FdVSubs or not. 我需要解决FaDirection是否已存储在FdVSubs中的问题。

Thank You. 谢谢。

First and most important, you need to decide what defines two arrays as being equal. 首先也是最重要的是,您需要确定什么将两个数组定义为相等。 Then you'll have to adjust my code to match that definition. 然后,您必须调整我的代码以匹配该定义。 For the answer I'll assume arrays are equal if and only if they have identical object references at each corresponding position in the arrays. 对于答案,我将假设且仅当数组在数组中每个对应位置具有相同的对象引用时,数组才相等。

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 ...

NOTES 笔记

  • DirectionExistsIn uses an Open Array, so it doesn't matter how many elements are defined for the FdVSubs array. DirectionExistsIn使用一个开放数组,因此为FdVSubs数组定义多少个元素都没有关系。
  • A small change to DirectionExistsIn can allow you to return the position of the duplicate. 对DirectionExistsIn进行小的更改可以使您返回重复项的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM