Mercurial > emacs
annotate lispref/hash.texi @ 76519:2ce1d66ecddf
* autorevert.el (auto-revert-tail-handler): Call after-revert-hook.
author | Chong Yidong <cyd@stupidchicken.com> |
---|---|
date | Sat, 17 Mar 2007 20:38:04 +0000 |
parents | 6d19c76d81c5 |
children | 8c3661f15764 95d0cdf160ea |
rev | line source |
---|---|
25634 | 1 @c -*-texinfo-*- |
2 @c This is part of the GNU Emacs Lisp Reference Manual. | |
75250
6d19c76d81c5
Update copyright for years from Emacs 21 to present (mainly adding
Glenn Morris <rgm@gnu.org>
parents:
71957
diff
changeset
|
3 @c Copyright (C) 1999, 2001, 2002, 2003, 2004, 2005, |
6d19c76d81c5
Update copyright for years from Emacs 21 to present (mainly adding
Glenn Morris <rgm@gnu.org>
parents:
71957
diff
changeset
|
4 @c 2006, 2007 Free Software Foundation, Inc. |
25634 | 5 @c See the file elisp.texi for copying conditions. |
6 @setfilename ../info/hash | |
7 @node Hash Tables, Symbols, Sequences Arrays Vectors, Top | |
8 @chapter Hash Tables | |
9 @cindex hash tables | |
10 | |
11 A hash table is a very fast kind of lookup table, somewhat like | |
12 an alist in that it maps keys to corresponding values. It differs | |
13 from an alist in these ways: | |
14 | |
15 @itemize @bullet | |
16 @item | |
26710
ed440ffea308
(Hash Tables): Note that alists win for small tables.
Dave Love <fx@gnu.org>
parents:
26303
diff
changeset
|
17 Lookup in a hash table is extremely fast for large tables---in fact, the |
ed440ffea308
(Hash Tables): Note that alists win for small tables.
Dave Love <fx@gnu.org>
parents:
26303
diff
changeset
|
18 time required is essentially @emph{independent} of how many elements are |
ed440ffea308
(Hash Tables): Note that alists win for small tables.
Dave Love <fx@gnu.org>
parents:
26303
diff
changeset
|
19 stored in the table. For smaller tables (a few tens of elements) |
ed440ffea308
(Hash Tables): Note that alists win for small tables.
Dave Love <fx@gnu.org>
parents:
26303
diff
changeset
|
20 alists may still be faster because hash tables have a more-or-less |
ed440ffea308
(Hash Tables): Note that alists win for small tables.
Dave Love <fx@gnu.org>
parents:
26303
diff
changeset
|
21 constant overhead. |
25634 | 22 |
23 @item | |
24 The correspondences in a hash table are in no particular order. | |
25 | |
26 @item | |
27 There is no way to share structure between two hash tables, | |
28 the way two alists can share a common tail. | |
29 @end itemize | |
30 | |
60446
b26c30e5d770
(Hash Tables): Get rid of "Emacs 21".
Richard M. Stallman <rms@gnu.org>
parents:
60039
diff
changeset
|
31 Emacs Lisp provides a general-purpose hash table data type, along |
b26c30e5d770
(Hash Tables): Get rid of "Emacs 21".
Richard M. Stallman <rms@gnu.org>
parents:
60039
diff
changeset
|
32 with a series of functions for operating on them. Hash tables have no |
b26c30e5d770
(Hash Tables): Get rid of "Emacs 21".
Richard M. Stallman <rms@gnu.org>
parents:
60039
diff
changeset
|
33 read syntax, and print in hash notation, like this: |
25634 | 34 |
35 @example | |
36 (make-hash-table) | |
37 @result{} #<hash-table 'eql nil 0/65 0x83af980> | |
38 @end example | |
39 | |
26303 | 40 @noindent |
41 (The term ``hash notation'' refers to the initial @samp{#} | |
42 character---@pxref{Printed Representation}---and has nothing to do with | |
43 the term ``hash table.'') | |
44 | |
25634 | 45 Obarrays are also a kind of hash table, but they are a different type |
46 of object and are used only for recording interned symbols | |
47 (@pxref{Creating Symbols}). | |
48 | |
49 @menu | |
60039
d1e57e5b8403
(Hash Tables): Add desc to menu items.
Richard M. Stallman <rms@gnu.org>
parents:
56215
diff
changeset
|
50 * Creating Hash:: Functions to create hash tables. |
d1e57e5b8403
(Hash Tables): Add desc to menu items.
Richard M. Stallman <rms@gnu.org>
parents:
56215
diff
changeset
|
51 * Hash Access:: Reading and writing the hash table contents. |
d1e57e5b8403
(Hash Tables): Add desc to menu items.
Richard M. Stallman <rms@gnu.org>
parents:
56215
diff
changeset
|
52 * Defining Hash:: Defining new comparison methods |
d1e57e5b8403
(Hash Tables): Add desc to menu items.
Richard M. Stallman <rms@gnu.org>
parents:
56215
diff
changeset
|
53 * Other Hash:: Miscellaneous. |
25634 | 54 @end menu |
55 | |
56 @node Creating Hash | |
57 @section Creating Hash Tables | |
58 | |
59 The principal function for creating a hash table is | |
60 @code{make-hash-table}. | |
61 | |
62 @defun make-hash-table &rest keyword-args | |
63 This function creates a new hash table according to the specified | |
64 arguments. The arguments should consist of alternating keywords | |
65 (particular symbols recognized specially) and values corresponding to | |
66 them. | |
67 | |
68 Several keywords make sense in @code{make-hash-table}, but the only two | |
26182 | 69 that you really need to know about are @code{:test} and @code{:weakness}. |
25634 | 70 |
71 @table @code | |
72 @item :test @var{test} | |
73 This specifies the method of key lookup for this hash table. The | |
74 default is @code{eql}; @code{eq} and @code{equal} are other | |
75 alternatives: | |
76 | |
77 @table @code | |
78 @item eql | |
53025
53be3d7fa5da
(Creating Hash): Clarify description of `eql'. `makehash' is obsolete.
Luc Teirlinck <teirllm@auburn.edu>
parents:
52401
diff
changeset
|
79 Keys which are numbers are ``the same'' if they are @code{equal}, that |
53be3d7fa5da
(Creating Hash): Clarify description of `eql'. `makehash' is obsolete.
Luc Teirlinck <teirllm@auburn.edu>
parents:
52401
diff
changeset
|
80 is, if they are equal in value and either both are integers or both |
53037
9958d8677feb
*** empty log message ***
Luc Teirlinck <teirllm@auburn.edu>
parents:
53025
diff
changeset
|
81 are floating point numbers; otherwise, two distinct objects are never |
71957
61cb5aae3bc3
Put period and comma inside quotes.
Richard M. Stallman <rms@gnu.org>
parents:
71922
diff
changeset
|
82 ``the same.'' |
25634 | 83 |
84 @item eq | |
85 Any two distinct Lisp objects are ``different'' as keys. | |
86 | |
87 @item equal | |
71957
61cb5aae3bc3
Put period and comma inside quotes.
Richard M. Stallman <rms@gnu.org>
parents:
71922
diff
changeset
|
88 Two Lisp objects are ``the same,'' as keys, if they are equal |
25634 | 89 according to @code{equal}. |
90 @end table | |
91 | |
92 You can use @code{define-hash-table-test} (@pxref{Defining Hash}) to | |
93 define additional possibilities for @var{test}. | |
94 | |
95 @item :weakness @var{weak} | |
96 The weakness of a hash table specifies whether the presence of a key or | |
97 value in the hash table preserves it from garbage collection. | |
98 | |
99 The value, @var{weak}, must be one of @code{nil}, @code{key}, | |
30524 | 100 @code{value}, @code{key-or-value}, @code{key-and-value}, or @code{t} |
54263
41fd82e27495
(Creating Hash): Correct the meaning of t for WEAK in make-hash-table.
Richard M. Stallman <rms@gnu.org>
parents:
54031
diff
changeset
|
101 which is an alias for @code{key-and-value}. If @var{weak} is @code{key} |
30524 | 102 then the hash table does not prevent its keys from being collected as |
103 garbage (if they are not referenced anywhere else); if a particular key | |
104 does get collected, the corresponding association is removed from the | |
105 hash table. | |
25634 | 106 |
30524 | 107 If @var{weak} is @code{value}, then the hash table does not prevent |
108 values from being collected as garbage (if they are not referenced | |
109 anywhere else); if a particular value does get collected, the | |
25634 | 110 corresponding association is removed from the hash table. |
111 | |
54031
fcad69b9ff7b
(Creating Hash): Correct and clarify doc of WEAK values.
Richard M. Stallman <rms@gnu.org>
parents:
53480
diff
changeset
|
112 If @var{weak} is @code{key-and-value} or @code{t}, both the key and |
fcad69b9ff7b
(Creating Hash): Correct and clarify doc of WEAK values.
Richard M. Stallman <rms@gnu.org>
parents:
53480
diff
changeset
|
113 the value must be live in order to preserve the association. Thus, |
fcad69b9ff7b
(Creating Hash): Correct and clarify doc of WEAK values.
Richard M. Stallman <rms@gnu.org>
parents:
53480
diff
changeset
|
114 the hash table does not protect either keys or values from garbage |
fcad69b9ff7b
(Creating Hash): Correct and clarify doc of WEAK values.
Richard M. Stallman <rms@gnu.org>
parents:
53480
diff
changeset
|
115 collection; if either one is collected as garbage, that removes the |
fcad69b9ff7b
(Creating Hash): Correct and clarify doc of WEAK values.
Richard M. Stallman <rms@gnu.org>
parents:
53480
diff
changeset
|
116 association. |
38786 | 117 |
54031
fcad69b9ff7b
(Creating Hash): Correct and clarify doc of WEAK values.
Richard M. Stallman <rms@gnu.org>
parents:
53480
diff
changeset
|
118 If @var{weak} is @code{key-or-value}, either the key or |
fcad69b9ff7b
(Creating Hash): Correct and clarify doc of WEAK values.
Richard M. Stallman <rms@gnu.org>
parents:
53480
diff
changeset
|
119 the value can preserve the association. Thus, associations are |
fcad69b9ff7b
(Creating Hash): Correct and clarify doc of WEAK values.
Richard M. Stallman <rms@gnu.org>
parents:
53480
diff
changeset
|
120 removed from the hash table when both their key and value would be |
fcad69b9ff7b
(Creating Hash): Correct and clarify doc of WEAK values.
Richard M. Stallman <rms@gnu.org>
parents:
53480
diff
changeset
|
121 collected as garbage (if not for references from weak hash tables). |
30524 | 122 |
25634 | 123 The default for @var{weak} is @code{nil}, so that all keys and values |
54031
fcad69b9ff7b
(Creating Hash): Correct and clarify doc of WEAK values.
Richard M. Stallman <rms@gnu.org>
parents:
53480
diff
changeset
|
124 referenced in the hash table are preserved from garbage collection. |
25634 | 125 |
126 @item :size @var{size} | |
127 This specifies a hint for how many associations you plan to store in the | |
128 hash table. If you know the approximate number, you can make things a | |
26182 | 129 little more efficient by specifying it this way. If you specify too |
25634 | 130 small a size, the hash table will grow automatically when necessary, but |
26303 | 131 doing that takes some extra time. |
25634 | 132 |
133 The default size is 65. | |
134 | |
135 @item :rehash-size @var{rehash-size} | |
136 When you add an association to a hash table and the table is ``full,'' | |
137 it grows automatically. This value specifies how to make the hash table | |
138 larger, at that time. | |
139 | |
25875 | 140 If @var{rehash-size} is an integer, it should be positive, and the hash |
141 table grows by adding that much to the nominal size. If | |
142 @var{rehash-size} is a floating point number, it had better be greater | |
143 than 1, and the hash table grows by multiplying the old size by that | |
144 number. | |
25634 | 145 |
146 The default value is 1.5. | |
147 | |
148 @item :rehash-threshold @var{threshold} | |
60039
d1e57e5b8403
(Hash Tables): Add desc to menu items.
Richard M. Stallman <rms@gnu.org>
parents:
56215
diff
changeset
|
149 This specifies the criterion for when the hash table is ``full'' (so |
d1e57e5b8403
(Hash Tables): Add desc to menu items.
Richard M. Stallman <rms@gnu.org>
parents:
56215
diff
changeset
|
150 it should be made larger). The value, @var{threshold}, should be a |
d1e57e5b8403
(Hash Tables): Add desc to menu items.
Richard M. Stallman <rms@gnu.org>
parents:
56215
diff
changeset
|
151 positive floating point number, no greater than 1. The hash table is |
d1e57e5b8403
(Hash Tables): Add desc to menu items.
Richard M. Stallman <rms@gnu.org>
parents:
56215
diff
changeset
|
152 ``full'' whenever the actual number of entries exceeds this fraction |
d1e57e5b8403
(Hash Tables): Add desc to menu items.
Richard M. Stallman <rms@gnu.org>
parents:
56215
diff
changeset
|
153 of the nominal size. The default for @var{threshold} is 0.8. |
25634 | 154 @end table |
155 @end defun | |
156 | |
157 @defun makehash &optional test | |
158 This is equivalent to @code{make-hash-table}, but with a different style | |
159 argument list. The argument @var{test} specifies the method | |
160 of key lookup. | |
161 | |
53025
53be3d7fa5da
(Creating Hash): Clarify description of `eql'. `makehash' is obsolete.
Luc Teirlinck <teirllm@auburn.edu>
parents:
52401
diff
changeset
|
162 This function is obsolete. Use @code{make-hash-table} instead. |
25634 | 163 @end defun |
164 | |
165 @node Hash Access | |
166 @section Hash Table Access | |
167 | |
168 This section describes the functions for accessing and storing | |
60039
d1e57e5b8403
(Hash Tables): Add desc to menu items.
Richard M. Stallman <rms@gnu.org>
parents:
56215
diff
changeset
|
169 associations in a hash table. In general, any Lisp object can be used |
d1e57e5b8403
(Hash Tables): Add desc to menu items.
Richard M. Stallman <rms@gnu.org>
parents:
56215
diff
changeset
|
170 as a hash key, unless the comparison method imposes limits. Any Lisp |
d1e57e5b8403
(Hash Tables): Add desc to menu items.
Richard M. Stallman <rms@gnu.org>
parents:
56215
diff
changeset
|
171 object can also be used as the value. |
25634 | 172 |
173 @defun gethash key table &optional default | |
174 This function looks up @var{key} in @var{table}, and returns its | |
175 associated @var{value}---or @var{default}, if @var{key} has no | |
176 association in @var{table}. | |
177 @end defun | |
178 | |
49600
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
38786
diff
changeset
|
179 @defun puthash key value table |
25634 | 180 This function enters an association for @var{key} in @var{table}, with |
181 value @var{value}. If @var{key} already has an association in | |
182 @var{table}, @var{value} replaces the old associated value. | |
183 @end defun | |
184 | |
185 @defun remhash key table | |
186 This function removes the association for @var{key} from @var{table}, if | |
187 there is one. If @var{key} has no association, @code{remhash} does | |
188 nothing. | |
53025
53be3d7fa5da
(Creating Hash): Clarify description of `eql'. `makehash' is obsolete.
Luc Teirlinck <teirllm@auburn.edu>
parents:
52401
diff
changeset
|
189 |
53be3d7fa5da
(Creating Hash): Clarify description of `eql'. `makehash' is obsolete.
Luc Teirlinck <teirllm@auburn.edu>
parents:
52401
diff
changeset
|
190 @b{Common Lisp note:} In Common Lisp, @code{remhash} returns |
53be3d7fa5da
(Creating Hash): Clarify description of `eql'. `makehash' is obsolete.
Luc Teirlinck <teirllm@auburn.edu>
parents:
52401
diff
changeset
|
191 non-@code{nil} if it actually removed an association and @code{nil} |
53be3d7fa5da
(Creating Hash): Clarify description of `eql'. `makehash' is obsolete.
Luc Teirlinck <teirllm@auburn.edu>
parents:
52401
diff
changeset
|
192 otherwise. In Emacs Lisp, @code{remhash} always returns @code{nil}. |
25634 | 193 @end defun |
194 | |
195 @defun clrhash table | |
196 This function removes all the associations from hash table @var{table}, | |
197 so that it becomes empty. This is also called @dfn{clearing} the hash | |
198 table. | |
53025
53be3d7fa5da
(Creating Hash): Clarify description of `eql'. `makehash' is obsolete.
Luc Teirlinck <teirllm@auburn.edu>
parents:
52401
diff
changeset
|
199 |
53be3d7fa5da
(Creating Hash): Clarify description of `eql'. `makehash' is obsolete.
Luc Teirlinck <teirllm@auburn.edu>
parents:
52401
diff
changeset
|
200 @b{Common Lisp note:} In Common Lisp, @code{clrhash} returns the empty |
53be3d7fa5da
(Creating Hash): Clarify description of `eql'. `makehash' is obsolete.
Luc Teirlinck <teirllm@auburn.edu>
parents:
52401
diff
changeset
|
201 @var{table}. In Emacs Lisp, it returns @code{nil}. |
25634 | 202 @end defun |
203 | |
56215 | 204 @defun maphash function table |
53480
879f6aa2d2c7
(Hash Access): Add anchor.
Luc Teirlinck <teirllm@auburn.edu>
parents:
53037
diff
changeset
|
205 @anchor{Definition of maphash} |
25634 | 206 This function calls @var{function} once for each of the associations in |
207 @var{table}. The function @var{function} should accept two | |
208 arguments---a @var{key} listed in @var{table}, and its associated | |
60039
d1e57e5b8403
(Hash Tables): Add desc to menu items.
Richard M. Stallman <rms@gnu.org>
parents:
56215
diff
changeset
|
209 @var{value}. @code{maphash} returns @code{nil}. |
25634 | 210 @end defun |
211 | |
212 @node Defining Hash | |
213 @section Defining Hash Comparisons | |
214 @cindex hash code | |
215 | |
216 You can define new methods of key lookup by means of | |
217 @code{define-hash-table-test}. In order to use this feature, you need | |
218 to understand how hash tables work, and what a @dfn{hash code} means. | |
219 | |
220 You can think of a hash table conceptually as a large array of many | |
221 slots, each capable of holding one association. To look up a key, | |
222 @code{gethash} first computes an integer, the hash code, from the key. | |
223 It reduces this integer modulo the length of the array, to produce an | |
224 index in the array. Then it looks in that slot, and if necessary in | |
225 other nearby slots, to see if it has found the key being sought. | |
226 | |
227 Thus, to define a new method of key lookup, you need to specify both a | |
228 function to compute the hash code from a key, and a function to compare | |
229 two keys directly. | |
230 | |
231 @defun define-hash-table-test name test-fn hash-fn | |
232 This function defines a new hash table test, named @var{name}. | |
233 | |
234 After defining @var{name} in this way, you can use it as the @var{test} | |
235 argument in @code{make-hash-table}. When you do that, the hash table | |
236 will use @var{test-fn} to compare key values, and @var{hash-fn} to compute | |
237 a ``hash code'' from a key value. | |
238 | |
239 The function @var{test-fn} should accept two arguments, two keys, and | |
240 return non-@code{nil} if they are considered ``the same.'' | |
241 | |
242 The function @var{hash-fn} should accept one argument, a key, and return | |
243 an integer that is the ``hash code'' of that key. For good results, the | |
244 function should use the whole range of integer values for hash codes, | |
245 including negative integers. | |
246 | |
247 The specified functions are stored in the property list of @var{name} | |
248 under the property @code{hash-table-test}; the property value's form is | |
249 @code{(@var{test-fn} @var{hash-fn})}. | |
250 @end defun | |
251 | |
252 @defun sxhash obj | |
253 This function returns a hash code for Lisp object @var{obj}. | |
254 This is an integer which reflects the contents of @var{obj} | |
255 and the other Lisp objects it points to. | |
256 | |
257 If two objects @var{obj1} and @var{obj2} are equal, then @code{(sxhash | |
258 @var{obj1})} and @code{(sxhash @var{obj2})} are the same integer. | |
259 | |
260 If the two objects are not equal, the values returned by @code{sxhash} | |
53025
53be3d7fa5da
(Creating Hash): Clarify description of `eql'. `makehash' is obsolete.
Luc Teirlinck <teirllm@auburn.edu>
parents:
52401
diff
changeset
|
261 are usually different, but not always; once in a rare while, by luck, |
53be3d7fa5da
(Creating Hash): Clarify description of `eql'. `makehash' is obsolete.
Luc Teirlinck <teirllm@auburn.edu>
parents:
52401
diff
changeset
|
262 you will encounter two distinct-looking objects that give the same |
25634 | 263 result from @code{sxhash}. |
264 @end defun | |
265 | |
38786 | 266 This example creates a hash table whose keys are strings that are |
267 compared case-insensitively. | |
268 | |
269 @example | |
270 (defun case-fold-string= (a b) | |
271 (compare-strings a nil nil b nil nil t)) | |
272 (defun case-fold-string-hash (a) | |
273 (sxhash (upcase a))) | |
274 | |
64842
0021965e29a2
(Defining Hash): Delete stray paren in example.
Richard M. Stallman <rms@gnu.org>
parents:
60446
diff
changeset
|
275 (define-hash-table-test 'case-fold |
0021965e29a2
(Defining Hash): Delete stray paren in example.
Richard M. Stallman <rms@gnu.org>
parents:
60446
diff
changeset
|
276 'case-fold-string= 'case-fold-string-hash) |
38786 | 277 |
278 (make-hash-table :test 'case-fold) | |
279 @end example | |
280 | |
281 Here is how you could define a hash table test equivalent to the | |
282 predefined test value @code{equal}. The keys can be any Lisp object, | |
283 and equal-looking objects are considered the same key. | |
284 | |
285 @example | |
286 (define-hash-table-test 'contents-hash 'equal 'sxhash) | |
287 | |
288 (make-hash-table :test 'contents-hash) | |
289 @end example | |
290 | |
25634 | 291 @node Other Hash |
292 @section Other Hash Table Functions | |
293 | |
294 Here are some other functions for working with hash tables. | |
295 | |
296 @defun hash-table-p table | |
297 This returns non-@code{nil} if @var{table} is a hash table object. | |
298 @end defun | |
299 | |
300 @defun copy-hash-table table | |
301 This function creates and returns a copy of @var{table}. Only the table | |
302 itself is copied---the keys and values are shared. | |
303 @end defun | |
304 | |
305 @defun hash-table-count table | |
306 This function returns the actual number of entries in @var{table}. | |
307 @end defun | |
308 | |
26182 | 309 @defun hash-table-test table |
25875 | 310 This returns the @var{test} value that was given when @var{table} was |
311 created, to specify how to hash and compare keys. See | |
25634 | 312 @code{make-hash-table} (@pxref{Creating Hash}). |
313 @end defun | |
314 | |
315 @defun hash-table-weakness table | |
316 This function returns the @var{weak} value that was specified for hash | |
317 table @var{table}. | |
318 @end defun | |
319 | |
320 @defun hash-table-rehash-size table | |
321 This returns the rehash size of @var{table}. | |
322 @end defun | |
323 | |
324 @defun hash-table-rehash-threshold table | |
325 This returns the rehash threshold of @var{table}. | |
326 @end defun | |
327 | |
26182 | 328 @defun hash-table-size table |
25634 | 329 This returns the current nominal size of @var{table}. |
330 @end defun | |
52401 | 331 |
332 @ignore | |
333 arch-tag: 3b5107f9-d2f0-47d5-ad61-3498496bea0e | |
334 @end ignore |