16.0.0 2 16.0.0 4
16.0.0 5
eq, those whose keys are compared with eql, those whose keys are compared with equal, and those whose keys are compared with equalp.
16.0.0 6
make-hash-table. gethash is used to look up a key and find the associated value. New entries are added to hash tables using setf with gethash. remhash is used to remove an entry. For example:
(setq a (make-hash-table)) → #<HASH-TABLE EQL 0/120 32536573> (setf (gethash 'color a) 'brown) → BROWN (setf (gethash 'name a) 'fred) → FRED (gethash 'color a) → BROWN, true (gethash 'name a) → FRED, true (gethash 'pointy a) → NIL, false
16.0.0 7In this example, the symbols color and name are being used as keys, and the symbols brown and fred are being used as the associated values. The hash table has two items in it, one of which associates from color to brown, and the other of which associates from name to fred.
16.0.0 8
The following will be deleted. 16.0.0 9 \itemitem{--} A \term{hash table}'s size is the maximum number of entries it can hold without collisions. Usually the actual capacity of the table is somewhat less, since the hashing is not perfectly collision-free. If so many entries are added that the capacity is exceeded, the \term{hash table} will automatically grow, and the entries will be rehashed (new hash values will be recomputed, and everything will be rearranged so that the fast hash lookup still works). This is transparent to the caller; it all happens automatically.
16.0.0 10
gethash.
clrhash | hash-table-p | remhash |
gethash | make-hash-table | sxhash |
hash-table-count | maphash |
Figure 18–1. Hash-table defined names
The function supplied as the :test argument to make-hash-table specifies the `equivalence test' for the hash table it creates.
An object is `visibly modified' with regard to an equivalence test if there exists some set of objects (or potential objects) which are equivalent to the object before the modification but are no longer equivalent afterwards.
If an \term{object} is used as a key in a \term{hash table} and is then visibly modified with regard to the equivalence test of the \term{hash table}, then using the \term{object} as a key in further operations on the \term{hash table} has unspecified consequences. Moreover, this applies for other \term{objects} which either are or were equivalent to the key object. Further, undoing the modification does not remove this effect.
If an object is used as a key in a hash table and is then visibly modified with regard to the equivalence test of , then the consequences are unspecified if , or any object equivalent to under the equivalence test (either before or after the modification), is used as a key in further operations on . The consequences of using as a key are unspecified even if is visibly modified and then later modified again in such a way as to undo the visible modification.
Following are specifications of the modifications which are visible to the equivalence tests which must be supported by hash tables. The modifications are described in terms of modification of components, and are defined recursively. Visible modifications of components of the object are visible modifications of the object.
No standardized function is provided that is capable of visibly modifying an object with regard to eq or eql.
As a consequence of the behavior for equal, the rules for visible modification of objects not explicitly mentioned in this section are inherited from those in Section 18.1.2.1 (Visible Modification of Objects with respect to EQ and EQL).
equal.
bit-vector or of type string, any visible change to an active element of the vector, or to the length of the vector (if it is actually adjustable or has a fill pointer) is considered a visible modification with regard to equal.
equalp, the rules for visible modification of objects not explicitly mentioned in this section are inherited from those in Section 18.1.2.2 (Visible Modification of Objects with respect to EQUAL).
equalp.
equalp.
equalp.
Note that the visibility of modifications to the keys depends on the equivalence test of the hash table, not on the specification of equalp.
Implementations that extend the language by defining additional acceptable equivalence tests for hash tables (allowing additional values for the :test argument to make-hash-table) must document the visible components of these tests.
!!! Maybe consider making these things visible... Test Cases/Examples: (setf ht (make-hash-table :test #'eq)) (setf a (cons 1 2)) (setf (gethash a ht) 'win) (setf (cdr a) 3) (gethash a ht 'lose) => win t The same example with :test #'equal in the first line would be an error. The following example is not an error, because EQUAL does not examine the components of general vectors: (setf ht (make-hash-table :test #'equal)) (setf a (vector 1 2)) (setf (gethash a ht) 'win) (setf (aref a 1) 3) (gethash a ht 'lose) => win t The following example is not an error, because EQUALP is limited by the fill-pointer when examining the elements of a vector: (setf ht (make-hash-table :test #'equalp)) (setf a (make-array 3 :fill-pointer 2 :initial-contents '(1 2 3))) (setf (gethash a ht) 'win) (setf (aref a 2) 4) (gethash a ht 'lose) => win t The following example is an error, because EQUALP may examine the dimensions of arrays: (setf ht (make-hash-table :test #'equalp)) (setf a (make-array '(2 3) :adjustable t)) (setf (gethash a ht) 'win) (setf a (adjust-array a '(3 2))) (gethash a ht 'lose) => `unspecified' The following example is not an error, because EQUALP is case insensitive: (setf ht (make-hash-table :test #'equalp)) (setf a "ABC") (setf (gethash a ht) 'win) (setf (char a 0) #\a) (gethash a ht 'lose) => win t The same example with :test #'equal in the first line would be an error.
hash-table System Classmake-hash-table Functionhash-table-p Functionhash-table-count Functionhash-table-rehash-size Functionhash-table-rehash-threshold Functionhash-table-size Functionhash-table-test Functiongethash Accessorremhash Functionmaphash Functionwith-hash-table-iterator Macroclrhash Functionsxhash Function