Performance of hashes and associative arrays

Ali Çehreli acehreli at yahoo.com
Sat Nov 10 17:35:14 PST 2012


On 11/10/2012 05:37 AM, Joseph Rushton Wakeling wrote:
 > On 11/07/2012 07:38 AM, "Raphaël.Jakse"
 > <raphael.jakse at gmail.com>"@puremagic.com wrote:
 >> We want to be able to get the hash of s. Therefore, we re-implement
 >> the toHash
 >> method of the Student class :
 >
 > OK, now I'm curious. Assuming I don't write a custom re-implementation,
 > how would a custom struct or class be hashed? (What about a tuple?)
 >
 > I ask because I'm considering associative arrays where the key is a
 > custom class or tuple as part of a current coding project.

For classes, because they are reference types, it is the object identity 
that determines the hash value (probably the pointer of the actual 
object). As a result, even when the values of the members are the same, 
two objects have different hash values.

For structs, because they are value types, it is the bytes that make up 
the object determine the hash value.

But beware: The associative array members of structs are not hashed by 
values of their elements. (There was a long discussion recently on the 
the main newsgroup on this topic.)

Ali



More information about the Digitalmars-d-learn mailing list