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