This is the mail archive of the kawa@sources.redhat.com mailing list for the Kawa project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

question about "object"


Thanks, Bruce, for your BRL hashtable code.  The project I'm working
on requires using lists as keys, e.g. '(x y z) might map to 'xyz.
Unfortunately, two lists which are equal? might not have the same hash 
code:

#|kawa:4|# (invoke '(x y z) 'equals '(x y z))
#t
#|kawa:5|# (list (invoke '(x y z) 'hashCode) (invoke '(x y z) 'hashCode))
(1340566 7490205)

Although it would be nice if hash codes were in fact equal for equal
lists, 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).  So, I tried
wrapping the lists using an inner class:

(define (hashtable-key list)
  (object ()
    ((hashCode) :: int
     (list-hash list))
    ((equals obj :: <java.lang.Object>) :: boolean
     (same-list? list (invoke obj 'getList)))
    ((getList)
     list)))

(define (list-hash list) ;hash a list of objects.
  ;; FIXME: should differentiate permutations.
  (apply + (map object-hash list)))

(define (same-list? list1 list2) ;compare two flat lists with eq?.
  (and (= (length list1) (length list2))
       (every eq? list1 list2))) ;every is defined in SLIB

(define (object-hash obj)
  (invoke obj 'hashCode))

This seems to work:

#|kawa:25|# (invoke (hashtable-key '(x y z)) 'equals (hashtable-key '(x y z)))
#t
#|kawa:26|# (list (invoke (hashtable-key '(x y z)) 'hashCode) (invoke (hashtable-key '(x y z)) 'hashCode))
(363 363)

...but not when you actually stick it in a hashtable:

#|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.

Thanks,
--dougo@ccs.neu.edu


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]