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