1 |
578 |
markom |
'\"
|
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 |
|
|
'\" RCS: @(#) $Id: Hash.3,v 1.1.1.1 2002-01-16 10:25:24 markom Exp $
|
9 |
|
|
'\"
|
10 |
|
|
.so man.macros
|
11 |
|
|
.TH Tcl_Hash 3 "" Tcl "Tcl Library Procedures"
|
12 |
|
|
.BS
|
13 |
|
|
.SH NAME
|
14 |
|
|
Tcl_InitHashTable, 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
|
15 |
|
|
.SH SYNOPSIS
|
16 |
|
|
.nf
|
17 |
|
|
\fB#include \fR
|
18 |
|
|
.sp
|
19 |
|
|
\fBTcl_InitHashTable\fR(\fItablePtr, keyType\fR)
|
20 |
|
|
.sp
|
21 |
|
|
\fBTcl_DeleteHashTable\fR(\fItablePtr\fR)
|
22 |
|
|
.sp
|
23 |
|
|
Tcl_HashEntry *
|
24 |
|
|
\fBTcl_CreateHashEntry\fR(\fItablePtr, key, newPtr\fR)
|
25 |
|
|
.sp
|
26 |
|
|
\fBTcl_DeleteHashEntry\fR(\fIentryPtr\fR)
|
27 |
|
|
.sp
|
28 |
|
|
Tcl_HashEntry *
|
29 |
|
|
\fBTcl_FindHashEntry\fR(\fItablePtr, key\fR)
|
30 |
|
|
.sp
|
31 |
|
|
ClientData
|
32 |
|
|
\fBTcl_GetHashValue\fR(\fIentryPtr\fR)
|
33 |
|
|
.sp
|
34 |
|
|
\fBTcl_SetHashValue\fR(\fIentryPtr, value\fR)
|
35 |
|
|
.sp
|
36 |
|
|
char *
|
37 |
|
|
\fBTcl_GetHashKey\fR(\fItablePtr, entryPtr\fR)
|
38 |
|
|
.sp
|
39 |
|
|
Tcl_HashEntry *
|
40 |
|
|
\fBTcl_FirstHashEntry\fR(\fItablePtr, searchPtr\fR)
|
41 |
|
|
.sp
|
42 |
|
|
Tcl_HashEntry *
|
43 |
|
|
\fBTcl_NextHashEntry\fR(\fIsearchPtr\fR)
|
44 |
|
|
.sp
|
45 |
|
|
char *
|
46 |
|
|
\fBTcl_HashStats\fR(\fItablePtr\fR)
|
47 |
|
|
.SH ARGUMENTS
|
48 |
|
|
.AS Tcl_HashSearch *searchPtr
|
49 |
|
|
.AP Tcl_HashTable *tablePtr in
|
50 |
|
|
Address of hash table structure (for all procedures but
|
51 |
|
|
\fBTcl_InitHashTable\fR, this must have been initialized by
|
52 |
|
|
previous call to \fBTcl_InitHashTable\fR).
|
53 |
|
|
.AP int keyType in
|
54 |
|
|
Kind of keys to use for new hash table. Must be either
|
55 |
|
|
TCL_STRING_KEYS, TCL_ONE_WORD_KEYS, or an integer value
|
56 |
|
|
greater than 1.
|
57 |
|
|
.AP char *key in
|
58 |
|
|
Key to use for probe into table. Exact form depends on
|
59 |
|
|
\fIkeyType\fR used to create table.
|
60 |
|
|
.AP int *newPtr out
|
61 |
|
|
The word at \fI*newPtr\fR is set to 1 if a new entry was created
|
62 |
|
|
and 0 if there was already an entry for \fIkey\fR.
|
63 |
|
|
.AP Tcl_HashEntry *entryPtr in
|
64 |
|
|
Pointer to hash table entry.
|
65 |
|
|
.AP ClientData value in
|
66 |
|
|
New value to assign to hash table entry. Need not have type
|
67 |
|
|
ClientData, but must fit in same space as ClientData.
|
68 |
|
|
.AP Tcl_HashSearch *searchPtr in
|
69 |
|
|
Pointer to record to use to keep track of progress in enumerating
|
70 |
|
|
all the entries in a hash table.
|
71 |
|
|
.BE
|
72 |
|
|
|
73 |
|
|
.SH DESCRIPTION
|
74 |
|
|
.PP
|
75 |
|
|
A hash table consists of zero or more entries, each consisting of
|
76 |
|
|
a key and a value.
|
77 |
|
|
Given the key for an entry, the hashing routines can very quickly
|
78 |
|
|
locate the entry, and hence its value.
|
79 |
|
|
There may be at most one entry in a hash table with a
|
80 |
|
|
particular key, but many entries may have the same value.
|
81 |
|
|
Keys can take one of three forms: strings,
|
82 |
|
|
one-word values, or integer arrays.
|
83 |
|
|
All of the keys in a given table have the same form, which is
|
84 |
|
|
specified when the table is initialized.
|
85 |
|
|
.PP
|
86 |
|
|
The value of a hash table entry can be anything that fits in
|
87 |
|
|
the same space as a ``char *'' pointer.
|
88 |
|
|
Values for hash table entries are managed entirely by clients,
|
89 |
|
|
not by the hash module itself.
|
90 |
|
|
Typically each entry's value is a pointer to a data structure
|
91 |
|
|
managed by client code.
|
92 |
|
|
.PP
|
93 |
|
|
Hash tables grow gracefully as the number of entries increases,
|
94 |
|
|
so that there are always less than three entries per hash bucket,
|
95 |
|
|
on average.
|
96 |
|
|
This allows for fast lookups regardless of the number of entries
|
97 |
|
|
in a table.
|
98 |
|
|
.PP
|
99 |
|
|
\fBTcl_InitHashTable\fR initializes a structure that describes
|
100 |
|
|
a new hash table.
|
101 |
|
|
The space for the structure is provided by the caller, not by
|
102 |
|
|
the hash module.
|
103 |
|
|
The value of \fIkeyType\fR indicates what kinds of keys will
|
104 |
|
|
be used for all entries in the table. \fIKeyType\fR must have
|
105 |
|
|
one of the following values:
|
106 |
|
|
.IP \fBTCL_STRING_KEYS\fR 25
|
107 |
|
|
Keys are null-terminated ASCII strings.
|
108 |
|
|
They are passed to hashing routines using the address of the
|
109 |
|
|
first character of the string.
|
110 |
|
|
.IP \fBTCL_ONE_WORD_KEYS\fR 25
|
111 |
|
|
Keys are single-word values; they are passed to hashing routines
|
112 |
|
|
and stored in hash table entries as ``char *'' values.
|
113 |
|
|
The pointer value is the key; it need not (and usually doesn't)
|
114 |
|
|
actually point to a string.
|
115 |
|
|
.IP \fIother\fR 25
|
116 |
|
|
If \fIkeyType\fR is not TCL_STRING_KEYS or TCL_ONE_WORD_KEYS,
|
117 |
|
|
then it must be an integer value greater than 1.
|
118 |
|
|
In this case the keys will be arrays of ``int'' values, where
|
119 |
|
|
\fIkeyType\fR gives the number of ints in each key.
|
120 |
|
|
This allows structures to be used as keys.
|
121 |
|
|
All keys must have the same size.
|
122 |
|
|
Array keys are passed into hashing functions using the address
|
123 |
|
|
of the first int in the array.
|
124 |
|
|
.PP
|
125 |
|
|
\fBTcl_DeleteHashTable\fR deletes all of the entries in a hash
|
126 |
|
|
table and frees up the memory associated with the table's
|
127 |
|
|
bucket array and entries.
|
128 |
|
|
It does not free the actual table structure (pointed to
|
129 |
|
|
by \fItablePtr\fR), since that memory is assumed to be managed
|
130 |
|
|
by the client.
|
131 |
|
|
\fBTcl_DeleteHashTable\fR also does not free or otherwise
|
132 |
|
|
manipulate the values of the hash table entries.
|
133 |
|
|
If the entry values point to dynamically-allocated memory, then
|
134 |
|
|
it is the client's responsibility to free these structures
|
135 |
|
|
before deleting the table.
|
136 |
|
|
.PP
|
137 |
|
|
\fBTcl_CreateHashEntry\fR locates the entry corresponding to a
|
138 |
|
|
particular key, creating a new entry in the table if there
|
139 |
|
|
wasn't already one with the given key.
|
140 |
|
|
If an entry already existed with the given key then \fI*newPtr\fR
|
141 |
|
|
is set to zero.
|
142 |
|
|
If a new entry was created, then \fI*newPtr\fR is set to a non-zero
|
143 |
|
|
value and the value of the new entry will be set to zero.
|
144 |
|
|
The return value from \fBTcl_CreateHashEntry\fR is a pointer to
|
145 |
|
|
the entry, which may be used to retrieve and modify the entry's
|
146 |
|
|
value or to delete the entry from the table.
|
147 |
|
|
.PP
|
148 |
|
|
\fBTcl_DeleteHashEntry\fR will remove an existing entry from a
|
149 |
|
|
table.
|
150 |
|
|
The memory associated with the entry itself will be freed, but
|
151 |
|
|
the client is responsible for any cleanup associated with the
|
152 |
|
|
entry's value, such as freeing a structure that it points to.
|
153 |
|
|
.PP
|
154 |
|
|
\fBTcl_FindHashEntry\fR is similar to \fBTcl_CreateHashEntry\fR
|
155 |
|
|
except that it doesn't create a new entry if the key doesn't exist;
|
156 |
|
|
instead, it returns NULL as result.
|
157 |
|
|
.PP
|
158 |
|
|
\fBTcl_GetHashValue\fR and \fBTcl_SetHashValue\fR are used to
|
159 |
|
|
read and write an entry's value, respectively.
|
160 |
|
|
Values are stored and retrieved as type ``ClientData'', which is
|
161 |
|
|
large enough to hold a pointer value. On almost all machines this is
|
162 |
|
|
large enough to hold an integer value too.
|
163 |
|
|
.PP
|
164 |
|
|
\fBTcl_GetHashKey\fR returns the key for a given hash table entry,
|
165 |
|
|
either as a pointer to a string, a one-word (``char *'') key, or
|
166 |
|
|
as a pointer to the first word of an array of integers, depending
|
167 |
|
|
on the \fIkeyType\fR used to create a hash table.
|
168 |
|
|
In all cases \fBTcl_GetHashKey\fR returns a result with type
|
169 |
|
|
``char *''.
|
170 |
|
|
When the key is a string or array, the result of \fBTcl_GetHashKey\fR
|
171 |
|
|
points to information in the table entry; this information will
|
172 |
|
|
remain valid until the entry is deleted or its table is deleted.
|
173 |
|
|
.PP
|
174 |
|
|
\fBTcl_FirstHashEntry\fR and \fBTcl_NextHashEntry\fR may be used
|
175 |
|
|
to scan all of the entries in a hash table.
|
176 |
|
|
A structure of type ``Tcl_HashSearch'', provided by the client,
|
177 |
|
|
is used to keep track of progress through the table.
|
178 |
|
|
\fBTcl_FirstHashEntry\fR initializes the search record and
|
179 |
|
|
returns the first entry in the table (or NULL if the table is
|
180 |
|
|
empty).
|
181 |
|
|
Each subsequent call to \fBTcl_NextHashEntry\fR returns the
|
182 |
|
|
next entry in the table or
|
183 |
|
|
NULL if the end of the table has been reached.
|
184 |
|
|
A call to \fBTcl_FirstHashEntry\fR followed by calls to
|
185 |
|
|
\fBTcl_NextHashEntry\fR will return each of the entries in
|
186 |
|
|
the table exactly once, in an arbitrary order.
|
187 |
|
|
It is unadvisable to modify the structure of the table, e.g.
|
188 |
|
|
by creating or deleting entries, while the search is in
|
189 |
|
|
progress.
|
190 |
|
|
.PP
|
191 |
|
|
\fBTcl_HashStats\fR returns a dynamically-allocated string with
|
192 |
|
|
overall information about a hash table, such as the number of
|
193 |
|
|
entries it contains, the number of buckets in its hash array,
|
194 |
|
|
and the utilization of the buckets.
|
195 |
|
|
It is the caller's responsibility to free the result string
|
196 |
|
|
by passing it to \fBfree\fR.
|
197 |
|
|
.PP
|
198 |
|
|
The header file \fBtcl.h\fR defines the actual data structures
|
199 |
|
|
used to implement hash tables.
|
200 |
|
|
This is necessary so that clients can allocate Tcl_HashTable
|
201 |
|
|
structures and so that macros can be used to read and write
|
202 |
|
|
the values of entries.
|
203 |
|
|
However, users of the hashing routines should never refer directly
|
204 |
|
|
to any of the fields of any of the hash-related data structures;
|
205 |
|
|
use the procedures and macros defined here.
|
206 |
|
|
|
207 |
|
|
.SH KEYWORDS
|
208 |
|
|
hash table, key, lookup, search, value
|