Newsgroups: comp.lang.smalltalk
Path: cantaloupe.srv.cs.cmu.edu!das-news2.harvard.edu!oitnews.harvard.edu!purdue!lerc.nasa.gov!magnus.acs.ohio-state.edu!math.ohio-state.edu!howland.reston.ans.net!agate!news.mindlink.net!vanbc.wimsey.com!fonorola!news!dbuck
From: dbuck@infoweb.magi.com (David Buck)
Subject: Re: Set bug in VW 2.0?
Sender: news@magi.com
Message-ID: <DBDMI5.J74@magi.com>
Date: Sat, 8 Jul 1995 02:49:16 GMT
References: <rshapiro-0707951539530001@esb.bbn.com>
Nntp-Posting-Host: infoweb.magi.com
Organization: Magi Data Consulting
Lines: 41

In article <rshapiro-0707951539530001@esb.bbn.com>,
R Shapiro <rshapiro@bbn.com> wrote:
>As I understand the Set class, a set should never add: a member which is
>#= to some existing member. Sets in VW 2.0 don't seem to behave this way.
>Consider the following simple example:
> ... example clipped ...
>Ie, sometimes the set does what it should do, sometimes it doesn't. Have I
>misunderstood the way Set works, or is this in fact a pretty basic bug in
>VW?
>This is the Windows of VW, btw.

Short Answer:
Whenever you implement '=', you must also implement 'hash'.

Long Answer:

Sets and dictionaries in Smalltalk work using a hash table.  When you add 
an element to a Set, it hashes the element by sending it a 'hash' message 
and uses the resulting integer to find a nice spot for it in the Set.  If 
the spot is already occopied, the Set does a sequential search for the 
next available spot.

When trying to find an element in a Set, the element is first hashed to 
find the expected position in the Set.  If the element there isn't equal 
(=) to the element being found, the set is searched sequentially until a 
nil is encountered.

So, in order for Sets and Dictionaries to work, the = and the hash have 
to correspond.  The rule is that any two objects which are equal must 
return the same hash value.  If the objects are not equal, they may or 
may not have the same hash - it doesn't matter.

One other rule with equals methods:  Equals methods shouldn't fail even 
when being compared to objects of a different class.  It's a good idea, 
therefore, to do an isMemberOf: check on the parameter before trying to 
access methods only present for a specific kind of object.

David Buck
dbuck@magi.com
The Object People

