[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

hash-tables



I am having trouble with MCL's hash tables. Or at least I think the
problem lies with MCL - perhaps it is my understanding of CL that is
lacking. I am trying to retrieve objects that are uniquely identified via
a tuple, rather than a single item for a key. CLtL2 states (with reference
to hash tables): "Keys do not have to be symbols, they can be any Lisp
object". I am using a list containing two values. Here are the details:

(defvar *attribute-table* (make-hash-table :test #'equalp))
;; note the use of #'equalp

;; Insert the first (and only) entry
(setf (gethash (list (<fn> <arg>) (<fn> <arg>)) *attribute-table*) <value>)
;;     where (<fn> <arg>) ==> #<OMEGA-CLASS #x942B09>    
;;                            (this is an instance of a CLOS class)
;;     and <value> ==> #<INSTANCEOF-LINK #x943271>
;;                     (this is an instance of a CLOS class)

;; Bind test variables:
;; t1:
(setq t1 (list (<fn> <arg>) (<fn> <arg>))
;; where (<fn> <arg>) ==> #<OMEGA-CLASS #x942B09>

;; t2:
(maphash #'(lambda (key value)
             (declare (special t2) (ignore value)) 
             (setq t2 key)) 
         *attribute-table*)
;; since there is just one entry in the table, t2 will be bound to the 
;; expected key

;; Now:
t1 ==> (#<OMEGA-CLASS #x942B09> #<OMEGA-CLASS #x942B09>)
t2 ==> (#<OMEGA-CLASS #x942B09> #<OMEGA-CLASS #x942B09>)
(eq t1 t2) ==> NIL
;; but:
(equalp t1 t2) ==> T
;; Okay so far

;; Check further:
(sxhash t1) ==> 11752109
(sxhash t2) ==> 11752109
;; All looks okay...

;; But:
(gethash t1 *attribute-table*) ==> NIL NIL
(gethash t2 *attribute-table*) ==> #<INSTANCEOF-LINK #x943271> T
;; Why???

Can anyone shed light on why this happens? I have tried to reproduce the
problem with a simpler example (e.g. (list 'a 'b) as a key) but it works
as expected. Could it have something to do with the fact that I am using
CLOS instances?

Any help would be appreciated.

Thanks

Martin


-------------------------------------------------------------------------------
Martin Stanley                                  Department of Computer Science
mts@ai.toronto.edu  -or-                        University of Toronto
mts@ai.utoronto.ca                              Pratt Building, Room 264A
                                                6 Kings College Circle
                                                Toronto, Ontario
-------------------------------------------------------------------------------