Hash table extensions supported by SBCL are all controlled by keyword
- Determines how keys are compared. Must a designator for one of the standard hash table tests, or a hash table test defined using
sb-ext:define-hash-table-test. Additionally, when an explicit
hash-functionis provided (see below), any two argument equivalence predicate can be used as the
- A hint as to how many elements will be put in this hash table.
- Indicates how to expand the table when it fills up. If an integer, add space for that many elements. If a floating point number (which must be greater than 1.0), multiply the size by that amount.
- Indicates how dense the table can become before forcing a rehash. Can be any positive number <=1, with density approaching zero as the threshold approaches 0. Density 1 means an average of one entry per bucket.
nil(the default), a hash function based on the
testargument is used, which then must be one of the standardized hash table test functions, or one for which a default hash function has been defined using
hash-functionis specified, the
testargument can be any two argument predicate consistent with it. The
hash-functionis expected to return a non-negative fixnum hash code.
nil(the default) it is a normal non-weak hash table. If one of
:key-or-valueit is a weak hash table. Depending on the type of weakness the lack of references to the key and the value may allow for removal of the entry. If
:keyand the key would otherwise be garbage the entry is eligible for removal from the hash table. Similarly, if
:valuethe life of an entry depends on its value's references. If
:key-and-valueand either the key or the value would otherwise be garbage the entry can be removed. If
:key-or-valueand both the key and the value would otherwise be garbage the entry can be removed.
nil(the default), the hash-table may have multiple concurrent readers, but results are undefined if a thread writes to the hash-table concurrently with another reader or writer. If
t, all concurrent accesses are safe, but note that
clhs3.6 (Traversal Rules and Side Effects) remains in force. See also:
sb-ext:with-locked-hash-table. This keyword argument is experimental, and may change incompatibly or be removed in the future.
namemust be a symbol naming a global two argument equivalence predicate. Afterwards both
#'namecan be used with
:testargument. In both cases
hash-table-testwill return the symbol
hash-functionmust be a symbol naming a global hash function consistent with the predicate, or be a
lambdaform implementing one in the current lexical environment. The hash function must compute the same hash code for any two objects for which
namereturns true, and subsequent calls with already hashed objects must always return the same hash code.
:hash-functionkeyword argument to
make-hash-tablecan be used to override the specified default hash-function.
Attempting to define
namein a locked package as hash-table test causes a package lock violation.
Examples:;;; 1.;; We want to use objects of type FOO as keys (by their ;; names.) EQUALP would work, but would make the names ;; case-insensitive -- wich we don't want. (defstruct foo (name nil :type (or null string)));; Define an equivalence test function and a hash function. (defun foo-name= (f1 f2) (equal (foo-name f1) (foo-name f2))) (defun sxhash-foo-name (f) (sxhash (foo-name f)))(define-hash-table-test foo-name= sxhash-foo-name);; #'foo-name would work too. (defun make-foo-table () (make-hash-table :test 'foo-name=));;; 2.(defun == (x y) (= x y))(define-hash-table-test == (lambda (x) ;; Hash codes must be consistent with test, so ;; not (SXHASH X), since ;; (= 1 1.0) => T ;; (= (SXHASH 1) (SXHASH 1.0)) => NIL ;; Note: this doesn't deal with complex numbers or ;; bignums too large to represent as double floats. (sxhash (coerce x 'double-float))));; #'== would work too (defun make-number-table () (make-hash-table :test '==))
Limits concurrent accesses to
hash-tablefor the duration of
bodywill execute with exclusive ownership of the table. If
hash-tableis not synchronized,
bodywill execute with other
--exclusion of hash-table accesses not surrounded by