question about "object"
Per Bothner
per@bothner.com
Sun Dec 16 14:32:00 GMT 2001
Doug Orleans <dougo@ccs.neu.edu> writes:
> Unfortunately, two lists which are equal? might not have the same hash
> code:
Could you try the cvs version? It seems to work there.
> Although it would be nice if hash codes were in fact equal for equal
> lists,
It is more than nice - it is required.
> I still need to override the equals method because the keys are
> actually lists of objects which might be circular data structures
> (which causes equal? to go into an infinite loop).
That may be more difficult. In general, it might be quite expensive
to handle self-referential structures correctly. The general trick
is you keep a table of the objects you've seen, and when you see
an object in the table, you know you have a cycle. For hashCode we
can handle circular lists by taking the hashCode of (say) the
first 10 car value. Handling equals is trickier - it might be possile
to use a variant of the list-length algorithm.
> #|kawa:27|# (define ht (make <java.util.HashMap>))
> #|kawa:28|# (invoke ht 'put (hashtable-key '(x y z)) 'xyz)
> #!null
> #|kawa:29|# (invoke ht 'containsKey (hashtable-key '(x y z)))
> #f
>
> Can anyone figure out what's going wrong here? I'm not sure how to
> debug this.
Did you verify that:
(equal? (hashtable-key '(x y z)) (hashtable-key '(x y z)))
and
(invoke (hashtable-key '(x y z) 'equals (hashtable-key '(x y z))))
--
--Per Bothner
per@bothner.com http://www.bothner.com/~per/
More information about the Kawa
mailing list