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 ;;; Without the locking the next vector can get cyclic causing
18 ;;; looping in a WITHOUT-GCING form, SHRINK-VECTOR can corrupt memory
19 ;;; and who knows what else.
21 ;;; WITHOUT-GCING implies WITHOUT-INTERRUPTS.
22 (defmacro with-spinlock-and-without-gcing ((spinlock) block &body body)
24 (declare (ignore spinlock))
29 (sb!thread::get-spinlock ,spinlock)
30 (block ,block ,@body))
32 (sb!thread::release-spinlock ,spinlock))))
34 (eval-when (:compile-toplevel :load-toplevel :execute)
35 (defconstant max-hash sb!xc:most-positive-fixnum))
38 `(integer 0 ,max-hash))
40 ;;; FIXME: Does this always make a nonnegative FIXNUM? If so, then
41 ;;; explain why. If not (or if the reason it always makes a
42 ;;; nonnegative FIXNUM is only the accident that pointers in supported
43 ;;; architectures happen to be in the lower half of the address
44 ;;; space), then fix it.
45 #!-sb-fluid (declaim (inline pointer-hash))
46 (defun pointer-hash (key)
47 (declare (values hash))
48 (truly-the hash (%primitive sb!c:make-fixnum key)))
50 #!-sb-fluid (declaim (inline eq-hash))
52 (declare (values hash (member t nil)))
53 (values (pointer-hash key)
54 (oddp (get-lisp-obj-address key))))
56 #!-sb-fluid (declaim (inline equal-hash))
57 (defun equal-hash (key)
58 (declare (values hash (member t nil)))
60 ;; For some types the definition of EQUAL implies a special hash
61 ((or string cons number bit-vector pathname)
62 (values (sxhash key) nil))
63 ;; Otherwise use an EQ hash, rather than SXHASH, since the values
64 ;; of SXHASH will be extremely badly distributed due to the
65 ;; requirements of the spec fitting badly with our implementation
70 #!-sb-fluid (declaim (inline eql-hash))
72 (declare (values hash (member t nil)))
77 (defun equalp-hash (key)
78 (declare (values hash (member t nil)))
80 ;; Types requiring special treatment. Note that PATHNAME and
81 ;; HASH-TABLE are caught by the STRUCTURE-OBJECT test.
82 ((or array cons number character structure-object)
83 (values (psxhash key) nil))
87 (defun almost-primify (num)
88 (declare (type index num))
90 "Return an almost prime number greater than or equal to NUM."
99 ;;;; user-defined hash table tests
101 (defvar *hash-table-tests* nil)
103 (defun define-hash-table-test (name test-fun hash-fun)
105 "Define a new kind of hash table test."
106 (declare (type symbol name)
107 (type function test-fun hash-fun))
108 (setf *hash-table-tests*
109 (cons (list name test-fun hash-fun)
110 (remove name *hash-table-tests* :test #'eq :key #'car)))
113 ;;;; construction and simple accessors
115 (defconstant +min-hash-table-size+ 16)
116 (defconstant +min-hash-table-rehash-threshold+ (float 1/16 1.0))
118 (defun make-hash-table (&key (test 'eql)
119 (size +min-hash-table-size+)
124 "Create and return a new hash table. The keywords are as follows:
125 :TEST -- Indicates what kind of test to use.
126 :SIZE -- A hint as to how many elements will be put in this hash
128 :REHASH-SIZE -- Indicates how to expand the table when it fills up.
129 If an integer, add space for that many elements. If a floating
130 point number (which must be greater than 1.0), multiply the size
132 :REHASH-THRESHOLD -- Indicates how dense the table can become before
133 forcing a rehash. Can be any positive number <=1, with density
134 approaching zero as the threshold approaches 0. Density 1 means an
135 average of one entry per bucket.
136 :WEAKNESS -- IF NIL (the default) it is a normal non-weak hash table.
137 If one of :KEY, :VALUE, :KEY-AND-VALUE, :KEY-OR-VALUE it is a weak
139 Depending on the type of weakness the lack of references to the
140 key and the value may allow for removal of the entry. If WEAKNESS
141 is :KEY and the key would otherwise be garbage the entry is eligible
142 for removal from the hash table. Similarly, if WEAKNESS is :VALUE
143 the life of an entry depends on its value's references. If WEAKNESS
144 is :KEY-AND-VALUE and either the key or the value would otherwise be
145 garbage the entry can be removed. If WEAKNESS is :KEY-OR-VALUE and
146 both the key and the value would otherwise be garbage the entry can
148 (declare (type (or function symbol) test))
149 (declare (type unsigned-byte size))
150 (multiple-value-bind (test test-fun hash-fun)
151 (cond ((or (eq test #'eq) (eq test 'eq))
152 (values 'eq #'eq #'eq-hash))
153 ((or (eq test #'eql) (eq test 'eql))
154 (values 'eql #'eql #'eql-hash))
155 ((or (eq test #'equal) (eq test 'equal))
156 (values 'equal #'equal #'equal-hash))
157 ((or (eq test #'equalp) (eq test 'equalp))
158 (values 'equalp #'equalp #'equalp-hash))
160 ;; FIXME: I'd like to remove *HASH-TABLE-TESTS* stuff.
161 ;; Failing that, I'd like to rename it to
162 ;; *USER-HASH-TABLE-TESTS*.
163 (dolist (info *hash-table-tests*
164 (error "unknown :TEST for MAKE-HASH-TABLE: ~S"
166 (destructuring-bind (test-name test-fun hash-fun) info
167 (when (or (eq test test-name) (eq test test-fun))
168 (return (values test-name test-fun hash-fun)))))))
169 (let* ((size (max +min-hash-table-size+
171 ;; SIZE is just a hint, so if the user asks
172 ;; for a SIZE which'd be too big for us to
173 ;; easily implement, we bump it down.
174 (floor array-dimension-limit 1024))))
175 (rehash-size (if (integerp rehash-size)
177 (float rehash-size 1.0)))
178 ;; FIXME: Original REHASH-THRESHOLD default should be 1.0,
179 ;; not 1, to make it easier for the compiler to avoid
181 (rehash-threshold (max +min-hash-table-rehash-threshold+
182 (float rehash-threshold 1.0)))
183 (size+1 (1+ size)) ; The first element is not usable.
184 ;; KLUDGE: The most natural way of expressing the below is
185 ;; (round (/ (float size+1) rehash-threshold)), and indeed
186 ;; it was expressed like that until 0.7.0. However,
187 ;; MAKE-HASH-TABLE is called very early in cold-init, and
188 ;; the SPARC has no primitive instructions for rounding,
189 ;; but only for truncating; therefore, we fudge this issue
190 ;; a little. The other uses of truncate, below, similarly
191 ;; used to be round. -- CSR, 2002-10-01
193 ;; Note that this has not yet been audited for
194 ;; correctness. It just seems to work. -- CSR, 2002-11-02
195 (scaled-size (truncate (/ (float size+1) rehash-threshold)))
196 (length (almost-primify (max scaled-size
197 (1+ +min-hash-table-size+))))
198 (index-vector (make-array length
200 '(unsigned-byte #.sb!vm:n-word-bits)
202 ;; Needs to be the half the length of the KV vector to link
203 ;; KV entries - mapped to indeces at 2i and 2i+1 -
205 (next-vector (make-array size+1
207 '(unsigned-byte #.sb!vm:n-word-bits)))
208 (kv-vector (make-array (* 2 size+1)
209 :initial-element +empty-ht-slot+))
210 (table (%make-hash-table
214 :rehash-size rehash-size
215 :rehash-threshold rehash-threshold
219 :index-vector index-vector
220 :next-vector next-vector
222 (unless (eq test 'eq)
224 :element-type '(unsigned-byte
226 :initial-element +magic-hash-vector-value+))
227 :spinlock (sb!thread::make-spinlock))))
228 (declare (type index size+1 scaled-size length))
229 ;; Set up the free list, all free. These lists are 0 terminated.
232 (setf (aref next-vector i) (1+ i)))
233 (setf (aref next-vector size) 0)
234 (setf (hash-table-next-free-kv table) 1)
235 (setf (hash-table-needing-rehash table) 0)
236 (setf (aref kv-vector 0) table)
239 (defun hash-table-count (hash-table)
241 "Return the number of entries in the given HASH-TABLE."
242 (declare (type hash-table hash-table)
244 (hash-table-number-entries hash-table))
247 (setf (fdocumentation 'hash-table-rehash-size 'function)
248 "Return the rehash-size HASH-TABLE was created with.")
251 (setf (fdocumentation 'hash-table-rehash-threshold 'function)
252 "Return the rehash-threshold HASH-TABLE was created with.")
254 (defun hash-table-size (hash-table)
256 "Return a size that can be used with MAKE-HASH-TABLE to create a hash
257 table that can hold however many entries HASH-TABLE can hold without
259 (hash-table-rehash-trigger hash-table))
262 (setf (fdocumentation 'hash-table-test 'function)
263 "Return the test HASH-TABLE was created with.")
266 (setf (fdocumentation 'hash-table-weakness 'function)
267 "Return the WEAKNESS of HASH-TABLE which is one of NIL, :KEY,
268 :VALUE, :KEY-AND-VALUE, :KEY-OR-VALUE.")
270 ;;;; accessing functions
272 ;;; Make new vectors for the table, extending the table based on the
274 (defun rehash (table)
275 (declare (type hash-table table))
276 (let* ((old-kv-vector (hash-table-table table))
277 (old-next-vector (hash-table-next-vector table))
278 (old-hash-vector (hash-table-hash-vector table))
279 (old-size (length old-next-vector))
281 (let ((rehash-size (hash-table-rehash-size table)))
282 (etypecase rehash-size
284 (+ rehash-size old-size))
286 (the index (truncate (* rehash-size old-size)))))))
287 (new-kv-vector (make-array (* 2 new-size)
288 :initial-element +empty-ht-slot+))
291 :element-type '(unsigned-byte #.sb!vm:n-word-bits)
294 (when old-hash-vector
296 :element-type '(unsigned-byte #.sb!vm:n-word-bits)
297 :initial-element +magic-hash-vector-value+)))
298 (old-index-vector (hash-table-index-vector table))
299 (new-length (almost-primify
300 (truncate (/ (float new-size)
301 (hash-table-rehash-threshold table)))))
303 (make-array new-length
304 :element-type '(unsigned-byte #.sb!vm:n-word-bits)
305 :initial-element 0)))
306 (declare (type index new-size new-length old-size))
308 ;; Disable GC tricks on the OLD-KV-VECTOR.
309 (set-header-data old-kv-vector sb!vm:vector-normal-subtype)
311 ;; Non-empty weak hash tables always need GC support.
312 (when (and (hash-table-weakness table) (plusp (hash-table-count table)))
313 (set-header-data new-kv-vector sb!vm:vector-valid-hashing-subtype))
315 ;; FIXME: here and in several other places in the hash table code,
316 ;; loops like this one are used when FILL or REPLACE would be
317 ;; appropriate. why are standard CL functions not used?
318 ;; Performance issues? General laziness? -- NJF, 2004-03-10
320 ;; Copy over the kv-vector. The element positions should not move
321 ;; in case there are active scans.
322 (dotimes (i (* old-size 2))
323 (declare (type index i))
324 (setf (aref new-kv-vector i) (aref old-kv-vector i)))
326 ;; Copy over the hash-vector.
327 (when old-hash-vector
328 (dotimes (i old-size)
329 (setf (aref new-hash-vector i) (aref old-hash-vector i))))
331 (setf (hash-table-next-free-kv table) 0)
332 (setf (hash-table-needing-rehash table) 0)
333 ;; Rehash all the entries; last to first so that after the pushes
334 ;; the chains are first to last.
335 (do ((i (1- new-size) (1- i)))
337 (declare (type index/2 i))
338 (let ((key (aref new-kv-vector (* 2 i)))
339 (value (aref new-kv-vector (1+ (* 2 i)))))
340 (cond ((and (eq key +empty-ht-slot+)
341 (eq value +empty-ht-slot+))
342 ;; Slot is empty, push it onto the free list.
343 (setf (aref new-next-vector i)
344 (hash-table-next-free-kv table))
345 (setf (hash-table-next-free-kv table) i))
346 ((and new-hash-vector
347 (not (= (aref new-hash-vector i)
348 +magic-hash-vector-value+)))
349 ;; Can use the existing hash value (not EQ based)
350 (let* ((hashing (aref new-hash-vector i))
351 (index (rem hashing new-length))
352 (next (aref new-index-vector index)))
353 (declare (type index index)
355 ;; Push this slot into the next chain.
356 (setf (aref new-next-vector i) next)
357 (setf (aref new-index-vector index) i)))
361 (set-header-data new-kv-vector
362 sb!vm:vector-valid-hashing-subtype)
363 (let* ((hashing (pointer-hash key))
364 (index (rem hashing new-length))
365 (next (aref new-index-vector index)))
366 (declare (type index index)
368 ;; Push this slot onto the next chain.
369 (setf (aref new-next-vector i) next)
370 (setf (aref new-index-vector index) i))))))
371 (setf (hash-table-table table) new-kv-vector)
372 (setf (hash-table-index-vector table) new-index-vector)
373 (setf (hash-table-next-vector table) new-next-vector)
374 (setf (hash-table-hash-vector table) new-hash-vector)
375 ;; Shrink the old vectors to 0 size to help the conservative GC.
376 (%shrink-vector old-kv-vector 0)
377 (%shrink-vector old-index-vector 0)
378 (%shrink-vector old-next-vector 0)
379 (when old-hash-vector
380 (%shrink-vector old-hash-vector 0))
381 (setf (hash-table-rehash-trigger table) new-size))
384 ;;; Use the same size as before, re-using the vectors.
385 (defun rehash-without-growing (table)
386 (declare (type hash-table table))
387 (let* ((kv-vector (hash-table-table table))
388 (next-vector (hash-table-next-vector table))
389 (hash-vector (hash-table-hash-vector table))
390 (size (length next-vector))
391 (index-vector (hash-table-index-vector table))
392 (length (length index-vector)))
393 (declare (type index size length))
395 ;; Non-empty weak hash tables always need GC support.
396 (unless (and (hash-table-weakness table) (plusp (hash-table-count table)))
397 ;; Disable GC tricks, they will be re-enabled during the re-hash
399 (set-header-data kv-vector sb!vm:vector-normal-subtype))
401 ;; Rehash all the entries.
402 (setf (hash-table-next-free-kv table) 0)
403 (setf (hash-table-needing-rehash table) 0)
405 (setf (aref next-vector i) 0))
407 (setf (aref index-vector i) 0))
408 (do ((i (1- size) (1- i)))
410 (declare (type index/2 i))
411 (let ((key (aref kv-vector (* 2 i)))
412 (value (aref kv-vector (1+ (* 2 i)))))
413 (cond ((and (eq key +empty-ht-slot+)
414 (eq value +empty-ht-slot+))
415 ;; Slot is empty, push it onto free list.
416 (setf (aref next-vector i) (hash-table-next-free-kv table))
417 (setf (hash-table-next-free-kv table) i))
418 ((and hash-vector (not (= (aref hash-vector i)
419 +magic-hash-vector-value+)))
420 ;; Can use the existing hash value (not EQ based)
421 (let* ((hashing (aref hash-vector i))
422 (index (rem hashing length))
423 (next (aref index-vector index)))
424 (declare (type index index))
425 ;; Push this slot into the next chain.
426 (setf (aref next-vector i) next)
427 (setf (aref index-vector index) i)))
431 (set-header-data kv-vector sb!vm:vector-valid-hashing-subtype)
432 (let* ((hashing (pointer-hash key))
433 (index (rem hashing length))
434 (next (aref index-vector index)))
435 (declare (type index index)
437 ;; Push this slot into the next chain.
438 (setf (aref next-vector i) next)
439 (setf (aref index-vector index) i)))))))
442 (defun flush-needing-rehash (table)
443 (let* ((kv-vector (hash-table-table table))
444 (index-vector (hash-table-index-vector table))
445 (next-vector (hash-table-next-vector table))
446 (length (length index-vector)))
447 (do ((next (hash-table-needing-rehash table)))
449 (declare (type index/2 next))
450 (let* ((key (aref kv-vector (* 2 next)))
451 (hashing (pointer-hash key))
452 (index (rem hashing length))
453 (temp (aref next-vector next)))
454 (setf (aref next-vector next) (aref index-vector index))
455 (setf (aref index-vector index) next)
457 (setf (hash-table-needing-rehash table) 0)
460 (defun gethash (key hash-table &optional default)
462 "Finds the entry in HASH-TABLE whose key is KEY and returns the associated
463 value and T as multiple values, or returns DEFAULT and NIL if there is no
464 such entry. Entries can be added using SETF."
465 (declare (type hash-table hash-table)
466 (values t (member t nil)))
467 (gethash3 key hash-table default))
469 (defun gethash2 (key hash-table)
471 "Two argument version of GETHASH"
472 (declare (type hash-table hash-table)
473 (values t (member t nil)))
474 (gethash3 key hash-table nil))
476 (defun gethash3 (key hash-table default)
478 "Three argument version of GETHASH"
479 (declare (type hash-table hash-table)
480 (values t (member t nil)))
481 (with-spinlock-and-without-gcing ((hash-table-spinlock hash-table))
483 (cond ((= (get-header-data (hash-table-table hash-table))
484 sb!vm:vector-must-rehash-subtype)
485 (rehash-without-growing hash-table))
486 ((not (zerop (hash-table-needing-rehash hash-table)))
487 (flush-needing-rehash hash-table)))
489 ;; First check the cache. Use EQ here for speed.
490 (let ((cache (hash-table-cache hash-table))
491 (table (hash-table-table hash-table)))
493 (if (and cache (< cache (length table)) (eq (aref table cache) key))
494 (values (aref table (1+ cache)) t)
496 ;; Search for key in the hash table.
497 (multiple-value-bind (hashing eq-based)
498 (funcall (hash-table-hash-fun hash-table) key)
499 (declare (type hash hashing))
500 (let* ((index-vector (hash-table-index-vector hash-table))
501 (length (length index-vector))
502 (index (rem hashing length))
503 (next (aref index-vector index))
504 (next-vector (hash-table-next-vector hash-table))
505 (hash-vector (hash-table-hash-vector hash-table))
506 (test-fun (hash-table-test-fun hash-table)))
507 (declare (type index index))
508 ;; Search next-vector chain for a matching key.
509 (if (or eq-based (not hash-vector))
510 (do ((next next (aref next-vector next)))
511 ((zerop next) (values default nil))
512 (declare (type index/2 next))
513 (when (eq key (aref table (* 2 next)))
514 (setf (hash-table-cache hash-table) (* 2 next))
515 (return (values (aref table (1+ (* 2 next))) t))))
516 (do ((next next (aref next-vector next)))
517 ((zerop next) (values default nil))
518 (declare (type index/2 next))
519 (when (and (= hashing (aref hash-vector next))
520 (funcall test-fun key (aref table (* 2 next))))
522 (setf (hash-table-cache hash-table) (* 2 next))
523 (return (values (aref table (1+ (* 2 next))) t)))))))))))
525 ;;; so people can call #'(SETF GETHASH)
526 (defun (setf gethash) (new-value key table &optional default)
527 (declare (ignore default))
528 (%puthash key table new-value))
530 (defun %puthash (key hash-table value)
531 (declare (type hash-table hash-table))
532 (aver (hash-table-index-vector hash-table))
533 (with-spinlock-and-without-gcing ((hash-table-spinlock hash-table))
535 ;; We need to rehash here so that a current key can be found if it
536 ;; exists. Check that there is room for one more entry. May not be
537 ;; needed if the key is already present.
538 (cond ((zerop (hash-table-next-free-kv hash-table))
540 ((= (get-header-data (hash-table-table hash-table))
541 sb!vm:vector-must-rehash-subtype)
542 (rehash-without-growing hash-table))
543 ((not (zerop (hash-table-needing-rehash hash-table)))
544 (flush-needing-rehash hash-table)))
546 (let ((cache (hash-table-cache hash-table))
547 (kv-vector (hash-table-table hash-table)))
550 (if (and cache (< cache (length kv-vector))
551 (eq (aref kv-vector cache) key))
552 ;; If cached, just store here
553 (setf (aref kv-vector (1+ cache)) value)
555 ;; Search for key in the hash table.
556 (multiple-value-bind (hashing eq-based)
557 (funcall (hash-table-hash-fun hash-table) key)
558 (declare (type hash hashing))
559 (let* ((index-vector (hash-table-index-vector hash-table))
560 (length (length index-vector))
561 (index (rem hashing length))
562 (next (aref index-vector index))
563 (kv-vector (hash-table-table hash-table))
564 (next-vector (hash-table-next-vector hash-table))
565 (hash-vector (hash-table-hash-vector hash-table))
566 (test-fun (hash-table-test-fun hash-table)))
567 (declare (type index index next))
568 (when (hash-table-weakness hash-table)
569 (set-header-data kv-vector sb!vm:vector-valid-hashing-subtype))
570 (cond ((or eq-based (not hash-vector))
572 (set-header-data kv-vector
573 sb!vm:vector-valid-hashing-subtype))
575 ;; Search next-vector chain for a matching key.
576 (do ((next next (aref next-vector next)))
578 (declare (type index/2 next))
579 (when (eq key (aref kv-vector (* 2 next)))
580 ;; Found, just replace the value.
581 (setf (hash-table-cache hash-table) (* 2 next))
582 (setf (aref kv-vector (1+ (* 2 next))) value)
583 (return-from %puthash value))))
585 ;; Search next-vector chain for a matching key.
586 (do ((next next (aref next-vector next)))
588 (declare (type index/2 next))
589 (when (and (= hashing (aref hash-vector next))
590 (funcall test-fun key
591 (aref kv-vector (* 2 next))))
592 ;; Found, just replace the value.
593 (setf (hash-table-cache hash-table) (* 2 next))
594 (setf (aref kv-vector (1+ (* 2 next))) value)
595 (return-from %puthash value)))))
597 ;; Pop a KV slot off the free list
598 (let ((free-kv-slot (hash-table-next-free-kv hash-table)))
599 (declare (type index/2 free-kv-slot))
600 ;; Double-check for overflow.
601 (aver (not (zerop free-kv-slot)))
602 (setf (hash-table-next-free-kv hash-table)
603 (aref next-vector free-kv-slot))
604 (incf (hash-table-number-entries hash-table))
606 (setf (hash-table-cache hash-table) (* 2 free-kv-slot))
607 (setf (aref kv-vector (* 2 free-kv-slot)) key)
608 (setf (aref kv-vector (1+ (* 2 free-kv-slot))) value)
610 ;; Setup the hash-vector if necessary.
613 (setf (aref hash-vector free-kv-slot) hashing)
614 (aver (= (aref hash-vector free-kv-slot)
615 +magic-hash-vector-value+))))
617 ;; Push this slot into the next chain.
618 (setf (aref next-vector free-kv-slot) next)
619 (setf (aref index-vector index) free-kv-slot)))))))
622 (defun remhash (key hash-table)
624 "Remove the entry in HASH-TABLE associated with KEY. Return T if there
625 was such an entry, or NIL if not."
626 (declare (type hash-table hash-table)
627 (values (member t nil)))
628 (with-spinlock-and-without-gcing ((hash-table-spinlock hash-table))
630 ;; We need to rehash here so that a current key can be found if it
632 (cond ((= (get-header-data (hash-table-table hash-table))
633 sb!vm:vector-must-rehash-subtype)
634 (rehash-without-growing hash-table))
635 ((not (zerop (hash-table-needing-rehash hash-table)))
636 (flush-needing-rehash hash-table)))
638 ;; For now, just clear the cache
639 (setf (hash-table-cache hash-table) nil)
641 ;; Search for key in the hash table.
642 (multiple-value-bind (hashing eq-based)
643 (funcall (hash-table-hash-fun hash-table) key)
644 (declare (type hash hashing))
645 (let* ((index-vector (hash-table-index-vector hash-table))
646 (length (length index-vector))
647 (index (rem hashing length))
648 (next (aref index-vector index))
649 (table (hash-table-table hash-table))
650 (next-vector (hash-table-next-vector hash-table))
651 (hash-vector (hash-table-hash-vector hash-table))
652 (test-fun (hash-table-test-fun hash-table)))
653 (declare (type index index)
655 (flet ((clear-slot (chain-vector prior-slot-location slot-location)
656 (declare (type index/2 slot-location))
657 ;; Mark slot as empty.
658 (setf (aref table (* 2 slot-location)) +empty-ht-slot+
659 (aref table (1+ (* 2 slot-location))) +empty-ht-slot+)
660 ;; Update the prior pointer in the chain to skip this.
661 (setf (aref chain-vector prior-slot-location)
662 (aref next-vector slot-location))
663 ;; Push KV slot onto free chain.
664 (setf (aref next-vector slot-location)
665 (hash-table-next-free-kv hash-table))
666 (setf (hash-table-next-free-kv hash-table) slot-location)
668 (setf (aref hash-vector slot-location)
669 +magic-hash-vector-value+))
670 (decf (hash-table-number-entries hash-table))
674 ((if (or eq-based (not hash-vector))
675 (eq key (aref table (* 2 next)))
676 (and (= hashing (aref hash-vector next))
677 (funcall test-fun key (aref table (* 2 next)))))
678 (clear-slot index-vector index next))
679 ;; Search next-vector chain for a matching key.
680 ((or eq-based (not hash-vector))
682 (do ((prior next next)
683 (next (aref next-vector next) (aref next-vector next)))
685 (declare (type index next))
686 (when (eq key (aref table (* 2 next)))
687 (return-from remhash (clear-slot next-vector prior next)))))
690 (do ((prior next next)
691 (next (aref next-vector next) (aref next-vector next)))
693 (declare (type index/2 next))
694 (when (and (= hashing (aref hash-vector next))
695 (funcall test-fun key (aref table (* 2 next))))
697 (clear-slot next-vector prior next)))))))))))
699 (defun clrhash (hash-table)
701 "This removes all the entries from HASH-TABLE and returns the hash table
703 (declare (optimize speed))
704 (with-spinlock-and-without-gcing ((hash-table-spinlock hash-table))
706 (let* ((kv-vector (hash-table-table hash-table))
707 (next-vector (hash-table-next-vector hash-table))
708 (hash-vector (hash-table-hash-vector hash-table))
709 (size (length next-vector))
710 (index-vector (hash-table-index-vector hash-table)))
711 ;; Disable GC tricks.
712 (set-header-data kv-vector sb!vm:vector-normal-subtype)
713 ;; Mark all slots as empty by setting all keys and values to magic
715 (aver (eq (aref kv-vector 0) hash-table))
716 (fill kv-vector +empty-ht-slot+ :start 2)
717 ;; Set up the free list, all free.
720 (setf (aref next-vector i) (1+ i)))
721 (setf (aref next-vector (1- size)) 0)
722 (setf (hash-table-next-free-kv hash-table) 1)
723 (setf (hash-table-needing-rehash hash-table) 0)
724 ;; Clear the index-vector.
725 (fill index-vector 0)
726 ;; Clear the hash-vector.
728 (fill hash-vector +magic-hash-vector-value+)))
729 (setf (hash-table-cache hash-table) nil)
730 (setf (hash-table-number-entries hash-table) 0))
735 ;;; FIXME: This should be made into a compiler transform for two reasons:
736 ;;; 1. It would then be available for compiling the entire system,
737 ;;; not only parts of the system which are defined after DEFUN MAPHASH.
738 ;;; 2. It could be conditional on compilation policy, so that
739 ;;; it could be compiled as a full call instead of an inline
740 ;;; expansion when SPACE>SPEED.
741 (declaim (inline maphash))
742 (defun maphash (function-designator hash-table)
744 "For each entry in HASH-TABLE, call the designated two-argument function on
745 the key and value of the entry. Return NIL."
746 ;; This essentially duplicates WITH-HASH-TABLE-ITERATOR, so
747 ;; any changes here should be reflected there as well.
748 (let ((fun (%coerce-callable-to-fun function-designator))
749 (size (length (hash-table-next-vector hash-table))))
750 (declare (type function fun))
753 (declare (type index/2 i))
754 (let* ((kv-vector (hash-table-table hash-table))
755 (key (aref kv-vector (* 2 i)))
756 (value (aref kv-vector (1+ (* 2 i)))))
757 ;; We are running without locking or WITHOUT-GCING. For a weak
758 ;; :VALUE hash table it's possible that the GC hit after KEY
759 ;; was read and now the entry is gone. So check if either the
760 ;; key or the value is empty.
761 (unless (or (eq key +empty-ht-slot+)
762 (eq value +empty-ht-slot+))
763 (funcall fun key value))))))
765 ;;;; methods on HASH-TABLE
767 ;;; Return a list of keyword args and values to use for MAKE-HASH-TABLE
768 ;;; when reconstructing HASH-TABLE.
769 (defun %hash-table-ctor-args (hash-table)
770 `(:test ',(hash-table-test hash-table)
771 :size ',(hash-table-size hash-table)
772 :rehash-size ',(hash-table-rehash-size hash-table)
773 :rehash-threshold ',(hash-table-rehash-threshold hash-table)
774 :weakness ',(hash-table-weakness hash-table)))
776 ;;; Return an association list representing the same data as HASH-TABLE.
777 (defun %hash-table-alist (hash-table)
779 (maphash (lambda (key value)
780 (push (cons key value) result))
784 ;;; Stuff an association list into HASH-TABLE. Return the hash table,
785 ;;; so that we can use this for the *PRINT-READABLY* case in
786 ;;; PRINT-OBJECT (HASH-TABLE T) without having to worry about LET
787 ;;; forms and readable gensyms and stuff.
788 (defun %stuff-hash-table (hash-table alist)
790 (setf (gethash (car x) hash-table) (cdr x)))
793 (def!method print-object ((hash-table hash-table) stream)
794 (declare (type stream stream))
795 (cond ((or (not *print-readably*) (not *read-eval*))
796 (print-unreadable-object (hash-table stream :type t :identity t)
799 (hash-table-test hash-table)
800 (hash-table-count hash-table))))
802 (with-standard-io-syntax
805 `(%stuff-hash-table (make-hash-table ,@(%hash-table-ctor-args
807 ',(%hash-table-alist hash-table)))))))
809 (def!method make-load-form ((hash-table hash-table) &optional environment)
810 (declare (ignore environment))
811 (values `(make-hash-table ,@(%hash-table-ctor-args hash-table))
812 `(%stuff-hash-table ,hash-table ',(%hash-table-alist hash-table))))