当前位置: 动力学知识库 > 问答 > 编程问答 >

delphi - GetHashCode good practice?

问题描述:

For a Delphi project (built with RAD Studio XE7), I want to create a dictionary of brushes. Each dictionary item contains a TMyBrush object as key, that describes the brush to retrieve, and a GDI+ brush as value.

The TMyBrush class contains 3 fields

  • An enumerated type to determine the kind of brush (solid, gradient, ...)
  • A TBrushInfo class that describes the brush content (color, wrap mode, ...)
  • A TRect that represents a clamp field

In my dictionary, I want to retrieve a brush based on his characteristics, and not on his instance. For example, I want to get a black solid brush from my dictionary by creating a local TMyBrush instance, configuring it to black solid, and getting the matching GDI+ value using the TryGetValue() function. For that, I created a TMyBrushComparer.

Writing the Equals() function isn't a problem for me. However I don't know what is the best practice to write the GetHashCode() function. I would tend to write a function like this:

function TMyBrushComparer.GetHashCode(const pValue: TMyBrush): Integer;

begin

Result := BobJenkinsHash(pValue, SizeOf(TMyBrush), 0);

end;

however I feel that is not a very good practice, it is correct? So, what is the best practice to write a good GetHashCode() function for my TMyBrushComparer?

Regards

网友答案:

The code in the question hashes the address of the object rather than its value and so is not consistent with your definition of equality.

Your definition of equality is that three of the fields are equal. Your hash function should match that definition. Hash each of the three fields, and combine the values, for instance using the approach outlined here: http://stackoverflow.com/a/263416/505088

Two of your fields are value types. They are easy to hash to match value identity. The brush info field appears to be a reference type. So again you need to decide what form of identity you want (reference identity, value identity, or perhaps something else) and then implement matching equality test and hash.

分享给朋友:
您可能感兴趣的文章:
随机阅读: