gpt4 book ai didi

delphi - 我可以为 TObjectList.IndexOf 传入一个函数,为 TObjectList.Sort 传入另一个函数吗?

转载 作者:行者123 更新时间:2023-12-01 22:36:46 25 4
gpt4 key购买 nike

总结:

TList.IndexOf(TList 在 Classes.pas 单元中定义)线性迭代所包含的项目,并比较引用。 TList.IndexOf(在 Generics.Collections.pas 单元中定义的 TList)也线性迭代所包含的项目,但使用比较器来比较项目是否相等。

TList.Sort 和 TList.Sort 都可以使用比较器。

================================================== ==

对于以下单元中定义的 TForceList 类型的实例,我可以使用

instance.Sort(@ForceCompare);

使用其值字段作为排序标准对其进行快速排序。然而,当我打电话

instance.IndexOf(AnotherInstance)

我想使用它的 ElementZ 字段作为比较条件,即 ForceEqual 函数。我想知道如何才能实现这一目标?

PS:如果使用泛型集合,我想我可以使用

TList<TForce>.Create(TComparer<TForce>.Construct(ForceEqual));

单位:

    unit uChemParserCommonForce;

interface

uses
uMathVector3D,
Contnrs;

type

TForce = class;
TForceList = class;

TForce = class
private
FElementZ: Integer;
FValue: TVector3D;
public
property ElementZ: Integer read FElementZ;
property Value: TVector3D read FValue;
constructor Create(aElementZ: Integer; aX, aY, aZ: Double);
function ToString(): string; {$IF DEFINED(FPC) OR DEFINED(VER210)} override; {$IFEND}
end;

// Mastering Delphi 6 - Chapter 5 -
TForceList = class(TObjectList)
protected
procedure SetObject(Index: Integer; Item: TForce);
function GetObject(Index: Integer): TForce;
public
function Add(Obj: TForce): Integer;
procedure Insert(Index: Integer; Obj: TForce);
property Objects[Index: Integer]: TForce read GetObject
write SetObject; default;
end;

function ForceCompare(Item1, Item2: Pointer): Integer;
function ForceEqual(Item1, Item2: Pointer): Boolean;

implementation

uses
Math, SysUtils;

function ForceCompare(Item1, Item2: Pointer): Integer;
begin
// Ascendent
// Result := CompareValue(TForce(Item1).Value.Len, TForce(Item2).Value.Len);
// Descendent
Result := CompareValue(TForce(Item2).Value.Len, TForce(Item1).Value.Len);
end;

function ForceEqual(Item1, Item2: Pointer): Boolean;
begin
Result := TForce(Item1).ElementZ = TForce(Item2).ElementZ;
end;

constructor TForce.Create(aElementZ: Integer; aX, aY, aZ: Double);
begin
FElementZ := aElementZ;
FValue := TVector3D.Create(aX, aY, aZ);
end;

function TForce.ToString: string;
begin
Result := IntToStr(FElementZ) + ' X: ' + FloatToStr(FValue.X) + ' Y: ' +
FloatToStr(FValue.Y) + ' Z: ' + FloatToStr(FValue.Z);
end;

{ TForceList }

function TForceList.Add(Obj: TForce): Integer;
begin
Result := inherited Add(Obj);
end;

procedure TForceList.SetObject(Index: Integer; Item: TForce);
begin
inherited SetItem(Index, Item);
end;

function TForceList.GetObject(Index: Integer): TForce;
begin
Result := inherited GetItem(Index) as TForce;
end;

procedure TForceList.Insert(Index: Integer; Obj: TForce);
begin
inherited Insert(Index, Obj);
end;

end.

最佳答案

非通用TObjectList使用TList.IndexOf ,它只是迭代内部数组并比较指针。

同样,通用 TObjectList<T>使用TList<T>.IndexOf ,它使用 IComparerTList<T>.Sort使用TArray.Sort<T>传入任何IComparer在创建列表时分配。

比较器是私有(private)的,仅在列表构造函数中分配,因此我没有看到覆盖此行为的简单方法。

更新

TList<T>提供并重载Sort接受比较器作为参数,不修改私有(private)比较器。因此,您可以使用一个比较器进行排序,而 indexof 可以使用不同的比较器。

关于delphi - 我可以为 TObjectList.IndexOf 传入一个函数,为 TObjectList.Sort 传入另一个函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5266008/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com