(Compatible with UNIX System V)
#include <search.h> ENTRY *hsearch(); loc = hsearch(item,action);
The "hsearch" function is used to make an entry in a hash table. This hash table must have already been created by the "hcreate" function. A program can only have one such hash table at a time.
The ENTRY type is a structure containing the fields "key" and "data". Both of these are pointers to characters. The "key" points to the comparison key of a hash table entry and the "data" points to any other data associated with the entry. The comparison key and the data may have any type, but pointers to them must be cast to (char *) when setting up "item.key" and "item.data". It is up to the programmer to allocate space to hold comparison keys and data for hash table entries. The hash table only contains pointers to this information, not the information itself.
"hsearch" normally uses open addressing with a multiplicative hash function. However, the "hsearch" source code has been written to provide other options if the user chooses. This is done by #defining various symbols at the start of the "hsearch" source, then recompiling.
If you #define a symbol DIV, "hsearch" will use remainder modulo table size as its hash function instead of the multiplication function.
If you #define a symbol USCR, "hsearch" will use a user-supplied comparison routine to compare table entries to "item" arguments. This routine should be named "hcompar". Its arguments should be character pointers (one pointing to a table entry and the other to an item). The result should be an integer: zero if the entry and item are the same, positive if the item compares greater than the entry, and negative if the item compares less than the entry.
If you #define a symbol CHAINED, "hsearch" will use a linked list to resolve collisions. When CHAINED is #defined, three other symbols are also important.
Copyright © 1996, Thinkage Ltd.