【问题标题】:Delphi TObjectDictionary having a class instance key [duplicate]Delphi TObjectDictionary 具有类实例键[重复]
【发布时间】:2016-01-13 17:59:28
【问题描述】:

我有以下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;

结果是:“未找到匹配”、“找到匹配”。我应该怎么做才能比较每个键的字段值而不是它们的地址?

【问题讨论】:

    标签: delphi generics dictionary memory-address


    【解决方案1】:

    实例引用变量的默认相等比较器比较引用而不是对象。所以你得到的是对象身份而不是值身份。

    因此,如果您希望强加值标识,则需要提供自定义相等比较器。

    TestDict := TObjectDictionary<TTest, string>.Create(
      [doOwnsKeys], 
      TEqualityComparer<TTest>.Construct(EqualityComparison, Hasher)
    );
    

    其中EqualityComparisonHasher 是比较函数和散列函数。它们可以这样实现:

    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;
    

    【讨论】:

    • 谢谢@David。补充一点:编译器建议我使用System.Hash.THashBobJenkins.Hash 而不是BobJenkinsHash,因为BobJenkinsHash 已被弃用。
    • 这听起来很合理。我每天都在使用 XE7,因此我不会注意到弃用。
    猜你喜欢
    • 1970-01-01
    • 2011-08-01
    • 2011-08-17
    • 2020-01-01
    • 2020-10-14
    • 1970-01-01
    • 2010-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多