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 doesnt help.
Thanks,
Houssam
____
"Le paradis touche les pieds des mères..."
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**
Generally
I use
iv hash bitXor: iv2 hash
Following Kent beck best practices
But now I learned that I want to avoid as much as possible to redefine = and hash and if this is just to sorted objects I used SortedCollection sortBlock: [:a :b| a iv > b iv] for example.
I think that identity related issues is one of the most complex in OO Design.
Stef
Le 1 juil. 05 à 11:04, Houssam Fakih a écrit :
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..."
I redefine the '=:' and 'hash' methods of the elements contained in an ordered collection. My aim is to check if anOrderedCollection contains any duplicate object. So I try the following code: (anOrderedCollection size ~~ anOrderedCollection asSet size)
I would be grateful if you might suggest another solution without using the redefinition of the twin methods.
I think that identity related issues is one of the most complex in OO Design.
I'll be interested in documents discussing these issues if any.
Thanks, Houssam ____ "Le paradis touche les pieds des mères..."
-----Message d'origine----- De : squeak-dev-bounces@lists.squeakfoundation.org [mailto:squeak-dev- bounces@lists.squeakfoundation.org] De la part de stéphane ducasse Envoyé : vendredi 1 juillet 2005 12:27 À : The general-purpose Squeak developers list Objet : Re: hash method
Generally
I use
iv hash bitXor: iv2 hash
Following Kent beck best practices
But now I learned that I want to avoid as much as possible to redefine = and hash and if this is just to sorted objects I used SortedCollection sortBlock: [:a :b| a iv > b iv] for example.
I think that identity related issues is one of the most complex in OO Design.
Stef
Le 1 juil. 05 à 11:04, Houssam Fakih a écrit :
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 doesnt help.
Thanks,
Houssam
"Le paradis touche les pieds des mères..."
Houssam Fakih <fakih <at> ensm-douai.fr> writes:
I think that identity related issues is one of the most complex in OO Design.
I'll be interested in documents discussing these issues if any.
Thanks, Houssam ____ "Le paradis touche les pieds des mères..."
Thanks for asking this question, I found it very interesting.
I did find one interesting paper on this subject: http://www.cs.concordia.ca/~grogono/Writings/equality.pdf
cheers, Scott
squeak-dev@lists.squeakfoundation.org