/[cmucl]/src/code/hash.lisp
ViewVC logotype

Diff of /src/code/hash.lisp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.9 by ram, Fri Feb 8 13:33:08 1991 UTC revision 1.9.2.3 by wlott, Wed Jul 31 16:36:57 1991 UTC
# Line 20  Line 20 
20            hash-table-count sxhash            hash-table-count sxhash
21            with-hash-table-iterator))            with-hash-table-iterator))
22    
23  ;;; Vector subtype codes.  ;;; Hash-values are all positive fixnums:
24    (deftype hash-value ()
25  (defconstant valid-hashing 2)    '(and fixnum (unsigned-byte)))
 (defconstant must-rehash 3)  
   
   
 ;;; What a hash-table is:  
26    
27  (defstruct (hash-table (:constructor make-hash-table-structure)  (defstruct (hash-table (:constructor make-hash-table-structure)
28                         (:conc-name hash-table-)                         (:conc-name hash-table-)
29                         (:print-function %print-hash-table))                         (:print-function %print-hash-table))
30    "Structure used to implement hash tables."    "Structure used to implement hash tables."
31    (kind 'eq)    (kind 'eq :type (member eq eql equal))
32    (size 65 :type fixnum)    (rehash-size 101 :type real)                  ; might be a float
   (rehash-size 101)                             ; might be a float  
33    (rehash-threshold 57 :type fixnum)    (rehash-threshold 57 :type fixnum)
34    (number-entries 0 :type fixnum)    (number-entries 0 :type fixnum)
35    (table () :type simple-vector))    (table (required-argument) :type simple-vector)
36      (needing-rehash nil :type list))
37  ;;; A hash-table-table is a vector of association lists.  When an  ;;;
 ;;; entry is made in a hash table, a pair of (key . value) is consed onto  
 ;;; the element in the vector arrived at by hashing.  
   
 ;;; How to print one:  
   
38  (defun %print-hash-table (structure stream depth)  (defun %print-hash-table (structure stream depth)
39    (declare (ignore depth))    (declare (ignore depth))
40    (format stream "#<~A Hash Table {~X}>"    (format stream "#<~A Hash Table {~X}>"
# Line 52  Line 42 
42            (system:%primitive make-fixnum structure)))            (system:%primitive make-fixnum structure)))
43    
44    
45    (defstruct (hash-table-bucket
46                (:print-function %print-hash-table-bucket)
47                (:constructor %make-bucket (key hash value next)))
48      (key nil :type t)
49      (hash 0 :type hash-value)
50      (value nil :type t)
51      (next nil :type (or null hash-table-bucket))
52      (scavhook nil :type (or null scavenger-hook)))
53    ;;;
54    (defun %print-hash-table-bucket (bucket stream depth)
55      (declare (ignore depth))
56      (format stream "#<hash-table-bucket ~S->~S>"
57              (hash-table-bucket-key bucket)
58              (hash-table-bucket-value bucket)))
59    
60    (defun make-hash-table-bucket (table key hash value next scav-hook-p)
61      (declare (type hash-table table) (type hash-value hash)
62               (type hash-table-bucket next))
63      (let ((bucket (%make-bucket key hash value next)))
64        (when scav-hook-p
65          (setf (hash-table-bucket-scavhook bucket)
66                (make-scavenger-hook
67                 key
68                 #'(lambda ()
69                     (push bucket (hash-table-needing-rehash table))
70                     (setf (scavenger-hook-value
71                            (hash-table-bucket-scavhook bucket))
72                           nil)))))
73        bucket))
74    
75    
76    
77  ;;; Hashing functions for the three kinds of hash tables:  ;;; Hashing functions for the three kinds of hash tables:
78    
79  (eval-when (compile)  (eval-when (compile eval)
80    
81  (defmacro eq-hash (object)  (defmacro eq-hash (object)
82    "Gives us a hashing of an object such that (eq a b) implies    "Gives us a hashing of an object such that (eq a b) implies
83     (= (eq-hash a) (eq-hash b))"     (= (eq-hash a) (eq-hash b))"
84    `(truly-the (unsigned-byte 24) (%primitive make-fixnum ,object)))    `(values (truly-the hash-value (%primitive make-fixnum ,object))
85               t))
86    
87  (defmacro eql-hash (object)  (defmacro eql-hash (object)
88    "Gives us a hashing of an object such that (eql a b) implies    "Gives us a hashing of an object such that (eql a b) implies
89     (= (eql-hash a) (eql-hash b))"     (= (eql-hash a) (eql-hash b))"
90    `(if (numberp ,object)    `(if (numberp ,object)
91         (logand (truncate ,object) most-positive-fixnum)         (values (%eql-hash object) nil)
92         (truly-the fixnum (%primitive make-fixnum ,object))))         (eq-hash object)))
93    
94  (defmacro equal-hash (object)  (defmacro equal-hash (object)
95    "Gives us a hashing of an object such that (equal a b) implies    "Gives us a hashing of an object such that (equal a b) implies
96     (= (equal-hash a) (equal-hash b))"     (= (equal-hash a) (equal-hash b))"
97    `(sxhash ,object))    `(values (sxhash ,object) nil))
98    
99  )  )
100    
101    (defun %eql-hash (number)
102      (etypecase number
103        (fixnum
104         (logand number most-positive-fixnum))
105        (integer
106         (logand number most-positive-fixnum))
107        (float
108         (%eql-hash (integer-decode-float number)))
109        (ratio
110         (logxor (%eql-hash (numerator number))
111                 (%eql-hash (denominator number))))
112        (complex
113         (logxor (%eql-hash (realpart number))
114                 (%eql-hash (imagpart number))))))
115    
116    
117    (defun hash (table object)
118      (ecase (hash-table-kind table)
119        (eq (eq-hash object))
120        (eql (eql-hash object))
121        (equal (equal-hash object))))
122    
123    
124  ;;; Rehashing functions:  ;;; Rehashing functions:
125    
# Line 82  Line 127 
127    (declare (fixnum num))    (declare (fixnum num))
128    "Almost-Primify returns an almost prime number greater than or equal    "Almost-Primify returns an almost prime number greater than or equal
129     to NUM."     to NUM."
130    (if (= (rem num 2) 0)    (when (zerop (rem num 2))
131        (setq num (+ 1 num)))      (incf num))
132    (if (= (rem num 3) 0)    (when (zerop (rem num 3))
133        (setq num (+ 2 num)))      (incf num 2))
134    (if (= (rem num 7) 0)    (when (zerop (rem num 7))
135        (setq num (+ 4 num)))      (incf num 4))
136    num)    num)
137    
138  (eval-when (compile)  (defun rehash (table)
139      "Rehashes all the entries in the hash table TABLE.  Must only be called
140  (defmacro grow-size (table)     inside a WITHOUT-GCING."
141    "Returns a fixnum for the next size of a growing hash-table."    (let* ((old-vector (hash-table-table table))
142    `(let ((rehash-size (hash-table-rehash-size ,table)))           (old-length (length old-vector))
143       (if (floatp rehash-size)           (rehash-size (hash-table-rehash-size table))
144           (ceiling (* rehash-size (hash-table-size ,table)))           (new-length (if (floatp rehash-size)
145           (+ rehash-size (hash-table-size ,table)))))                           (ceiling (* rehash-size old-length))
146                             (+ rehash-size old-length)))
147  (defmacro grow-rehash-threshold (table new-length)           (new-vector (make-array new-length :initial-element nil)))
148    "Returns the next rehash threshold for the table."      (declare (type simple-vector old-vector new-vector)
149    table               (type index old-length new-length))
150    `,new-length      (flet ((reenter-bucket (bucket)
151  ;  `(ceiling (* (hash-table-rehash-threshold ,table)               (let ((key (hash-table-bucket-key bucket)))
152  ;              (/ ,new-length (hash-table-size ,table))))                 (multiple-value-bind
153    )                     (hashing needs-scav-hook)
154                       (hash table key)
155  (defmacro hash-set (vector key value length hashing-function)                   (let ((index (rem hashing new-length))
156    "Used for rehashing.  Enters the value for the key into the vector                         (value (hash-table-bucket-value bucket)))
157     by hashing.  Never grows the vector.  Assumes the key is not yet                     (setf (svref new-vector index)
158     entered."                           (make-hash-table-bucket table key hashing value
159    `(let ((index (rem (the fixnum (funcall ,hashing-function ,key))                                                   (svref new-vector index)
160                       (the fixnum ,length))))                                                   needs-scav-hook)))))))
161       (declare (fixnum index))        (dotimes (i old-length)
162       (setf (aref (the simple-vector ,vector) index)          (do ((bucket (aref old-vector i) (hash-table-bucket-next bucket)))
163             (cons (cons ,key ,value)              ((null bucket))
164                   (aref (the simple-vector ,vector) index)))))            (reenter-bucket bucket))
165            (setf (aref old-vector i) nil))
166          (dolist (bucket (hash-table-needing-rehash table))
167            (reenter-bucket bucket)))
168        (setf (hash-table-table table) new-vector)
169        (setf (hash-table-needing-rehash table) nil)
170        (when (> new-length old-length)
171          (setf (hash-table-rehash-threshold table) new-length))))
172    
173    (defun find-bucket (hash-table bucket-list key hashing)
174      (declare (type hash-table hash-table)
175               (type (or hash-table-bucket null) bucket-list)
176               (type hash-value hashing))
177      (flet ((frob (test)
178               (do ((prev nil bucket)
179                    (bucket bucket-list (hash-table-bucket-next bucket)))
180                   ((or (null bucket)
181                        (and (= hashing (hash-table-bucket-hash bucket))
182                             (funcall test (hash-table-bucket-key bucket) key)))
183                    (values bucket prev)))))
184        (declare (inline frob))
185        (ecase (hash-table-kind hash-table)
186          (equal (frob #'equal))
187          (eql (frob #'eql))
188          (eq (frob #'eq)))))
189    
190    (defun flush-needing-rehash (table)
191      (declare (type hash-table table))
192      (let* ((vector (hash-table-table table))
193             (length (length vector)))
194        (declare (type simple-vector vector)
195                 (type index length))
196        (dolist (bucket (hash-table-needing-rehash table))
197          (declare (type (or null hash-table-bucket) bucket))
198          (let ((index (rem (hash-table-bucket-hash bucket) length)))
199            (declare (type index index))
200            (do ((prev nil ptr)
201                 (ptr (svref vector index) (hash-table-bucket-next ptr)))
202                ((or (null bucket) (eq bucket ptr))
203                 (unless bucket
204                   (error "Can't find the bucket in the hash table.  ~
205                           Something is broken bigtime."))
206                 (if prev
207                     (setf (hash-table-bucket-next prev)
208                           (hash-table-bucket-next bucket))
209                     (setf (svref vector index)
210                           (hash-table-bucket-next bucket))))
211              (declare (type (or null hash-table-bucket) prev ptr))))
212          (let* ((key (hash-table-bucket-key bucket))
213                 (hashing (hash table key))
214                 (index (rem hashing length)))
215            (declare (type hash-value hashing) (type index index))
216            (setf (hash-table-bucket-next bucket) (svref vector index))
217            (setf (svref vector index) bucket)
218            (setf (scavenger-hook-value (hash-table-bucket-scavhook bucket)) key)
219            (setf (hash-table-bucket-hash bucket) hashing))))
220      (setf (hash-table-needing-rehash table) nil))
221    
 )  
222    
 (defun rehash (structure hash-vector new-length)  
   (declare (simple-vector hash-vector))  
   (declare (fixnum new-length))  
   "Rehashes a hash table and replaces the TABLE entry in the structure if  
    someone hasn't done so already.  New vector is of NEW-LENGTH."  
   (do ((new-vector (make-array new-length :initial-element nil))  
        (i 0 (1+ i))  
        (size (hash-table-size structure))  
        (hashing-function (case (hash-table-kind structure)  
                            (eq #'(lambda (x) (eq-hash x)))  
                            (eql #'(lambda (x) (eql-hash x)))  
                            (equal #'(lambda (x) (equal-hash x))))))  
       ((= i size)  
        (cond ((eq hash-vector (hash-table-table structure))  
               (cond ((> new-length size)  
                      (setf (hash-table-table structure) new-vector)  
                      (setf (hash-table-rehash-threshold structure)  
                            (grow-rehash-threshold structure new-length))  
                      (setf (hash-table-size structure) new-length))  
                     (t  
                      (setf (hash-table-table structure) new-vector)))  
               (if (not (eq (hash-table-kind structure) 'equal))  
                   (%primitive set-vector-subtype new-vector  
                               valid-hashing)))))  
     (declare (fixnum i size))  
     (do ((bucket (aref hash-vector i) (cdr bucket)))  
         ((null bucket))  
       (hash-set new-vector (caar bucket) (cdar bucket) new-length  
                 hashing-function))  
     (setf (aref hash-vector i) nil)))  
   
 ;;; Macros for Gethash, %Puthash, and Remhash:  
   
 (eval-when (compile)  
   
 ;;; Hashop dispatches on the kind of hash table we've got, rehashes if  
 ;;; necessary, and binds Vector to the hash vector, Index to the index  
 ;;; into that vector that the Key points to, and Size to the size of the  
 ;;; hash vector.  Since Equal hash tables only need to be maybe rehashed  
 ;;; sometimes, one can tell it if it's one of those times with the  
 ;;; Equal-Needs-To-Rehash-P argument.  
   
 (defmacro hashop (equal-needs-to-rehash-p eq-body eql-body equal-body)  
   `(let* ((vector (hash-table-table hash-table))  
           (size (length vector)))  
      (declare (simple-vector vector) (fixnum size)  
               (inline assoc))  
      (case (hash-table-kind hash-table)  
        (equal  
         ,@(if equal-needs-to-rehash-p `((equal-rehash-if-needed)))  
         (let ((index (rem (the fixnum (equal-hash key)) size)))  
           (declare (fixnum index))  
           ,equal-body))  
        (eq  
         (without-gcing  
           (eq-rehash-if-needed)  
           (let ((index (rem (the fixnum (eq-hash key)) size)))  
             (declare (fixnum index))  
             ,eq-body)))  
        (eql  
         (without-gcing  
           (eq-rehash-if-needed)  
           (let ((index (rem (the fixnum (eql-hash key)) size)))  
             (declare (fixnum index))  
             ,eql-body))))))  
   
 (defmacro eq-rehash-if-needed ()  
   `(let ((subtype (truly-the (unsigned-byte 24)  
                              (%primitive get-vector-subtype vector))))  
      (declare (type (unsigned-byte 24) subtype))  
      (cond ((/= subtype valid-hashing)  
             (rehash hash-table vector size)  
             (setq vector (hash-table-table hash-table)))  
            ((> (hash-table-number-entries hash-table)  
                (hash-table-rehash-threshold hash-table))  
             (rehash hash-table vector (grow-size hash-table))  
             (setq vector (hash-table-table hash-table))  
             (setq size (length vector))))))  
   
 (defmacro equal-rehash-if-needed ()  
   `(cond ((> (hash-table-number-entries hash-table)  
              (hash-table-rehash-threshold hash-table))  
           (rehash hash-table vector (grow-size hash-table))  
           (setq vector (hash-table-table hash-table))  
           (setq size (length vector)))))  
   
 (defmacro rehash-if-needed ()  
   `(let ((subtype (truly-the (unsigned-byte 24)  
                              (%primitive get-vector-subtype vector)))  
          (size (length vector)))  
      (declare (type (unsigned-byte 24) subtype)  
               (fixnum size))  
      (cond ((and (not (eq (hash-table-kind hash-table) 'equal))  
                  (/= subtype valid-hashing))  
             (rehash hash-table vector size)  
             (setq vector (hash-table-table hash-table))  
             (setq size (length vector)))  
            ((> (hash-table-number-entries hash-table)  
                (hash-table-rehash-threshold hash-table))  
             (rehash hash-table vector (grow-size hash-table))  
             (setq vector (hash-table-table hash-table))  
             (setq size (length vector))))))  
   
 )  
223    
224  ;;; Making hash tables:  ;;; Making hash tables:
225    
# Line 231  Line 227 
227                               rehash-threshold)                               rehash-threshold)
228    "Creates and returns a hash table.  See manual for details."    "Creates and returns a hash table.  See manual for details."
229    (declare (fixnum size))    (declare (fixnum size))
230    (cond ((eq test #'eq) (setq test 'eq))    (let* ((test (cond ((eq test #'eq) 'eq)
231          ((eq test #'eql) (setq test 'eql))                       ((eq test #'eql) 'eql)
232          ((eq test #'equal) (setq test 'equal)))                       ((eq test #'equal) 'equal)
233    (if (not (member test '(eq eql equal) :test #'eq))                       ((member test '(eq eql equal) :test #'eq)
234        (error "~S is an illegal :Test for hash tables." test))                        test)
235    (setq size (if (<= size 37) 37 (almost-primify size)))                       (t
236    (cond ((null rehash-threshold)                        (error "~S is an illegal :Test for hash tables." test))))
237           (setq rehash-threshold size))           (size (if (<= size 37)
238          ((floatp rehash-threshold)                     37
239           (setq rehash-threshold (ceiling (* rehash-threshold size)))))                     (almost-primify size)))
240    (make-hash-table-structure :size size           (rehash-threshold (cond ((null rehash-threshold)
241                               :rehash-size rehash-size                                    size)
242                                     ((floatp rehash-threshold)
243                                      (ceiling (* rehash-threshold size)))
244                                     (t
245                                      rehash-threshold))))
246      (make-hash-table-structure :rehash-size rehash-size
247                               :rehash-threshold rehash-threshold                               :rehash-threshold rehash-threshold
248                               :table                               :table (make-array size :initial-element nil)
                              (if (eq test 'equal)  
                                  (make-array size :initial-element nil)  
                                  (%primitive set-vector-subtype  
                                              (make-array size  
                                                          :initial-element nil)  
                                              valid-hashing))  
249                               :kind test)))                               :kind test)))
250    
251    
252    
253  ;;; Manipulating hash tables:  ;;; Manipulating hash tables:
254    
# Line 259  Line 256 
256    "Finds the entry in Hash-Table whose key is Key and returns the associated    "Finds the entry in Hash-Table whose key is Key and returns the associated
257     value and T as multiple values, or returns Default and Nil if there is no     value and T as multiple values, or returns Default and Nil if there is no
258     such entry."     such entry."
259    (macrolet ((lookup (test)    (without-gcing
260                 `(let ((cons (assoc key (aref vector index) :test #',test)))      (when (hash-table-needing-rehash hash-table)
261                    (declare (list cons))        (flush-needing-rehash hash-table))
262                    (if cons      (let* ((vector (hash-table-table hash-table))
263                        (values (cdr cons) t)             (size (length vector))
264                        (values default nil)))))             (hashing (hash hash-table key))
265      (hashop nil             (index (rem hashing size))
266        (lookup eq)             (bucket (find-bucket hash-table (svref vector index) key hashing)))
267        (lookup eql)        (if bucket
268        (lookup equal))))            (values (hash-table-bucket-value bucket) t)
269              (values default nil)))))
270    
271    
272  (defun %puthash (key hash-table value)  (defun %puthash (key hash-table value)
273    "Create an entry in HASH-TABLE associating KEY with VALUE; if there already    "Create an entry in HASH-TABLE associating KEY with VALUE; if there already
274     is an entry for KEY, replace it.  Returns VALUE."     is an entry for KEY, replace it.  Returns VALUE."
275    (macrolet ((store (test)    (without-gcing
276                 `(let ((cons (assoc key (aref vector index) :test #',test)))      (cond ((> (hash-table-number-entries hash-table)
277                    (declare (list cons))                (hash-table-rehash-threshold hash-table))
278                    (cond (cons (setf (cdr cons) value))             (rehash hash-table))
279                          (t            ((hash-table-needing-rehash hash-table)
280                           (push (cons key value) (aref vector index))             (flush-needing-rehash hash-table)))
281                           (incf (hash-table-number-entries hash-table))      (let* ((vector (hash-table-table hash-table))
282                           value)))))             (size (length vector)))
283      (hashop t        (multiple-value-bind (hashing scav-hook-p) (hash hash-table key)
284        (store eq)          (let* ((index (rem hashing size))
285        (store eql)                 (bucket (find-bucket hash-table (svref vector index)
286        (store equal))))                                      key hashing)))
287              (if bucket
288                  (setf (hash-table-bucket-value bucket) value)
289                  (setf (svref vector index)
290                        (make-hash-table-bucket hash-table key hashing value
291                                                (svref vector index)
292                                                scav-hook-p)))))))
293      value)
294    
295  (defun remhash (key hash-table)  (defun remhash (key hash-table)
296    "Remove any entry for KEY in HASH-TABLE.  Returns T if such an entry    "Remove any entry for KEY in HASH-TABLE.  Returns T if such an entry
297     existed; () otherwise."     existed, and NIL if not."
298    (hashop nil    (without-gcing
299     (let ((bucket (aref vector index)))          ; EQ case      (when (hash-table-needing-rehash hash-table)
300       (cond ((and bucket (eq (caar bucket) key))        (flush-needing-rehash hash-table))
301              (pop (aref vector index))      (let* ((vector (hash-table-table hash-table))
302              (decf (hash-table-number-entries hash-table))             (size (length vector))
303              t)             (hashing (hash hash-table key))
304             (t             (index (rem hashing size)))
305              (do ((last bucket bucket)        (multiple-value-bind
306                   (bucket (cdr bucket) (cdr bucket)))            (bucket prev)
307                  ((null bucket) ())            (find-bucket hash-table (svref vector index) key hashing)
308                (when (eq (caar bucket) key)          (when bucket
309                  (rplacd last (cdr bucket))            (if prev
310                  (decf (hash-table-number-entries hash-table))                (setf (hash-table-bucket-next prev)
311                  (return t))))))                      (hash-table-bucket-next bucket))
312     (let ((bucket (aref vector index)))          ; EQL case                (setf (svref vector index)
313       (cond ((and bucket (eql (caar bucket) key))                      (hash-table-bucket-next bucket)))
314              (pop (aref vector index))            (decf (hash-table-number-entries hash-table))
315              (decf (hash-table-number-entries hash-table))            t)))))
316              t)  
317             (t  
             (do ((last bucket bucket)  
                  (bucket (cdr bucket) (cdr bucket)))  
                 ((null bucket) ())  
               (when (eql (caar bucket) key)  
                 (rplacd last (cdr bucket))  
                 (decf (hash-table-number-entries hash-table))  
                 (return t))))))  
    (let ((bucket (aref vector index)))          ; EQUAL case  
      (cond ((and bucket (equal (caar bucket) key))  
             (pop (aref vector index))  
             (decf (hash-table-number-entries hash-table))  
             t)  
            (t  
             (do ((last bucket bucket)  
                  (bucket (cdr bucket) (cdr bucket)))  
                 ((null bucket) ())  
               (when (equal (caar bucket) key)  
                 (rplacd last (cdr bucket))  
                 (decf (hash-table-number-entries hash-table))  
                 (return t))))))))  
   
318  (defun maphash (map-function hash-table)  (defun maphash (map-function hash-table)
319    "For each entry in HASH-TABLE, calls MAP-FUNCTION on the key and value    "For each entry in HASH-TABLE, calls MAP-FUNCTION on the key and value
320    of the entry; returns T."    of the entry; returns T."
321    (let ((vector (hash-table-table hash-table)))    (let ((vector (hash-table-table hash-table)))
322      (declare (simple-vector vector))      (declare (simple-vector vector))
323      (rehash-if-needed)      (dotimes (index (length vector))
324      (do ((i 0 (1+ i))        (do ((bucket (aref vector index) (hash-table-bucket-next bucket)))
          (size (hash-table-size hash-table)))  
         ((= i size))  
       (declare (fixnum i size))  
       (do ((bucket (aref vector i) (cdr bucket)))  
325            ((null bucket))            ((null bucket))
326            (funcall map-function
327          (funcall map-function (caar bucket) (cdar bucket))))))                   (hash-table-bucket-key bucket)
328                     (hash-table-bucket-value bucket))))))
329    
330  (defun clrhash (hash-table)  (defun clrhash (hash-table)
331    "Removes all entries of HASH-TABLE and returns the hash table itself."    "Removes all entries of HASH-TABLE and returns the hash table itself."
332      (declare (type hash-table hash-table))
333    (let ((vector (hash-table-table hash-table)))    (let ((vector (hash-table-table hash-table)))
334      (declare (simple-vector vector))      (declare (simple-vector vector))
335      (setf (hash-table-number-entries hash-table) 0)      (setf (hash-table-number-entries hash-table) 0)
336      (do ((i 0 (1+ i))      (dotimes (i (length vector))
337           (size (hash-table-size hash-table)))        (setf (svref vector i) nil))
338          ((= i size) hash-table)      (setf (hash-table-needing-rehash hash-table) nil))
339        (declare (fixnum i size))    hash-table)
       (setf (aref vector i) nil))))  
340    
341  (defun hash-table-count (hash-table)  (defun hash-table-count (hash-table)
342    "Returns the number of entries in the given Hash-Table."    "Returns the number of entries in the given Hash-Table."
343    (hash-table-number-entries hash-table))    (hash-table-number-entries hash-table))
344    
345    
346    
347  ;;; Primitive Hash Function  ;;; Primitive Hash Function
348    
# Line 468  Line 452 
452     invocation, returns three values.  First, whether there are any more objects     invocation, returns three values.  First, whether there are any more objects
453     in the hash-table, second, the key, and third, the value."     in the hash-table, second, the key, and third, the value."
454    (let ((counter (gensym))    (let ((counter (gensym))
455          (pointer (gensym))          (bucket (gensym))
456          (table (gensym))          (table (gensym))
457          (size (gensym))          (size (gensym)))
458          (the-table (gensym)))      `(let* ((,table (hash-table-table ,hash-table))
459      `(let* ((,the-table ,hash-table)              (,size (length ,table))
             (,table (hash-table-table ,the-table))  
             (,size (hash-table-size ,the-table))  
460              (,counter 0)              (,counter 0)
461              (,pointer nil))              (,bucket (svref ,table 0)))
462           (declare (type index ,counter ,size)
463                    (type simple-vector ,table)
464                    (type (or null hash-table-bucket) ,bucket))
465         (macrolet ((,function ()         (macrolet ((,function ()
466                       `(loop                       `(loop
467                          (when (= ,',counter ,',size) (return))                          (when (= ,',counter ,',size)
468                          (let ((bucket (or ,',pointer                            (return))
469                                            (aref ,',table ,',counter))))                          (if ,',bucket
470                            (when bucket                              (return
471                              (cond ((cdr bucket)                               (multiple-value-prog1
472                                     (setf ,',pointer (cdr bucket)))                                   (values t
473                                    (t                                           (hash-table-bucket-key ,',bucket)
474                                     (setf ,',pointer nil)                                           (hash-table-bucket-value ,',bucket))
475                                     (incf ,',counter)))                                 (setf ,',bucket
476                              (return (values t (caar bucket) (cdar bucket)))))                                       (hash-table-bucket-next ,',bucket))))
477                          (incf ,',counter))))                              (setf ,',bucket
478                                      (svref ,table (incf ,',counter)))))))
479           ,@body))))           ,@body))))

Legend:
Removed from v.1.9  
changed lines
  Added in v.1.9.2.3

  ViewVC Help
Powered by ViewVC 1.1.5