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