| 1 |
'\" |
| 2 |
'\" Copyright (c) 1989-1993 The Regents of the University of California. |
| 3 |
'\" Copyright (c) 1994-1996 Sun Microsystems, Inc. |
| 4 |
'\" |
| 5 |
'\" See the file "license.terms" for information on usage and redistribution |
| 6 |
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 7 |
'\" |
| 8 |
.TH Tcl_Hash 3 "" Tcl "Tcl Library Procedures" |
| 9 |
.so man.macros |
| 10 |
.BS |
| 11 |
.SH NAME |
| 12 |
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 |
| 13 |
.SH SYNOPSIS |
| 14 |
.nf |
| 15 |
\fB#include <tcl.h>\fR |
| 16 |
.sp |
| 17 |
\fBTcl_InitHashTable\fR(\fItablePtr, keyType\fR) |
| 18 |
.sp |
| 19 |
\fBTcl_InitCustomHashTable\fR(\fItablePtr, keyType, typePtr\fR) |
| 20 |
.sp |
| 21 |
\fBTcl_InitObjHashTable\fR(\fItablePtr\fR) |
| 22 |
.sp |
| 23 |
\fBTcl_DeleteHashTable\fR(\fItablePtr\fR) |
| 24 |
.sp |
| 25 |
Tcl_HashEntry * |
| 26 |
\fBTcl_CreateHashEntry\fR(\fItablePtr, key, newPtr\fR) |
| 27 |
.sp |
| 28 |
\fBTcl_DeleteHashEntry\fR(\fIentryPtr\fR) |
| 29 |
.sp |
| 30 |
Tcl_HashEntry * |
| 31 |
\fBTcl_FindHashEntry\fR(\fItablePtr, key\fR) |
| 32 |
.sp |
| 33 |
ClientData |
| 34 |
\fBTcl_GetHashValue\fR(\fIentryPtr\fR) |
| 35 |
.sp |
| 36 |
\fBTcl_SetHashValue\fR(\fIentryPtr, value\fR) |
| 37 |
.sp |
| 38 |
void * |
| 39 |
\fBTcl_GetHashKey\fR(\fItablePtr, entryPtr\fR) |
| 40 |
.sp |
| 41 |
Tcl_HashEntry * |
| 42 |
\fBTcl_FirstHashEntry\fR(\fItablePtr, searchPtr\fR) |
| 43 |
.sp |
| 44 |
Tcl_HashEntry * |
| 45 |
\fBTcl_NextHashEntry\fR(\fIsearchPtr\fR) |
| 46 |
.sp |
| 47 |
char * |
| 48 |
\fBTcl_HashStats\fR(\fItablePtr\fR) |
| 49 |
.SH ARGUMENTS |
| 50 |
.AS "const Tcl_HashKeyType" *searchPtr out |
| 51 |
.AP Tcl_HashTable *tablePtr in |
| 52 |
Address of hash table structure (for all procedures but |
| 53 |
\fBTcl_InitHashTable\fR, this must have been initialized by |
| 54 |
previous call to \fBTcl_InitHashTable\fR). |
| 55 |
.AP int keyType in |
| 56 |
Kind of keys to use for new hash table. Must be either |
| 57 |
\fBTCL_STRING_KEYS\fR, \fBTCL_ONE_WORD_KEYS\fR, \fBTCL_CUSTOM_TYPE_KEYS\fR, |
| 58 |
\fBTCL_CUSTOM_PTR_KEYS\fR, or an integer value greater than 1. |
| 59 |
.AP Tcl_HashKeyType *typePtr in |
| 60 |
Address of structure which defines the behavior of the hash table. |
| 61 |
.AP "const void" *key in |
| 62 |
Key to use for probe into table. Exact form depends on |
| 63 |
\fIkeyType\fR used to create table. |
| 64 |
.AP int *newPtr out |
| 65 |
The word at \fI*newPtr\fR is set to 1 if a new entry was created |
| 66 |
and 0 if there was already an entry for \fIkey\fR. |
| 67 |
.AP Tcl_HashEntry *entryPtr in |
| 68 |
Pointer to hash table entry. |
| 69 |
.AP ClientData value in |
| 70 |
New value to assign to hash table entry. Need not have type |
| 71 |
ClientData, but must fit in same space as ClientData. |
| 72 |
.AP Tcl_HashSearch *searchPtr in |
| 73 |
Pointer to record to use to keep track of progress in enumerating |
| 74 |
all the entries in a hash table. |
| 75 |
.BE |
| 76 |
.SH DESCRIPTION |
| 77 |
.PP |
| 78 |
A hash table consists of zero or more entries, each consisting of a |
| 79 |
key and a value. Given the key for an entry, the hashing routines can |
| 80 |
very quickly locate the entry, and hence its value. There may be at |
| 81 |
most one entry in a hash table with a particular key, but many entries |
| 82 |
may have the same value. Keys can take one of four forms: strings, |
| 83 |
one-word values, integer arrays, or custom keys defined by a |
| 84 |
Tcl_HashKeyType structure (See section \fBTHE TCL_HASHKEYTYPE STRUCTURE\fR |
| 85 |
below). All of the keys in a given table have the same |
| 86 |
form, which is specified when the table is initialized. |
| 87 |
.PP |
| 88 |
The value of a hash table entry can be anything that fits in the same |
| 89 |
space as a |
| 90 |
.QW "char *" |
| 91 |
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 |
| 94 |
code. |
| 95 |
.PP |
| 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 |
| 99 |
entries in a table. |
| 100 |
.PP |
| 101 |
The core provides three functions for the initialization of hash |
| 102 |
tables, Tcl_InitHashTable, Tcl_InitObjHashTable and |
| 103 |
Tcl_InitCustomHashTable. |
| 104 |
.PP |
| 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. |
| 111 |
.PP |
| 112 |
\fBTcl_InitObjHashTable\fR is a wrapper around |
| 113 |
\fBTcl_InitCustomHashTable\fR and initializes a hash table whose keys |
| 114 |
are Tcl_Obj *. |
| 115 |
.PP |
| 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 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 |
| 128 |
.QW "char *" |
| 129 |
values. |
| 130 |
The pointer value is the key; it need not (and usually does not) |
| 131 |
actually point to a string. |
| 132 |
.IP \fBTCL_CUSTOM_TYPE_KEYS\fR 25 |
| 133 |
Keys are of arbitrary type, and are stored in the entry. Hashing |
| 134 |
and comparison is determined by \fItypePtr\fR. The Tcl_HashKeyType |
| 135 |
structure is described in the section |
| 136 |
\fBTHE TCL_HASHKEYTYPE STRUCTURE\fR below. |
| 137 |
.IP \fBTCL_CUSTOM_PTR_KEYS\fR 25 |
| 138 |
Keys are pointers to an arbitrary type, and are stored in the entry. Hashing |
| 139 |
and comparison is determined by \fItypePtr\fR. The Tcl_HashKeyType |
| 140 |
structure is described in the section |
| 141 |
\fBTHE TCL_HASHKEYTYPE STRUCTURE\fR below. |
| 142 |
.IP \fIother\fR 25 |
| 143 |
If \fIkeyType\fR is not one of the above, |
| 144 |
then it must be an integer value greater than 1. |
| 145 |
In this case the keys will be arrays of |
| 146 |
.QW int |
| 147 |
values, where |
| 148 |
\fIkeyType\fR gives the number of ints in each key. |
| 149 |
This allows structures to be used as keys. |
| 150 |
All keys must have the same size. |
| 151 |
Array keys are passed into hashing functions using the address |
| 152 |
of the first int in the array. |
| 153 |
.PP |
| 154 |
\fBTcl_DeleteHashTable\fR deletes all of the entries in a hash |
| 155 |
table and frees up the memory associated with the table's |
| 156 |
bucket array and entries. |
| 157 |
It does not free the actual table structure (pointed to |
| 158 |
by \fItablePtr\fR), since that memory is assumed to be managed |
| 159 |
by the client. |
| 160 |
\fBTcl_DeleteHashTable\fR also does not free or otherwise |
| 161 |
manipulate the values of the hash table entries. |
| 162 |
If the entry values point to dynamically-allocated memory, then |
| 163 |
it is the client's responsibility to free these structures |
| 164 |
before deleting the table. |
| 165 |
.PP |
| 166 |
\fBTcl_CreateHashEntry\fR locates the entry corresponding to a |
| 167 |
particular key, creating a new entry in the table if there |
| 168 |
was not already one with the given key. |
| 169 |
If an entry already existed with the given key then \fI*newPtr\fR |
| 170 |
is set to zero. |
| 171 |
If a new entry was created, then \fI*newPtr\fR is set to a non-zero |
| 172 |
value and the value of the new entry will be set to zero. |
| 173 |
The return value from \fBTcl_CreateHashEntry\fR is a pointer to |
| 174 |
the entry, which may be used to retrieve and modify the entry's |
| 175 |
value or to delete the entry from the table. |
| 176 |
.PP |
| 177 |
\fBTcl_DeleteHashEntry\fR will remove an existing entry from a |
| 178 |
table. |
| 179 |
The memory associated with the entry itself will be freed, but |
| 180 |
the client is responsible for any cleanup associated with the |
| 181 |
entry's value, such as freeing a structure that it points to. |
| 182 |
.PP |
| 183 |
\fBTcl_FindHashEntry\fR is similar to \fBTcl_CreateHashEntry\fR |
| 184 |
except that it does not create a new entry if the key doesn't exist; |
| 185 |
instead, it returns NULL as result. |
| 186 |
.PP |
| 187 |
\fBTcl_GetHashValue\fR and \fBTcl_SetHashValue\fR are used to |
| 188 |
read and write an entry's value, respectively. |
| 189 |
Values are stored and retrieved as type |
| 190 |
.QW ClientData , |
| 191 |
which is |
| 192 |
large enough to hold a pointer value. On almost all machines this is |
| 193 |
large enough to hold an integer value too. |
| 194 |
.PP |
| 195 |
\fBTcl_GetHashKey\fR returns the key for a given hash table entry, |
| 196 |
either as a pointer to a string, a one-word |
| 197 |
.PQ "char *" |
| 198 |
key, or |
| 199 |
as a pointer to the first word of an array of integers, depending |
| 200 |
on the \fIkeyType\fR used to create a hash table. |
| 201 |
In all cases \fBTcl_GetHashKey\fR returns a result with type |
| 202 |
.QW "char *" . |
| 203 |
When the key is a string or array, the result of \fBTcl_GetHashKey\fR |
| 204 |
points to information in the table entry; this information will |
| 205 |
remain valid until the entry is deleted or its table is deleted. |
| 206 |
.PP |
| 207 |
\fBTcl_FirstHashEntry\fR and \fBTcl_NextHashEntry\fR may be used |
| 208 |
to scan all of the entries in a hash table. |
| 209 |
A structure of type |
| 210 |
.QW Tcl_HashSearch , |
| 211 |
provided by the client, |
| 212 |
is used to keep track of progress through the table. |
| 213 |
\fBTcl_FirstHashEntry\fR initializes the search record and |
| 214 |
returns the first entry in the table (or NULL if the table is |
| 215 |
empty). |
| 216 |
Each subsequent call to \fBTcl_NextHashEntry\fR returns the |
| 217 |
next entry in the table or |
| 218 |
NULL if the end of the table has been reached. |
| 219 |
A call to \fBTcl_FirstHashEntry\fR followed by calls to |
| 220 |
\fBTcl_NextHashEntry\fR will return each of the entries in |
| 221 |
the table exactly once, in an arbitrary order. |
| 222 |
It is inadvisable to modify the structure of the table, e.g. |
| 223 |
by creating or deleting entries, while the search is in progress, |
| 224 |
with the exception of deleting the entry returned by |
| 225 |
\fBTcl_FirstHashEntry\fR or \fBTcl_NextHashEntry\fR. |
| 226 |
.PP |
| 227 |
\fBTcl_HashStats\fR returns a dynamically-allocated string with |
| 228 |
overall information about a hash table, such as the number of |
| 229 |
entries it contains, the number of buckets in its hash array, |
| 230 |
and the utilization of the buckets. |
| 231 |
It is the caller's responsibility to free the result string |
| 232 |
by passing it to \fBckfree\fR. |
| 233 |
.PP |
| 234 |
The header file \fBtcl.h\fR defines the actual data structures |
| 235 |
used to implement hash tables. |
| 236 |
This is necessary so that clients can allocate Tcl_HashTable |
| 237 |
structures and so that macros can be used to read and write |
| 238 |
the values of entries. |
| 239 |
However, users of the hashing routines should never refer directly |
| 240 |
to any of the fields of any of the hash-related data structures; |
| 241 |
use the procedures and macros defined here. |
| 242 |
.SH "THE TCL_HASHKEYTYPE STRUCTURE" |
| 243 |
.PP |
| 244 |
Extension writers can define new hash key types by defining four procedures, |
| 245 |
initializing a \fBTcl_HashKeyType\fR structure to describe the type, and |
| 246 |
calling \fBTcl_InitCustomHashTable\fR. The \fBTcl_HashKeyType\fR structure is |
| 247 |
defined as follows: |
| 248 |
.PP |
| 249 |
.CS |
| 250 |
typedef struct Tcl_HashKeyType { |
| 251 |
int \fIversion\fR; |
| 252 |
int \fIflags\fR; |
| 253 |
Tcl_HashKeyProc *\fIhashKeyProc\fR; |
| 254 |
Tcl_CompareHashKeysProc *\fIcompareKeysProc\fR; |
| 255 |
Tcl_AllocHashEntryProc *\fIallocEntryProc\fR; |
| 256 |
Tcl_FreeHashEntryProc *\fIfreeEntryProc\fR; |
| 257 |
} \fBTcl_HashKeyType\fR; |
| 258 |
.CE |
| 259 |
.PP |
| 260 |
The \fIversion\fR member is the version of the table. If this structure is |
| 261 |
extended in future then the version can be used to distinguish between |
| 262 |
different structures. It should be set to \fBTCL_HASH_KEY_TYPE_VERSION\fR. |
| 263 |
.PP |
| 264 |
The \fIflags\fR member is 0 or one or more of the following values OR'ed |
| 265 |
together: |
| 266 |
.IP \fBTCL_HASH_KEY_RANDOMIZE_HASH\fR 25 |
| 267 |
There are some things, pointers for example which do not hash well because |
| 268 |
they do not use the lower bits. If this flag is set then the hash table will |
| 269 |
attempt to rectify this by randomizing the bits and then using the upper N |
| 270 |
bits as the index into the table. |
| 271 |
.IP \fBTCL_HASH_KEY_SYSTEM_HASH\fR 25 |
| 272 |
This flag forces Tcl to use the memory allocation procedures provided by the |
| 273 |
operating system when allocating and freeing memory used to store the hash |
| 274 |
table data structures, and not any of Tcl's own customized memory allocation |
| 275 |
routines. This is important if the hash table is to be used in the |
| 276 |
implementation of a custom set of allocation routines, or something that a |
| 277 |
custom set of allocation routines might depend on, in order to avoid any |
| 278 |
circular dependency. |
| 279 |
.PP |
| 280 |
The \fIhashKeyProc\fR member contains the address of a function called to |
| 281 |
calculate a hash value for the key. |
| 282 |
.PP |
| 283 |
.CS |
| 284 |
typedef unsigned int \fBTcl_HashKeyProc\fR( |
| 285 |
Tcl_HashTable *\fItablePtr\fR, |
| 286 |
void *\fIkeyPtr\fR); |
| 287 |
.CE |
| 288 |
.PP |
| 289 |
If this is NULL then \fIkeyPtr\fR is used and |
| 290 |
\fBTCL_HASH_KEY_RANDOMIZE_HASH\fR is assumed. |
| 291 |
.PP |
| 292 |
The \fIcompareKeysProc\fR member contains the address of a function called to |
| 293 |
compare two keys. |
| 294 |
.PP |
| 295 |
.CS |
| 296 |
typedef int \fBTcl_CompareHashKeysProc\fR( |
| 297 |
void *\fIkeyPtr\fR, |
| 298 |
Tcl_HashEntry *\fIhPtr\fR); |
| 299 |
.CE |
| 300 |
.PP |
| 301 |
If this is NULL then the \fIkeyPtr\fR pointers are compared. If the keys do |
| 302 |
not match then the function returns 0, otherwise it returns 1. |
| 303 |
.PP |
| 304 |
The \fIallocEntryProc\fR member contains the address of a function called to |
| 305 |
allocate space for an entry and initialize the key and clientData. |
| 306 |
.PP |
| 307 |
.CS |
| 308 |
typedef Tcl_HashEntry *\fBTcl_AllocHashEntryProc\fR( |
| 309 |
Tcl_HashTable *\fItablePtr\fR, |
| 310 |
void *\fIkeyPtr\fR); |
| 311 |
.CE |
| 312 |
.PP |
| 313 |
If this is NULL then \fBTcl_Alloc\fR is used to allocate enough space for a |
| 314 |
Tcl_HashEntry, the key pointer is assigned to key.oneWordValue and the |
| 315 |
clientData is set to NULL. String keys and array keys use this function to |
| 316 |
allocate enough space for the entry and the key in one block, rather than |
| 317 |
doing it in two blocks. This saves space for a pointer to the key from the |
| 318 |
entry and another memory allocation. Tcl_Obj* keys use this function to |
| 319 |
allocate enough space for an entry and increment the reference count on the |
| 320 |
value. |
| 321 |
.PP |
| 322 |
The \fIfreeEntryProc\fR member contains the address of a function called to |
| 323 |
free space for an entry. |
| 324 |
.PP |
| 325 |
.CS |
| 326 |
typedef void \fBTcl_FreeHashEntryProc\fR( |
| 327 |
Tcl_HashEntry *\fIhPtr\fR); |
| 328 |
.CE |
| 329 |
.PP |
| 330 |
If this is NULL then \fBTcl_Free\fR is used to free the space for the entry. |
| 331 |
Tcl_Obj* keys use this function to decrement the reference count on the |
| 332 |
value. |
| 333 |
.SH KEYWORDS |
| 334 |
hash table, key, lookup, search, value |