25634
|
1 @c -*-texinfo-*-
|
|
2 @c This is part of the GNU Emacs Lisp Reference Manual.
|
|
3 @c Copyright (C) 1999 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
|
|
10 A hash table is a very fast kind of lookup table, somewhat like
|
|
11 an alist in that it maps keys to corresponding values. It differs
|
|
12 from an alist in these ways:
|
|
13
|
|
14 @itemize @bullet
|
|
15 @item
|
|
16 Lookup in a hash table is extremely fast---in fact, the time required
|
|
17 is essentially @emph{independent} of how many elements are stored
|
|
18 in the table.
|
|
19
|
|
20 @item
|
|
21 The correspondences in a hash table are in no particular order.
|
|
22
|
|
23 @item
|
|
24 There is no way to share structure between two hash tables,
|
|
25 the way two alists can share a common tail.
|
|
26 @end itemize
|
|
27
|
|
28 Emacs Lisp (starting with Emacs 21) provides a general-purpose hash
|
|
29 table data type, along with a series of functions for operating on them.
|
|
30 Hash tables have no read syntax, and print in hash notation, like this:
|
|
31
|
|
32 @example
|
|
33 (make-hash-table)
|
|
34 @result{} #<hash-table 'eql nil 0/65 0x83af980>
|
|
35 @end example
|
|
36
|
26303
|
37 @noindent
|
|
38 (The term ``hash notation'' refers to the initial @samp{#}
|
|
39 character---@pxref{Printed Representation}---and has nothing to do with
|
|
40 the term ``hash table.'')
|
|
41
|
25634
|
42 Obarrays are also a kind of hash table, but they are a different type
|
|
43 of object and are used only for recording interned symbols
|
|
44 (@pxref{Creating Symbols}).
|
|
45
|
|
46 @menu
|
|
47 * Creating Hash::
|
|
48 * Hash Access::
|
|
49 * Defining Hash::
|
|
50 * Other Hash::
|
|
51 @end menu
|
|
52
|
|
53 @node Creating Hash
|
|
54 @section Creating Hash Tables
|
|
55
|
|
56 The principal function for creating a hash table is
|
|
57 @code{make-hash-table}.
|
|
58
|
|
59 @tindex make-hash-table
|
|
60 @defun make-hash-table &rest keyword-args
|
|
61 This function creates a new hash table according to the specified
|
|
62 arguments. The arguments should consist of alternating keywords
|
|
63 (particular symbols recognized specially) and values corresponding to
|
|
64 them.
|
|
65
|
|
66 Several keywords make sense in @code{make-hash-table}, but the only two
|
26182
|
67 that you really need to know about are @code{:test} and @code{:weakness}.
|
25634
|
68
|
|
69 @table @code
|
|
70 @item :test @var{test}
|
|
71 This specifies the method of key lookup for this hash table. The
|
|
72 default is @code{eql}; @code{eq} and @code{equal} are other
|
|
73 alternatives:
|
|
74
|
|
75 @table @code
|
|
76 @item eql
|
|
77 Keys which are numbers are ``the same'' if they are equal in value;
|
|
78 otherwise, two distinct objects are never ``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} or @code{t}. If @var{weak} is @code{key} or @code{t}, then
|
|
97 the hash table does not prevent its keys from being collected as garbage
|
|
98 (if they are not referenced anywhere else); if a particular key does get
|
|
99 collected, the corresponding association is removed from the hash table.
|
|
100
|
|
101 Likewise, if @var{weak} is @code{value} or @code{t}, then the hash table
|
|
102 does not prevent values from being collected as garbage (if they are not
|
|
103 referenced anywhere else); if a particular value does get collected, the
|
|
104 corresponding association is removed from the hash table.
|
|
105
|
|
106 The default for @var{weak} is @code{nil}, so that all keys and values
|
25875
|
107 referenced in the hash table are preserved from garbage collection. If
|
|
108 @var{weak} is @code{t}, neither keys nor values are protected (that is,
|
|
109 both are weak).
|
25634
|
110
|
|
111 @item :size @var{size}
|
|
112 This specifies a hint for how many associations you plan to store in the
|
|
113 hash table. If you know the approximate number, you can make things a
|
26182
|
114 little more efficient by specifying it this way. If you specify too
|
25634
|
115 small a size, the hash table will grow automatically when necessary, but
|
26303
|
116 doing that takes some extra time.
|
25634
|
117
|
|
118 The default size is 65.
|
|
119
|
|
120 @item :rehash-size @var{rehash-size}
|
|
121 When you add an association to a hash table and the table is ``full,''
|
|
122 it grows automatically. This value specifies how to make the hash table
|
|
123 larger, at that time.
|
|
124
|
25875
|
125 If @var{rehash-size} is an integer, it should be positive, and the hash
|
|
126 table grows by adding that much to the nominal size. If
|
|
127 @var{rehash-size} is a floating point number, it had better be greater
|
|
128 than 1, and the hash table grows by multiplying the old size by that
|
|
129 number.
|
25634
|
130
|
|
131 The default value is 1.5.
|
|
132
|
|
133 @item :rehash-threshold @var{threshold}
|
|
134 This specifies the criterion for when the hash table is ``full.'' The
|
|
135 value, @var{threshold}, should be a positive floating point number, no
|
|
136 greater than 1. The hash table is ``full'' whenever the actual number of
|
|
137 entries exceeds this fraction of the nominal size. The default for
|
|
138 @var{threshold} is 0.8.
|
|
139 @end table
|
|
140 @end defun
|
|
141
|
|
142 @tindex makehash
|
|
143 @defun makehash &optional test
|
|
144 This is equivalent to @code{make-hash-table}, but with a different style
|
|
145 argument list. The argument @var{test} specifies the method
|
|
146 of key lookup.
|
|
147
|
|
148 If you want to specify other parameters, you should use
|
|
149 @code{make-hash-table}.
|
|
150 @end defun
|
|
151
|
|
152 @node Hash Access
|
|
153 @section Hash Table Access
|
|
154
|
|
155 This section describes the functions for accessing and storing
|
|
156 associations in a hash table.
|
|
157
|
|
158 @tindex gethash
|
|
159 @defun gethash key table &optional default
|
|
160 This function looks up @var{key} in @var{table}, and returns its
|
|
161 associated @var{value}---or @var{default}, if @var{key} has no
|
|
162 association in @var{table}.
|
|
163 @end defun
|
|
164
|
|
165 @tindex puthash
|
|
166 @defun puthash key value table
|
|
167 This function enters an association for @var{key} in @var{table}, with
|
|
168 value @var{value}. If @var{key} already has an association in
|
|
169 @var{table}, @var{value} replaces the old associated value.
|
|
170 @end defun
|
|
171
|
|
172 @tindex remhash
|
|
173 @defun remhash key table
|
|
174 This function removes the association for @var{key} from @var{table}, if
|
|
175 there is one. If @var{key} has no association, @code{remhash} does
|
|
176 nothing.
|
|
177 @end defun
|
|
178
|
|
179 @tindex clrhash
|
|
180 @defun clrhash table
|
|
181 This function removes all the associations from hash table @var{table},
|
|
182 so that it becomes empty. This is also called @dfn{clearing} the hash
|
|
183 table.
|
|
184 @end defun
|
|
185
|
|
186 @tindex maphash
|
|
187 @defun maphash function table
|
|
188 This function calls @var{function} once for each of the associations in
|
|
189 @var{table}. The function @var{function} should accept two
|
|
190 arguments---a @var{key} listed in @var{table}, and its associated
|
|
191 @var{value}.
|
|
192 @end defun
|
|
193
|
|
194 @node Defining Hash
|
|
195 @section Defining Hash Comparisons
|
|
196 @cindex hash code
|
|
197
|
|
198 You can define new methods of key lookup by means of
|
|
199 @code{define-hash-table-test}. In order to use this feature, you need
|
|
200 to understand how hash tables work, and what a @dfn{hash code} means.
|
|
201
|
|
202 You can think of a hash table conceptually as a large array of many
|
|
203 slots, each capable of holding one association. To look up a key,
|
|
204 @code{gethash} first computes an integer, the hash code, from the key.
|
|
205 It reduces this integer modulo the length of the array, to produce an
|
|
206 index in the array. Then it looks in that slot, and if necessary in
|
|
207 other nearby slots, to see if it has found the key being sought.
|
|
208
|
|
209 Thus, to define a new method of key lookup, you need to specify both a
|
|
210 function to compute the hash code from a key, and a function to compare
|
|
211 two keys directly.
|
|
212
|
|
213 @tindex define-hash-table-test
|
|
214 @defun define-hash-table-test name test-fn hash-fn
|
|
215 This function defines a new hash table test, named @var{name}.
|
|
216
|
|
217 After defining @var{name} in this way, you can use it as the @var{test}
|
|
218 argument in @code{make-hash-table}. When you do that, the hash table
|
|
219 will use @var{test-fn} to compare key values, and @var{hash-fn} to compute
|
|
220 a ``hash code'' from a key value.
|
|
221
|
|
222 The function @var{test-fn} should accept two arguments, two keys, and
|
|
223 return non-@code{nil} if they are considered ``the same.''
|
|
224
|
|
225 The function @var{hash-fn} should accept one argument, a key, and return
|
|
226 an integer that is the ``hash code'' of that key. For good results, the
|
|
227 function should use the whole range of integer values for hash codes,
|
|
228 including negative integers.
|
|
229
|
|
230 The specified functions are stored in the property list of @var{name}
|
|
231 under the property @code{hash-table-test}; the property value's form is
|
|
232 @code{(@var{test-fn} @var{hash-fn})}.
|
|
233
|
|
234 This example creates a hash table whose keys are strings that are
|
|
235 compared case-insensitively.
|
|
236
|
|
237 @example
|
|
238 (defun case-fold-string= (a b)
|
|
239 (compare-strings a nil nil b nil nil t))
|
|
240
|
|
241 (defun case-fold-string-hash (a)
|
|
242 (sxhash (upcase a)))
|
|
243
|
|
244 (define-hash-table-test 'case-fold 'case-fold-string=
|
|
245 'case-fold-string-hash))
|
|
246
|
|
247 (make-hash-table :test 'case-fold)
|
|
248 @end example
|
|
249 @end defun
|
|
250
|
|
251 @tindex sxhash
|
|
252 @defun sxhash obj
|
|
253 This function returns a hash code for Lisp object @var{obj}.
|
|
254 This is an integer which reflects the contents of @var{obj}
|
|
255 and the other Lisp objects it points to.
|
|
256
|
|
257 If two objects @var{obj1} and @var{obj2} are equal, then @code{(sxhash
|
|
258 @var{obj1})} and @code{(sxhash @var{obj2})} are the same integer.
|
|
259
|
|
260 If the two objects are not equal, the values returned by @code{sxhash}
|
|
261 are usually different, but not always; but once in a rare while, by
|
|
262 luck, you will encounter two distinct-looking objects that give the same
|
|
263 result from @code{sxhash}.
|
|
264 @end defun
|
|
265
|
|
266 @node Other Hash
|
|
267 @section Other Hash Table Functions
|
|
268
|
|
269 Here are some other functions for working with hash tables.
|
|
270
|
|
271 @tindex hash-table-p
|
|
272 @defun hash-table-p table
|
|
273 This returns non-@code{nil} if @var{table} is a hash table object.
|
|
274 @end defun
|
|
275
|
|
276 @tindex copy-hash-table
|
|
277 @defun copy-hash-table table
|
|
278 This function creates and returns a copy of @var{table}. Only the table
|
|
279 itself is copied---the keys and values are shared.
|
|
280 @end defun
|
|
281
|
|
282 @tindex hash-table-count
|
|
283 @defun hash-table-count table
|
|
284 This function returns the actual number of entries in @var{table}.
|
|
285 @end defun
|
|
286
|
26182
|
287 @tindex hash-table-test
|
|
288 @defun hash-table-test table
|
25875
|
289 This returns the @var{test} value that was given when @var{table} was
|
|
290 created, to specify how to hash and compare keys. See
|
25634
|
291 @code{make-hash-table} (@pxref{Creating Hash}).
|
|
292 @end defun
|
|
293
|
|
294 @tindex hash-table-weakness
|
|
295 @defun hash-table-weakness table
|
|
296 This function returns the @var{weak} value that was specified for hash
|
|
297 table @var{table}.
|
|
298 @end defun
|
|
299
|
|
300 @tindex hash-table-rehash-size
|
|
301 @defun hash-table-rehash-size table
|
|
302 This returns the rehash size of @var{table}.
|
|
303 @end defun
|
|
304
|
|
305 @tindex hash-table-rehash-threshold
|
|
306 @defun hash-table-rehash-threshold table
|
|
307 This returns the rehash threshold of @var{table}.
|
|
308 @end defun
|
|
309
|
26182
|
310 @tindex hash-table-size
|
|
311 @defun hash-table-size table
|
25634
|
312 This returns the current nominal size of @var{table}.
|
|
313 @end defun
|