annotate man/lispref/hash-tables.texi @ 284:558f606b08ae r21-0b40

Import from CVS: tag r21-0b40
author cvs
date Mon, 13 Aug 2007 10:34:13 +0200
parents 376386a54a3c
children 8626e4521993
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 @c -*-texinfo-*-
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2 @c This is part of the XEmacs Lisp Reference Manual.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3 @c Copyright (C) 1996 Ben Wing.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4 @c See the file lispref.texi for copying conditions.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5 @setfilename ../../info/hash-tables.info
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6 @node Hash Tables, Range Tables, Display, top
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 @chapter Hash Tables
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8 @cindex hash table
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10 @defun hashtablep object
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11 This function returns non-@code{nil} if @var{object} is a hash table.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 @menu
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15 * Introduction to Hash Tables:: Hash tables are fast data structures for
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16 implementing simple tables (i.e. finite
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17 mappings from keys to values).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18 * Working With Hash Tables:: Hash table functions.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19 * Weak Hash Tables:: Hash tables with special garbage-collection
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20 behavior.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21 @end menu
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23 @node Introduction to Hash Tables
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24 @section Introduction to Hash Tables
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26 A hash table is a data structure that provides mappings from
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 arbitrary Lisp objects (called @dfn{keys}) to other arbitrary Lisp
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 objects (called @dfn{values}). There are many ways other than
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 hash tables of implementing the same sort of mapping, e.g.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 association lists (@pxref{Association Lists}) and property lists
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 (@pxref{Property Lists}), but hash tables provide much faster lookup.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 When you create a hash table, you specify a size, which indicates the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 expected number of elements that the table will hold. You are not
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 bound by this size, however; hash tables automatically resize themselves
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 if the number of elements becomes too large.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 (Internally, hash tables are hashed using a modification of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 @dfn{linear probing} hash table method. This method hashes each
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 key to a particular spot in the hash table, and then scans forward
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 sequentially until a blank entry is found. To look up a key, hash
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 to the appropriate spot, then search forward for the key until either
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 a key is found or a blank entry stops the search. The modification
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 actually used is called @dfn{double hashing} and involves moving forward
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 by a fixed increment, whose value is computed from the original hash
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 value, rather than always moving forward by one. This eliminates
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 problems with clustering that can arise from the simple linear probing
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 method. For more information, see @cite{Algorithms} (second edition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 by Robert Sedgewick, pp. 236-241.)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 @defun make-hashtable size &optional test-fun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 This function makes a hash table of initial size @var{size}. Comparison
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 between keys is normally done with @code{eql}; i.e. two keys must be the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 same object to be considered equivalent. However, you can explicitly
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 specify the comparison function using @var{test-fun}, which must be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 one of @code{eq}, @code{eql}, or @code{equal}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 Note that currently, @code{eq} and @code{eql} are the same. This will
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 change when bignums are implemented.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 @defun copy-hashtable old-table
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 This function makes a new hash table which contains the same keys and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 values as the given table. The keys and values will not themselves be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 copied.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 @defun hashtable-fullness table
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 This function returns number of entries in @var{table}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 @node Working With Hash Tables
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 @section Working With Hash Tables
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 @defun puthash key val table
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 This function hashes @var{key} to @var{val} in @var{table}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 @defun gethash key table &optional default
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 This function finds the hash value for @var{key} in @var{table}. If
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 there is no corresponding value, @var{default} is returned (defaults to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 @code{nil}).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 @defun remhash key table
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 This function removes the hash value for @var{key} in @var{table}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 @defun clrhash table
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 This function flushes @var{table}. Afterwards, the hash table will
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 contain no entries.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 @defun maphash function table
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 This function maps @var{function} over entries in @var{table}, calling
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 it with two args, each key and value in the table.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 @node Weak Hash Tables
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 @section Weak Hash Tables
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 @cindex hash table, weak
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 @cindex weak hash table
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 A @dfn{weak hash table} is a special variety of hash table whose
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 elements do not count as GC referents. For any key-value pair in such a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 hash table, if either the key or value (or in some cases, if one
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 particular one of the two) has no references to it outside of weak hash
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 tables (and similar structures such as weak lists), the pair will be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 removed from the table, and the key and value collected. A non-weak
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 hash table (or any other pointer) would prevent the objects from being
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 collected.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 Weak hash tables are useful for keeping track of information in a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 non-obtrusive way, for example to implement caching. If the cache
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 contains objects such as buffers, markers, image instances, etc. that
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 will eventually disappear and get garbage-collected, using a weak hash
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 table ensures that these objects are collected normally rather than
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 remaining around forever, long past their actual period of use.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 (Otherwise, you'd have to explicitly map over the hash table every so
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 often and remove unnecessary elements.)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 There are three types of weak hash tables:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 @table @asis
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 @item fully weak hash tables
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 In these hash tables, a pair disappears if either the key or the value
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 is unreferenced outside of the table.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 @item key-weak hash tables
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 In these hash tables, a pair disappears if the key is unreferenced outside
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 of the table, regardless of how the value is referenced.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 @item value-weak hash tables
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 In these hash tables, a pair disappears if the value is unreferenced outside
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 of the table, regardless of how the key is referenced.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 @end table
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 Also see @ref{Weak Lists}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 @defun make-weak-hashtable size &optional test-fun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 This function makes a fully weak hash table of initial size @var{size}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 @var{test-fun} is as in @code{make-hashtable}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 @defun make-key-weak-hashtable size &optional test-fun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 This function makes a key-weak hash table of initial size @var{size}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 @var{test-fun} is as in @code{make-hashtable}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 @defun make-value-weak-hashtable size &optional test-fun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 This function makes a value-weak hash table of initial size @var{size}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 @var{test-fun} is as in @code{make-hashtable}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 @end defun