Mercurial > emacs
annotate lispref/hash.texi @ 32274:5428e8f8fbda
(Font Lock): Explain how to customize Font Lock faces.
(Faces): Document set-face-foreground and set-face-background.
author | Eli Zaretskii <eliz@gnu.org> |
---|---|
date | Sun, 08 Oct 2000 08:43:31 +0000 |
parents | 62ed067637af |
children | 4d3fd773cd30 |
rev | line source |
---|---|
25634 | 1 @c -*-texinfo-*- |
2 @c This is part of the GNU Emacs Lisp Reference Manual. | |
3 @c Copyright (C) 1999 Free Software Foundation, Inc. | |
4 @c See the file elisp.texi for copying conditions. | |
5 @setfilename ../info/hash | |
6 @node Hash Tables, Symbols, Sequences Arrays Vectors, Top | |
7 @chapter Hash Tables | |
8 @cindex hash tables | |
9 | |
10 A hash table is a very fast kind of lookup table, somewhat like | |
11 an alist in that it maps keys to corresponding values. It differs | |
12 from an alist in these ways: | |
13 | |
14 @itemize @bullet | |
15 @item | |
26710
ed440ffea308
(Hash Tables): Note that alists win for small tables.
Dave Love <fx@gnu.org>
parents:
26303
diff
changeset
|
16 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
|
17 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
|
18 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
|
19 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
|
20 constant overhead. |
25634 | 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 (starting with Emacs 21) provides a general-purpose hash | |
31 table data type, along with a series of functions for operating on them. | |
32 Hash tables have no read syntax, and print in hash notation, like this: | |
33 | |
34 @example | |
35 (make-hash-table) | |
36 @result{} #<hash-table 'eql nil 0/65 0x83af980> | |
37 @end example | |
38 | |
26303 | 39 @noindent |
40 (The term ``hash notation'' refers to the initial @samp{#} | |
41 character---@pxref{Printed Representation}---and has nothing to do with | |
42 the term ``hash table.'') | |
43 | |
25634 | 44 Obarrays are also a kind of hash table, but they are a different type |
45 of object and are used only for recording interned symbols | |
46 (@pxref{Creating Symbols}). | |
47 | |
48 @menu | |
49 * Creating Hash:: | |
50 * Hash Access:: | |
51 * Defining Hash:: | |
52 * Other Hash:: | |
53 @end menu | |
54 | |
55 @node Creating Hash | |
56 @section Creating Hash Tables | |
57 | |
58 The principal function for creating a hash table is | |
59 @code{make-hash-table}. | |
60 | |
61 @tindex make-hash-table | |
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 | |
79 Keys which are numbers are ``the same'' if they are equal in value; | |
80 otherwise, two distinct objects are never ``the same''. | |
81 | |
82 @item eq | |
83 Any two distinct Lisp objects are ``different'' as keys. | |
84 | |
85 @item equal | |
86 Two Lisp objects are ``the same'', as keys, if they are equal | |
87 according to @code{equal}. | |
88 @end table | |
89 | |
90 You can use @code{define-hash-table-test} (@pxref{Defining Hash}) to | |
91 define additional possibilities for @var{test}. | |
92 | |
93 @item :weakness @var{weak} | |
94 The weakness of a hash table specifies whether the presence of a key or | |
95 value in the hash table preserves it from garbage collection. | |
96 | |
97 The value, @var{weak}, must be one of @code{nil}, @code{key}, | |
30524 | 98 @code{value}, @code{key-or-value}, @code{key-and-value}, or @code{t} |
99 which is an alias for @code{key-and-value}. If @var{weak} is @code{key} | |
100 then the hash table does not prevent its keys from being collected as | |
101 garbage (if they are not referenced anywhere else); if a particular key | |
102 does get collected, the corresponding association is removed from the | |
103 hash table. | |
25634 | 104 |
30524 | 105 If @var{weak} is @code{value}, then the hash table does not prevent |
106 values from being collected as garbage (if they are not referenced | |
107 anywhere else); if a particular value does get collected, the | |
25634 | 108 corresponding association is removed from the hash table. |
109 | |
30524 | 110 If @var{weak} is @code{key-or-value}, associations are removed from the |
111 hash table when either their key or their value part would be collected | |
112 as garbage, not counting references to the key and value from weak hash | |
113 tables. Likewise, if @var{weak} is @code{key-and-value}, associations | |
114 are removed from the hash table when both their key and value would be | |
115 collected as garbage, again not considering references to the key and | |
116 value from weak hash tables. | |
117 | |
25634 | 118 The default for @var{weak} is @code{nil}, so that all keys and values |
25875 | 119 referenced in the hash table are preserved from garbage collection. If |
120 @var{weak} is @code{t}, neither keys nor values are protected (that is, | |
121 both are weak). | |
25634 | 122 |
123 @item :size @var{size} | |
124 This specifies a hint for how many associations you plan to store in the | |
125 hash table. If you know the approximate number, you can make things a | |
26182 | 126 little more efficient by specifying it this way. If you specify too |
25634 | 127 small a size, the hash table will grow automatically when necessary, but |
26303 | 128 doing that takes some extra time. |
25634 | 129 |
130 The default size is 65. | |
131 | |
132 @item :rehash-size @var{rehash-size} | |
133 When you add an association to a hash table and the table is ``full,'' | |
134 it grows automatically. This value specifies how to make the hash table | |
135 larger, at that time. | |
136 | |
25875 | 137 If @var{rehash-size} is an integer, it should be positive, and the hash |
138 table grows by adding that much to the nominal size. If | |
139 @var{rehash-size} is a floating point number, it had better be greater | |
140 than 1, and the hash table grows by multiplying the old size by that | |
141 number. | |
25634 | 142 |
143 The default value is 1.5. | |
144 | |
145 @item :rehash-threshold @var{threshold} | |
146 This specifies the criterion for when the hash table is ``full.'' The | |
147 value, @var{threshold}, should be a positive floating point number, no | |
148 greater than 1. The hash table is ``full'' whenever the actual number of | |
149 entries exceeds this fraction of the nominal size. The default for | |
150 @var{threshold} is 0.8. | |
151 @end table | |
152 @end defun | |
153 | |
154 @tindex makehash | |
155 @defun makehash &optional test | |
156 This is equivalent to @code{make-hash-table}, but with a different style | |
157 argument list. The argument @var{test} specifies the method | |
158 of key lookup. | |
159 | |
160 If you want to specify other parameters, you should use | |
161 @code{make-hash-table}. | |
162 @end defun | |
163 | |
164 @node Hash Access | |
165 @section Hash Table Access | |
166 | |
167 This section describes the functions for accessing and storing | |
168 associations in a hash table. | |
169 | |
170 @tindex gethash | |
171 @defun gethash key table &optional default | |
172 This function looks up @var{key} in @var{table}, and returns its | |
173 associated @var{value}---or @var{default}, if @var{key} has no | |
174 association in @var{table}. | |
175 @end defun | |
176 | |
177 @tindex puthash | |
178 @defun puthash key value table | |
179 This function enters an association for @var{key} in @var{table}, with | |
180 value @var{value}. If @var{key} already has an association in | |
181 @var{table}, @var{value} replaces the old associated value. | |
182 @end defun | |
183 | |
184 @tindex remhash | |
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. | |
189 @end defun | |
190 | |
191 @tindex clrhash | |
192 @defun clrhash table | |
193 This function removes all the associations from hash table @var{table}, | |
194 so that it becomes empty. This is also called @dfn{clearing} the hash | |
195 table. | |
196 @end defun | |
197 | |
198 @tindex maphash | |
199 @defun maphash function table | |
200 This function calls @var{function} once for each of the associations in | |
201 @var{table}. The function @var{function} should accept two | |
202 arguments---a @var{key} listed in @var{table}, and its associated | |
203 @var{value}. | |
204 @end defun | |
205 | |
206 @node Defining Hash | |
207 @section Defining Hash Comparisons | |
208 @cindex hash code | |
209 | |
210 You can define new methods of key lookup by means of | |
211 @code{define-hash-table-test}. In order to use this feature, you need | |
212 to understand how hash tables work, and what a @dfn{hash code} means. | |
213 | |
214 You can think of a hash table conceptually as a large array of many | |
215 slots, each capable of holding one association. To look up a key, | |
216 @code{gethash} first computes an integer, the hash code, from the key. | |
217 It reduces this integer modulo the length of the array, to produce an | |
218 index in the array. Then it looks in that slot, and if necessary in | |
219 other nearby slots, to see if it has found the key being sought. | |
220 | |
221 Thus, to define a new method of key lookup, you need to specify both a | |
222 function to compute the hash code from a key, and a function to compare | |
223 two keys directly. | |
224 | |
225 @tindex define-hash-table-test | |
226 @defun define-hash-table-test name test-fn hash-fn | |
227 This function defines a new hash table test, named @var{name}. | |
228 | |
229 After defining @var{name} in this way, you can use it as the @var{test} | |
230 argument in @code{make-hash-table}. When you do that, the hash table | |
231 will use @var{test-fn} to compare key values, and @var{hash-fn} to compute | |
232 a ``hash code'' from a key value. | |
233 | |
234 The function @var{test-fn} should accept two arguments, two keys, and | |
235 return non-@code{nil} if they are considered ``the same.'' | |
236 | |
237 The function @var{hash-fn} should accept one argument, a key, and return | |
238 an integer that is the ``hash code'' of that key. For good results, the | |
239 function should use the whole range of integer values for hash codes, | |
240 including negative integers. | |
241 | |
242 The specified functions are stored in the property list of @var{name} | |
243 under the property @code{hash-table-test}; the property value's form is | |
244 @code{(@var{test-fn} @var{hash-fn})}. | |
245 | |
246 This example creates a hash table whose keys are strings that are | |
247 compared case-insensitively. | |
248 | |
249 @example | |
250 (defun case-fold-string= (a b) | |
251 (compare-strings a nil nil b nil nil t)) | |
252 | |
253 (defun case-fold-string-hash (a) | |
254 (sxhash (upcase a))) | |
255 | |
256 (define-hash-table-test 'case-fold 'case-fold-string= | |
257 'case-fold-string-hash)) | |
258 | |
259 (make-hash-table :test 'case-fold) | |
260 @end example | |
261 @end defun | |
262 | |
263 @tindex sxhash | |
264 @defun sxhash obj | |
265 This function returns a hash code for Lisp object @var{obj}. | |
266 This is an integer which reflects the contents of @var{obj} | |
267 and the other Lisp objects it points to. | |
268 | |
269 If two objects @var{obj1} and @var{obj2} are equal, then @code{(sxhash | |
270 @var{obj1})} and @code{(sxhash @var{obj2})} are the same integer. | |
271 | |
272 If the two objects are not equal, the values returned by @code{sxhash} | |
273 are usually different, but not always; but once in a rare while, by | |
274 luck, you will encounter two distinct-looking objects that give the same | |
275 result from @code{sxhash}. | |
276 @end defun | |
277 | |
278 @node Other Hash | |
279 @section Other Hash Table Functions | |
280 | |
281 Here are some other functions for working with hash tables. | |
282 | |
283 @tindex hash-table-p | |
284 @defun hash-table-p table | |
285 This returns non-@code{nil} if @var{table} is a hash table object. | |
286 @end defun | |
287 | |
288 @tindex copy-hash-table | |
289 @defun copy-hash-table table | |
290 This function creates and returns a copy of @var{table}. Only the table | |
291 itself is copied---the keys and values are shared. | |
292 @end defun | |
293 | |
294 @tindex hash-table-count | |
295 @defun hash-table-count table | |
296 This function returns the actual number of entries in @var{table}. | |
297 @end defun | |
298 | |
26182 | 299 @tindex hash-table-test |
300 @defun hash-table-test table | |
25875 | 301 This returns the @var{test} value that was given when @var{table} was |
302 created, to specify how to hash and compare keys. See | |
25634 | 303 @code{make-hash-table} (@pxref{Creating Hash}). |
304 @end defun | |
305 | |
306 @tindex hash-table-weakness | |
307 @defun hash-table-weakness table | |
308 This function returns the @var{weak} value that was specified for hash | |
309 table @var{table}. | |
310 @end defun | |
311 | |
312 @tindex hash-table-rehash-size | |
313 @defun hash-table-rehash-size table | |
314 This returns the rehash size of @var{table}. | |
315 @end defun | |
316 | |
317 @tindex hash-table-rehash-threshold | |
318 @defun hash-table-rehash-threshold table | |
319 This returns the rehash threshold of @var{table}. | |
320 @end defun | |
321 | |
26182 | 322 @tindex hash-table-size |
323 @defun hash-table-size table | |
25634 | 324 This returns the current nominal size of @var{table}. |
325 @end defun |