xref: /titanic_41/usr/src/lib/libast/common/man/hash.3 (revision cde2885fdf538266ee2a3b08dee2d5075ce8fa2b)
.fp 5 CW .. .nr ;G \\n(.f .Af "\\$1" "\\$2" "\\$3" "\\$4" "\\$5" "\\$6" "\\$7" "\\$8" "\\$9" \\*(;G .. .aF 5 \\n(.f "\\$1" "\\$2" "\\$3" "\\$4" "\\$5" "\\$6" "\\$7" .. .aF 5 1 "\\$1" "\\$2" "\\$3" "\\$4" "\\$5" "\\$6" "\\$7" .. .aF 1 5 "\\$1" "\\$2" "\\$3" "\\$4" "\\$5" "\\$6" "\\$7" ..

0

..

..

HASH 3
NAME
hash - hash table support - obsolete - use cdt
SYNOPSIS
.L "#include <hash.h>"
DESCRIPTION
The hash routines manipulate collections of dynamic, scoped hash tables.

A hash table provides an association between a key and its value . A key is a sequence of .L char elements and a value is a user supplied pointer to the value. Each hash table has a dynamic number of slots, each pointing to the head of a forward linked "collision chain" .

Hashing occurs as follows: a "hash function" takes a key as an argument and returns a non-negative index. The index modulo the table size produces a slot that points to a "collision chain" . The collision chain is sequentially searched until a match is found for key . The hash tables are automatically resized as new entries are added.

Each hash table has one key type. The default key type is a pointer to a null-terminated string. The alternate key type is a pointer to a fixed length byte buffer, declared for a given table by the .L hashalloc() function described below.

Hash table information is partitioned into two parts for efficient scoping. The root part contains fixed information that is shared among a set of related hash tables. The remaining information is maintained on a per-table basis.

These basic types are defined in the header file hash.h (alternate names are listed in parenthesis):

.L "Hash_table_t (HASHTABLE)" The per-table information. The readonly public elements are:

.L "int buckets" The number of table entries.

.L "char* name" The hash table name.

.L "root" The root information. The public elements are:

.L "int root->accesses" The number of lookups.

.L "int root->collisions" The number of lookup collisions.

.L "Hash_table_t* scope" The table that this scope covers, .L NULL if the table is not a scope.

.L "int size" The current hash table size.

.L "Hash_bucket_t (HASHBUCKET)" A collision chain hash bucket. The public structure elements are:

.L "char* hashname(Hash_bucket_t*)" Returns a pointer to the hash bucket key given the bucket pointer.

.L "char* value" The value associated with the key.

.L "Hash_header_t (HASHHEADER)" The hash bucket header that must be the first element in all user defined buckets. .L HASH_VALUE may not be used with user defined buckets.

.L "Hash_position_t (HASHPOSITION)" Stores the hash table position for .LR hashscan . The public elements are:

.L "Hash_bucket_t* bucket" The current hash bucket pointer.

The routines are:

.L "Hash_table_t* hashalloc(Hash_table_t* ref, int op, ...)" Creates a new hash table and returns a pointer to the table. malloc (3) is used to allocate space for the table. .L NULL is returned if the table cannot be created. .L ref is a pointer to a reference hash table that provides default values for unspecified information. The new hash table and .L ref share the same root information. If .L ref is .L NULL then new root information is created for the new table. The remaining arguments appear in op-arg pairs, followed by a final .L 0 argument. The op-arg pairs are:

.L "HASH_alloc, (char(*)()) alloc" .L alloc is a function that is called to process .L Hash_bucket_t .L value assignments. The single argument is .L "char* value" and the processed .L char* value is returned.

.L "HASH_clear, int flags" .L flags are the .L ref flags to be cleared in the new hash table. See .L HASH_set below.

.L "HASH_compare, (int(*)()) compare" Specifies an alternate key comparison function. The arguments and return value for .L compare are the same as for strncmp (3) if .L HASH_namesize is specified and strcmp (3) otherwise. The first argument is the key from the current hash bucket on the "collision chain" and the second argument is the user supplied key .

.L "HASH_free, (int(*)()) free" .L free is a function that is called when a hash bucket is freed. If .L HASH_BUCKET was set in .L hashalloc then the hash bucket pointer is passed, otherwise the bucket .L value pointer is passed.

.L "HASH_hash, (int(*)()) hash" Specifies an alternate key hash function. A .L char* key argument (and, if .L HASH_namesize is specified, an .L int key size argument) is passed to .LR hash . The return value must be a non-negative .LR int .

.L "HASH_meanchain, int meanchain" Specifies the mean collision chain length. The hash table is automatically resized when this value is exceeded. The default mean chain length is 2.

.L "HASH_name, char* name" Associates .L name with the hash table. Used by .LR hashdump) .

.L "HASH_namesize, int namesize" The fixed size in bytes for keys in the table. If .L namesize is 0 (the default) then the keys are interpreted as null-terminated strings.

.L "HASH_set, int flags" Changes the hash table flags by or ing in .LR flags . The flags, which may be or ed together, are:

.L HASH_ALLOCATE Keys for new hash table entries are to be copied to data areas obtained from malloc (3).

.L HASH_FIXED Fixes the hash table size, disabling any automatic table resizing.

.L HASH_SCOPE The new hash table is a scope that is to be pushed on top of .LR ref . .L ref must be .RL non- NULL .

.L "HASH_va_list, va_list ap" .L ap is a .L va_list variable argument list pointer (see .LR <stdarg.h> ).

.L "Hash_table_t* hashfree(Hash_table_t* tab)" The hash table .L tab is freed. The scope covered table pointer is returned, .L NULL if .L tab is not a scope.

.L "char* hashlook(Hash_table_t* tab, char* name, int flags, char* value)" Operates on the key .L name in the hash table .L tab according to .L flags and .LR value . A .L Hash_bucket_t pointer is returned unless otherwise noted. There are three basic lookup operations:

.L HASH_CREATE .L name is entered into the top level scope if it does not already exist. If .L name also appears in a lower scope and .L HASH_ALLOC is set for the table then the new bucket will share the .L name field value with the lower scope.

.L HASH_DELETE .L name is deleted from the top level scope if it exists. .L NULL is returned.

.L HASH_LOOKUP The scopes are searched in order from top to bottom for .L name . The bucket pointer for the first occurrence is returned. .L NULL is returned if .L name is not found.

The basic operations may be qualified by the following (the qualifiers are restricted to the basic operations in the parenthesized list):

.L "HASH_BUCKET (HASH_CREATE,HASH_DELETE,HASH_LOOKUP)" .L name is a pointer to a bucket that has already been entered into the table.

.L "HASH_FIXED (HASH_CREATE)" .L value is taken to be the size of the hash bucket to be created for .L name in the top level scope. The minimum bucket size is silently restricted to .LR sizeof(Hash_header_t) .

.L "HASH_INSTALL (HASH_CREATE)" .L name is a pointer to a bucket that has not been entered into the table.

.L "HASH_NOSCOPE (HASH_LOOKUP)" The lookup is restricted to the top level scope.

.L "HASH_OPAQUE (HASH_CREATE,HASH_DELETE)" Sets .L (HASH_CREATE) or clears .L (HASH_DELETE) the opaque property for the bucket. An opaque bucket is not visible in lower scopes.

.L "HASH_SCOPE (HASH_CREATE,HASH_DELETE)" All scopes are searched for the bucket. If the bucket is not found for .L HASH_CREATE then a new bucket is created in the lowest scope.

.L "HASH_VALUE (HASH_CREATE,HASH_LOOKUP)" For .L HASH_CREATE the bucket .L value field is set to .L value and the bucket .L name value is returned. For .L HASH_LOOKUP the bucket .L value field is returned, .L NULL if the bucket is not found.

If .L name .L NULL then the name from the most recent .L hashlook() is used, avoiding recomputation of some internal parameters.

.L "char* hashget(Hash_table_t* tab, char* name)" Returns the value associated with the key .L name in the hash table .LR tab . If .L name is .L NULL then the name from the most recent .L hashget() is used, avoiding recomputation of some internal parameters. .L NULL is returned if .L name is not in the table. All scope covered tables are searched.

.L "Hash_bucket_t* hashlast(Hash_table_t* tab)" Returns a pointer to the most recent hash bucket for .LR tab . The value is set by .LR hashlook() , .L hashscan() and .LR hashwalk() .

.L "char* hashput(Hash_table_t* tab, char* name, char* value)" Set the value of the key .L name to .L value in the top level scope of the hash table .LR tab . .L name is entered into the top level scope if necessary. The (possibly re-allocated) key name pointer is returned (see .LR HASH_ALLOCATE ). If .L name is 0 then the most recent lookup .L name to .L hashlook() or .L hashget() is used. This eliminates a re-hash and re-lookup of .LR name .

.L "int hashwalk(Hash_table_t* tab, int flags, (int(*)()) walker, char* handle)" The function .L walker is applied to each entry (not covered by a scope starting at .LR tab ) in the hash table .LR tab . If .L flags is .L HASH_NOSCOPE then only the top level hash table is used, otherwise the walk includes all scope covered tables. .L walker is called with .L char* key as the first argument, .L char* value as the second argument, and .L char* handle as the third argument. handle may be .LR 0 . The walk terminates after the last entry or when .L walker returns a negative value. The return value of the last call to .L walker is returned. Only one walk may be active within a collection of scoped tables.

.L "Hash_position_t* hashscan(Hash_table_t* tab, int flags)" Returns a .L Hash_position_t pointer for a sequential scan on the hash table .LR tab . If .L flags is .L HASH_NOSCOPE then only the top level hash table is used, otherwise the scan includes all scope covered tables. Only one scan may be active within a collection of scoped tables. .L hashdone() must be called to terminate the scan. .L 0 is returned on error.

.L "Hash_bucket_t* hashnext(Hash_position_t* pos)" Returnes a pointer to the next bucket in the sequential scan set up by .L hashscan() on .LR pos . If no elements remain then .L 0 is returned.

.L "void hashdone(Hash_position_t* pos)" Completes a scan initiated by .L hashscan() on .LR pos .

.L "int hashset(Hash_table_t* tab, int flags)" Sets the flags for the hash table .L tab by or ing in .LR flags . Only .L HASH_ALLOCATE and .L HASH_FIXED may be set.

.L "int hashclear(Hash_table_t* tab, int flags)" Clears the flags for the hash table .L tab by masking out .LR flags . Only .L HASH_ALLOCATE and .L HASH_FIXED may be cleared.

.L "void hashdump(Hash_table_t* tab, int flags)" Dumps hash table accounting info to standard error. If .L tab is .L NULL then all allocated hash tables are dumped, otherwise only information on .L tab is dumped. If .L flags is .L HASH_BUCKET then the hash bucket key-value pairs for each collision chain are also dumped.

.L "void hashsize(Hash_table_t* tab, int size)" Changes the size of the hash table .L tab to .L size where .L size must be a power of 2. Explicit calls to this routine are not necessary as hash tables are automatically resized.

.L "int strhash(char* name)" Hashes the null terminated character string .L name using a linear congruent pseudo-random number generator algorithm and returns a non-negative .L int hash value.

.L "int memhash(char* buf, int siz)" Hashes the buffer .L buf of .L siz bytes using a linear congruent pseudo-random number generator algorithm and returns a non-negative .L int hash value.

.L "long strsum(char* name, long sum)" Returns a running 31-bit checksum of the string .L name where .L sum is .L 0 on the first call and the return value from a previous .L memsum or .L strsum call otherwise. The checksum value is consistent across all implementations.

.L "long memsum(char* buf, int siz, long sum)" Returns a running 31-bit checksum of buffer .L buf of .L siz bytes where .L sum is .L 0 on the first call and the return value from a previous .L memsum or .L strsum call otherwise. The checksum value is consistent across all implementations.

"SEE ALSO"
sum(1)