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)
84 (defconstant +min-hash-table-rehash-threshold+ (float 1/16 1.0))
86 (defun make-hash-table (&key (test 'eql)
87 (size +min-hash-table-size+)
92 "Create and return a new hash table. The keywords are as follows:
93 :TEST -- Indicates what kind of test to use.
94 :SIZE -- A hint as to how many elements will be put in this hash
96 :REHASH-SIZE -- Indicates how to expand the table when it fills up.
97 If an integer, add space for that many elements. If a floating
98 point number (which must be greater than 1.0), multiply the size
100 :REHASH-THRESHOLD -- Indicates how dense the table can become before
101 forcing a rehash. Can be any positive number <=1, with density
102 approaching zero as the threshold approaches 0. Density 1 means an
103 average of one entry per bucket.
104 :WEAK-P -- (This is an extension from CMU CL, not currently supported
105 in SBCL 0.6.6, but perhaps supported in a future version.) If T,
106 don't keep entries if the key would otherwise be garbage."
107 (declare (type (or function symbol) test))
108 (declare (type unsigned-byte size))
110 (error "stub: unsupported WEAK-P option"))
111 (multiple-value-bind (test test-fun hash-fun)
112 (cond ((or (eq test #'eq) (eq test 'eq))
113 (values 'eq #'eq #'eq-hash))
114 ((or (eq test #'eql) (eq test 'eql))
115 (values 'eql #'eql #'eql-hash))
116 ((or (eq test #'equal) (eq test 'equal))
117 (values 'equal #'equal #'equal-hash))
118 ((or (eq test #'equalp) (eq test 'equalp))
119 (values 'equalp #'equalp #'equalp-hash))
121 ;; FIXME: I'd like to remove *HASH-TABLE-TESTS* stuff.
122 ;; Failing that, I'd like to rename it to
123 ;; *USER-HASH-TABLE-TESTS*.
124 (dolist (info *hash-table-tests*
125 (error "unknown :TEST for MAKE-HASH-TABLE: ~S"
127 (destructuring-bind (test-name test-fun hash-fun) info
128 (when (or (eq test test-name) (eq test test-fun))
129 (return (values test-name test-fun hash-fun)))))))
130 (let* ((size (max +min-hash-table-size+
132 ;; SIZE is just a hint, so if the user asks
133 ;; for a SIZE which'd be too big for us to
134 ;; easily implement, we bump it down.
135 (floor array-dimension-limit 1024))))
136 (rehash-size (if (integerp rehash-size)
138 (float rehash-size 1.0)))
139 ;; FIXME: Original REHASH-THRESHOLD default should be 1.0,
140 ;; not 1, to make it easier for the compiler to avoid
142 (rehash-threshold (max +min-hash-table-rehash-threshold+
143 (float rehash-threshold 1.0)))
144 (size+1 (1+ size)) ; The first element is not usable.
145 ;; KLUDGE: The most natural way of expressing the below is
146 ;; (round (/ (float size+1) rehash-threshold)), and indeed
147 ;; it was expressed like that until 0.7.0. However,
148 ;; MAKE-HASH-TABLE is called very early in cold-init, and
149 ;; the SPARC has no primitive instructions for rounding,
150 ;; but only for truncating; therefore, we fudge this issue
151 ;; a little. The other uses of truncate, below, similarly
152 ;; used to be round. -- CSR, 2002-10-01
154 ;; Note that this has not yet been audited for
155 ;; correctness. It just seems to work. -- CSR, 2002-11-02
156 (scaled-size (truncate (/ (float size+1) rehash-threshold)))
157 (length (almost-primify (max scaled-size
158 (1+ +min-hash-table-size+))))
159 (index-vector (make-array length
160 :element-type '(unsigned-byte 32)
162 ;; needs to be the same length as the KV vector
163 (next-vector (make-array size+1
164 :element-type '(unsigned-byte 32)))
165 (kv-vector (make-array (* 2 size+1)
166 :initial-element +empty-ht-slot+))
167 (table (%make-hash-table
171 :rehash-size rehash-size
172 :rehash-threshold rehash-threshold
176 :index-vector index-vector
177 :next-vector next-vector
178 :hash-vector (unless (eq test 'eq)
180 :element-type '(unsigned-byte 32)
181 ;; as explained by pmai on
182 ;; openprojects #lisp IRC
183 ;; 2002-07-30: #x80000000 is
184 ;; bigger than any possible nonEQ
185 ;; hash value, and thus indicates
186 ;; an empty slot; and EQ hash
188 ;; HASH-TABLE-HASH-VECTOR
189 :initial-element #x80000000)))))
190 (declare (type index size+1 scaled-size length))
191 ;; Set up the free list, all free. These lists are 0 terminated.
194 (setf (aref next-vector i) (1+ i)))
195 (setf (aref next-vector size) 0)
196 (setf (hash-table-next-free-kv table) 1)
197 (setf (hash-table-needing-rehash table) 0)
198 (setf (aref kv-vector 0) table)
201 (defun hash-table-count (hash-table)
203 "Return the number of entries in the given HASH-TABLE."
204 (declare (type hash-table hash-table)
206 (hash-table-number-entries hash-table))
209 (setf (fdocumentation 'hash-table-rehash-size 'function)
210 "Return the rehash-size HASH-TABLE was created with.")
213 (setf (fdocumentation 'hash-table-rehash-threshold 'function)
214 "Return the rehash-threshold HASH-TABLE was created with.")
216 (defun hash-table-size (hash-table)
218 "Return a size that can be used with MAKE-HASH-TABLE to create a hash
219 table that can hold however many entries HASH-TABLE can hold without
221 (hash-table-rehash-trigger hash-table))
224 (setf (fdocumentation 'hash-table-test 'function)
225 "Return the test HASH-TABLE was created with.")
228 (setf (fdocumentation 'hash-table-weak-p 'function)
229 "Return T if HASH-TABLE will not keep entries for keys that would
230 otherwise be garbage, and NIL if it will.")
232 ;;;; accessing functions
234 ;;; Make new vectors for the table, extending the table based on the
236 (defun rehash (table)
237 (declare (type hash-table table))
238 (let* ((old-kv-vector (hash-table-table table))
239 (old-next-vector (hash-table-next-vector table))
240 (old-hash-vector (hash-table-hash-vector table))
241 (old-size (length old-next-vector))
243 (let ((rehash-size (hash-table-rehash-size table)))
244 (etypecase rehash-size
246 (+ rehash-size old-size))
248 (the index (truncate (* rehash-size old-size)))))))
249 (new-kv-vector (make-array (* 2 new-size)
250 :initial-element +empty-ht-slot+))
251 (new-next-vector (make-array new-size
252 :element-type '(unsigned-byte 32)
254 (new-hash-vector (when old-hash-vector
256 :element-type '(unsigned-byte 32)
257 :initial-element #x80000000)))
258 (old-index-vector (hash-table-index-vector table))
259 (new-length (almost-primify
260 (truncate (/ (float new-size)
261 (hash-table-rehash-threshold table)))))
262 (new-index-vector (make-array new-length
263 :element-type '(unsigned-byte 32)
264 :initial-element 0)))
265 (declare (type index new-size new-length old-size))
267 ;; Disable GC tricks on the OLD-KV-VECTOR.
268 (set-header-data old-kv-vector sb!vm:vector-normal-subtype)
270 ;; Copy over the kv-vector. The element positions should not move
271 ;; in case there are active scans.
272 (dotimes (i (* old-size 2))
273 (declare (type index i))
274 (setf (aref new-kv-vector i) (aref old-kv-vector i)))
276 ;; Copy over the hash-vector.
277 (when old-hash-vector
278 (dotimes (i old-size)
279 (setf (aref new-hash-vector i) (aref old-hash-vector i))))
281 (setf (hash-table-next-free-kv table) 0)
282 (setf (hash-table-needing-rehash table) 0)
283 ;; Rehash all the entries; last to first so that after the pushes
284 ;; the chains are first to last.
285 (do ((i (1- new-size) (1- i)))
287 (let ((key (aref new-kv-vector (* 2 i)))
288 (value (aref new-kv-vector (1+ (* 2 i)))))
289 (cond ((and (eq key +empty-ht-slot+)
290 (eq value +empty-ht-slot+))
291 ;; Slot is empty, push it onto the free list.
292 (setf (aref new-next-vector i)
293 (hash-table-next-free-kv table))
294 (setf (hash-table-next-free-kv table) i))
295 ((and new-hash-vector
296 (not (= (aref new-hash-vector i) #x80000000)))
297 ;; Can use the existing hash value (not EQ based)
298 (let* ((hashing (aref new-hash-vector i))
299 (index (rem hashing new-length))
300 (next (aref new-index-vector index)))
301 (declare (type index index)
303 ;; Push this slot into the next chain.
304 (setf (aref new-next-vector i) next)
305 (setf (aref new-index-vector index) i)))
309 (set-header-data new-kv-vector
310 sb!vm:vector-valid-hashing-subtype)
311 (let* ((hashing (pointer-hash key))
312 (index (rem hashing new-length))
313 (next (aref new-index-vector index)))
314 (declare (type index index)
316 ;; Push this slot onto the next chain.
317 (setf (aref new-next-vector i) next)
318 (setf (aref new-index-vector index) i))))))
319 (setf (hash-table-table table) new-kv-vector)
320 (setf (hash-table-index-vector table) new-index-vector)
321 (setf (hash-table-next-vector table) new-next-vector)
322 (setf (hash-table-hash-vector table) new-hash-vector)
323 ;; Shrink the old vectors to 0 size to help the conservative GC.
324 (shrink-vector old-kv-vector 0)
325 (shrink-vector old-index-vector 0)
326 (shrink-vector old-next-vector 0)
327 (when old-hash-vector
328 (shrink-vector old-hash-vector 0))
329 (setf (hash-table-rehash-trigger table) new-size))
332 ;;; Use the same size as before, re-using the vectors.
333 (defun rehash-without-growing (table)
334 (declare (type hash-table table))
335 (let* ((kv-vector (hash-table-table table))
336 (next-vector (hash-table-next-vector table))
337 (hash-vector (hash-table-hash-vector table))
338 (size (length next-vector))
339 (index-vector (hash-table-index-vector table))
340 (length (length index-vector)))
341 (declare (type index size length)
342 (type (simple-array (unsigned-byte 32) (*))))
344 ;; Disable GC tricks, they will be re-enabled during the re-hash
346 (set-header-data kv-vector sb!vm:vector-normal-subtype)
348 ;; Rehash all the entries.
349 (setf (hash-table-next-free-kv table) 0)
350 (setf (hash-table-needing-rehash table) 0)
352 (setf (aref next-vector i) 0))
354 (setf (aref index-vector i) 0))
355 (do ((i (1- size) (1- i)))
357 (let ((key (aref kv-vector (* 2 i)))
358 (value (aref kv-vector (1+ (* 2 i)))))
359 (cond ((and (eq key +empty-ht-slot+)
360 (eq value +empty-ht-slot+))
361 ;; Slot is empty, push it onto free list.
362 (setf (aref next-vector i) (hash-table-next-free-kv table))
363 (setf (hash-table-next-free-kv table) i))
364 ((and hash-vector (not (= (aref hash-vector i) #x80000000)))
365 ;; Can use the existing hash value (not EQ based)
366 (let* ((hashing (aref hash-vector i))
367 (index (rem hashing length))
368 (next (aref index-vector index)))
369 (declare (type index index))
370 ;; Push this slot into the next chain.
371 (setf (aref next-vector i) next)
372 (setf (aref index-vector index) i)))
376 (set-header-data kv-vector sb!vm:vector-valid-hashing-subtype)
377 (let* ((hashing (pointer-hash key))
378 (index (rem hashing length))
379 (next (aref index-vector index)))
380 (declare (type index index)
382 ;; Push this slot into the next chain.
383 (setf (aref next-vector i) next)
384 (setf (aref index-vector index) i)))))))
387 (defun flush-needing-rehash (table)
388 (let* ((kv-vector (hash-table-table table))
389 (index-vector (hash-table-index-vector table))
390 (next-vector (hash-table-next-vector table))
391 (length (length index-vector)))
392 (do ((next (hash-table-needing-rehash table)))
394 (declare (type index next))
395 (let* ((key (aref kv-vector (* 2 next)))
396 (hashing (pointer-hash key))
397 (index (rem hashing length))
398 (temp (aref next-vector next)))
399 (setf (aref next-vector next) (aref index-vector index))
400 (setf (aref index-vector index) next)
402 (setf (hash-table-needing-rehash table) 0)
405 (defun gethash (key hash-table &optional default)
407 "Finds the entry in HASH-TABLE whose key is KEY and returns the associated
408 value and T as multiple values, or returns DEFAULT and NIL if there is no
409 such entry. Entries can be added using SETF."
410 (declare (type hash-table hash-table)
411 (values t (member t nil)))
413 (cond ((= (get-header-data (hash-table-table hash-table))
414 sb!vm:vector-must-rehash-subtype)
415 (rehash-without-growing hash-table))
416 ((not (zerop (hash-table-needing-rehash hash-table)))
417 (flush-needing-rehash hash-table)))
418 ;; Search for key in the hash table.
419 (multiple-value-bind (hashing eq-based)
420 (funcall (hash-table-hash-fun hash-table) key)
421 (declare (type hash hashing))
422 (let* ((index-vector (hash-table-index-vector hash-table))
423 (length (length index-vector))
424 (index (rem hashing length))
425 (next (aref index-vector index))
426 (table (hash-table-table hash-table))
427 (next-vector (hash-table-next-vector hash-table))
428 (hash-vector (hash-table-hash-vector hash-table))
429 (test-fun (hash-table-test-fun hash-table)))
430 (declare (type index index))
431 ;; Search next-vector chain for a matching key.
432 (if (or eq-based (not hash-vector))
433 (do ((next next (aref next-vector next)))
434 ((zerop next) (values default nil))
435 (declare (type index next))
436 (when (eq key (aref table (* 2 next)))
437 (return (values (aref table (1+ (* 2 next))) t))))
438 (do ((next next (aref next-vector next)))
439 ((zerop next) (values default nil))
440 (declare (type index next))
441 (when (and (= hashing (aref hash-vector next))
442 (funcall test-fun key (aref table (* 2 next))))
444 (return (values (aref table (1+ (* 2 next))) t)))))))))
446 ;;; so people can call #'(SETF GETHASH)
447 (defun (setf gethash) (new-value key table &optional default)
448 (declare (ignore default))
449 (%puthash key table new-value))
451 (defun %puthash (key hash-table value)
452 (declare (type hash-table hash-table))
453 (aver (hash-table-index-vector hash-table))
455 ;; We need to rehash here so that a current key can be found if it
456 ;; exists. Check that there is room for one more entry. May not be
457 ;; needed if the key is already present.
458 (cond ((zerop (hash-table-next-free-kv hash-table))
460 ((= (get-header-data (hash-table-table hash-table))
461 sb!vm:vector-must-rehash-subtype)
462 (rehash-without-growing hash-table))
463 ((not (zerop (hash-table-needing-rehash hash-table)))
464 (flush-needing-rehash hash-table)))
466 ;; Search for key in the hash table.
467 (multiple-value-bind (hashing eq-based)
468 (funcall (hash-table-hash-fun hash-table) key)
469 (declare (type hash hashing))
470 (let* ((index-vector (hash-table-index-vector hash-table))
471 (length (length index-vector))
472 (index (rem hashing length))
473 (next (aref index-vector index))
474 (kv-vector (hash-table-table hash-table))
475 (next-vector (hash-table-next-vector hash-table))
476 (hash-vector (hash-table-hash-vector hash-table))
477 (test-fun (hash-table-test-fun hash-table)))
478 (declare (type index index))
480 (cond ((or eq-based (not hash-vector))
482 (set-header-data kv-vector sb!vm:vector-valid-hashing-subtype))
484 ;; Search next-vector chain for a matching key.
485 (do ((next next (aref next-vector next)))
487 (declare (type index next))
488 (when (eq key (aref kv-vector (* 2 next)))
489 ;; Found, just replace the value.
490 (setf (aref kv-vector (1+ (* 2 next))) value)
491 (return-from %puthash value))))
493 ;; Search next-vector chain for a matching key.
494 (do ((next next (aref next-vector next)))
496 (declare (type index next))
497 (when (and (= hashing (aref hash-vector next))
498 (funcall test-fun key
499 (aref kv-vector (* 2 next))))
500 ;; Found, just replace the value.
501 (setf (aref kv-vector (1+ (* 2 next))) value)
502 (return-from %puthash value)))))
504 ;; Pop a KV slot off the free list
505 (let ((free-kv-slot (hash-table-next-free-kv hash-table)))
506 ;; Double-check for overflow.
507 (aver (not (zerop free-kv-slot)))
508 (setf (hash-table-next-free-kv hash-table)
509 (aref next-vector free-kv-slot))
510 (incf (hash-table-number-entries hash-table))
512 (setf (aref kv-vector (* 2 free-kv-slot)) key)
513 (setf (aref kv-vector (1+ (* 2 free-kv-slot))) value)
515 ;; Setup the hash-vector if necessary.
518 (setf (aref hash-vector free-kv-slot) hashing)
519 (aver (= (aref hash-vector free-kv-slot) #x80000000))))
521 ;; Push this slot into the next chain.
522 (setf (aref next-vector free-kv-slot) next)
523 (setf (aref index-vector index) free-kv-slot)))))
526 (defun remhash (key hash-table)
528 "Remove the entry in HASH-TABLE associated with KEY. Return T if there
529 was such an entry, or NIL if not."
530 (declare (type hash-table hash-table)
531 (values (member t nil)))
533 ;; We need to rehash here so that a current key can be found if it
535 (cond ((= (get-header-data (hash-table-table hash-table))
536 sb!vm:vector-must-rehash-subtype)
537 (rehash-without-growing hash-table))
538 ((not (zerop (hash-table-needing-rehash hash-table)))
539 (flush-needing-rehash hash-table)))
541 ;; Search for key in the hash table.
542 (multiple-value-bind (hashing eq-based)
543 (funcall (hash-table-hash-fun hash-table) key)
544 (declare (type hash hashing))
545 (let* ((index-vector (hash-table-index-vector hash-table))
546 (length (length index-vector))
547 (index (rem hashing length))
548 (next (aref index-vector index))
549 (table (hash-table-table hash-table))
550 (next-vector (hash-table-next-vector hash-table))
551 (hash-vector (hash-table-hash-vector hash-table))
552 (test-fun (hash-table-test-fun hash-table)))
553 (declare (type index index next))
556 ((if (or eq-based (not hash-vector))
557 (eq key (aref table (* 2 next)))
558 (and (= hashing (aref hash-vector next))
559 (funcall test-fun key (aref table (* 2 next)))))
561 ;; FIXME: Substantially the same block of code seems to
562 ;; appear in all three cases. (In the first case, it
563 ;; appear bare; in the other two cases, it's wrapped in
564 ;; DO.) It should be defined in a separate (possibly
565 ;; inline) DEFUN or FLET.
567 ;; Mark slot as empty.
568 (setf (aref table (* 2 next)) +empty-ht-slot+
569 (aref table (1+ (* 2 next))) +empty-ht-slot+)
570 ;; Update the index-vector pointer.
571 (setf (aref index-vector index) (aref next-vector next))
572 ;; Push KV slot onto free chain.
573 (setf (aref next-vector next)
574 (hash-table-next-free-kv hash-table))
575 (setf (hash-table-next-free-kv hash-table) next)
577 (setf (aref hash-vector next) #x80000000))
578 (decf (hash-table-number-entries hash-table))
580 ;; Search next-vector chain for a matching key.
581 ((or eq-based (not hash-vector))
583 (do ((prior next next)
584 (next (aref next-vector next) (aref next-vector next)))
586 (declare (type index next))
587 (when (eq key (aref table (* 2 next)))
588 ;; Mark slot as empty.
589 (setf (aref table (* 2 next)) +empty-ht-slot+
590 (aref table (1+ (* 2 next))) +empty-ht-slot+)
591 ;; Update the prior pointer in the chain to skip this.
592 (setf (aref next-vector prior) (aref next-vector next))
593 ;; Push KV slot onto free chain.
594 (setf (aref next-vector next)
595 (hash-table-next-free-kv hash-table))
596 (setf (hash-table-next-free-kv hash-table) next)
598 (setf (aref hash-vector next) #x80000000))
599 (decf (hash-table-number-entries hash-table))
603 (do ((prior next next)
604 (next (aref next-vector next) (aref next-vector next)))
606 (declare (type index next))
607 (when (and (= hashing (aref hash-vector next))
608 (funcall test-fun key (aref table (* 2 next))))
609 ;; Mark slot as empty.
610 (setf (aref table (* 2 next)) +empty-ht-slot+)
611 (setf (aref table (1+ (* 2 next))) +empty-ht-slot+)
612 ;; Update the prior pointer in the chain to skip this.
613 (setf (aref next-vector prior) (aref next-vector next))
614 ;; Push KV slot onto free chain.
615 (setf (aref next-vector next)
616 (hash-table-next-free-kv hash-table))
617 (setf (hash-table-next-free-kv hash-table) next)
619 (setf (aref hash-vector next) #x80000000))
620 (decf (hash-table-number-entries hash-table))
623 (defun clrhash (hash-table)
625 "This removes all the entries from HASH-TABLE and returns the hash table
627 (let* ((kv-vector (hash-table-table hash-table))
628 (kv-length (length kv-vector))
629 (next-vector (hash-table-next-vector hash-table))
630 (hash-vector (hash-table-hash-vector hash-table))
631 (size (length next-vector))
632 (index-vector (hash-table-index-vector hash-table))
633 (length (length index-vector)))
634 ;; Disable GC tricks.
635 (set-header-data kv-vector sb!vm:vector-normal-subtype)
636 ;; Mark all slots as empty by setting all keys and values to magic
640 (setf (aref kv-vector i) +empty-ht-slot+))
641 (aver (eq (aref kv-vector 0) hash-table))
642 ;; Set up the free list, all free.
645 (setf (aref next-vector i) (1+ i)))
646 (setf (aref next-vector (1- size)) 0)
647 (setf (hash-table-next-free-kv hash-table) 1)
648 (setf (hash-table-needing-rehash hash-table) 0)
649 ;; Clear the index-vector.
651 (setf (aref index-vector i) 0))
652 ;; Clear the hash-vector.
655 (setf (aref hash-vector i) #x80000000))))
656 (setf (hash-table-number-entries hash-table) 0)
661 ;;; FIXME: This should be made into a compiler transform for two reasons:
662 ;;; 1. It would then be available for compiling the entire system,
663 ;;; not only parts of the system which are defined after DEFUN MAPHASH.
664 ;;; 2. It could be conditional on compilation policy, so that
665 ;;; it could be compiled as a full call instead of an inline
666 ;;; expansion when SPACE>SPEED.
667 (declaim (inline maphash))
668 (defun maphash (function-designator hash-table)
670 "For each entry in HASH-TABLE, call the designated two-argument function
671 on the key and value of the entry. Return NIL."
672 (let ((fun (%coerce-callable-to-fun function-designator))
673 (size (length (hash-table-next-vector hash-table))))
674 (declare (type function fun))
677 (declare (type index i))
678 (let* ((kv-vector (hash-table-table hash-table))
679 (key (aref kv-vector (* 2 i)))
680 (value (aref kv-vector (1+ (* 2 i)))))
681 (unless (and (eq key +empty-ht-slot+)
682 (eq value +empty-ht-slot+))
683 (funcall fun key value))))))
685 ;;;; methods on HASH-TABLE
687 ;;; Return a list of keyword args and values to use for MAKE-HASH-TABLE
688 ;;; when reconstructing HASH-TABLE.
689 (defun %hash-table-ctor-args (hash-table)
690 (when (hash-table-weak-p hash-table)
691 ;; FIXME: This might actually work with no trouble, but as of
692 ;; sbcl-0.6.12.10 when this code was written, weak hash tables
693 ;; weren't working yet, so I couldn't test it. When weak hash
694 ;; tables are supported again, this should be fixed.
695 (error "can't dump weak hash tables readably")) ; defensive programming..
696 `(:test ',(hash-table-test hash-table)
697 :size ',(hash-table-size hash-table)
698 :rehash-size ',(hash-table-rehash-size hash-table)
699 :rehash-threshold ',(hash-table-rehash-threshold hash-table)))
701 ;;; Return an association list representing the same data as HASH-TABLE.
702 (defun %hash-table-alist (hash-table)
704 (maphash (lambda (key value)
705 (push (cons key value) result))
709 ;;; Stuff an association list into HASH-TABLE. Return the hash table,
710 ;;; so that we can use this for the *PRINT-READABLY* case in
711 ;;; PRINT-OBJECT (HASH-TABLE T) without having to worry about LET
712 ;;; forms and readable gensyms and stuff.
713 (defun %stuff-hash-table (hash-table alist)
715 (setf (gethash (car x) hash-table) (cdr x)))
718 (def!method print-object ((hash-table hash-table) stream)
719 (declare (type stream stream))
720 (cond ((not *print-readably*)
721 (print-unreadable-object (hash-table stream :type t :identity t)
724 (hash-table-test hash-table)
725 (hash-table-count hash-table))))
727 (error "can't print hash tables readably without *READ-EVAL*"))
729 (with-standard-io-syntax
732 `(%stuff-hash-table (make-hash-table ,@(%hash-table-ctor-args
734 ',(%hash-table-alist hash-table)))))))
736 (def!method make-load-form ((hash-table hash-table) &optional environment)
737 (declare (ignore environment))
738 (values `(make-hash-table ,@(%hash-table-ctor-args hash-table))
739 `(%stuff-hash-table ,hash-table ',(%hash-table-alist hash-table))))