Fix if/else scope in yow.c from Rudi
[sxemacs] / info / lispref / hash-tables.texi
1 @c -*-texinfo-*-
2 @c This is part of the SXEmacs Lisp Reference Manual.
3 @c Copyright (C) 1996 Ben Wing.
4 @c Copyright (C) 2005 Sebastian Freundt <hroptatyr@sxemacs.org>
5 @c See the file lispref.texi for copying conditions.
6 @setfilename ../../info/hash-tables.info
7
8 @node Hash Tables, Range Tables, Media, top
9 @chapter Hash Tables
10 @cindex hash table
11
12 @defun hash-table-p object
13 This function returns @code{t} if @var{object} is a hash table, else @code{nil}.
14 @end defun
15
16 @menu
17 * Introduction to Hash Tables:: Hash tables are fast data structures for
18                                 implementing simple tables (i.e. finite
19                                 mappings from keys to values).
20 * Working With Hash Tables::    Hash table functions.
21 * Weak Hash Tables::            Hash tables with special garbage-collection
22                                 behavior.
23 @end menu
24
25
26 @node Introduction to Hash Tables
27 @section Introduction to Hash Tables
28
29 A @dfn{hash table} is a data structure that provides mappings from
30 arbitrary Lisp objects called @dfn{keys} to other arbitrary Lisp objects
31 called @dfn{values}.  A key/value pair is sometimes called an
32 @dfn{entry} in the hash table.  There are many ways other than hash
33 tables of implementing the same sort of mapping, e.g.  association lists
34 (@pxref{Association Lists}) and property lists (@pxref{Property Lists}),
35 but hash tables provide much faster lookup when there are many entries
36 in the mapping.  Hash tables are an implementation of the abstract data
37 type @dfn{dictionary}, also known as @dfn{associative array}.
38
39 Internally, hash tables are hashed using the @dfn{linear probing} hash
40 table implementation method.  This method hashes each key to a
41 particular spot in the hash table, and then scans forward sequentially
42 until a blank entry is found.  To look up a key, hash to the appropriate
43 spot, then search forward for the key until either a key is found or a
44 blank entry stops the search.  This method is used in preference to
45 double hashing because of changes in recent hardware.  The penalty for
46 non-sequential access to memory has been increasing, and this
47 compensates for the problem of clustering that linear probing entails.
48
49 When hash tables are created, the user may (but is not required to)
50 specify initial properties that influence performance.
51
52 Use the @code{:size} parameter to specify the number of entries that are
53 likely to be stored in the hash table, to avoid the overhead of resizing
54 the table.  But if the pre-allocated space for the entries is never
55 used, it is simply wasted and makes SXEmacs slower.  Excess unused hash
56 table entries exact a small continuous performance penalty, since they
57 must be scanned at every garbage collection.  If the number of entries
58 in the hash table is unknown, simply avoid using the @code{:size}
59 keyword.
60
61 Use the @code{:rehash-size} and @code{:rehash-threshold} keywords to
62 adjust the algorithm for deciding when to rehash the hash table.  For
63 temporary hash tables that are going to be very heavily used, use a
64 small rehash threshold, for example, 0.4 and a large rehash size, for
65 example 2.0.  For permanent hash tables that will be infrequently used,
66 specify a large rehash threshold, for example 0.8.
67
68 Hash tables can also be created by the lisp reader using structure
69 syntax, for example:
70 @example
71 #s(hash-table size 20 data (foo 1 bar 2))
72 @end example
73
74 The structure syntax accepts the same keywords as @code{make-hash-table}
75 (without the @code{:} character), as well as the additional keyword
76 @code{data}, which specifies the initial hash table contents.
77
78 @defun make-hash-table &key @code{test} @code{size} @code{rehash-size} @code{rehash-threshold} @code{weakness}
79 This function returns a new empty hash table object.
80
81 Keyword @code{:test} can be @code{eq}, @code{eql} (default) or @code{equal}.
82 Comparison between keys is done using this function.
83 If speed is important, consider using @code{eq}.
84 When storing strings in the hash table, you will likely need to use @code{equal}.
85
86 Keyword @code{:size} specifies the number of keys likely to be inserted.
87 This number of entries can be inserted without enlarging the hash table.
88
89 Keyword @code{:rehash-size} must be a float greater than 1.0, and specifies
90 the factor by which to increase the size of the hash table when enlarging.
91
92 Keyword @code{:rehash-threshold} must be a float between 0.0 and 1.0,
93 and specifies the load factor of the hash table which triggers enlarging.
94
95 Non-standard keyword @code{:weakness} can be @code{nil} (default),
96 @code{t}, @code{key-and-value}, @code{key}, @code{value} or
97 @code{key-or-value}.  @code{t} is an alias for @code{key-and-value}.
98
99 A key-and-value-weak hash table, also known as a fully-weak or simply
100 as a weak hash table, is one whose pointers do not count as GC
101 referents: for any key-value pair in the hash table, if the only
102 remaining pointer to either the key or the value is in a weak hash
103 table, then the pair will be removed from the hash table, and the key
104 and value collected.  A non-weak hash table (or any other pointer)
105 would prevent the object from being collected.
106
107 A key-weak hash table is similar to a fully-weak hash table except that
108 a key-value pair will be removed only if the key remains unmarked
109 outside of weak hash tables.  The pair will remain in the hash table if
110 the key is pointed to by something other than a weak hash table, even
111 if the value is not.
112
113 A value-weak hash table is similar to a fully-weak hash table except
114 that a key-value pair will be removed only if the value remains
115 unmarked outside of weak hash tables.  The pair will remain in the
116 hash table if the value is pointed to by something other than a weak
117 hash table, even if the key is not.
118
119 A key-or-value-weak hash table is similar to a fully-weak hash table except
120 that a key-value pair will be removed only if the value and the key remain
121 unmarked outside of weak hash tables.  The pair will remain in the
122 hash table if the value or key are pointed to by something other than a weak
123 hash table, even if the other is not.
124 @end defun
125
126 @defun copy-hash-table hash-table
127 This function returns a new hash table which contains the same keys and
128 values as @var{hash-table}.  The keys and values will not themselves be
129 copied.
130 @end defun
131
132 @defun hash-table-count hash-table
133 This function returns the number of entries in @var{hash-table}.
134 @end defun
135
136 @defun hash-table-test hash-table
137 This function returns the test function of @var{hash-table}.
138 This can be one of @code{eq}, @code{eql} or @code{equal}.
139 @end defun
140
141 @defun hash-table-size hash-table
142 This function returns the current number of slots in @var{hash-table},
143 whether occupied or not.
144 @end defun
145
146 @defun hash-table-rehash-size hash-table
147 This function returns the current rehash size of @var{hash-table}.
148 This is a float greater than 1.0; the factor by which @var{hash-table}
149 is enlarged when the rehash threshold is exceeded.
150 @end defun
151
152 @defun hash-table-rehash-threshold hash-table
153 This function returns the current rehash threshold of @var{hash-table}.
154 This is a float between 0.0 and 1.0; the maximum @dfn{load factor} of
155 @var{hash-table}, beyond which the @var{hash-table} is enlarged by rehashing.
156 @end defun
157
158 @defun hash-table-weakness hash-table
159 This function returns the weakness of @var{hash-table}.
160 This can be one of @code{nil}, @code{t}, @code{key} or @code{value}.
161 @end defun
162
163
164 @node Working With Hash Tables
165 @section Working With Hash Tables
166
167 @defun puthash key value hash-table
168 This function hashes @var{key} to @var{value} in @var{hash-table}.
169 @end defun
170
171 @defun gethash key hash-table &optional default
172 This function finds the hash value for @var{key} in @var{hash-table}.
173 If there is no entry for @var{key} in @var{hash-table}, @var{default} is
174 returned (which in turn defaults to @code{nil}).
175 @end defun
176
177 @defun remhash key hash-table
178 This function removes the entry for @var{key} from @var{hash-table}.
179 Does nothing if there is no entry for @var{key} in @var{hash-table}.
180 @end defun
181
182 @defun clrhash hash-table
183 This function removes all entries from @var{hash-table}, leaving it empty.
184 @end defun
185
186 @defun maphash function hash-table
187 This function maps @var{function} over entries in @var{hash-table},
188 calling it with two args, each key and value in the hash table.
189
190 @var{function} may not modify @var{hash-table}, with the one exception
191 that @var{function} may remhash or puthash the entry currently being
192 processed by @var{function}.
193 @end defun
194
195
196 @node Weak Hash Tables
197 @section Weak Hash Tables
198 @cindex hash table, weak
199 @cindex weak hash table
200
201 A @dfn{weak hash table} is a special variety of hash table whose
202 elements do not count as GC referents.  For any key-value pair in such a
203 hash table, if either the key or value (or in some cases, if one
204 particular one of the two) has no references to it outside of weak hash
205 tables (and similar structures such as weak lists), the pair will be
206 removed from the table, and the key and value collected.  A non-weak
207 hash table (or any other pointer) would prevent the objects from being
208 collected.
209
210 Weak hash tables are useful for keeping track of information in a
211 non-obtrusive way, for example to implement caching.  If the cache
212 contains objects such as buffers, markers, image instances, etc. that
213 will eventually disappear and get garbage-collected, using a weak hash
214 table ensures that these objects are collected normally rather than
215 remaining around forever, long past their actual period of use.
216 (Otherwise, you'd have to explicitly map over the hash table every so
217 often and remove unnecessary elements.)
218
219 There are four types of weak hash tables:
220
221 @table @asis
222 @item key-and-value-weak hash tables
223 In these hash tables, also known as fully weak or simply as weak hash
224 tables, a pair disappears if either the key or the value is unreferenced
225 outside of the table.
226 @item key-weak hash tables
227 In these hash tables, a pair disappears if the key is unreferenced outside
228 of the table, regardless of how the value is referenced.
229 @item value-weak hash tables
230 In these hash tables, a pair disappears if the value is unreferenced outside
231 of the table, regardless of how the key is referenced.
232 @item key-or-value-weak hash tables
233 In these hash tables, a pair disappears if both the key and the value
234 are unreferenced outside of the table.
235 @end table
236
237 Also see @ref{Weak Lists}.
238
239 Weak hash tables are created by specifying the @code{:weakness} keyword to
240 @code{make-hash-table}.