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