annotate man/lispref/hash-tables.texi @ 398:74fd4e045ea6 r21-2-29

Import from CVS: tag r21-2-29
author cvs
date Mon, 13 Aug 2007 11:13:30 +0200
parents 8626e4521993
children 697ef44129c6
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
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
10 @defun hash-table-p object
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
11 This function returns @code{t} if @var{object} is a hash table, else @code{nil}.
0
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
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
26 A @dfn{hash table} is a data structure that provides mappings from
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
27 arbitrary Lisp objects called @dfn{keys} to other arbitrary Lisp objects
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
28 called @dfn{values}. A key/value pair is sometimes called an
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
29 @dfn{entry} in the hash table. There are many ways other than hash
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
30 tables of implementing the same sort of mapping, e.g. association lists
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
31 (@pxref{Association Lists}) and property lists (@pxref{Property Lists}),
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
32 but hash tables provide much faster lookup when there are many entries
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
33 in the mapping. Hash tables are an implementation of the abstract data
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
34 type @dfn{dictionary}, also known as @dfn{associative array}.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
36 Internally, hash tables are hashed using the @dfn{linear probing} hash
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
37 table implementation method. This method hashes each key to a
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
38 particular spot in the hash table, and then scans forward sequentially
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
39 until a blank entry is found. To look up a key, hash to the appropriate
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
40 spot, then search forward for the key until either a key is found or a
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
41 blank entry stops the search. This method is used in preference to
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
42 double hashing because of changes in recent hardware. The penalty for
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
43 non-sequential access to memory has been increasing, and this
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
44 compensates for the problem of clustering that linear probing entails.
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
45
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
46 When hash tables are created, the user may (but is not required to)
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
47 specify initial properties that influence performance.
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
48
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
49 Use the @code{:size} parameter to specify the number of entries that are
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
50 likely to be stored in the hash table, to avoid the overhead of resizing
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
51 the table. But if the pre-allocated space for the entries is never
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
52 used, it is simply wasted and makes XEmacs slower. Excess unused hash
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
53 table entries exact a small continuous performance penalty, since they
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
54 must be scanned at every garbage collection. If the number of entries
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
55 in the hash table is unknown, simply avoid using the @code{:size}
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
56 keyword.
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
57
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
58 Use the @code{:rehash-size} and @code{:rehash-threshold} keywords to
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
59 adjust the algorithm for deciding when to rehash the hash table. For
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
60 temporary hash tables that are going to be very heavily used, use a
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
61 small rehash threshold, for example, 0.4 and a large rehash size, for
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
62 example 2.0. For permanent hash tables that will be infrequently used,
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
63 specify a large rehash threshold, for example 0.8.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
65 Hash tables can also be created by the lisp reader using structure
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
66 syntax, for example:
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
67 @example
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
68 #s(hash-table size 20 data (foo 1 bar 2))
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
69 @end example
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
70
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
71 The structure syntax accepts the same keywords as @code{make-hash-table}
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
72 (without the @code{:} character), as well as the additional keyword
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
73 @code{data}, which specifies the initial hash table contents.
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
74
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 380
diff changeset
75 @defun make-hash-table &key @code{test} @code{size} @code{rehash-size} @code{rehash-threshold} @code{weakness}
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
76 This function returns a new empty hash table object.
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
77
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
78 Keyword @code{:test} can be @code{eq}, @code{eql} (default) or @code{equal}.
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
79 Comparison between keys is done using this function.
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
80 If speed is important, consider using @code{eq}.
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
81 When storing strings in the hash table, you will likely need to use @code{equal}.
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
82
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 380
diff changeset
83 Keyword @code{:size} specifies the number of keys likely to be inserted.
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 380
diff changeset
84 This number of entries can be inserted without enlarging the hash table.
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 380
diff changeset
85
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 380
diff changeset
86 Keyword @code{:rehash-size} must be a float greater than 1.0, and specifies
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 380
diff changeset
87 the factor by which to increase the size of the hash table when enlarging.
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 380
diff changeset
88
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 380
diff changeset
89 Keyword @code{:rehash-threshold} must be a float between 0.0 and 1.0,
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 380
diff changeset
90 and specifies the load factor of the hash table which triggers enlarging.
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 380
diff changeset
91
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 380
diff changeset
92 Keyword @code{:weakness} can be @code{nil} (default), @code{t},
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 380
diff changeset
93 @code{key} or @code{value}.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
95 A weak hash table is one whose pointers do not count as GC referents:
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
96 for any key-value pair in the hash table, if the only remaining pointer
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
97 to either the key or the value is in a weak hash table, then the pair
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
98 will be removed from the hash table, and the key and value collected.
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
99 A non-weak hash table (or any other pointer) would prevent the object
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
100 from being collected.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
102 A key-weak hash table is similar to a fully-weak hash table except that
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
103 a key-value pair will be removed only if the key remains unmarked
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
104 outside of weak hash tables. The pair will remain in the hash table if
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
105 the key is pointed to by something other than a weak hash table, even
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
106 if the value is not.
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
107
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
108 A value-weak hash table is similar to a fully-weak hash table except
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
109 that a key-value pair will be removed only if the value remains
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
110 unmarked outside of weak hash tables. The pair will remain in the
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
111 hash table if the value is pointed to by something other than a weak
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
112 hash table, even if the key is not.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
115 @defun copy-hash-table hash-table
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
116 This function returns a new hash table which contains the same keys and
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
117 values as @var{hash-table}. The keys and values will not themselves be
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 copied.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
121 @defun hash-table-count hash-table
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
122 This function returns the number of entries in @var{hash-table}.
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
123 @end defun
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
124
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 380
diff changeset
125 @defun hash-table-test hash-table
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 380
diff changeset
126 This function returns the test function of @var{hash-table}.
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 380
diff changeset
127 This can be one of @code{eq}, @code{eql} or @code{equal}.
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 380
diff changeset
128 @end defun
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 380
diff changeset
129
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
130 @defun hash-table-size hash-table
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
131 This function returns the current number of slots in @var{hash-table},
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
132 whether occupied or not.
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
133 @end defun
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
134
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
135 @defun hash-table-rehash-size hash-table
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
136 This function returns the current rehash size of @var{hash-table}.
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
137 This is a float greater than 1.0; the factor by which @var{hash-table}
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
138 is enlarged when the rehash threshold is exceeded.
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
139 @end defun
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
140
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
141 @defun hash-table-rehash-threshold hash-table
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
142 This function returns the current rehash threshold of @var{hash-table}.
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
143 This is a float between 0.0 and 1.0; the maximum @dfn{load factor} of
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
144 @var{hash-table}, beyond which the @var{hash-table} is enlarged by rehashing.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 380
diff changeset
147 @defun hash-table-weakness hash-table
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 380
diff changeset
148 This function returns the weakness of @var{hash-table}.
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 380
diff changeset
149 This can be one of @code{nil}, @code{t}, @code{key} or @code{value}.
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 380
diff changeset
150 @end defun
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 380
diff changeset
151
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 @node Working With Hash Tables
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 @section Working With Hash Tables
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
155 @defun puthash key value hash-table
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
156 This function hashes @var{key} to @var{value} in @var{hash-table}.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
159 @defun gethash key hash-table &optional default
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
160 This function finds the hash value for @var{key} in @var{hash-table}.
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
161 If there is no entry for @var{key} in @var{hash-table}, @var{default} is
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
162 returned (which in turn defaults to @code{nil}).
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
165 @defun remhash key hash-table
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
166 This function removes the entry for @var{key} from @var{hash-table}.
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
167 Does nothing if there is no entry for @var{key} in @var{hash-table}.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
170 @defun clrhash hash-table
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
171 This function removes all entries from @var{hash-table}, leaving it empty.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
174 @defun maphash function hash-table
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
175 This function maps @var{function} over entries in @var{hash-table},
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
176 calling it with two args, each key and value in the hash table.
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
177
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
178 @var{function} may not modify @var{hash-table}, with the one exception
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
179 that @var{function} may remhash or puthash the entry currently being
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
180 processed by @var{function}.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 380
diff changeset
183
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184 @node Weak Hash Tables
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 @section Weak Hash Tables
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186 @cindex hash table, weak
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 @cindex weak hash table
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189 A @dfn{weak hash table} is a special variety of hash table whose
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 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
191 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
192 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
193 tables (and similar structures such as weak lists), the pair will be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194 removed from the table, and the key and value collected. A non-weak
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195 hash table (or any other pointer) would prevent the objects from being
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196 collected.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 Weak hash tables are useful for keeping track of information in a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199 non-obtrusive way, for example to implement caching. If the cache
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200 contains objects such as buffers, markers, image instances, etc. that
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201 will eventually disappear and get garbage-collected, using a weak hash
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202 table ensures that these objects are collected normally rather than
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203 remaining around forever, long past their actual period of use.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204 (Otherwise, you'd have to explicitly map over the hash table every so
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205 often and remove unnecessary elements.)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207 There are three types of weak hash tables:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209 @table @asis
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 @item fully weak hash tables
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211 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
212 is unreferenced outside of the table.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213 @item key-weak hash tables
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214 In these hash tables, a pair disappears if the key is unreferenced outside
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 of the table, regardless of how the value is referenced.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216 @item value-weak hash tables
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 In these hash tables, a pair disappears if the value is unreferenced outside
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218 of the table, regardless of how the key is referenced.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 @end table
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 Also see @ref{Weak Lists}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 380
diff changeset
223 Weak hash tables are created by specifying the @code{:weakness} keyword to
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 0
diff changeset
224 @code{make-hash-table}.