2 '\" Copyright (c) 1989-1993 The Regents of the University of California.
3 '\" Copyright (c) 1994-1996 Sun Microsystems, Inc.
5 '\" See the file "license.terms" for information on usage and redistribution
6 '\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 .TH Tcl_Hash 3 "" Tcl "Tcl Library Procedures"
14 Tcl_InitHashTable, Tcl_InitCustomHashTable, Tcl_InitObjHashTable, Tcl_DeleteHashTable, Tcl_CreateHashEntry, Tcl_DeleteHashEntry, Tcl_FindHashEntry, Tcl_GetHashValue, Tcl_SetHashValue, Tcl_GetHashKey, Tcl_FirstHashEntry, Tcl_NextHashEntry, Tcl_HashStats \- procedures to manage hash tables
17 \fB#include <tcl.h>\fR
19 \fBTcl_InitHashTable\fR(\fItablePtr, keyType\fR)
21 \fBTcl_InitCustomHashTable\fR(\fItablePtr, keyType, typePtr\fR)
23 \fBTcl_InitObjHashTable\fR(\fItablePtr\fR)
25 \fBTcl_DeleteHashTable\fR(\fItablePtr\fR)
28 \fBTcl_CreateHashEntry\fR(\fItablePtr, key, newPtr\fR)
30 \fBTcl_DeleteHashEntry\fR(\fIentryPtr\fR)
33 \fBTcl_FindHashEntry\fR(\fItablePtr, key\fR)
36 \fBTcl_GetHashValue\fR(\fIentryPtr\fR)
38 \fBTcl_SetHashValue\fR(\fIentryPtr, value\fR)
41 \fBTcl_GetHashKey\fR(\fItablePtr, entryPtr\fR)
44 \fBTcl_FirstHashEntry\fR(\fItablePtr, searchPtr\fR)
47 \fBTcl_NextHashEntry\fR(\fIsearchPtr\fR)
50 \fBTcl_HashStats\fR(\fItablePtr\fR)
52 .AS Tcl_HashSearch *searchPtr
53 .AP Tcl_HashTable *tablePtr in
54 Address of hash table structure (for all procedures but
55 \fBTcl_InitHashTable\fR, this must have been initialized by
56 previous call to \fBTcl_InitHashTable\fR).
58 Kind of keys to use for new hash table. Must be either
59 TCL_STRING_KEYS, TCL_ONE_WORD_KEYS, TCL_CUSTOM_TYPE_KEYS,
60 TCL_CUSTOM_PTR_KEYS, or an integer value greater than 1.
61 .AP Tcl_HashKeyType *typePtr in
62 Address of structure which defines the behaviour of the hash table.
63 .AP "CONST char" *key in
64 Key to use for probe into table. Exact form depends on
65 \fIkeyType\fR used to create table.
67 The word at \fI*newPtr\fR is set to 1 if a new entry was created
68 and 0 if there was already an entry for \fIkey\fR.
69 .AP Tcl_HashEntry *entryPtr in
70 Pointer to hash table entry.
71 .AP ClientData value in
72 New value to assign to hash table entry. Need not have type
73 ClientData, but must fit in same space as ClientData.
74 .AP Tcl_HashSearch *searchPtr in
75 Pointer to record to use to keep track of progress in enumerating
76 all the entries in a hash table.
80 A hash table consists of zero or more entries, each consisting of a
81 key and a value. Given the key for an entry, the hashing routines can
82 very quickly locate the entry, and hence its value. There may be at
83 most one entry in a hash table with a particular key, but many entries
84 may have the same value. Keys can take one of four forms: strings,
85 one-word values, integer arrays, or custom keys defined by a
86 Tcl_HashKeyType structure (See section \fBTHE TCL_HASHKEYTYPE
87 STRUCTURE\fR below). All of the keys in a given table have the same
88 form, which is specified when the table is initialized.
90 The value of a hash table entry can be anything that fits in the same
91 space as a ``char *'' pointer. Values for hash table entries are
92 managed entirely by clients, not by the hash module itself. Typically
93 each entry's value is a pointer to a data structure managed by client
96 Hash tables grow gracefully as the number of entries increases, so
97 that there are always less than three entries per hash bucket, on
98 average. This allows for fast lookups regardless of the number of
101 The core provides three functions for the initialization of hash
102 tables, Tcl_InitHashTable, Tcl_InitObjHashTable and
103 Tcl_InitCustomHashTable.
105 \fBTcl_InitHashTable\fR initializes a structure that describes a new
106 hash table. The space for the structure is provided by the caller,
107 not by the hash module. The value of \fIkeyType\fR indicates what
108 kinds of keys will be used for all entries in the table. All of the
109 key types described later are allowed, with the exception of
110 \fBTCL_CUSTOM_TYPE_KEYS\fR and \fBTCL_CUSTOM_PTR_KEYS\fR.
112 \fBTcl_InitObjHashTable\fR is a wrapper around
113 \fBTcl_InitCustomHashTable\fR and initializes a hash table whose keys
116 \fBTcl_InitCustomHashTable\fR initializes a structure that describes a
117 new hash table. The space for the structure is provided by the
118 caller, not by the hash module. The value of \fIkeyType\fR indicates
119 what kinds of keys will be used for all entries in the table.
120 \fIKeyType\fR must have one of the following values:
121 .IP \fBTCL_STRING_KEYS\fR 25
122 Keys are null-terminated ASCII strings.
123 They are passed to hashing routines using the address of the
124 first character of the string.
125 .IP \fBTCL_ONE_WORD_KEYS\fR 25
126 Keys are single-word values; they are passed to hashing routines
127 and stored in hash table entries as ``char *'' values.
128 The pointer value is the key; it need not (and usually doesn't)
129 actually point to a string.
130 .IP \fBTCL_CUSTOM_TYPE_KEYS\fR 25
131 Keys are of arbitrary type, and are stored in the entry. Hashing
132 and comparison is determined by \fItypePtr\fR. The Tcl_HashKeyType
133 structure is described in the section
134 \fBTHE TCL_HASHKEYTYPE STRUCTURE\fR below.
135 .IP \fBTCL_CUSTOM_PTR_KEYS\fR 25
136 Keys are pointers to an arbitrary type, and are stored in the entry. Hashing
137 and comparison is determined by \fItypePtr\fR. The Tcl_HashKeyType
138 structure is described in the section
139 \fBTHE TCL_HASHKEYTYPE STRUCTURE\fR below.
141 If \fIkeyType\fR is not one of the above,
142 then it must be an integer value greater than 1.
143 In this case the keys will be arrays of ``int'' values, where
144 \fIkeyType\fR gives the number of ints in each key.
145 This allows structures to be used as keys.
146 All keys must have the same size.
147 Array keys are passed into hashing functions using the address
148 of the first int in the array.
150 \fBTcl_DeleteHashTable\fR deletes all of the entries in a hash
151 table and frees up the memory associated with the table's
152 bucket array and entries.
153 It does not free the actual table structure (pointed to
154 by \fItablePtr\fR), since that memory is assumed to be managed
156 \fBTcl_DeleteHashTable\fR also does not free or otherwise
157 manipulate the values of the hash table entries.
158 If the entry values point to dynamically-allocated memory, then
159 it is the client's responsibility to free these structures
160 before deleting the table.
162 \fBTcl_CreateHashEntry\fR locates the entry corresponding to a
163 particular key, creating a new entry in the table if there
164 wasn't already one with the given key.
165 If an entry already existed with the given key then \fI*newPtr\fR
167 If a new entry was created, then \fI*newPtr\fR is set to a non-zero
168 value and the value of the new entry will be set to zero.
169 The return value from \fBTcl_CreateHashEntry\fR is a pointer to
170 the entry, which may be used to retrieve and modify the entry's
171 value or to delete the entry from the table.
173 \fBTcl_DeleteHashEntry\fR will remove an existing entry from a
175 The memory associated with the entry itself will be freed, but
176 the client is responsible for any cleanup associated with the
177 entry's value, such as freeing a structure that it points to.
179 \fBTcl_FindHashEntry\fR is similar to \fBTcl_CreateHashEntry\fR
180 except that it doesn't create a new entry if the key doesn't exist;
181 instead, it returns NULL as result.
183 \fBTcl_GetHashValue\fR and \fBTcl_SetHashValue\fR are used to
184 read and write an entry's value, respectively.
185 Values are stored and retrieved as type ``ClientData'', which is
186 large enough to hold a pointer value. On almost all machines this is
187 large enough to hold an integer value too.
189 \fBTcl_GetHashKey\fR returns the key for a given hash table entry,
190 either as a pointer to a string, a one-word (``char *'') key, or
191 as a pointer to the first word of an array of integers, depending
192 on the \fIkeyType\fR used to create a hash table.
193 In all cases \fBTcl_GetHashKey\fR returns a result with type
195 When the key is a string or array, the result of \fBTcl_GetHashKey\fR
196 points to information in the table entry; this information will
197 remain valid until the entry is deleted or its table is deleted.
199 \fBTcl_FirstHashEntry\fR and \fBTcl_NextHashEntry\fR may be used
200 to scan all of the entries in a hash table.
201 A structure of type ``Tcl_HashSearch'', provided by the client,
202 is used to keep track of progress through the table.
203 \fBTcl_FirstHashEntry\fR initializes the search record and
204 returns the first entry in the table (or NULL if the table is
206 Each subsequent call to \fBTcl_NextHashEntry\fR returns the
207 next entry in the table or
208 NULL if the end of the table has been reached.
209 A call to \fBTcl_FirstHashEntry\fR followed by calls to
210 \fBTcl_NextHashEntry\fR will return each of the entries in
211 the table exactly once, in an arbitrary order.
212 It is unadvisable to modify the structure of the table, e.g.
213 by creating or deleting entries, while the search is in
216 \fBTcl_HashStats\fR returns a dynamically-allocated string with
217 overall information about a hash table, such as the number of
218 entries it contains, the number of buckets in its hash array,
219 and the utilization of the buckets.
220 It is the caller's responsibility to free the result string
221 by passing it to \fBckfree\fR.
223 The header file \fBtcl.h\fR defines the actual data structures
224 used to implement hash tables.
225 This is necessary so that clients can allocate Tcl_HashTable
226 structures and so that macros can be used to read and write
227 the values of entries.
228 However, users of the hashing routines should never refer directly
229 to any of the fields of any of the hash-related data structures;
230 use the procedures and macros defined here.
231 .SH "THE TCL_HASHKEYTYPE STRUCTURE"
233 Extension writers can define new hash key types by defining four
234 procedures, initializing a Tcl_HashKeyType structure to describe
235 the type, and calling \fBTcl_InitCustomHashTable\fR.
236 The \fBTcl_HashKeyType\fR structure is defined as follows:
238 typedef struct Tcl_HashKeyType {
241 Tcl_HashKeyProc *\fIhashKeyProc\fR;
242 Tcl_CompareHashKeysProc *\fIcompareKeysProc\fR;
243 Tcl_AllocHashEntryProc *\fIallocEntryProc\fR;
244 Tcl_FreeHashEntryProc *\fIfreeEntryProc\fR;
248 The \fIversion\fR member is the version of the table. If this
249 structure is extended in future then the version can be used
250 to distinguish between different structures. It should be set
251 to \fBTCL_HASH_KEY_TYPE_VERSION\fR.
253 The \fIflags\fR member is one or more of the following values OR'ed together:
254 .IP \fBTCL_HASH_KEY_RANDOMIZE_HASH\fR 25
255 There are some things, pointers for example which don't hash well
256 because they do not use the lower bits. If this flag is set then the
257 hash table will attempt to rectify this by randomising the bits and
258 then using the upper N bits as the index into the table.
260 The \fIhashKeyProc\fR member contains the address of a function
261 called to calculate a hash value for the key.
263 typedef unsigned int (Tcl_HashKeyProc) (
264 Tcl_HashTable *\fItablePtr\fR,
267 If this is NULL then \fIkeyPtr\fR is used and
268 \fBTCL_HASH_KEY_RANDOMIZE_HASH\fR is assumed.
270 The \fIcompareKeysProc\fR member contains the address of a function
271 called to compare two keys.
273 typedef int (Tcl_CompareHashKeysProc) (VOID *\fIkeyPtr\fR,
274 Tcl_HashEntry *\fIhPtr\fR);
276 If this is NULL then the \fIkeyPtr\fR pointers are compared.
277 If the keys don't match then the function returns 0, otherwise
280 The \fIallocEntryProc\fR member contains the address of a function
281 called to allocate space for an entry and initialise the key.
283 typedef Tcl_HashEntry *(Tcl_AllocHashEntryProc) (
284 Tcl_HashTable *\fItablePtr\fR, VOID *\fIkeyPtr\fR);
286 If this is NULL then Tcl_Alloc is used to allocate enough space for a
287 Tcl_HashEntry and the key pointer is assigned to key.oneWordValue.
288 String keys and array keys use this function to allocate enough
289 space for the entry and the key in one block, rather than doing
290 it in two blocks. This saves space for a pointer to the key from
291 the entry and another memory allocation. Tcl_Obj * keys use this
292 function to allocate enough space for an entry and increment the
293 reference count on the object.
296 The \fIfreeEntryProc\fR member contains the address of a function
297 called to free space for an entry.
299 typedef void (Tcl_FreeHashEntryProc) (Tcl_HashEntry *\fIhPtr\fR);
301 If this is NULL then Tcl_Free is used to free the space for the
302 entry. Tcl_Obj * keys use this function to decrement the
303 reference count on the object.
305 hash table, key, lookup, search, value