0.6.11.7:
[sbcl.git] / src / code / hash-table.lisp
1 ;;;; the needed-on-the-cross-compilation-host part of HASH-TABLE
2 ;;;; implementation
3
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
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.
12
13 (in-package "SB!IMPL")
14
15 ;;; HASH-TABLE is implemented as a STRUCTURE-OBJECT.
16 (sb!xc:defstruct (hash-table (:constructor %make-hash-table))
17   ;; The type of hash table this is. Only used for printing and as
18   ;; part of the exported interface.
19   (test (required-argument) :type symbol :read-only t)
20   ;; The function used to compare two keys. Returns T if they are the
21   ;; same and NIL if not.
22   (test-fun (required-argument) :type function :read-only t)
23   ;; The function used to compute the hashing of a key. Returns two
24   ;; values: the index hashing and T if that might change with the
25   ;; next GC.
26   (hash-fun (required-argument) :type function :read-only t)
27   ;; how much to grow the hash table by when it fills up. If an index,
28   ;; then add that amount. If a floating point number, then multiply
29   ;; it by that.
30   (rehash-size (required-argument) :type (or index (single-float (1.0)))
31                :read-only t)
32   ;; how full the hash table has to get before we rehash
33   (rehash-threshold (required-argument) :type (single-float (0.0) 1.0)
34                     :read-only t)
35   ;; The number of entries before a rehash, just one less than the
36   ;; size of the next-vector, hash-vector, and half the size of the
37   ;; kv-vector.
38   (rehash-trigger (required-argument) :type index)
39   ;; The current number of entries in the table.
40   (number-entries 0 :type index)
41   ;; The Key-Value pair vector.
42   (table (required-argument) :type simple-vector)
43   ;; True if this is a weak hash table, meaning that key->value
44   ;; mappings will disappear if there are no other references to the
45   ;; key. Note: this only matters if the hash function indicates that
46   ;; the hashing is EQ based.
47   (weak-p nil :type (member t nil))
48   ;; Index into the next-vector, chaining together buckets that need
49   ;; to be rehashed because their hashing is EQ based and the key has
50   ;; been moved by the garbage collector.
51   (needing-rehash 0 :type index)
52   ;; Index into the Next vector chaining together free slots in the KV
53   ;; vector.
54   (next-free-kv 0 :type index)
55   ;; The index vector. This may be larger than the hash size to help
56   ;; reduce collisions.
57   (index-vector (required-argument)
58                 :type (simple-array (unsigned-byte 32) (*)))
59   ;; This table parallels the KV vector, and is used to chain together
60   ;; the hash buckets, the free list, and the values needing rehash, a
61   ;; slot will only ever be in one of these lists.
62   (next-vector (required-argument) :type (simple-array (unsigned-byte 32) (*)))
63   ;; This table parallels the KV table, and can be used to store the
64   ;; hash associated with the key, saving recalculation. Could be
65   ;; useful for EQL, and EQUAL hash tables. This table is not needed
66   ;; for EQ hash tables, and when present the value of #x8000000
67   ;; represents EQ-based hashing on the respective key.
68   (hash-vector nil :type (or null (simple-array (unsigned-byte 32) (*)))))
69 \f
70 (defmacro-mundanely with-hash-table-iterator ((function hash-table) &body body)
71   #!+sb-doc
72   "WITH-HASH-TABLE-ITERATOR ((function hash-table) &body body)
73    provides a method of manually looping over the elements of a hash-table.
74    FUNCTION is bound to a generator-macro that, within the scope of the
75    invocation, returns one or three values. The first value tells whether
76    any objects remain in the hash table. When the first value is non-NIL,
77    the second and third values are the key and the value of the next object."
78   (let ((n-function (gensym "WITH-HASH-TABLE-ITERATOR-")))
79     `(let ((,n-function
80             (let* ((table ,hash-table)
81                    (length (length (hash-table-next-vector table)))
82                    (index 1))
83               (declare (type (mod #.(floor most-positive-fixnum 2)) index))
84               (labels
85                   ((,function ()
86                      ;; (We grab the table again on each iteration just in
87                      ;; case it was rehashed by a PUTHASH.)
88                      (let ((kv-vector (hash-table-table table)))
89                        (do ()
90                            ((>= index length) (values nil))
91                          (let ((key (aref kv-vector (* 2 index)))
92                                (value (aref kv-vector (1+ (* 2 index)))))
93                            (incf index)
94                            (unless (and (eq key +empty-ht-slot+)
95                                         (eq value +empty-ht-slot+))
96                              (return (values t key value))))))))
97                 #',function))))
98       (macrolet ((,function () '(funcall ,n-function)))
99         ,@body))))