Collection>>Includes:...

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

Collection>>Includes:...

florent trolat
Hi,

I've define an '=' message for an oject and I want to use this object in
a Collection.

I've seen that 'incudes:' message use '=' or not '==' and it's not fun
for me!

I think that '=' message is to compare two objects and  '==' to know if  
the two object are one!

So for me 'includes:' must use '==' or not '='...

May be I have not understand something...

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

Re: Collection>>Includes:...

Mathieu SUEN
florent trolat a écrit :

> Hi,
>
> I've define an '=' message for an oject and I want to use this object in
> a Collection.
>
> I've seen that 'incudes:' message use '=' or not '==' and it's not fun
> for me!
>
> I think that '=' message is to compare two objects and  '==' to know if
> the two object are one!
>
> So for me 'includes:' must use '==' or not '='...
>
> May be I have not understand something...
>
> Thanks to help me....

Hi florent

You can do

***********
= anObject

        ^anObject == self

***********

But it is not pretty so the best way is to remove = and the default
behavior of = is == (unless inherit from someone else...).

But tell us a bit more.
Why do you want = and so on?

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

Re: Collection>>Includes:...

florent trolat
Mathieu a écrit :

> florent trolat a écrit :
>  
>> Hi,
>>
>> I've define an '=' message for an oject and I want to use this object in
>> a Collection.
>>
>> I've seen that 'incudes:' message use '=' or not '==' and it's not fun
>> for me!
>>
>> I think that '=' message is to compare two objects and  '==' to know if
>> the two object are one!
>>
>> So for me 'includes:' must use '==' or not '='...
>>
>> May be I have not understand something...
>>
>> Thanks to help me....
>>    
>
> Hi florent
>
> You can do
>
> ***********
> = anObject
>
> ^anObject == self
>
> ***********
>
> But it is not pretty so the best way is to remove = and the default
> behavior of = is == (unless inherit from someone else...).
>  
Ok I understand ... it was my job to see this, sorry...
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Collection>>Includes:...

David T. Lewis
In reply to this post by florent trolat
On Fri, Aug 04, 2006 at 08:22:40PM +0200, florent trolat wrote:

> Hi,
>
> I've define an '=' message for an oject and I want to use this object in
> a Collection.
>
> I've seen that 'incudes:' message use '=' or not '==' and it's not fun
> for me!
>
> I think that '=' message is to compare two objects and  '==' to know if  
> the two object are one!
>
> So for me 'includes:' must use '==' or not '='...
>
> May be I have not understand something...

Many kinds of collections use '=' for comparison, but there are
also collections that do exactly what you want. For example, an
IdentityDictionary is a Dictionary that uses '==' for comparison.
Look for classes with "Identity" in their names to see them all.

FYI, just as a matter of convention, it is common to refer to
the method 'includes:' as #includes, or as '==' as #==. This
convention comes about because within Smalltalk, a method selector
is a symbol such as #includes:, and this is the way folks usually
refer to them in email.

I'll also mention that if you implement the #= method, you
probably also will want to implement #hash. See the comment in
the #= method of class object (which, by the way, would usually
be written as Object>>= or Object>>hash in an email). Look at
implementors of #hash for examples of what to do.

Dave

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

Re: Collection>>Includes:...

stéphane ducasse-2
In reply to this post by florent trolat
Hi florent

Read K. Beck idiom on redefining = normally you MUST also redefine hash

the pattern Kent suggested was


Book>>= anBook
        (self author = anBook author ) and: [self name = anBook name]

Book>>hash
        self author hash bitXor: self  name hash





Now most of the time I try to use sortedCollection to avoid to have  
to fix the equality of an object,
SortedCollection sortBlock: [:a :b | a name > b name and.....]

On 5 août 06, at 12:56, florent trolat wrote:

> Mathieu a écrit :
>> florent trolat a écrit :
>>
>>> Hi,
>>>
>>> I've define an '=' message for an oject and I want to use this  
>>> object in
>>> a Collection.
>>>
>>> I've seen that 'incudes:' message use '=' or not '==' and it's  
>>> not fun
>>> for me!
>>>
>>> I think that '=' message is to compare two objects and  '==' to  
>>> know if the two object are one!
>>>
>>> So for me 'includes:' must use '==' or not '='...
>>>
>>> May be I have not understand something...
>>>
>>> Thanks to help me....
>>>
>>
>> Hi florent
>>
>> You can do
>>
>> ***********
>> = anObject
>>
>> ^anObject == self
>>
>> ***********
>>
>> But it is not pretty so the best way is to remove = and the default
>> behavior of = is == (unless inherit from someone else...).
>>
> Ok I understand ... it was my job to see this, sorry...
> _______________________________________________
> 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: Collection>>Includes:...

cedreek


2006/8/7, stéphane ducasse <[hidden email]>:
Hi florent

Read K. Beck idiom on redefining = normally you MUST also redefine hash

the pattern Kent suggested was


Book>>= anBook
        (self author = anBook author ) and: [self name = anBook name]     (a)

Book>>hash
        self author hash bitXor: self  name hash     (b)

I'm aware of that... but I just don't understand what's: 'hash bitXor:'

why (a) is not sufficient ?

hash tables doesn't talk to me too much ;). 
What I imagine is that bitXOr gives a logical (byte) representation ?  and these are used in a table (hash) to access objects (pointers)?

Cédrick



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

Re: Collection>>Includes:...

Damien Cassou-3
cdrick wrote:

>
>
> 2006/8/7, stéphane ducasse <[hidden email]
> <mailto:[hidden email]>>:
>
>     Hi florent
>
>     Read K. Beck idiom on redefining = normally you MUST also redefine hash
>
>     the pattern Kent suggested was
>
>
>     Book>>= anBook
>             (self author = anBook author ) and: [self name = anBook
>     name]     (a)
>
>     Book>>hash
>             self author hash bitXor: self  name hash     (b)
>
>
> I'm aware of that... but I just don't understand what's: 'hash bitXor:'
>
> why (a) is not sufficient ?
>
> hash tables doesn't talk to me too much ;).
> What I imagine is that bitXOr gives a logical (byte) representation ?  
> and these are used in a table (hash) to access objects (pointers)?

Set and Dictionaries have a special ways of storing objects. They
compute a position in an array from a number calculated on the object.
This number is returned from the #hash method sent on the object and
manipulated to have an index in the array (using a modulo operator for
example).

http://en.wikipedia.org/wiki/Hash_table

As an example, you want to store aBook in an empty hash table.

aHashTable add: aBook


image the hash table has an array of size 10 filled with nil because the
table is empty.

So, when adding, the hash table will compute the index in which the
object will be stored:


HashTable>>computeIndexFor: anObject
   ^ anObject hash \\ self array size

\\ is the implementation of the modulo function which basically returns
a number between 0 and the parameter based on the receiver.

Then, the add: method is:

HashTable>>add: anObject
   self array
     at: (self computeIndexFor: anObject)
     put: anObject.

This is very very simplified but the idea is there in my opinion.

Now, you should find how the hash table can store more than 10 objects.
Hint: the array does not have to be resized (but sometimes it will for
efficiency).


Is it clearer ? Feel free to ask more
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners