3 .ds ;G \\*(;G\\f\\$1\\$3\\f\\$2
4 .if !
\a\\$4
\a\a .Af \\$2 \\$1 "\\$4" "\\$5" "\\$6" "\\$7" "\\$8" "\\$9"
7 .ie
\a\\$3
\a\a .ft \\$1
11 .Af "\\$1" "\\$2" "\\$3" "\\$4" "\\$5" "\\$6" "\\$7" "\\$8" "\\$9"
16 .aF 5 \\n(.f "\\$1" "\\$2" "\\$3" "\\$4" "\\$5" "\\$6" "\\$7"
19 .aF 5 1 "\\$1" "\\$2" "\\$3" "\\$4" "\\$5" "\\$6" "\\$7"
22 .aF 1 5 "\\$1" "\\$2" "\\$3" "\\$4" "\\$5" "\\$6" "\\$7"
24 .de EX \" start example
41 hash \- hash table support (obsolete: use \fBcdt\fP instead)
43 .L "#include <hash.h>"
47 routines manipulate collections of dynamic, scoped hash tables.
49 A hash table provides an association between a
59 is a user supplied pointer to the value.
60 Each hash table has a dynamic number of slots,
61 each pointing to the head of a forward linked
62 .IR "collision chain" .
64 Hashing occurs as follows:
69 as an argument and returns a non-negative index.
70 The index modulo the table size produces a
72 .IR "collision chain" .
73 The collision chain is sequentially searched until a match is found for
75 The hash tables are automatically resized as new entries are added.
77 Each hash table has one key type.
78 The default key type is a pointer to a null-terminated string.
79 The alternate key type is a pointer to a fixed length byte buffer,
80 declared for a given table by the
82 function described below.
84 Hash table information is partitioned into two parts for efficient scoping.
87 part contains fixed information that is shared among a set of related
89 The remaining information is maintained on a per-table basis.
91 These basic types are defined in the header file
93 (alternate names are listed in parenthesis):
95 .L "Hash_table_t (HASHTABLE)"
96 The per-table information.
97 The readonly public elements are:
101 The number of table entries.
107 The root information.
108 The public elements are:
111 .L "int root->accesses"
112 The number of lookups.
114 .L "int root->collisions"
115 The number of lookup collisions.
118 .L "Hash_table_t* scope"
119 The table that this scope covers,
121 if the table is not a scope.
124 The current hash table size.
127 .L "Hash_bucket_t (HASHBUCKET)"
128 A collision chain hash bucket.
129 The public structure elements are:
132 .L "char* hashname(Hash_bucket_t*)"
133 Returns a pointer to the hash bucket key given the bucket pointer.
136 The value associated with the key.
139 .L "Hash_header_t (HASHHEADER)"
140 The hash bucket header that must be the first element in all user defined
143 may not be used with user defined buckets.
145 .L "Hash_position_t (HASHPOSITION)"
146 Stores the hash table position for
148 The public elements are:
151 .L "Hash_bucket_t* bucket"
152 The current hash bucket pointer.
157 .L "Hash_table_t* hashalloc(Hash_table_t* ref, int op, ...)"
158 Creates a new hash table and returns a pointer to the table.
160 is used to allocate space for the table.
162 is returned if the table cannot be created.
164 is a pointer to a reference hash table that provides
165 default values for unspecified information.
166 The new hash table and
168 share the same root information.
173 then new root information is created for the new table.
174 The remaining arguments appear in
176 pairs, followed by a final
184 .L "HASH_alloc, (char(*)()) alloc"
186 is a function that is called to process
190 The single argument is
196 .L "HASH_clear, int flags"
200 flags to be cleared in the new hash table.
205 .L "HASH_compare, (int(*)()) compare"
206 Specifies an alternate
209 The arguments and return value for
218 The first argument is the
220 from the current hash bucket on the
222 and the second argument is the user supplied
225 .L "HASH_free, (int(*)()) free"
227 is a function that is called when a hash bucket is freed.
232 then the hash bucket pointer is passed, otherwise the bucket
236 .L "HASH_hash, (int(*)()) hash"
237 Specifies an alternate
242 key argument (and, if
246 key size argument) is passed to
248 The return value must be a non-negative
251 .L "HASH_meanchain, int meanchain"
252 Specifies the mean collision chain length.
253 The hash table is automatically resized when this value is exceeded.
254 The default mean chain length is 2.
256 .L "HASH_name, char* name"
263 .L "HASH_namesize, int namesize"
264 The fixed size in bytes for
269 is 0 (the default) then the
271 are interpreted as null-terminated strings.
273 .L "HASH_set, int flags"
274 Changes the hash table flags by
278 The flags, which may be
284 Keys for new hash table entries are to be copied to data areas obtained from
288 Fixes the hash table size, disabling any automatic table resizing.
291 The new hash table is a scope that is to be pushed on top of
298 .L "HASH_va_list, va_list ap"
302 variable argument list pointer
307 .L "Hash_table_t* hashfree(Hash_table_t* tab)"
311 The scope covered table pointer is returned,
317 .L "char* hashlook(Hash_table_t* tab, char* name, int flags, char* value)"
328 pointer is returned unless otherwise noted.
329 There are three basic lookup operations:
334 is entered into the top level scope if it does not already exist.
337 also appears in a lower scope and
339 is set for the table then the new bucket will share the
341 field value with the lower scope.
345 is deleted from the top level scope if it exists.
350 The scopes are searched in order from top to bottom for
352 The bucket pointer for the first occurrence is returned.
358 The basic operations may be qualified by the following
359 (the qualifiers are restricted to the basic operations in
360 the parenthesized list):
363 .L "HASH_BUCKET (HASH_CREATE,HASH_DELETE,HASH_LOOKUP)"
365 is a pointer to a bucket that has already been entered into the table.
367 .L "HASH_FIXED (HASH_CREATE)"
369 is taken to be the size of the hash bucket to be created for
371 in the top level scope.
372 The minimum bucket size is silently restricted to
373 .LR sizeof(Hash_header_t) .
375 .L "HASH_INSTALL (HASH_CREATE)"
377 is a pointer to a bucket that has not been entered into the table.
379 .L "HASH_NOSCOPE (HASH_LOOKUP)"
380 The lookup is restricted to the top level scope.
382 .L "HASH_OPAQUE (HASH_CREATE,HASH_DELETE)"
389 property for the bucket.
390 An opaque bucket is not visible in lower scopes.
392 .L "HASH_SCOPE (HASH_CREATE,HASH_DELETE)"
393 All scopes are searched for the bucket.
394 If the bucket is not found for
396 then a new bucket is created in the lowest scope.
398 .L "HASH_VALUE (HASH_CREATE,HASH_LOOKUP)"
414 if the bucket is not found.
419 then the name from the most recent
421 is used, avoiding recomputation of some internal parameters.
423 .L "char* hashget(Hash_table_t* tab, char* name)"
425 associated with the key
433 then the name from the most recent
435 is used, avoiding recomputation of some internal parameters.
440 All scope covered tables are searched.
442 .L "Hash_bucket_t* hashlast(Hash_table_t* tab)"
443 Returns a pointer to the most recent hash bucket for
451 .L "char* hashput(Hash_table_t* tab, char* name, char* value)"
452 Set the value of the key
456 in the top level scope of the hash table
459 is entered into the top level scope if necessary.
460 The (possibly re-allocated) key name pointer is returned
465 is 0 then the most recent lookup
472 This eliminates a re-hash and re-lookup of
475 .L "int hashwalk(Hash_table_t* tab, int flags, (int(*)()) walker, char* handle)"
478 is applied to each entry (not covered by a scope starting at
486 then only the top level hash table is used, otherwise the walk includes
487 all scope covered tables.
492 as the first argument,
495 as the second argument, and
498 as the third argument.
502 The walk terminates after the last entry or when
504 returns a negative value.
505 The return value of the last call to
508 Only one walk may be active within a collection of scoped tables.
510 .L "Hash_position_t* hashscan(Hash_table_t* tab, int flags)"
513 pointer for a sequential scan on the hash table
519 then only the top level hash table is used, otherwise the scan includes
520 all scope covered tables.
521 Only one scan may be active within a collection of scoped tables.
523 must be called to terminate the scan.
525 is returned on error.
527 .L "Hash_bucket_t* hashnext(Hash_position_t* pos)"
528 Returnes a pointer to the next bucket in the sequential scan set up by
532 If no elements remain then
536 .L "void hashdone(Hash_position_t* pos)"
537 Completes a scan initiated by
542 .L "int hashset(Hash_table_t* tab, int flags)"
543 Sets the flags for the hash table
555 .L "int hashclear(Hash_table_t* tab, int flags)"
556 Clears the flags for the hash table
566 .L "void hashdump(Hash_table_t* tab, int flags)"
567 Dumps hash table accounting info to standard error.
572 then all allocated hash tables are dumped, otherwise only information on
581 pairs for each collision chain are also dumped.
583 .L "void hashsize(Hash_table_t* tab, int size)"
584 Changes the size of the hash table
590 must be a power of 2.
591 Explicit calls to this routine are not necessary as hash tables
592 are automatically resized.
594 .L "int strhash(char* name)"
595 Hashes the null terminated character string
597 using a linear congruent pseudo-random number generator algorithm
598 and returns a non-negative
602 .L "int memhash(char* buf, int siz)"
607 bytes using a linear congruent pseudo-random number generator algorithm
608 and returns a non-negative
612 .L "long strsum(char* name, long sum)"
613 Returns a running 31-bit checksum of the string
619 on the first call and
620 the return value from a previous
625 The checksum value is consistent across all implementations.
627 .L "long memsum(char* buf, int siz, long sum)"
628 Returns a running 31-bit checksum of buffer
636 on the first call and
637 the return value from a previous
642 The checksum value is consistent across all implementations.