aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaul Eggert2019-07-21 11:20:07 -0700
committerPaul Eggert2019-07-21 11:24:11 -0700
commitcf285946bee56912286f75e4d1215214bc7c5b4b (patch)
tree7d2e8c14783d757b1af8998b0654ce3622bbdc07
parent4a1507b88e813e3d54614f4cb59211234e05334a (diff)
downloademacs-cf285946bee56912286f75e4d1215214bc7c5b4b.tar.gz
emacs-cf285946bee56912286f75e4d1215214bc7c5b4b.zip
Improve doc for hash tables
* doc/lispref/hash.texi (Creating Hash, Defining Hash): * src/fns.c (Fsxhash_eq, Fsxhash_eql, Fsxhash_equal): Say that hashes are fixnums. (Fmake_hash_table): Say that that an integer rehash-size should be a fixnum. * doc/lispref/hash.texi (Defining Hash): Say that hash and comparison functions should be consistent and pure, and should return quickly.
-rw-r--r--doc/lispref/hash.texi27
-rw-r--r--src/fns.c8
2 files changed, 20 insertions, 15 deletions
diff --git a/doc/lispref/hash.texi b/doc/lispref/hash.texi
index 9b900e63099..051531491c0 100644
--- a/doc/lispref/hash.texi
+++ b/doc/lispref/hash.texi
@@ -132,7 +132,7 @@ When you add an association to a hash table and the table is full,
132it grows automatically. This value specifies how to make the hash table 132it grows automatically. This value specifies how to make the hash table
133larger, at that time. 133larger, at that time.
134 134
135If @var{rehash-size} is an integer, it should be positive, and the hash 135If @var{rehash-size} is a fixnum, it should be positive and the hash
136table grows by adding approximately that much to the nominal size. If 136table grows by adding approximately that much to the nominal size. If
137@var{rehash-size} is floating point, it had better be greater 137@var{rehash-size} is floating point, it had better be greater
138than 1, and the hash table grows by multiplying the old size by 138than 1, and the hash table grows by multiplying the old size by
@@ -239,14 +239,19 @@ to understand how hash tables work, and what a @dfn{hash code} means.
239 239
240 You can think of a hash table conceptually as a large array of many 240 You can think of a hash table conceptually as a large array of many
241slots, each capable of holding one association. To look up a key, 241slots, each capable of holding one association. To look up a key,
242@code{gethash} first computes an integer, the hash code, from the key. 242@code{gethash} first computes a fixnum, the hash code, from the key.
243It reduces this integer modulo the length of the array, to produce an 243It reduces this fixnum modulo the length of the array, to produce an
244index in the array. Then it looks in that slot, and if necessary in 244index in the array. Then it looks in that slot, and if necessary in
245other nearby slots, to see if it has found the key being sought. 245other nearby slots, to see if it has found the key being sought.
246 246
247 Thus, to define a new method of key lookup, you need to specify both a 247 Thus, to define a new method of key lookup, you need to specify both a
248function to compute the hash code from a key, and a function to compare 248function to compute the hash code from a key, and a function to compare
249two keys directly. 249two keys directly. The two functions should be consistent with each
250other: that is, two keys' hash codes should be the same if the keys
251compare as equal. Also, since the two functions can be called at any
252time (such as by the garbage collector), the functions should be free
253of side effects and should return quickly, and their behavior should
254depend on only on properties of the keys that do not change.
250 255
251@defun define-hash-table-test name test-fn hash-fn 256@defun define-hash-table-test name test-fn hash-fn
252This function defines a new hash table test, named @var{name}. 257This function defines a new hash table test, named @var{name}.
@@ -260,9 +265,9 @@ The function @var{test-fn} should accept two arguments, two keys, and
260return non-@code{nil} if they are considered the same. 265return non-@code{nil} if they are considered the same.
261 266
262The function @var{hash-fn} should accept one argument, a key, and return 267The function @var{hash-fn} should accept one argument, a key, and return
263an integer that is the hash code of that key. For good results, the 268a fixnum that is the hash code of that key. For good results, the
264function should use the whole range of integers for hash codes, 269function should use the whole range of fixnums for hash codes,
265including negative integers. 270including negative fixnums.
266 271
267The specified functions are stored in the property list of @var{name} 272The specified functions are stored in the property list of @var{name}
268under the property @code{hash-table-test}; the property value's form is 273under the property @code{hash-table-test}; the property value's form is
@@ -271,12 +276,12 @@ under the property @code{hash-table-test}; the property value's form is
271 276
272@defun sxhash-equal obj 277@defun sxhash-equal obj
273This function returns a hash code for Lisp object @var{obj}. 278This function returns a hash code for Lisp object @var{obj}.
274This is an integer which reflects the contents of @var{obj} 279This is a fixnum that reflects the contents of @var{obj}
275and the other Lisp objects it points to. 280and the other Lisp objects it points to.
276 281
277If two objects @var{obj1} and @var{obj2} are @code{equal}, then 282If two objects @var{obj1} and @var{obj2} are @code{equal}, then
278@code{(sxhash-equal @var{obj1})} and @code{(sxhash-equal @var{obj2})} 283@code{(sxhash-equal @var{obj1})} and @code{(sxhash-equal @var{obj2})}
279are the same integer. 284are the same fixnum.
280 285
281If the two objects are not @code{equal}, the values returned by 286If the two objects are not @code{equal}, the values returned by
282@code{sxhash-equal} are usually different, but not always; once in a 287@code{sxhash-equal} are usually different, but not always; once in a
@@ -294,7 +299,7 @@ result reflects identity of @var{obj}, but not its contents.
294 299
295If two objects @var{obj1} and @var{obj2} are @code{eq}, then 300If two objects @var{obj1} and @var{obj2} are @code{eq}, then
296@code{(sxhash-eq @var{obj1})} and @code{(sxhash-eq @var{obj2})} are 301@code{(sxhash-eq @var{obj1})} and @code{(sxhash-eq @var{obj2})} are
297the same integer. 302the same fixnum.
298@end defun 303@end defun
299 304
300@defun sxhash-eql obj 305@defun sxhash-eql obj
@@ -305,7 +310,7 @@ in which case a hash code is generated for the value.
305 310
306If two objects @var{obj1} and @var{obj2} are @code{eql}, then 311If two objects @var{obj1} and @var{obj2} are @code{eql}, then
307@code{(sxhash-eql @var{obj1})} and @code{(sxhash-eql @var{obj2})} are 312@code{(sxhash-eql @var{obj1})} and @code{(sxhash-eql @var{obj2})} are
308the same integer. 313the same fixnum.
309@end defun 314@end defun
310 315
311 This example creates a hash table whose keys are strings that are 316 This example creates a hash table whose keys are strings that are
diff --git a/src/fns.c b/src/fns.c
index d7e123122d6..819eaec7c74 100644
--- a/src/fns.c
+++ b/src/fns.c
@@ -4700,7 +4700,7 @@ sxhash (Lisp_Object obj, int depth)
4700 ***********************************************************************/ 4700 ***********************************************************************/
4701 4701
4702DEFUN ("sxhash-eq", Fsxhash_eq, Ssxhash_eq, 1, 1, 0, 4702DEFUN ("sxhash-eq", Fsxhash_eq, Ssxhash_eq, 1, 1, 0,
4703 doc: /* Return an integer hash code for OBJ suitable for `eq'. 4703 doc: /* Return a fixnum hash code for OBJ suitable for `eq'.
4704If (eq A B), then (= (sxhash-eq A) (sxhash-eq B)). 4704If (eq A B), then (= (sxhash-eq A) (sxhash-eq B)).
4705 4705
4706Hash codes are not guaranteed to be preserved across Emacs sessions. */) 4706Hash codes are not guaranteed to be preserved across Emacs sessions. */)
@@ -4710,7 +4710,7 @@ Hash codes are not guaranteed to be preserved across Emacs sessions. */)
4710} 4710}
4711 4711
4712DEFUN ("sxhash-eql", Fsxhash_eql, Ssxhash_eql, 1, 1, 0, 4712DEFUN ("sxhash-eql", Fsxhash_eql, Ssxhash_eql, 1, 1, 0,
4713 doc: /* Return an integer hash code for OBJ suitable for `eql'. 4713 doc: /* Return a fixnum hash code for OBJ suitable for `eql'.
4714If (eql A B), then (= (sxhash-eql A) (sxhash-eql B)). 4714If (eql A B), then (= (sxhash-eql A) (sxhash-eql B)).
4715 4715
4716Hash codes are not guaranteed to be preserved across Emacs sessions. */) 4716Hash codes are not guaranteed to be preserved across Emacs sessions. */)
@@ -4720,7 +4720,7 @@ Hash codes are not guaranteed to be preserved across Emacs sessions. */)
4720} 4720}
4721 4721
4722DEFUN ("sxhash-equal", Fsxhash_equal, Ssxhash_equal, 1, 1, 0, 4722DEFUN ("sxhash-equal", Fsxhash_equal, Ssxhash_equal, 1, 1, 0,
4723 doc: /* Return an integer hash code for OBJ suitable for `equal'. 4723 doc: /* Return a fixnum hash code for OBJ suitable for `equal'.
4724If (equal A B), then (= (sxhash-equal A) (sxhash-equal B)). 4724If (equal A B), then (= (sxhash-equal A) (sxhash-equal B)).
4725 4725
4726Hash codes are not guaranteed to be preserved across Emacs sessions. */) 4726Hash codes are not guaranteed to be preserved across Emacs sessions. */)
@@ -4744,7 +4744,7 @@ keys. Default is `eql'. Predefined are the tests `eq', `eql', and
4744Default is 65. 4744Default is 65.
4745 4745
4746:rehash-size REHASH-SIZE - Indicates how to expand the table when it 4746:rehash-size REHASH-SIZE - Indicates how to expand the table when it
4747fills up. If REHASH-SIZE is an integer, increase the size by that 4747fills up. If REHASH-SIZE is a fixnum, increase the size by that
4748amount. If it is a float, it must be > 1.0, and the new size is the 4748amount. If it is a float, it must be > 1.0, and the new size is the
4749old size multiplied by that factor. Default is 1.5. 4749old size multiplied by that factor. Default is 1.5.
4750 4750