[BUG][FIX] WeakKeyDictionary>>keysAndValuesDo:

Boris Gaertner Boris.Gaertner at gmx.net
Mon Jun 14 13:52:15 UTC 2004


 "Martin Wirblat" <sql.mawi at t-link.de> wrote:

> Hi Richard,
> very interesting, don't you consider it to be a bug that Squeak Sets 
> change their hash values when their elements are changed? I just 
> tested 
> 
>     | hash s |
>     hash := ( s := #( 1 2 3 ) asSet ) hash.
>     s add: 4.
>     hash = s hash
> 
> in Dolphin, VSE and VW and it always evaluated to true. Only Squeak 
> thinks it is false.
In Collection, we have a definition of method hash that has a very
peculiar property:  For small collections, (with 10 elements at most),
the hash values of the elements are used to compute the collection
hash. For larger collections, this is not true. We conclude that
small collections change their hash values when elements are
added (or modified), but large collection do not.
SequenceableCollections are an exception to this rule;
they always use the values of all their elements to compute
the hash vaule.
In Dolphin and VW, all collections use the  identityHash
as an immutable hash value and that is probably a better
solution.

I think Collection>>hash and SequenceableCollection>>hash
are very questionable.
Richards examples are a strong hint that these definitions are
not only questionable, but wrong.

 
> For me it is totally nonintuitive not to be able 
> to put any Set into another one. In fact every object that is 
> explicitly designed to change its contents but which is not 
> maintaining its hash value will give the programmer no intuitive clue 
> that it can't be put in a Set. 
> 
> Regards
> Martin
> 
> Richard A. O'Keefe wrote:
> >
> >Someone wrote:
> > > Seems to be a good idea to me. First I've thought of taking "self" 
> >as > the placeholder, but then the Set couldn't contain itself...
> > 
> >A Set cannot contain itself anyway.  One of the fundamental things 
> >about a Set is that you shouldn't put anything in it whose hash value 
> >will change. Even if you add a Set to itself as the very last change, 
> >the hash value *before* the change will be used to place it, but 
> >afterwards its hash value will be different.
> >
> >    s := Set new.
> >    s hash
> >=>  2589
> >    s add: s
> >    s hash
> >=> WHOOPS, INFINITE LOOP.

The infinite loops disappears for collections with more than
10 elements:
  | s |

 s := (1 to: 11) asSet.
 s add: s.
 s hash

> >
> >In fact, a *lot* of Squeak collections assume they are trees, not 
> >graphs. 
> >    a := Array new: 1.
> >    a at: 1 put: a.
> >    a hash
> >=> WHOOPS, INFINITE LOOP.
> 
> 
We get an infinite loops for arrays of every size, this is
due to the definition of SequenceableCollection>>hash.

Greetings, Boris



More information about the Squeak-dev mailing list