]> code.delx.au - gnu-emacs/blob - doc/lispref/hash.texi
9d60cc38c25eb547d2e7ec921e09cf3220ac51d0
[gnu-emacs] / doc / lispref / hash.texi
1 @c -*-texinfo-*-
2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1999, 2001-2015 Free Software Foundation, Inc.
4 @c See the file elisp.texi for copying conditions.
5 @node Hash Tables
6 @chapter Hash Tables
7 @cindex hash tables
8 @cindex lookup tables
9
10 A hash table is a very fast kind of lookup table, somewhat like an
11 alist (@pxref{Association Lists}) in that it maps keys to
12 corresponding values. It differs from an alist in these ways:
13
14 @itemize @bullet
15 @item
16 Lookup in a hash table is extremely fast for large tables---in fact, the
17 time required is essentially @emph{independent} of how many elements are
18 stored in the table. For smaller tables (a few tens of elements)
19 alists may still be faster because hash tables have a more-or-less
20 constant overhead.
21
22 @item
23 The correspondences in a hash table are in no particular order.
24
25 @item
26 There is no way to share structure between two hash tables,
27 the way two alists can share a common tail.
28 @end itemize
29
30 Emacs Lisp provides a general-purpose hash table data type, along
31 with a series of functions for operating on them. Hash tables have a
32 special printed representation, which consists of @samp{#s} followed
33 by a list specifying the hash table properties and contents.
34 @xref{Creating Hash}. (Note that the term ``hash notation'', which
35 refers to the initial @samp{#} character used in the printed
36 representations of objects with no read representation, has nothing to
37 do with the term ``hash table''. @xref{Printed Representation}.)
38
39 Obarrays are also a kind of hash table, but they are a different type
40 of object and are used only for recording interned symbols
41 (@pxref{Creating Symbols}).
42
43 @menu
44 * Creating Hash:: Functions to create hash tables.
45 * Hash Access:: Reading and writing the hash table contents.
46 * Defining Hash:: Defining new comparison methods.
47 * Other Hash:: Miscellaneous.
48 @end menu
49
50 @node Creating Hash
51 @section Creating Hash Tables
52 @cindex creating hash tables
53
54 The principal function for creating a hash table is
55 @code{make-hash-table}.
56
57 @defun make-hash-table &rest keyword-args
58 This function creates a new hash table according to the specified
59 arguments. The arguments should consist of alternating keywords
60 (particular symbols recognized specially) and values corresponding to
61 them.
62
63 Several keywords make sense in @code{make-hash-table}, but the only two
64 that you really need to know about are @code{:test} and @code{:weakness}.
65
66 @table @code
67 @item :test @var{test}
68 This specifies the method of key lookup for this hash table. The
69 default is @code{eql}; @code{eq} and @code{equal} are other
70 alternatives:
71
72 @table @code
73 @item eql
74 Keys which are numbers are ``the same'' if they are @code{equal}, that
75 is, if they are equal in value and either both are integers or both
76 are floating point; otherwise, two distinct objects are never
77 ``the same''.
78
79 @item eq
80 Any two distinct Lisp objects are ``different'' as keys.
81
82 @item equal
83 Two Lisp objects are ``the same'', as keys, if they are equal
84 according to @code{equal}.
85 @end table
86
87 You can use @code{define-hash-table-test} (@pxref{Defining Hash}) to
88 define additional possibilities for @var{test}.
89
90 @item :weakness @var{weak}
91 The weakness of a hash table specifies whether the presence of a key or
92 value in the hash table preserves it from garbage collection.
93
94 The value, @var{weak}, must be one of @code{nil}, @code{key},
95 @code{value}, @code{key-or-value}, @code{key-and-value}, or @code{t}
96 which is an alias for @code{key-and-value}. If @var{weak} is @code{key}
97 then the hash table does not prevent its keys from being collected as
98 garbage (if they are not referenced anywhere else); if a particular key
99 does get collected, the corresponding association is removed from the
100 hash table.
101
102 If @var{weak} is @code{value}, then the hash table does not prevent
103 values from being collected as garbage (if they are not referenced
104 anywhere else); if a particular value does get collected, the
105 corresponding association is removed from the hash table.
106
107 If @var{weak} is @code{key-and-value} or @code{t}, both the key and
108 the value must be live in order to preserve the association. Thus,
109 the hash table does not protect either keys or values from garbage
110 collection; if either one is collected as garbage, that removes the
111 association.
112
113 If @var{weak} is @code{key-or-value}, either the key or
114 the value can preserve the association. Thus, associations are
115 removed from the hash table when both their key and value would be
116 collected as garbage (if not for references from weak hash tables).
117
118 The default for @var{weak} is @code{nil}, so that all keys and values
119 referenced in the hash table are preserved from garbage collection.
120
121 @item :size @var{size}
122 This specifies a hint for how many associations you plan to store in the
123 hash table. If you know the approximate number, you can make things a
124 little more efficient by specifying it this way. If you specify too
125 small a size, the hash table will grow automatically when necessary, but
126 doing that takes some extra time.
127
128 The default size is 65.
129
130 @item :rehash-size @var{rehash-size}
131 When you add an association to a hash table and the table is ``full'',
132 it grows automatically. This value specifies how to make the hash table
133 larger, at that time.
134
135 If @var{rehash-size} is an integer, it should be positive, and the hash
136 table grows by adding that much to the nominal size. If
137 @var{rehash-size} is floating point, it had better be greater
138 than 1, and the hash table grows by multiplying the old size by that
139 number.
140
141 The default value is 1.5.
142
143 @item :rehash-threshold @var{threshold}
144 This specifies the criterion for when the hash table is ``full'' (so
145 it should be made larger). The value, @var{threshold}, should be a
146 positive floating-point number, no greater than 1. The hash table is
147 ``full'' whenever the actual number of entries exceeds this fraction
148 of the nominal size. The default for @var{threshold} is 0.8.
149 @end table
150 @end defun
151
152 You can also create a new hash table using the printed representation
153 for hash tables. The Lisp reader can read this printed
154 representation, provided each element in the specified hash table has
155 a valid read syntax (@pxref{Printed Representation}). For instance,
156 the following specifies a new hash table containing the keys
157 @code{key1} and @code{key2} (both symbols) associated with @code{val1}
158 (a symbol) and @code{300} (a number) respectively.
159
160 @example
161 #s(hash-table size 30 data (key1 val1 key2 300))
162 @end example
163
164 @noindent
165 The printed representation for a hash table consists of @samp{#s}
166 followed by a list beginning with @samp{hash-table}. The rest of the
167 list should consist of zero or more property-value pairs specifying
168 the hash table's properties and initial contents. The properties and
169 values are read literally. Valid property names are @code{size},
170 @code{test}, @code{weakness}, @code{rehash-size},
171 @code{rehash-threshold}, and @code{data}. The @code{data} property
172 should be a list of key-value pairs for the initial contents; the
173 other properties have the same meanings as the matching
174 @code{make-hash-table} keywords (@code{:size}, @code{:test}, etc.),
175 described above.
176
177 Note that you cannot specify a hash table whose initial contents
178 include objects that have no read syntax, such as buffers and frames.
179 Such objects may be added to the hash table after it is created.
180
181 @node Hash Access
182 @section Hash Table Access
183 @cindex accessing hash tables
184 @cindex hash table access
185
186 This section describes the functions for accessing and storing
187 associations in a hash table. In general, any Lisp object can be used
188 as a hash key, unless the comparison method imposes limits. Any Lisp
189 object can also be used as the value.
190
191 @defun gethash key table &optional default
192 This function looks up @var{key} in @var{table}, and returns its
193 associated @var{value}---or @var{default}, if @var{key} has no
194 association in @var{table}.
195 @end defun
196
197 @defun puthash key value table
198 This function enters an association for @var{key} in @var{table}, with
199 value @var{value}. If @var{key} already has an association in
200 @var{table}, @var{value} replaces the old associated value.
201 @end defun
202
203 @defun remhash key table
204 This function removes the association for @var{key} from @var{table}, if
205 there is one. If @var{key} has no association, @code{remhash} does
206 nothing.
207
208 @b{Common Lisp note:} In Common Lisp, @code{remhash} returns
209 non-@code{nil} if it actually removed an association and @code{nil}
210 otherwise. In Emacs Lisp, @code{remhash} always returns @code{nil}.
211 @end defun
212
213 @defun clrhash table
214 This function removes all the associations from hash table @var{table},
215 so that it becomes empty. This is also called @dfn{clearing} the hash
216 table.
217
218 @b{Common Lisp note:} In Common Lisp, @code{clrhash} returns the empty
219 @var{table}. In Emacs Lisp, it returns @code{nil}.
220 @end defun
221
222 @defun maphash function table
223 @anchor{Definition of maphash}
224 This function calls @var{function} once for each of the associations in
225 @var{table}. The function @var{function} should accept two
226 arguments---a @var{key} listed in @var{table}, and its associated
227 @var{value}. @code{maphash} returns @code{nil}.
228 @end defun
229
230 @node Defining Hash
231 @section Defining Hash Comparisons
232 @cindex hash code
233 @cindex define hash comparisons
234
235 You can define new methods of key lookup by means of
236 @code{define-hash-table-test}. In order to use this feature, you need
237 to understand how hash tables work, and what a @dfn{hash code} means.
238
239 You can think of a hash table conceptually as a large array of many
240 slots, each capable of holding one association. To look up a key,
241 @code{gethash} first computes an integer, the hash code, from the key.
242 It reduces this integer modulo the length of the array, to produce an
243 index in the array. Then it looks in that slot, and if necessary in
244 other nearby slots, to see if it has found the key being sought.
245
246 Thus, to define a new method of key lookup, you need to specify both a
247 function to compute the hash code from a key, and a function to compare
248 two keys directly.
249
250 @defun define-hash-table-test name test-fn hash-fn
251 This function defines a new hash table test, named @var{name}.
252
253 After defining @var{name} in this way, you can use it as the @var{test}
254 argument in @code{make-hash-table}. When you do that, the hash table
255 will use @var{test-fn} to compare key values, and @var{hash-fn} to compute
256 a ``hash code'' from a key value.
257
258 The function @var{test-fn} should accept two arguments, two keys, and
259 return non-@code{nil} if they are considered ``the same''.
260
261 The function @var{hash-fn} should accept one argument, a key, and return
262 an integer that is the ``hash code'' of that key. For good results, the
263 function should use the whole range of integers for hash codes,
264 including negative integers.
265
266 The specified functions are stored in the property list of @var{name}
267 under the property @code{hash-table-test}; the property value's form is
268 @code{(@var{test-fn} @var{hash-fn})}.
269 @end defun
270
271 @defun sxhash obj
272 This function returns a hash code for Lisp object @var{obj}.
273 This is an integer which reflects the contents of @var{obj}
274 and the other Lisp objects it points to.
275
276 If two objects @var{obj1} and @var{obj2} are equal, then @code{(sxhash
277 @var{obj1})} and @code{(sxhash @var{obj2})} are the same integer.
278
279 If the two objects are not equal, the values returned by @code{sxhash}
280 are usually different, but not always; once in a rare while, by luck,
281 you will encounter two distinct-looking objects that give the same
282 result from @code{sxhash}.
283 @end defun
284
285 This example creates a hash table whose keys are strings that are
286 compared case-insensitively.
287
288 @example
289 (defun case-fold-string= (a b)
290 (eq t (compare-strings a nil nil b nil nil t)))
291 (defun case-fold-string-hash (a)
292 (sxhash (upcase a)))
293
294 (define-hash-table-test 'case-fold
295 'case-fold-string= 'case-fold-string-hash)
296
297 (make-hash-table :test 'case-fold)
298 @end example
299
300 Here is how you could define a hash table test equivalent to the
301 predefined test value @code{equal}. The keys can be any Lisp object,
302 and equal-looking objects are considered the same key.
303
304 @example
305 (define-hash-table-test 'contents-hash 'equal 'sxhash)
306
307 (make-hash-table :test 'contents-hash)
308 @end example
309
310 @node Other Hash
311 @section Other Hash Table Functions
312
313 Here are some other functions for working with hash tables.
314
315 @defun hash-table-p table
316 This returns non-@code{nil} if @var{table} is a hash table object.
317 @end defun
318
319 @defun copy-hash-table table
320 This function creates and returns a copy of @var{table}. Only the table
321 itself is copied---the keys and values are shared.
322 @end defun
323
324 @defun hash-table-count table
325 This function returns the actual number of entries in @var{table}.
326 @end defun
327
328 @defun hash-table-test table
329 This returns the @var{test} value that was given when @var{table} was
330 created, to specify how to hash and compare keys. See
331 @code{make-hash-table} (@pxref{Creating Hash}).
332 @end defun
333
334 @defun hash-table-weakness table
335 This function returns the @var{weak} value that was specified for hash
336 table @var{table}.
337 @end defun
338
339 @defun hash-table-rehash-size table
340 This returns the rehash size of @var{table}.
341 @end defun
342
343 @defun hash-table-rehash-threshold table
344 This returns the rehash threshold of @var{table}.
345 @end defun
346
347 @defun hash-table-size table
348 This returns the current nominal size of @var{table}.
349 @end defun