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