0.9.3.30:
[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 (missing-arg) :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 (missing-arg) :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 (missing-arg) :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 (missing-arg) :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 (missing-arg) :type (single-float (0.0) 1.0) :read-only t)
34   ;; The number of entries before a rehash, just one less than the
35   ;; size of the next-vector, hash-vector, and half the size of the
36   ;; kv-vector.
37   (rehash-trigger (missing-arg) :type index)
38   ;; The current number of entries in the table.
39   (number-entries 0 :type index)
40   ;; The Key-Value pair vector.
41   (table (missing-arg) :type simple-vector)
42   ;; True if this is a weak hash table, meaning that key->value
43   ;; mappings will disappear if there are no other references to the
44   ;; key. Note: this only matters if the hash function indicates that
45   ;; the hashing is EQ based.
46   (weak-p nil :type (member t nil))
47   ;; Index into the next-vector, chaining together buckets that need
48   ;; to be rehashed because their hashing is EQ based and the key has
49   ;; been moved by the garbage collector.
50   (needing-rehash 0 :type index)
51   ;; Index into the Next vector chaining together free slots in the KV
52   ;; vector.
53   (next-free-kv 0 :type index)
54   ;; A cache that is either nil or is an index into the hash table
55   ;; that should be checked first
56   (cache nil :type (or null index))
57   ;; The index vector. This may be larger than the hash size to help
58   ;; reduce collisions.
59   (index-vector (missing-arg)
60                 :type (simple-array (unsigned-byte #.sb!vm:n-word-bits) (*)))
61   ;; This table parallels the KV vector, and is used to chain together
62   ;; the hash buckets, the free list, and the values needing rehash, a
63   ;; slot will only ever be in one of these lists.
64   (next-vector (missing-arg)
65                :type (simple-array (unsigned-byte #.sb!vm:n-word-bits) (*)))
66   ;; This table parallels the KV table, and can be used to store the
67   ;; hash associated with the key, saving recalculation. Could be
68   ;; useful for EQL, and EQUAL hash tables. This table is not needed
69   ;; for EQ hash tables, and when present the value of #x80000000
70   ;; represents EQ-based hashing on the respective key.
71   (hash-vector nil :type (or null (simple-array (unsigned-byte
72                                                  #.sb!vm:n-word-bits) (*)))))
73 \f
74 (defmacro-mundanely with-hash-table-iterator ((function hash-table) &body body)
75   #!+sb-doc
76   "WITH-HASH-TABLE-ITERATOR ((function hash-table) &body body)
77    provides a method of manually looping over the elements of a hash-table.
78    FUNCTION is bound to a generator-macro that, within the scope of the
79    invocation, returns one or three values. The first value tells whether
80    any objects remain in the hash table. When the first value is non-NIL,
81    the second and third values are the key and the value of the next object."
82   (let ((n-function (gensym "WITH-HASH-TABLE-ITERATOR-")))
83     `(let ((,n-function
84             (let* ((table ,hash-table)
85                    (length (length (hash-table-next-vector table)))
86                    (index 1))
87               (declare (type (mod #.(floor most-positive-fixnum 2)) index))
88               (labels
89                   ((,function ()
90                      ;; (We grab the table again on each iteration just in
91                      ;; case it was rehashed by a PUTHASH.)
92                      (let ((kv-vector (hash-table-table table)))
93                        (do ()
94                            ((>= index length) (values nil))
95                          (let ((key (aref kv-vector (* 2 index)))
96                                (value (aref kv-vector (1+ (* 2 index)))))
97                            (incf index)
98                            (unless (and (eq key +empty-ht-slot+)
99                                         (eq value +empty-ht-slot+))
100                              (return (values t key value))))))))
101                 #',function))))
102       (macrolet ((,function () '(funcall ,n-function)))
103         ,@body))))