2.16 Hash tables


2.16.1 Extensions

2.16.1.1 Weakness in hash tables

Weak hash tables allow the garbage collector to reclaim some of the entries if they are not strongly referenced elsewhere. ECL supports four kinds of weakness in hash tables: :key, :value, :key-and-value and :key-or-value.

To make hash table weak, programmer has to provide :weakness keyword argument to cl:make-hash-table with the desired kind of weakness value (nil means that the hash table has only strong references).

For more information see Weak References - Data Types and Implementation by Bruno Haible.

Function: ext:hash-table-weakness ht

Returns type of the hash table weakness. Possible return values are: :key, :value, :key-and-value, :key-or-value or nil.

2.16.1.2 Thread-safe hash tables

By default ECL doesn’t protect hash tables from simultaneous access for performance reasons. Read and write access may is synchronized when :synchronized keyword argument to make-hash-table is t - (make-hash-table :synchronized t).

Function: ext:hash-table-synchronized-p ht

Predicate answering whether hash table is synchronized or not.

2.16.1.3 Hash tables serialization

Function: ext:hash-table-content ht

Returns freshly consed list of pairs (key . val) being contents of the hash table.

Function: ext:hash-table-fill ht values

Fills ht with values being list of (key . val). Hash table may have some content already, but conflicting keys will be overwritten.

2.16.1.4 Custom equivalence predicate

make-hash-table may accept arbitrary :test keyword for the equivalence predicate. If it is not one of the standard predicates (:eq, :eql, :equal, :equalp) a keyword argument :hashing-function must be a function accepting one argument and returning a positive fixnum. Otherwise the argument is ignored.

2.16.1.5 Example

CL-USER> (defparameter *ht*
           (make-hash-table :synchronized t
                            :weakness :key-or-value))
*HT*

CL-USER> (ext:hash-table-weakness *ht*)
:KEY-OR-VALUE

CL-USER> (ext:hash-table-synchronized-p *ht*)
T

CL-USER> (ext:hash-table-fill *ht* '((:foo 3) (:bar 4) (:quux 5)))
#<hash-table 000055b1229e0b40>

CL-USER> (ext:hash-table-content *ht*)
((#<weak-pointer 000055b121866350> . #<weak-pointer 000055b121866320>)
 (#<weak-pointer 000055b121866370> . #<weak-pointer 000055b121866360>)
 (#<weak-pointer 000055b121866390> . #<weak-pointer 000055b121866380>))

2.16.2 C Reference

2.16.2.1 ANSI dictionary

Common Lisp and C equivalence

Lisp symbolC function
clrhashcl_object cl_clrhash(cl_object hash_table)
gethashcl_object cl_gethash(cl_narg narg, cl_object key, cl_object hash_table, ...)
(setf gethash)cl_object si_hash_set(cl_object key, cl_object hash_table, cl_object value)
hash-table-countcl_object cl_hash_table_count(cl_object hash_table)
hash-table-pcl_object cl_hash_table_p(cl_object hash_table)
hash-table-rehash-sizecl_object cl_hash_table_rehash_size(cl_object hash_table)
hash-table-rehash-thresholdcl_object cl_hash_table_rehash_threshold(cl_object hash_table)
hash-table-sizecl_object cl_hash_table_size(cl_object hash_table)
hash-table-testcl_object cl_hash_table_test(cl_object hash_table)
make-hash-tablecl_object cl_make_hash_table(cl_narg narg, ...)
maphashcl_object cl_maphash(cl_object function, cl_object hash_table)
remhashcl_object cl_remhash(cl_object key, cl_object hash_table)
sxhashcl_object cl_sxhash(cl_object object)