home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tcl2-73c.zip / tcl7.3 / doc / Hash.3 < prev    next >
Text File  |  1993-07-23  |  9KB  |  223 lines

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