| 1 |
@node Hash tables |
| 2 |
@section Hash tables |
| 3 |
|
| 4 |
@menu |
| 5 |
* Hash tables - Extensions:: |
| 6 |
* Hash tables - C Reference:: |
| 7 |
@end menu |
| 8 |
|
| 9 |
@node Hash tables - Extensions |
| 10 |
@subsection Extensions |
| 11 |
|
| 12 |
@subsubsection Weakness in hash tables |
| 13 |
@cindex Weak hash tables |
| 14 |
@ftindex ECL-WEAK-HASH |
| 15 |
|
| 16 |
Weak hash tables allow the garbage collector to reclaim some of the |
| 17 |
entries if they are not strongly referenced elsewhere. ECL supports |
| 18 |
four kinds of weakness in hash tables: @code{:key}, @code{:value}, |
| 19 |
@code{:key-and-value} and @code{:key-or-value}. |
| 20 |
|
| 21 |
To make hash table weak, programmer has to provide @code{:weakness} |
| 22 |
keyword argument to @code{cl:make-hash-table} with the desired kind of |
| 23 |
weakness value (@code{nil} means that the hash table has only strong |
| 24 |
references). |
| 25 |
|
| 26 |
For more information see |
| 27 |
@url{https://www.haible.de/bruno/papers/cs/weak/WeakDatastructures-writeup.html,Weak |
| 28 |
References - Data Types and Implementation} by Bruno Haible. |
| 29 |
|
| 30 |
@lspdef ext:hash-table-weakness |
| 31 |
@defun ext:hash-table-weakness ht |
| 32 |
Returns type of the hash table weakness. Possible return values are: |
| 33 |
@code{:key}, @code{:value}, @code{:key-and-value}, @code{:key-or-value} |
| 34 |
or @code{nil}. |
| 35 |
@end defun |
| 36 |
|
| 37 |
@subsubsection Thread-safe hash tables |
| 38 |
@cindex Synchronized hash tables |
| 39 |
@cindex Thread-safe hash tables |
| 40 |
|
| 41 |
By default ECL doesn't protect hash tables from simultaneous access |
| 42 |
for performance reasons. Read and write access may is synchronized |
| 43 |
when @code{:synchronized} keyword argument to @code{make-hash-table} is |
| 44 |
@code{t} - @code{(make-hash-table :synchronized t)}. |
| 45 |
|
| 46 |
@lspdef ext:hash-table-synchronized-p |
| 47 |
@defun ext:hash-table-synchronized-p ht |
| 48 |
Predicate answering whether hash table is synchronized or not. |
| 49 |
@end defun |
| 50 |
|
| 51 |
@subsubsection Hash tables serialization |
| 52 |
@cindex Hash table serialization |
| 53 |
|
| 54 |
@lspdef hash-table-content |
| 55 |
@defun ext:hash-table-content ht |
| 56 |
Returns freshly consed list of pairs @code{(key . val)} being contents |
| 57 |
of the hash table. |
| 58 |
@end defun |
| 59 |
|
| 60 |
@lspdef ext:hash-table-fill |
| 61 |
@defun ext:hash-table-fill ht values |
| 62 |
Fills @var{ht} with @var{values} being list of @code{(key . val)}. Hash |
| 63 |
table may have some content already, but conflicting keys will be |
| 64 |
overwritten. |
| 65 |
@end defun |
| 66 |
|
| 67 |
@subsubsection Custom equivalence predicate |
| 68 |
@cindex Hash table generic test |
| 69 |
|
| 70 |
@code{make-hash-table} may accept arbitrary @code{:test} keyword for |
| 71 |
the equivalence predicate. If it is not one of the standard predicates |
| 72 |
(@code{:eq}, @code{:eql}, @code{:equal}, @code{:equalp}) a keyword |
| 73 |
argument @code{:hashing-function} must be a function accepting one |
| 74 |
argument and returning a positive fixnum. Otherwise the argument is |
| 75 |
ignored. |
| 76 |
|
| 77 |
@subsubsection Example |
| 78 |
@exindex Hash table extensions example |
| 79 |
@lisp |
| 80 |
CL-USER> (defparameter *ht* |
| 81 |
(make-hash-table :synchronized t |
| 82 |
:weakness :key-or-value)) |
| 83 |
*HT* |
| 84 |
|
| 85 |
CL-USER> (ext:hash-table-weakness *ht*) |
| 86 |
:KEY-OR-VALUE |
| 87 |
|
| 88 |
CL-USER> (ext:hash-table-synchronized-p *ht*) |
| 89 |
T |
| 90 |
|
| 91 |
CL-USER> (ext:hash-table-fill *ht* '((:foo 3) (:bar 4) (:quux 5))) |
| 92 |
#<hash-table 000055b1229e0b40> |
| 93 |
|
| 94 |
CL-USER> (ext:hash-table-content *ht*) |
| 95 |
((#<weak-pointer 000055b121866350> . #<weak-pointer 000055b121866320>) |
| 96 |
(#<weak-pointer 000055b121866370> . #<weak-pointer 000055b121866360>) |
| 97 |
(#<weak-pointer 000055b121866390> . #<weak-pointer 000055b121866380>)) |
| 98 |
@end lisp |
| 99 |
|
| 100 |
@node Hash tables - C Reference |
| 101 |
@subsection C Reference |
| 102 |
|
| 103 |
@subsubsection ANSI dictionary |
| 104 |
Common Lisp and C equivalence |
| 105 |
|
| 106 |
@multitable @columnfractions .3 .7 |
| 107 |
@headitem Lisp symbol @tab C function |
| 108 |
@item @clhs{f_clrhas.htm,clrhash} @tab cl_object cl_clrhash(cl_object hash_table) |
| 109 |
@item @clhs{f_gethas.htm,gethash} @tab cl_object cl_gethash(cl_narg narg, cl_object key, cl_object hash_table, ...) |
| 110 |
@item @clhs{f_gethas.htm,(setf gethash)} @tab cl_object si_hash_set(cl_object key, cl_object hash_table, cl_object value) |
| 111 |
@item @clhs{f_hash_1.htm,hash-table-count} @tab cl_object cl_hash_table_count(cl_object hash_table) |
| 112 |
@item @clhs{f_hash_t.htm,hash-table-p} @tab cl_object cl_hash_table_p(cl_object hash_table) |
| 113 |
@item @clhs{f_hash_2.htm,hash-table-rehash-size} @tab cl_object cl_hash_table_rehash_size(cl_object hash_table) |
| 114 |
@item @clhs{f_hash_3.htm,hash-table-rehash-threshold} @tab cl_object cl_hash_table_rehash_threshold(cl_object hash_table) |
| 115 |
@item @clhs{f_hash_4.htm,hash-table-size} @tab cl_object cl_hash_table_size(cl_object hash_table) |
| 116 |
@item @clhs{f_hash_5.htm,hash-table-test} @tab cl_object cl_hash_table_test(cl_object hash_table) |
| 117 |
@item @clhs{f_mk_has.htm,make-hash-table} @tab cl_object cl_make_hash_table(cl_narg narg, ...) |
| 118 |
@item @clhs{f_maphas.htm,maphash} @tab cl_object cl_maphash(cl_object function, cl_object hash_table) |
| 119 |
@item @clhs{f_remhas.htm,remhash} @tab cl_object cl_remhash(cl_object key, cl_object hash_table) |
| 120 |
@item @clhs{f_sxhash.htm,sxhash} @tab cl_object cl_sxhash(cl_object object) |
| 121 |
@end multitable |