annotate lispref/hash.texi @ 25823:4cecefebde6f

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