问题描述
摘要:
TList.IndexOf(在Classes.pas单元中定义的TList)对包含的项进行线性迭代,并比较引用. TList.IndexOf(在Generics.Collections.pas单元中定义的TList)也对包含的项进行线性迭代,但使用比较器比较项是否相等.
TList.IndexOf (TList defined in the unit Classes.pas) iterates linearly through the contained items, and compares the reference. TList.IndexOf (TList defined in the unit Generics.Collections.pas) also iterates linearly through the contained items, but uses a comparer to compare whether the items are equal.
TList.Sort和TList.Sort都可以使用比较器.
Both TList.Sort and TList.Sort can use a comparer.
================================================ ==
=================================================
对于在以下单元中定义的TForceList类型的实例,我可以使用
For an instance of the TForceList type defined in the following unit, I could use
instance.Sort(@ForceCompare);
使用其值"字段作为排序标准对其进行快速排序.但是,当我致电
to QuickSort it using its Value field as sorting criteria. However, when I call
instance.IndexOf(AnotherInstance)
我想使用其ElementZ字段作为比较标准,即ForceEqual
函数.我想知道如何实现这一目标?
I want to use its ElementZ field as comparing criteria, i.e., the ForceEqual
function. I am wondering how can I achieve this?
PS:如果使用了泛型集合,我想我可以使用
PS: If the generics collection is used, I guess I could use
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
,它仅迭代内部数组并比较指针.
The non-generic TObjectList
uses TList.IndexOf
, which simply iterates through the internal array and compares pointers.
同样,通用TObjectList<T>
使用TList<T>.IndexOf
,而TList<T>.IndexOf
使用IComparer
. TList<T>.Sort
使用TArray.Sort<T>
传入创建列表时分配的任何IComparer
.
Likewise, the generic TObjectList<T>
uses TList<T>.IndexOf
, which uses an IComparer
. TList<T>.Sort
uses TArray.Sort<T>
passing in whatever IComparer
was assigned at the list's creation.
比较器是私有的,仅在列表构造函数中分配,因此我看不到一种简单的方法来覆盖此行为.
The comparer is private and only assigned in the list constructor so I don't see an easy way to override this behavior.
TList<T>
提供并重载了Sort
,它接受比较器作为参数,而无需修改私有比较器.因此,您可以使用一个比较器进行排序,而indexof可以使用另一个比较器.
TList<T>
provides and overloaded Sort
that accepts a comparer as an argument, without modifying the private comparer. So you can sort using one comparer and indexof can use a different one.
这篇关于我可以为TObjectList.IndexOf传递一个函数,为TObjectList.Sort传递另一个函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!