开发者

Default IEqualityComparer for TDictionary<TObject, TObject>?

I just read

"Any class that implements the IEqualityComparer interface is expected to provide the impl开发者_运维技巧ementation for the Equals method." - (Delphi DocWiki)

and

"Any class that implements the IEqualityComparer interface is expected to provide the implementation for the GetHashCode method." - (Delphi DocWiki)

How will performance of a TDictionary be if I create a TDictionary<TObject, TObject> and do not implement a IEqualityComparer?

I have not found a default implementation (in Delphi 2009). So how will the hash code for they keys be calculated?

If it is simply the memory address of the object in the Dictionary entries key, will a search for a given entry be performed in sequential order?


The default implementation will perform extremely well with a TObject key. Equality is defined to be object identity, the same as testing if A=B. The hash is simply the address of the reference – it could not be more efficient.

The code looks like this:

function TObject.Equals(Obj: TObject): Boolean;
begin
  Result := Obj = Self;
end;

function TObject.GetHashCode: Integer;
begin
  Result := Integer(Self);
end;

Looking up in a hashed dictionary does not involve searching. It is a highly efficient O(1) operation. I think you should take a read of the Wikipedia article.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜