1 ;;;; that part of the implementation of HASH-TABLE which lives solely
2 ;;;; on the target system, not on the cross-compilation host
4 ;;;; This software is part of the SBCL system. See the README file for
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
13 (in-package "SB!IMPL")
17 (eval-when (:compile-toplevel :load-toplevel :execute)
18 (defconstant max-hash sb!xc:most-positive-fixnum))
21 `(integer 0 ,max-hash))
23 ;;; FIXME: Does this always make a nonnegative FIXNUM? If so, then
24 ;;; explain why. If not (or if the reason it always makes a
25 ;;; nonnegative FIXNUM is only the accident that pointers in supported
26 ;;; architectures happen to be in the lower half of the address
27 ;;; space), then fix it.
28 #!-sb-fluid (declaim (inline pointer-hash))
29 (defun pointer-hash (key)
30 (declare (values hash))
31 (truly-the hash (%primitive sb!c:make-fixnum key)))
33 #!-sb-fluid (declaim (inline eq-hash))
35 (declare (values hash (member t nil)))
36 (values (pointer-hash key)
37 (oddp (get-lisp-obj-address key))))
39 #!-sb-fluid (declaim (inline equal-hash))
40 (defun equal-hash (key)
41 (declare (values hash (member t nil)))
42 (values (sxhash key) nil))
44 #!-sb-fluid (declaim (inline eql-hash))
46 (declare (values hash (member t nil)))
51 (defun equalp-hash (key)
52 (declare (values hash (member t nil)))
53 (values (psxhash key) nil))
55 (defun almost-primify (num)
56 (declare (type index num))
58 "Return an almost prime number greater than or equal to NUM."
67 ;;;; user-defined hash table tests
69 (defvar *hash-table-tests* nil)
71 (defun define-hash-table-test (name test-fun hash-fun)
73 "Define a new kind of hash table test."
74 (declare (type symbol name)
75 (type function test-fun hash-fun))
76 (setf *hash-table-tests*
77 (cons (list name test-fun hash-fun)
78 (remove name *hash-table-tests* :test #'eq :key #'car)))
81 ;;;; construction and simple accessors
83 (defconstant +min-hash-table-size+ 16)
85 (defun make-hash-table (&key (test 'eql)
86 (size +min-hash-table-size+)
91 "Create and return a new hash table. The keywords are as follows:
92 :TEST -- Indicates what kind of test to use.
93 :SIZE -- A hint as to how many elements will be put in this hash
95 :REHASH-SIZE -- Indicates how to expand the table when it fills up.
96 If an integer, add space for that many elements. If a floating
97 point number (which must be greater than 1.0), multiply the size
99 :REHASH-THRESHOLD -- Indicates how dense the table can become before
100 forcing a rehash. Can be any positive number <=1, with density
101 approaching zero as the threshold approaches 0. Density 1 means an
102 average of one entry per bucket.
103 :WEAK-P -- (This is an extension from CMU CL, not currently supported
104 in SBCL 0.6.6, but perhaps supported in a future version.) If T,
105 don't keep entries if the key would otherwise be garbage."
106 (declare (type (or function symbol) test))
107 (declare (type unsigned-byte size))
109 (error "stub: unsupported WEAK-P option"))
110 (multiple-value-bind (test test-fun hash-fun)
111 (cond ((or (eq test #'eq) (eq test 'eq))
112 (values 'eq #'eq #'eq-hash))
113 ((or (eq test #'eql) (eq test 'eql))
114 (values 'eql #'eql #'eql-hash))
115 ((or (eq test #'equal) (eq test 'equal))
116 (values 'equal #'equal #'equal-hash))
117 ((or (eq test #'equalp) (eq test 'equalp))
118 (values 'equalp #'equalp #'equalp-hash))
120 ;; FIXME: I'd like to remove *HASH-TABLE-TESTS* stuff.
121 ;; Failing that, I'd like to rename it to
122 ;; *USER-HASH-TABLE-TESTS*.
123 (dolist (info *hash-table-tests*
124 (error "unknown :TEST for MAKE-HASH-TABLE: ~S"
126 (destructuring-bind (test-name test-fun hash-fun) info
127 (when (or (eq test test-name) (eq test test-fun))
128 (return (values test-name test-fun hash-fun)))))))
129 (let* ((size (max +min-hash-table-size+
131 ;; SIZE is just a hint, so if the user asks
132 ;; for a SIZE which'd be too big for us to
133 ;; easily implement, we bump it down.
134 (floor array-dimension-limit 16))))
135 (rehash-size (if (integerp rehash-size)
137 (float rehash-size 1.0)))
138 ;; FIXME: Original REHASH-THRESHOLD default should be 1.0,
139 ;; not 1, to make it easier for the compiler to avoid
141 (rehash-threshold (float rehash-threshold 1.0))
142 (size+1 (1+ size)) ; The first element is not usable.
143 ;; KLUDGE: The most natural way of expressing the below is
144 ;; (round (/ (float size+1) rehash-threshold)), and indeed
145 ;; it was expressed like that until 0.7.0. However,
146 ;; MAKE-HASH-TABLE is called very early in cold-init, and
147 ;; the SPARC has no primitive instructions for rounding,
148 ;; but only for truncating; therefore, we fudge this issue
149 ;; a little. The other uses of truncate, below, similarly
150 ;; used to be round. -- CSR, 2002-10-01
152 ;; Note that this has not yet been audited for
153 ;; correctness. It just seems to work. -- CSR, 2002-11-02
154 (scaled-size (truncate (/ (float size+1) rehash-threshold)))
155 (length (almost-primify (max scaled-size
156 (1+ +min-hash-table-size+))))
157 (index-vector (make-array length
158 :element-type '(unsigned-byte 32)
160 ;; needs to be the same length as the KV vector
161 (next-vector (make-array size+1
162 :element-type '(unsigned-byte 32)))
163 (kv-vector (make-array (* 2 size+1)
164 :initial-element +empty-ht-slot+))
165 (table (%make-hash-table
169 :rehash-size rehash-size
170 :rehash-threshold rehash-threshold
174 :index-vector index-vector
175 :next-vector next-vector
176 :hash-vector (unless (eq test 'eq)
178 :element-type '(unsigned-byte 32)
179 ;; as explained by pmai on
180 ;; openprojects #lisp IRC
181 ;; 2002-07-30: #x80000000 is
182 ;; bigger than any possible nonEQ
183 ;; hash value, and thus indicates
184 ;; an empty slot; and EQ hash
186 ;; HASH-TABLE-HASH-VECTOR
187 :initial-element #x80000000)))))
188 (declare (type index size+1 scaled-size length))
189 ;; Set up the free list, all free. These lists are 0 terminated.
192 (setf (aref next-vector i) (1+ i)))
193 (setf (aref next-vector size) 0)
194 (setf (hash-table-next-free-kv table) 1)
195 (setf (hash-table-needing-rehash table) 0)
196 (setf (aref kv-vector 0) table)
199 (defun hash-table-count (hash-table)
201 "Return the number of entries in the given HASH-TABLE."
202 (declare (type hash-table hash-table)
204 (hash-table-number-entries hash-table))
207 (setf (fdocumentation 'hash-table-rehash-size 'function)
208 "Return the rehash-size HASH-TABLE was created with.")
211 (setf (fdocumentation 'hash-table-rehash-threshold 'function)
212 "Return the rehash-threshold HASH-TABLE was created with.")
214 (defun hash-table-size (hash-table)
216 "Return a size that can be used with MAKE-HASH-TABLE to create a hash
217 table that can hold however many entries HASH-TABLE can hold without
219 (hash-table-rehash-trigger hash-table))
222 (setf (fdocumentation 'hash-table-test 'function)
223 "Return the test HASH-TABLE was created with.")
226 (setf (fdocumentation 'hash-table-weak-p 'function)
227 "Return T if HASH-TABLE will not keep entries for keys that would
228 otherwise be garbage, and NIL if it will.")
230 ;;;; accessing functions
232 ;;; Make new vectors for the table, extending the table based on the
234 (defun rehash (table)
235 (declare (type hash-table table))
236 (let* ((old-kv-vector (hash-table-table table))
237 (old-next-vector (hash-table-next-vector table))
238 (old-hash-vector (hash-table-hash-vector table))
239 (old-size (length old-next-vector))
241 (let ((rehash-size (hash-table-rehash-size table)))
242 (etypecase rehash-size
244 (+ rehash-size old-size))
246 (the index (truncate (* rehash-size old-size)))))))
247 (new-kv-vector (make-array (* 2 new-size)
248 :initial-element +empty-ht-slot+))
249 (new-next-vector (make-array new-size
250 :element-type '(unsigned-byte 32)
252 (new-hash-vector (when old-hash-vector
254 :element-type '(unsigned-byte 32)
255 :initial-element #x80000000)))
256 (old-index-vector (hash-table-index-vector table))
257 (new-length (almost-primify
258 (truncate (/ (float new-size)
259 (hash-table-rehash-threshold table)))))
260 (new-index-vector (make-array new-length
261 :element-type '(unsigned-byte 32)
262 :initial-element 0)))
263 (declare (type index new-size new-length old-size))
265 ;; Disable GC tricks on the OLD-KV-VECTOR.
266 (set-header-data old-kv-vector sb!vm:vector-normal-subtype)
268 ;; Copy over the kv-vector. The element positions should not move
269 ;; in case there are active scans.
270 (dotimes (i (* old-size 2))
271 (declare (type index i))
272 (setf (aref new-kv-vector i) (aref old-kv-vector i)))
274 ;; Copy over the hash-vector.
275 (when old-hash-vector
276 (dotimes (i old-size)
277 (setf (aref new-hash-vector i) (aref old-hash-vector i))))
279 (setf (hash-table-next-free-kv table) 0)
280 (setf (hash-table-needing-rehash table) 0)
281 ;; Rehash all the entries; last to first so that after the pushes
282 ;; the chains are first to last.
283 (do ((i (1- new-size) (1- i)))
285 (let ((key (aref new-kv-vector (* 2 i)))
286 (value (aref new-kv-vector (1+ (* 2 i)))))
287 (cond ((and (eq key +empty-ht-slot+)
288 (eq value +empty-ht-slot+))
289 ;; Slot is empty, push it onto the free list.
290 (setf (aref new-next-vector i)
291 (hash-table-next-free-kv table))
292 (setf (hash-table-next-free-kv table) i))
293 ((and new-hash-vector
294 (not (= (aref new-hash-vector i) #x80000000)))
295 ;; Can use the existing hash value (not EQ based)
296 (let* ((hashing (aref new-hash-vector i))
297 (index (rem hashing new-length))
298 (next (aref new-index-vector index)))
299 (declare (type index index)
301 ;; Push this slot into the next chain.
302 (setf (aref new-next-vector i) next)
303 (setf (aref new-index-vector index) i)))
307 (set-header-data new-kv-vector
308 sb!vm:vector-valid-hashing-subtype)
309 (let* ((hashing (pointer-hash key))
310 (index (rem hashing new-length))
311 (next (aref new-index-vector index)))
312 (declare (type index index)
314 ;; Push this slot onto the next chain.
315 (setf (aref new-next-vector i) next)
316 (setf (aref new-index-vector index) i))))))
317 (setf (hash-table-table table) new-kv-vector)
318 (setf (hash-table-index-vector table) new-index-vector)
319 (setf (hash-table-next-vector table) new-next-vector)
320 (setf (hash-table-hash-vector table) new-hash-vector)
321 ;; Shrink the old vectors to 0 size to help the conservative GC.
322 (shrink-vector old-kv-vector 0)
323 (shrink-vector old-index-vector 0)
324 (shrink-vector old-next-vector 0)
325 (when old-hash-vector
326 (shrink-vector old-hash-vector 0))
327 (setf (hash-table-rehash-trigger table) new-size))
330 ;;; Use the same size as before, re-using the vectors.
331 (defun rehash-without-growing (table)
332 (declare (type hash-table table))
333 (let* ((kv-vector (hash-table-table table))
334 (next-vector (hash-table-next-vector table))
335 (hash-vector (hash-table-hash-vector table))
336 (size (length next-vector))
337 (index-vector (hash-table-index-vector table))
338 (length (length index-vector)))
339 (declare (type index size length)
340 (type (simple-array (unsigned-byte 32) (*))))
342 ;; Disable GC tricks, they will be re-enabled during the re-hash
344 (set-header-data kv-vector sb!vm:vector-normal-subtype)
346 ;; Rehash all the entries.
347 (setf (hash-table-next-free-kv table) 0)
348 (setf (hash-table-needing-rehash table) 0)
350 (setf (aref next-vector i) 0))
352 (setf (aref index-vector i) 0))
353 (do ((i (1- size) (1- i)))
355 (let ((key (aref kv-vector (* 2 i)))
356 (value (aref kv-vector (1+ (* 2 i)))))
357 (cond ((and (eq key +empty-ht-slot+)
358 (eq value +empty-ht-slot+))
359 ;; Slot is empty, push it onto free list.
360 (setf (aref next-vector i) (hash-table-next-free-kv table))
361 (setf (hash-table-next-free-kv table) i))
362 ((and hash-vector (not (= (aref hash-vector i) #x80000000)))
363 ;; Can use the existing hash value (not EQ based)
364 (let* ((hashing (aref hash-vector i))
365 (index (rem hashing length))
366 (next (aref index-vector index)))
367 (declare (type index index))
368 ;; Push this slot into the next chain.
369 (setf (aref next-vector i) next)
370 (setf (aref index-vector index) i)))
374 (set-header-data kv-vector sb!vm:vector-valid-hashing-subtype)
375 (let* ((hashing (pointer-hash key))
376 (index (rem hashing length))
377 (next (aref index-vector index)))
378 (declare (type index index)
380 ;; Push this slot into the next chain.
381 (setf (aref next-vector i) next)
382 (setf (aref index-vector index) i)))))))
385 (defun flush-needing-rehash (table)
386 (let* ((kv-vector (hash-table-table table))
387 (index-vector (hash-table-index-vector table))
388 (next-vector (hash-table-next-vector table))
389 (length (length index-vector)))
390 (do ((next (hash-table-needing-rehash table)))
392 (declare (type index next))
393 (let* ((key (aref kv-vector (* 2 next)))
394 (hashing (pointer-hash key))
395 (index (rem hashing length))
396 (temp (aref next-vector next)))
397 (setf (aref next-vector next) (aref index-vector index))
398 (setf (aref index-vector index) next)
400 (setf (hash-table-needing-rehash table) 0)
403 (defun gethash (key hash-table &optional default)
405 "Finds the entry in HASH-TABLE whose key is KEY and returns the associated
406 value and T as multiple values, or returns DEFAULT and NIL if there is no
407 such entry. Entries can be added using SETF."
408 (declare (type hash-table hash-table)
409 (values t (member t nil)))
411 (cond ((= (get-header-data (hash-table-table hash-table))
412 sb!vm:vector-must-rehash-subtype)
413 (rehash-without-growing hash-table))
414 ((not (zerop (hash-table-needing-rehash hash-table)))
415 (flush-needing-rehash hash-table)))
416 ;; Search for key in the hash table.
417 (multiple-value-bind (hashing eq-based)
418 (funcall (hash-table-hash-fun hash-table) key)
419 (declare (type hash hashing))
420 (let* ((index-vector (hash-table-index-vector hash-table))
421 (length (length index-vector))
422 (index (rem hashing length))
423 (next (aref index-vector index))
424 (table (hash-table-table hash-table))
425 (next-vector (hash-table-next-vector hash-table))
426 (hash-vector (hash-table-hash-vector hash-table))
427 (test-fun (hash-table-test-fun hash-table)))
428 (declare (type index index))
429 ;; Search next-vector chain for a matching key.
430 (if (or eq-based (not hash-vector))
431 (do ((next next (aref next-vector next)))
432 ((zerop next) (values default nil))
433 (declare (type index next))
434 (when (eq key (aref table (* 2 next)))
435 (return (values (aref table (1+ (* 2 next))) t))))
436 (do ((next next (aref next-vector next)))
437 ((zerop next) (values default nil))
438 (declare (type index next))
439 (when (and (= hashing (aref hash-vector next))
440 (funcall test-fun key (aref table (* 2 next))))
442 (return (values (aref table (1+ (* 2 next))) t)))))))))
444 ;;; so people can call #'(SETF GETHASH)
445 (defun (setf gethash) (new-value key table &optional default)
446 (declare (ignore default))
447 (%puthash key table new-value))
449 (defun %puthash (key hash-table value)
450 (declare (type hash-table hash-table))
451 (aver (hash-table-index-vector hash-table))
453 ;; We need to rehash here so that a current key can be found if it
454 ;; exists. Check that there is room for one more entry. May not be
455 ;; needed if the key is already present.
456 (cond ((zerop (hash-table-next-free-kv hash-table))
458 ((= (get-header-data (hash-table-table hash-table))
459 sb!vm:vector-must-rehash-subtype)
460 (rehash-without-growing hash-table))
461 ((not (zerop (hash-table-needing-rehash hash-table)))
462 (flush-needing-rehash hash-table)))
464 ;; Search for key in the hash table.
465 (multiple-value-bind (hashing eq-based)
466 (funcall (hash-table-hash-fun hash-table) key)
467 (declare (type hash hashing))
468 (let* ((index-vector (hash-table-index-vector hash-table))
469 (length (length index-vector))
470 (index (rem hashing length))
471 (next (aref index-vector index))
472 (kv-vector (hash-table-table hash-table))
473 (next-vector (hash-table-next-vector hash-table))
474 (hash-vector (hash-table-hash-vector hash-table))
475 (test-fun (hash-table-test-fun hash-table)))
476 (declare (type index index))
478 (cond ((or eq-based (not hash-vector))
480 (set-header-data kv-vector sb!vm:vector-valid-hashing-subtype))
482 ;; Search next-vector chain for a matching key.
483 (do ((next next (aref next-vector next)))
485 (declare (type index next))
486 (when (eq key (aref kv-vector (* 2 next)))
487 ;; Found, just replace the value.
488 (setf (aref kv-vector (1+ (* 2 next))) value)
489 (return-from %puthash value))))
491 ;; Search next-vector chain for a matching key.
492 (do ((next next (aref next-vector next)))
494 (declare (type index next))
495 (when (and (= hashing (aref hash-vector next))
496 (funcall test-fun key
497 (aref kv-vector (* 2 next))))
498 ;; Found, just replace the value.
499 (setf (aref kv-vector (1+ (* 2 next))) value)
500 (return-from %puthash value)))))
502 ;; Pop a KV slot off the free list
503 (let ((free-kv-slot (hash-table-next-free-kv hash-table)))
504 ;; Double-check for overflow.
505 (aver (not (zerop free-kv-slot)))
506 (setf (hash-table-next-free-kv hash-table)
507 (aref next-vector free-kv-slot))
508 (incf (hash-table-number-entries hash-table))
510 (setf (aref kv-vector (* 2 free-kv-slot)) key)
511 (setf (aref kv-vector (1+ (* 2 free-kv-slot))) value)
513 ;; Setup the hash-vector if necessary.
516 (setf (aref hash-vector free-kv-slot) hashing)
517 (aver (= (aref hash-vector free-kv-slot) #x80000000))))
519 ;; Push this slot into the next chain.
520 (setf (aref next-vector free-kv-slot) next)
521 (setf (aref index-vector index) free-kv-slot)))))
524 (defun remhash (key hash-table)
526 "Remove the entry in HASH-TABLE associated with KEY. Return T if there
527 was such an entry, or NIL if not."
528 (declare (type hash-table hash-table)
529 (values (member t nil)))
531 ;; We need to rehash here so that a current key can be found if it
533 (cond ((= (get-header-data (hash-table-table hash-table))
534 sb!vm:vector-must-rehash-subtype)
535 (rehash-without-growing hash-table))
536 ((not (zerop (hash-table-needing-rehash hash-table)))
537 (flush-needing-rehash hash-table)))
539 ;; Search for key in the hash table.
540 (multiple-value-bind (hashing eq-based)
541 (funcall (hash-table-hash-fun hash-table) key)
542 (declare (type hash hashing))
543 (let* ((index-vector (hash-table-index-vector hash-table))
544 (length (length index-vector))
545 (index (rem hashing length))
546 (next (aref index-vector index))
547 (table (hash-table-table hash-table))
548 (next-vector (hash-table-next-vector hash-table))
549 (hash-vector (hash-table-hash-vector hash-table))
550 (test-fun (hash-table-test-fun hash-table)))
551 (declare (type index index next))
554 ((if (or eq-based (not hash-vector))
555 (eq key (aref table (* 2 next)))
556 (and (= hashing (aref hash-vector next))
557 (funcall test-fun key (aref table (* 2 next)))))
559 ;; FIXME: Substantially the same block of code seems to
560 ;; appear in all three cases. (In the first case, it
561 ;; appear bare; in the other two cases, it's wrapped in
562 ;; DO.) It should be defined in a separate (possibly
563 ;; inline) DEFUN or FLET.
565 ;; Mark slot as empty.
566 (setf (aref table (* 2 next)) +empty-ht-slot+
567 (aref table (1+ (* 2 next))) +empty-ht-slot+)
568 ;; Update the index-vector pointer.
569 (setf (aref index-vector index) (aref next-vector next))
570 ;; Push KV slot onto free chain.
571 (setf (aref next-vector next)
572 (hash-table-next-free-kv hash-table))
573 (setf (hash-table-next-free-kv hash-table) next)
575 (setf (aref hash-vector next) #x80000000))
576 (decf (hash-table-number-entries hash-table))
578 ;; Search next-vector chain for a matching key.
579 ((or eq-based (not hash-vector))
581 (do ((prior next next)
582 (next (aref next-vector next) (aref next-vector next)))
584 (declare (type index next))
585 (when (eq key (aref table (* 2 next)))
586 ;; Mark slot as empty.
587 (setf (aref table (* 2 next)) +empty-ht-slot+
588 (aref table (1+ (* 2 next))) +empty-ht-slot+)
589 ;; Update the prior pointer in the chain to skip this.
590 (setf (aref next-vector prior) (aref next-vector next))
591 ;; Push KV slot onto free chain.
592 (setf (aref next-vector next)
593 (hash-table-next-free-kv hash-table))
594 (setf (hash-table-next-free-kv hash-table) next)
596 (setf (aref hash-vector next) #x80000000))
597 (decf (hash-table-number-entries hash-table))
601 (do ((prior next next)
602 (next (aref next-vector next) (aref next-vector next)))
604 (declare (type index next))
605 (when (and (= hashing (aref hash-vector next))
606 (funcall test-fun key (aref table (* 2 next))))
607 ;; Mark slot as empty.
608 (setf (aref table (* 2 next)) +empty-ht-slot+)
609 (setf (aref table (1+ (* 2 next))) +empty-ht-slot+)
610 ;; Update the prior pointer in the chain to skip this.
611 (setf (aref next-vector prior) (aref next-vector next))
612 ;; Push KV slot onto free chain.
613 (setf (aref next-vector next)
614 (hash-table-next-free-kv hash-table))
615 (setf (hash-table-next-free-kv hash-table) next)
617 (setf (aref hash-vector next) #x80000000))
618 (decf (hash-table-number-entries hash-table))
621 (defun clrhash (hash-table)
623 "This removes all the entries from HASH-TABLE and returns the hash table
625 (let* ((kv-vector (hash-table-table hash-table))
626 (kv-length (length kv-vector))
627 (next-vector (hash-table-next-vector hash-table))
628 (hash-vector (hash-table-hash-vector hash-table))
629 (size (length next-vector))
630 (index-vector (hash-table-index-vector hash-table))
631 (length (length index-vector)))
632 ;; Disable GC tricks.
633 (set-header-data kv-vector sb!vm:vector-normal-subtype)
634 ;; Mark all slots as empty by setting all keys and values to magic
638 (setf (aref kv-vector i) +empty-ht-slot+))
639 (aver (eq (aref kv-vector 0) hash-table))
640 ;; Set up the free list, all free.
643 (setf (aref next-vector i) (1+ i)))
644 (setf (aref next-vector (1- size)) 0)
645 (setf (hash-table-next-free-kv hash-table) 1)
646 (setf (hash-table-needing-rehash hash-table) 0)
647 ;; Clear the index-vector.
649 (setf (aref index-vector i) 0))
650 ;; Clear the hash-vector.
653 (setf (aref hash-vector i) #x80000000))))
654 (setf (hash-table-number-entries hash-table) 0)
659 ;;; FIXME: This should be made into a compiler transform for two reasons:
660 ;;; 1. It would then be available for compiling the entire system,
661 ;;; not only parts of the system which are defined after DEFUN MAPHASH.
662 ;;; 2. It could be conditional on compilation policy, so that
663 ;;; it could be compiled as a full call instead of an inline
664 ;;; expansion when SPACE>SPEED.
665 (declaim (inline maphash))
666 (defun maphash (function-designator hash-table)
668 "For each entry in HASH-TABLE, call the designated two-argument function
669 on the key and value of the entry. Return NIL."
670 (let ((fun (%coerce-callable-to-fun function-designator))
671 (size (length (hash-table-next-vector hash-table))))
672 (declare (type function fun))
675 (declare (type index i))
676 (let* ((kv-vector (hash-table-table hash-table))
677 (key (aref kv-vector (* 2 i)))
678 (value (aref kv-vector (1+ (* 2 i)))))
679 (unless (and (eq key +empty-ht-slot+)
680 (eq value +empty-ht-slot+))
681 (funcall fun key value))))))
683 ;;;; methods on HASH-TABLE
685 ;;; Return a list of keyword args and values to use for MAKE-HASH-TABLE
686 ;;; when reconstructing HASH-TABLE.
687 (defun %hash-table-ctor-args (hash-table)
688 (when (hash-table-weak-p hash-table)
689 ;; FIXME: This might actually work with no trouble, but as of
690 ;; sbcl-0.6.12.10 when this code was written, weak hash tables
691 ;; weren't working yet, so I couldn't test it. When weak hash
692 ;; tables are supported again, this should be fixed.
693 (error "can't dump weak hash tables readably")) ; defensive programming..
694 `(:test ',(hash-table-test hash-table)
695 :size ',(hash-table-size hash-table)
696 :rehash-size ',(hash-table-rehash-size hash-table)
697 :rehash-threshold ',(hash-table-rehash-threshold hash-table)))
699 ;;; Return an association list representing the same data as HASH-TABLE.
700 (defun %hash-table-alist (hash-table)
702 (maphash (lambda (key value)
703 (push (cons key value) result))
707 ;;; Stuff an association list into HASH-TABLE. Return the hash table,
708 ;;; so that we can use this for the *PRINT-READABLY* case in
709 ;;; PRINT-OBJECT (HASH-TABLE T) without having to worry about LET
710 ;;; forms and readable gensyms and stuff.
711 (defun %stuff-hash-table (hash-table alist)
713 (setf (gethash (car x) hash-table) (cdr x)))
716 (def!method print-object ((hash-table hash-table) stream)
717 (declare (type stream stream))
718 (cond ((not *print-readably*)
719 (print-unreadable-object (hash-table stream :type t :identity t)
722 (hash-table-test hash-table)
723 (hash-table-count hash-table))))
725 (error "can't print hash tables readably without *READ-EVAL*"))
727 (with-standard-io-syntax
730 `(%stuff-hash-table (make-hash-table ,@(%hash-table-ctor-args
732 ',(%hash-table-alist hash-table)))))))
734 (def!method make-load-form ((hash-table hash-table) &optional environment)
735 (declare (ignore environment))
736 (values `(make-hash-table ,@(%hash-table-ctor-args hash-table))
737 `(%stuff-hash-table ,hash-table ',(%hash-table-alist hash-table))))