reimplementing #= and #hash

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

reimplementing #= and #hash

ching
Hi List,

I have a Class PoolQueue which has instvars 'member' and 'processor'. I find that an instance of PoolQueue when placed in an OrderedCollection can no longer be considered equal. I suppose I have to reimplement #= and #hash to allow PoolQueue instances to be compared.

My problem: how do I implement #hash? Or stated another way, what does #hash accomplish?

Thanks in advance.

Ching.

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: reimplementing #= and #hash

onierstrasz

Hi Ching,

#hash should be reimplemented to make sure that objects considered to  
be equal will end up in the same hash bucket.

The usual trick is to take a bitXor of the hashes of the instance  
variables:

PoolQueue>>hash
        ^ member hash bitXor: processor hash

Hope that helps.
Oscar

On Sep 23, 2007, at 9:52, Ching de la Serna wrote:

> Hi List,
>
> I have a Class PoolQueue which has instvars 'member' and  
> 'processor'. I find that an instance of PoolQueue when placed in an  
> OrderedCollection can no longer be considered equal. I suppose I  
> have to reimplement #= and #hash to allow PoolQueue instances to be  
> compared.
>
> My problem: how do I implement #hash? Or stated another way, what  
> does #hash accomplish?
>
> Thanks in advance.
>
> Ching.
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: reimplementing #= and #hash

Janko Mivšek
In reply to this post by ching
Hi Ching,

#= is test for equality while #== is test for identity. What do you
probably want is to override #= to compare two PoolQueues if they are
equal or not. But what "equal" means in your case is on you to define.
Maybe something like this:

PoolQueue>>= anotherPoolQueue

   ^(self member = anotherPoolQueue member)
        and: [self processor = anotherPoolQueue member]

But be sure that also member and processor instvars understand #= in a
correct way. All numbers, strings, symbols etc. understand equality as
expected.

I hope this helps a bit.
Janko


Ching de la Serna wrote:

> Hi List,
>
> I have a Class PoolQueue which has instvars 'member' and 'processor'. I
> find that an instance of PoolQueue when placed in an OrderedCollection
> can no longer be considered equal. I suppose I have to reimplement #=
> and #hash to allow PoolQueue instances to be compared.
>
> My problem: how do I implement #hash? Or stated another way, what does
> #hash accomplish?
>
> Thanks in advance.
>
> Ching.
>

--
Janko Mivšek
AIDA/Web
Smalltalk Web Application Server
http://www.aidaweb.si
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: reimplementing #= and #hash

ching
In reply to this post by onierstrasz
Hi Oscar,

Thanks for the help. I tried bitAnd: and it seems to work. I will try bitXor: too. Thanks

Ching

On 9/23/07, Oscar Nierstrasz < [hidden email]> wrote:

Hi Ching,

#hash should be reimplemented to make sure that objects considered to
be equal will end up in the same hash bucket.

The usual trick is to take a bitXor of the hashes of the instance
variables:

PoolQueue>>hash
        ^ member hash bitXor: processor hash

Hope that helps.
Oscar

On Sep 23, 2007, at 9:52, Ching de la Serna wrote:

> Hi List,
>
> I have a Class PoolQueue which has instvars 'member' and
> 'processor'. I find that an instance of PoolQueue when placed in an
> OrderedCollection can no longer be considered equal. I suppose I
> have to reimplement #= and #hash to allow PoolQueue instances to be
> compared.
>
> My problem: how do I implement #hash? Or stated another way, what
> does #hash accomplish?
>
> Thanks in advance.
>
> Ching.
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: reimplementing #= and #hash

ching
In reply to this post by Janko Mivšek
Hi Janko,

Thanks for your help. Your code makes it clear.

Ching

On 9/23/07, Janko Mivšek <[hidden email]> wrote:
Hi Ching,

#= is test for equality while #== is test for identity. What do you
probably want is to override #= to compare two PoolQueues if they are
equal or not. But what "equal" means in your case is on you to define.
Maybe something like this:

PoolQueue>>= anotherPoolQueue

   ^(self member = anotherPoolQueue member)
        and: [self processor = anotherPoolQueue member]

But be sure that also member and processor instvars understand #= in a
correct way. All numbers, strings, symbols etc. understand equality as
expected.

I hope this helps a bit.
Janko


Ching de la Serna wrote:

> Hi List,
>
> I have a Class PoolQueue which has instvars 'member' and 'processor'. I
> find that an instance of PoolQueue when placed in an OrderedCollection
> can no longer be considered equal. I suppose I have to reimplement #=
> and #hash to allow PoolQueue instances to be compared.
>
> My problem: how do I implement #hash? Or stated another way, what does
> #hash accomplish?
>
> Thanks in advance.
>
> Ching.
>

--
Janko Mivšek
AIDA/Web
Smalltalk Web Application Server
http://www.aidaweb.si
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners