1Tcl_Hash(3) Tcl Library Procedures Tcl_Hash(3)
2
3
4
5______________________________________________________________________________
6
8 Tcl_InitHashTable, Tcl_InitCustomHashTable, Tcl_InitObjHashTable,
9 Tcl_DeleteHashTable, Tcl_CreateHashEntry, Tcl_DeleteHashEntry,
10 Tcl_FindHashEntry, Tcl_GetHashValue, Tcl_SetHashValue, Tcl_GetHashKey,
11 Tcl_FirstHashEntry, Tcl_NextHashEntry, Tcl_HashStats - procedures to
12 manage hash tables
13
15 #include <tcl.h>
16
17 Tcl_InitHashTable(tablePtr, keyType)
18
19 Tcl_InitCustomHashTable(tablePtr, keyType, typePtr)
20
21 Tcl_InitObjHashTable(tablePtr)
22
23 Tcl_DeleteHashTable(tablePtr)
24
25 Tcl_HashEntry *
26 Tcl_CreateHashEntry(tablePtr, key, newPtr)
27
28 Tcl_DeleteHashEntry(entryPtr)
29
30 Tcl_HashEntry *
31 Tcl_FindHashEntry(tablePtr, key)
32
33 ClientData
34 Tcl_GetHashValue(entryPtr)
35
36 Tcl_SetHashValue(entryPtr, value)
37
38 char *
39 Tcl_GetHashKey(tablePtr, entryPtr)
40
41 Tcl_HashEntry *
42 Tcl_FirstHashEntry(tablePtr, searchPtr)
43
44 Tcl_HashEntry *
45 Tcl_NextHashEntry(searchPtr)
46
47 CONST char *
48 Tcl_HashStats(tablePtr)
49
51 Tcl_HashTable *tablePtr (in) Address of hash table structure
52 (for all procedures but
53 Tcl_InitHashTable, this must
54 have been initialized by previ‐
55 ous call to Tcl_InitHashTable).
56
57 int keyType (in) Kind of keys to use for new
58 hash table. Must be either
59 TCL_STRING_KEYS,
60 TCL_ONE_WORD_KEYS, TCL_CUS‐
61 TOM_TYPE_KEYS, TCL_CUS‐
62 TOM_PTR_KEYS, or an integer
63 value greater than 1.
64
65 Tcl_HashKeyType *typePtr (in) Address of structure which
66 defines the behaviour of the
67 hash table.
68
69 CONST char *key (in) Key to use for probe into ta‐
70 ble. Exact form depends on
71 keyType used to create table.
72
73 int *newPtr (out) The word at *newPtr is set to 1
74 if a new entry was created and
75 0 if there was already an entry
76 for key.
77
78 Tcl_HashEntry *entryPtr (in) Pointer to hash table entry.
79
80 ClientData value (in) New value to assign to hash ta‐
81 ble entry. Need not have type
82 ClientData, but must fit in
83 same space as ClientData.
84
85 Tcl_HashSearch *searchPtr (in) Pointer to record to use to
86 keep track of progress in enu‐
87 merating all the entries in a
88 hash table.
89_________________________________________________________________
90
92 A hash table consists of zero or more entries, each consisting of a key
93 and a value. Given the key for an entry, the hashing routines can very
94 quickly locate the entry, and hence its value. There may be at most one
95 entry in a hash table with a particular key, but many entries may have
96 the same value. Keys can take one of four forms: strings, one-word
97 values, integer arrays, or custom keys defined by a Tcl_HashKeyType
98 structure (See section THE TCL_HASHKEYTYPE STRUCTURE below). All of the
99 keys in a given table have the same form, which is specified when the
100 table is initialized.
101
102 The value of a hash table entry can be anything that fits in the same
103 space as a ``char *'' pointer. Values for hash table entries are man‐
104 aged entirely by clients, not by the hash module itself. Typically
105 each entry's value is a pointer to a data structure managed by client
106 code.
107
108 Hash tables grow gracefully as the number of entries increases, so that
109 there are always less than three entries per hash bucket, on average.
110 This allows for fast lookups regardless of the number of entries in a
111 table.
112
113 The core provides three functions for the initialization of hash
114 tables, Tcl_InitHashTable, Tcl_InitObjHashTable and Tcl_InitCus‐
115 tomHashTable.
116
117 Tcl_InitHashTable initializes a structure that describes a new hash ta‐
118 ble. The space for the structure is provided by the caller, not by the
119 hash module. The value of keyType indicates what kinds of keys will be
120 used for all entries in the table. All of the key types described later
121 are allowed, with the exception of TCL_CUSTOM_TYPE_KEYS and TCL_CUS‐
122 TOM_PTR_KEYS.
123
124 Tcl_InitObjHashTable is a wrapper around Tcl_InitCustomHashTable and
125 initializes a hash table whose keys are Tcl_Obj *.
126
127 Tcl_InitCustomHashTable initializes a structure that describes a new
128 hash table. The space for the structure is provided by the caller, not
129 by the hash module. The value of keyType indicates what kinds of keys
130 will be used for all entries in the table. KeyType must have one of
131 the following values:
132
133 TCL_STRING_KEYS Keys are null-terminated strings. They are
134 passed to hashing routines using the address
135 of the first character of the string.
136
137 TCL_ONE_WORD_KEYS Keys are single-word values; they are passed
138 to hashing routines and stored in hash table
139 entries as ``char *'' values. The pointer
140 value is the key; it need not (and usually
141 doesn't) actually point to a string.
142
143 TCL_CUSTOM_TYPE_KEYS Keys are of arbitrary type, and are stored in
144 the entry. Hashing and comparison is deter‐
145 mined by typePtr. The Tcl_HashKeyType struc‐
146 ture is described in the section THE
147 TCL_HASHKEYTYPE STRUCTURE below.
148
149 TCL_CUSTOM_PTR_KEYS Keys are pointers to an arbitrary type, and
150 are stored in the entry. Hashing and compari‐
151 son is determined by typePtr. The Tcl_HashKey‐
152 Type structure is described in the section THE
153 TCL_HASHKEYTYPE STRUCTURE below.
154
155 other If keyType is not one of the above, then it
156 must be an integer value greater than 1. In
157 this case the keys will be arrays of ``int''
158 values, where keyType gives the number of ints
159 in each key. This allows structures to be
160 used as keys. All keys must have the same
161 size. Array keys are passed into hashing
162 functions using the address of the first int
163 in the array.
164
165 Tcl_DeleteHashTable deletes all of the entries in a hash table and
166 frees up the memory associated with the table's bucket array and
167 entries. It does not free the actual table structure (pointed to by
168 tablePtr), since that memory is assumed to be managed by the client.
169 Tcl_DeleteHashTable also does not free or otherwise manipulate the val‐
170 ues of the hash table entries. If the entry values point to dynami‐
171 cally-allocated memory, then it is the client's responsibility to free
172 these structures before deleting the table.
173
174 Tcl_CreateHashEntry locates the entry corresponding to a particular
175 key, creating a new entry in the table if there wasn't already one with
176 the given key. If an entry already existed with the given key then
177 *newPtr is set to zero. If a new entry was created, then *newPtr is
178 set to a non-zero value and the value of the new entry will be set to
179 zero. The return value from Tcl_CreateHashEntry is a pointer to the
180 entry, which may be used to retrieve and modify the entry's value or to
181 delete the entry from the table.
182
183 Tcl_DeleteHashEntry will remove an existing entry from a table. The
184 memory associated with the entry itself will be freed, but the client
185 is responsible for any cleanup associated with the entry's value, such
186 as freeing a structure that it points to.
187
188 Tcl_FindHashEntry is similar to Tcl_CreateHashEntry except that it
189 doesn't create a new entry if the key doesn't exist; instead, it
190 returns NULL as result.
191
192 Tcl_GetHashValue and Tcl_SetHashValue are used to read and write an
193 entry's value, respectively. Values are stored and retrieved as type
194 ``ClientData'', which is large enough to hold a pointer value. On
195 almost all machines this is large enough to hold an integer value too.
196
197 Tcl_GetHashKey returns the key for a given hash table entry, either as
198 a pointer to a string, a one-word (``char *'') key, or as a pointer to
199 the first word of an array of integers, depending on the keyType used
200 to create a hash table. In all cases Tcl_GetHashKey returns a result
201 with type ``char *''. When the key is a string or array, the result of
202 Tcl_GetHashKey points to information in the table entry; this informa‐
203 tion will remain valid until the entry is deleted or its table is
204 deleted.
205
206 Tcl_FirstHashEntry and Tcl_NextHashEntry may be used to scan all of the
207 entries in a hash table. A structure of type ``Tcl_HashSearch'', pro‐
208 vided by the client, is used to keep track of progress through the ta‐
209 ble. Tcl_FirstHashEntry initializes the search record and returns the
210 first entry in the table (or NULL if the table is empty). Each subse‐
211 quent call to Tcl_NextHashEntry returns the next entry in the table or
212 NULL if the end of the table has been reached. A call to
213 Tcl_FirstHashEntry followed by calls to Tcl_NextHashEntry will return
214 each of the entries in the table exactly once, in an arbitrary order.
215 It is unadvisable to modify the structure of the table, e.g. by creat‐
216 ing or deleting entries, while the search is in progress.
217
218 Tcl_HashStats returns a dynamically-allocated string with overall
219 information about a hash table, such as the number of entries it con‐
220 tains, the number of buckets in its hash array, and the utilization of
221 the buckets. It is the caller's responsibility to free the result
222 string by passing it to ckfree.
223
224 The header file tcl.h defines the actual data structures used to imple‐
225 ment hash tables. This is necessary so that clients can allocate
226 Tcl_HashTable structures and so that macros can be used to read and
227 write the values of entries. However, users of the hashing routines
228 should never refer directly to any of the fields of any of the hash-
229 related data structures; use the procedures and macros defined here.
230
232 Extension writers can define new hash key types by defining four proce‐
233 dures, initializing a Tcl_HashKeyType structure to describe the type,
234 and calling Tcl_InitCustomHashTable. The Tcl_HashKeyType structure is
235 defined as follows:
236 typedef struct Tcl_HashKeyType {
237 int version;
238 int flags;
239 Tcl_HashKeyProc *hashKeyProc;
240 Tcl_CompareHashKeysProc *compareKeysProc;
241 Tcl_AllocHashEntryProc *allocEntryProc;
242 Tcl_FreeHashEntryProc *freeEntryProc;
243 } Tcl_HashKeyType;
244
245 The version member is the version of the table. If this structure is
246 extended in future then the version can be used to distinguish between
247 different structures. It should be set to TCL_HASH_KEY_TYPE_VERSION.
248
249 The flags member is one or more of the following values OR'ed together:
250
251 TCL_HASH_KEY_RANDOMIZE_HASH
252 There are some things, pointers for example
253 which don't hash well because they do not use
254 the lower bits. If this flag is set then the
255 hash table will attempt to rectify this by
256 randomising the bits and then using the upper
257 N bits as the index into the table.
258
259 The hashKeyProc member contains the address of a function called to
260 calculate a hash value for the key.
261 typedef unsigned int (Tcl_HashKeyProc) (
262 Tcl_HashTable *tablePtr,
263 VOID *keyPtr);
264 If this is NULL then keyPtr is used and TCL_HASH_KEY_RANDOMIZE_HASH is
265 assumed.
266
267 The compareKeysProc member contains the address of a function called to
268 compare two keys.
269 typedef int (Tcl_CompareHashKeysProc) (VOID *keyPtr,
270 Tcl_HashEntry *hPtr);
271 If this is NULL then the keyPtr pointers are compared. If the keys
272 don't match then the function returns 0, otherwise it returns 1.
273
274 The allocEntryProc member contains the address of a function called to
275 allocate space for an entry and initialise the key.
276 typedef Tcl_HashEntry *(Tcl_AllocHashEntryProc) (
277 Tcl_HashTable *tablePtr, VOID *keyPtr);
278 If this is NULL then Tcl_Alloc is used to allocate enough space for a
279 Tcl_HashEntry and the key pointer is assigned to key.oneWordValue.
280 String keys and array keys use this function to allocate enough space
281 for the entry and the key in one block, rather than doing it in two
282 blocks. This saves space for a pointer to the key from the entry and
283 another memory allocation. Tcl_Obj * keys use this function to allocate
284 enough space for an entry and increment the reference count on the
285 object. If
286
287 The freeEntryProc member contains the address of a function called to
288 free space for an entry.
289 typedef void (Tcl_FreeHashEntryProc) (Tcl_HashEntry *hPtr);
290 If this is NULL then Tcl_Free is used to free the space for the entry.
291 Tcl_Obj * keys use this function to decrement the reference count on
292 the object.
293
295 hash table, key, lookup, search, value
296
297
298
299Tcl Tcl_Hash(3)