Dictionary keys that exist but can't be found
Martin McClure
martin at hand2mouse.com
Thu Apr 11 01:36:41 UTC 2002
At 9:23 PM -0400 4/10/02, Chris Becker wrote:
>Hi all,
>
>I've been running into problems with a Dictionary whose keys are of the
>class Point. When I attempt to locate certain keys with the "at:" method, it
>fails to find them even though inspection shows they are clearly present.
>
>Everything seems to work fine until the dictionary grows in size, and then
>certain keys can no longer be found using "at:". I sent "keys inspect" to
>the dictionary and found nil keys sprinkled throughout, then went digging
>and found where Dictionary "at:" uses "findElementOrNil:", and this method
>stops searching when it hits a nil key.
>
>I've had this problem with the Dictionary class before using a class that I
>created as the key. Are there any caveats with this class I may not be aware
>of?
The most common cause of this kind of thing is: If your key changes
its hash after it is added to a Dictionary (or Set, or any hashed
collection) it will no longer be found, because the Dictionary will
be looking in the wrong place.
The hash of a Point is computed based on its X and Y values. Make
sure you aren't changing the X and Y values of your Points (I doubt
you are, it's not that easy to do, but if you are changing them,
that's the problem).
The Array inside a Dictionary normally contains many nils, and they
are interspersed among the live keys. This is because the position
that an element has in this array depends on its hash.
-Martin
More information about the Squeak-dev
mailing list
|