diff options
| author | Paul Eggert | 2019-07-21 11:20:07 -0700 |
|---|---|---|
| committer | Paul Eggert | 2019-07-21 11:24:11 -0700 |
| commit | cf285946bee56912286f75e4d1215214bc7c5b4b (patch) | |
| tree | 7d2e8c14783d757b1af8998b0654ce3622bbdc07 | |
| parent | 4a1507b88e813e3d54614f4cb59211234e05334a (diff) | |
| download | emacs-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.texi | 27 | ||||
| -rw-r--r-- | src/fns.c | 8 |
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, | |||
| 132 | it grows automatically. This value specifies how to make the hash table | 132 | it grows automatically. This value specifies how to make the hash table |
| 133 | larger, at that time. | 133 | larger, at that time. |
| 134 | 134 | ||
| 135 | If @var{rehash-size} is an integer, it should be positive, and the hash | 135 | If @var{rehash-size} is a fixnum, it should be positive and the hash |
| 136 | table grows by adding approximately that much to the nominal size. If | 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 | 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 | 138 | than 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 |
| 241 | slots, each capable of holding one association. To look up a key, | 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. | 242 | @code{gethash} first computes a fixnum, the hash code, from the key. |
| 243 | It reduces this integer modulo the length of the array, to produce an | 243 | It reduces this fixnum modulo the length of the array, to produce an |
| 244 | index in the array. Then it looks in that slot, and if necessary in | 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. | 245 | other 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 |
| 248 | function to compute the hash code from a key, and a function to compare | 248 | function to compute the hash code from a key, and a function to compare |
| 249 | two keys directly. | 249 | two keys directly. The two functions should be consistent with each |
| 250 | other: that is, two keys' hash codes should be the same if the keys | ||
| 251 | compare as equal. Also, since the two functions can be called at any | ||
| 252 | time (such as by the garbage collector), the functions should be free | ||
| 253 | of side effects and should return quickly, and their behavior should | ||
| 254 | depend 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 |
| 252 | This function defines a new hash table test, named @var{name}. | 257 | This 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 | |||
| 260 | return non-@code{nil} if they are considered the same. | 265 | return non-@code{nil} if they are considered the same. |
| 261 | 266 | ||
| 262 | The function @var{hash-fn} should accept one argument, a key, and return | 267 | 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 | 268 | a fixnum that is the hash code of that key. For good results, the |
| 264 | function should use the whole range of integers for hash codes, | 269 | function should use the whole range of fixnums for hash codes, |
| 265 | including negative integers. | 270 | including negative fixnums. |
| 266 | 271 | ||
| 267 | The specified functions are stored in the property list of @var{name} | 272 | 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 | 273 | under 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 |
| 273 | This function returns a hash code for Lisp object @var{obj}. | 278 | This function returns a hash code for Lisp object @var{obj}. |
| 274 | This is an integer which reflects the contents of @var{obj} | 279 | This is a fixnum that reflects the contents of @var{obj} |
| 275 | and the other Lisp objects it points to. | 280 | and the other Lisp objects it points to. |
| 276 | 281 | ||
| 277 | If two objects @var{obj1} and @var{obj2} are @code{equal}, then | 282 | If 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})} |
| 279 | are the same integer. | 284 | are the same fixnum. |
| 280 | 285 | ||
| 281 | If the two objects are not @code{equal}, the values returned by | 286 | 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 | 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 | ||
| 295 | If two objects @var{obj1} and @var{obj2} are @code{eq}, then | 300 | If 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 |
| 297 | the same integer. | 302 | the 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 | ||
| 306 | If two objects @var{obj1} and @var{obj2} are @code{eql}, then | 311 | If 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 |
| 308 | the same integer. | 313 | the 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 |
| @@ -4700,7 +4700,7 @@ sxhash (Lisp_Object obj, int depth) | |||
| 4700 | ***********************************************************************/ | 4700 | ***********************************************************************/ |
| 4701 | 4701 | ||
| 4702 | DEFUN ("sxhash-eq", Fsxhash_eq, Ssxhash_eq, 1, 1, 0, | 4702 | DEFUN ("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'. |
| 4704 | If (eq A B), then (= (sxhash-eq A) (sxhash-eq B)). | 4704 | If (eq A B), then (= (sxhash-eq A) (sxhash-eq B)). |
| 4705 | 4705 | ||
| 4706 | Hash codes are not guaranteed to be preserved across Emacs sessions. */) | 4706 | Hash 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 | ||
| 4712 | DEFUN ("sxhash-eql", Fsxhash_eql, Ssxhash_eql, 1, 1, 0, | 4712 | DEFUN ("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'. |
| 4714 | If (eql A B), then (= (sxhash-eql A) (sxhash-eql B)). | 4714 | If (eql A B), then (= (sxhash-eql A) (sxhash-eql B)). |
| 4715 | 4715 | ||
| 4716 | Hash codes are not guaranteed to be preserved across Emacs sessions. */) | 4716 | Hash 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 | ||
| 4722 | DEFUN ("sxhash-equal", Fsxhash_equal, Ssxhash_equal, 1, 1, 0, | 4722 | DEFUN ("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'. |
| 4724 | If (equal A B), then (= (sxhash-equal A) (sxhash-equal B)). | 4724 | If (equal A B), then (= (sxhash-equal A) (sxhash-equal B)). |
| 4725 | 4725 | ||
| 4726 | Hash codes are not guaranteed to be preserved across Emacs sessions. */) | 4726 | Hash 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 | |||
| 4744 | Default is 65. | 4744 | Default 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 |
| 4747 | fills up. If REHASH-SIZE is an integer, increase the size by that | 4747 | fills up. If REHASH-SIZE is a fixnum, increase the size by that |
| 4748 | amount. If it is a float, it must be > 1.0, and the new size is the | 4748 | amount. If it is a float, it must be > 1.0, and the new size is the |
| 4749 | old size multiplied by that factor. Default is 1.5. | 4749 | old size multiplied by that factor. Default is 1.5. |
| 4750 | 4750 | ||