hashing with collections.

Ralph Boland rboland at porchlight.ca
Tue Dec 16 01:30:49 UTC 2003


Sorry if things are slightly mangled here.
I couldn't get reply to work properly.

Ralph Boland wrote:


 >> Finally, a question.
 >> Would it not be a good idea for all (or at least most) Smalltalk
 >>  Collections (Sequenceable or otherwise) to store the hash value?
 >>
 >> I realize that this means that each at: or at:put: or similar operations
 >> then have to do additional work and perhaps since these operations are
 >> very common it might not actually be a good idea.

Ralph Boland wrote:


Joel Shellman wrote:


 > Except that the hash method for an object should be very fast. And if
 > you can't do it very fast, generally you should cache  the hash code
 > in
 > that object.

 >  So then the at: and at:put operations might not be affected all that 
 >  much.


I just thought of a serious problem with my idea.  If you have
collections within collections then you have the problem that
when the contained collection's hash value is changed so does
the hash value of the collection that contained it.
This is clearly a major problem so the idea of storing hash
values with collections can only work when the hash values of the
elements in a collection become immutable once the collection is
made an element of another collection.
So we can't apply this idea to Smalltalk collections in general.
However, they are still useful when you know that this is the
case.
In fact this is the case for my applications:
constructing finite state machines and LR based parsers.
This suggests that having kinds of collections that store
and update their hash value is useful but you better know
what you are doing when you use them.
I needed them for my application.  I am not sure if they
are useful enough to be made part of the standard
smalltalk class libary.

Ralph




More information about the Squeak-dev mailing list