Delphi 具有 class 实例键的 TObjectDictionary
Delphi TObjectDictionary having a class instance key
我有以下 class
:
TTest = class
private
FId: Integer;
FSecField: Integer;
FThirdField: Integer;
public
constructor Create(AId, ASecField, AThirdField: Integer);
// .....
end;
然后我创建一个 TObjectDictionary
这样的:
procedure TMainForm.btnTestClick(Sender: TObject);
var
TestDict: TObjectDictionary<TTest, string>;
Instance: TTest;
begin
TestDict := TObjectDictionary<TTest, string>.Create([doOwnsKeys]);
try
TestDict.Add(TTest.Create(1, 1, 1), '');
if TestDict.ContainsKey(TTest.Create(1, 1, 1)) then
ShowMessage('Match found')
else
ShowMessage('Match not found');
Instance := TTest.Create(1, 1, 1);
TestDict.Add(Instance, 'str');
if TestDict.ContainsKey(Instance) then
ShowMessage('Match found')
else
ShowMessage('Match not found');
finally
TestDict.Free;
end;
end;
结果为:"Match not found"、"Match found"。我应该怎么做才能比较每个键的字段值而不是它们的地址?
实例引用变量的默认相等比较器比较引用而不是对象。所以你得到的是对象标识而不是值标识。
因此,如果您希望强加值标识,则需要提供自定义相等比较器。
TestDict := TObjectDictionary<TTest, string>.Create(
[doOwnsKeys],
TEqualityComparer<TTest>.Construct(EqualityComparison, Hasher)
);
其中 EqualityComparison
、Hasher
是比较和散列函数。它们可以这样实现:
EqualityComparison :=
function(const Left, Right: TTest): Boolean
begin
Result := (Left.FId = Right.FId)
and (Left.FSecField = Right.FSecField)
and (Left.FThirdField = Right.FThirdField);
end;
Hasher :=
function(const Value: TTest): Integer
begin
Result := BobJenkinsHash(Value.FId, SizeOf(Value.FId), 0);
Result := BobJenkinsHash(Value.FSecField, SizeOf(Value.FSecField), Result);
Result := BobJenkinsHash(Value.FThirdField, SizeOf(Value.FThirdField), Result);
end;
我有以下 class
:
TTest = class
private
FId: Integer;
FSecField: Integer;
FThirdField: Integer;
public
constructor Create(AId, ASecField, AThirdField: Integer);
// .....
end;
然后我创建一个 TObjectDictionary
这样的:
procedure TMainForm.btnTestClick(Sender: TObject);
var
TestDict: TObjectDictionary<TTest, string>;
Instance: TTest;
begin
TestDict := TObjectDictionary<TTest, string>.Create([doOwnsKeys]);
try
TestDict.Add(TTest.Create(1, 1, 1), '');
if TestDict.ContainsKey(TTest.Create(1, 1, 1)) then
ShowMessage('Match found')
else
ShowMessage('Match not found');
Instance := TTest.Create(1, 1, 1);
TestDict.Add(Instance, 'str');
if TestDict.ContainsKey(Instance) then
ShowMessage('Match found')
else
ShowMessage('Match not found');
finally
TestDict.Free;
end;
end;
结果为:"Match not found"、"Match found"。我应该怎么做才能比较每个键的字段值而不是它们的地址?
实例引用变量的默认相等比较器比较引用而不是对象。所以你得到的是对象标识而不是值标识。
因此,如果您希望强加值标识,则需要提供自定义相等比较器。
TestDict := TObjectDictionary<TTest, string>.Create(
[doOwnsKeys],
TEqualityComparer<TTest>.Construct(EqualityComparison, Hasher)
);
其中 EqualityComparison
、Hasher
是比较和散列函数。它们可以这样实现:
EqualityComparison :=
function(const Left, Right: TTest): Boolean
begin
Result := (Left.FId = Right.FId)
and (Left.FSecField = Right.FSecField)
and (Left.FThirdField = Right.FThirdField);
end;
Hasher :=
function(const Value: TTest): Integer
begin
Result := BobJenkinsHash(Value.FId, SizeOf(Value.FId), 0);
Result := BobJenkinsHash(Value.FSecField, SizeOf(Value.FSecField), Result);
Result := BobJenkinsHash(Value.FThirdField, SizeOf(Value.FThirdField), Result);
end;