Hi -
The only requirement for a hash method is that it needs to compute the same values for objects which are considered equal via #=. Other than that there is no requirement - in fact, a perfectly valid hash function would be:
hash ^0
Albeit horribly slow (since it creates hash collisions everywhere) it would be perfectly valid hash function. An invalid hash function would (for example) be:
MyPoint>>= aPoint "Only compare x values" ^x = aPoint x
MyPoint>>hash ^y
Since the y value could be different for points that have equal x values and are therefore considered equal. And since you are asking about patterns - a classic pattern is to take the hash values of the objects that define equality and combine those via an appropriate operator, e.g.,
MyPoint>>= aPoint "Compare x and y" ^x = aPoint x and:[y = aPoint y]
MyPoint>>hash "Hash x and y" ^x hash bitXor: y hash
Cheers, - Andreas
Houssam Fakih wrote:
Hi,
When I redefine the = operation for a class I should redefine its hash method.
My question is about the definition of the hash method. I read that the method must return a SmallInteger whose value is related to the receiver. I would like to know if there is a general pattern adopted in Squeak for that. I get a look in the Squeak classes but it doesn’t help.
Thanks,
Houssam
**____**
**"Le paradis touche les pieds des mères..."**
**>>> http://csl.ensm-douai.fr/fakih**