Node:Hash Search Function, Next:Tree Search Function, Previous:Search/Sort Example, Up:Searching and Sorting
hsearch
function.The functions mentioned so far in this chapter are searching in a sorted or unsorted array. There are other methods to organize information which later should be searched. The costs of insert, delete and search differ. One possible implementation is using hashing tables.
int hcreate (size_t nel) | Function |
The hcreate function creates a hashing table which can contain at
least nel elements. There is no possibility to grow this table so
it is necessary to choose the value for nel wisely. The used
methods to implement this function might make it necessary to make the
number of elements in the hashing table larger than the expected maximal
number of elements. Hashing tables usually work inefficient if they are
filled 80% or more. The constant access time guaranteed by hashing can
only be achieved if few collisions exist. See Knuth's "The Art of
Computer Programming, Part 3: Searching and Sorting" for more
information.
The weakest aspect of this function is that there can be at most one hashing table used through the whole program. The table is allocated in local memory out of control of the programmer. As an extension the GNU C library provides an additional set of functions with an reentrant interface which provide a similar interface but which allow to keep arbitrarily many hashing tables. It is possible to use more than one hashing table in the program run if
the former table is first destroyed by a call to The function returns a non-zero value if successful. If it return zero something went wrong. This could either mean there is already a hashing table in use or the program runs out of memory. |
void hdestroy (void) | Function |
The hdestroy function can be used to free all the resources
allocated in a previous call of hcreate . After a call to this
function it is again possible to call hcreate and allocate a new
table with possibly different size.
It is important to remember that the elements contained in the hashing
table at the time |
Entries of the hashing table and keys for the search are defined using this type:
struct ENTRY | Data type |
Both elements of this structure are pointers to zero-terminated strings.
This is a limiting restriction of the functionality of the
hsearch functions. They can only be used for data sets which use
the NUL character always and solely to terminate the records. It is not
possible to handle general binary data.
|
ENTRY * hsearch (ENTRY item, ACTION action) | Function |
To search in a hashing table created using hcreate the
hsearch function must be used. This function can perform simple
search for an element (if action has the FIND ) or it can
alternatively insert the key element into the hashing table, possibly
replacing a previous value (if action is ENTER ).
The key is denoted by a pointer to an object of type The return value depends on the action parameter value. If it is
|
As mentioned before the hashing table used by the functions described so
far is global and there can be at any time at most one hashing table in
the program. A solution is to use the following functions which are a
GNU extension. All have in common that they operate on a hashing table
which is described by the content of an object of the type struct
hsearch_data
. This type should be treated as opaque, none of its
members should be changed directly.
int hcreate_r (size_t nel, struct hsearch_data *htab) | Function |
The hcreate_r function initializes the object pointed to by
htab to contain a hashing table with at least nel elements.
So this function is equivalent to the hcreate function except
that the initialized data structure is controlled by the user.
This allows having more than one hashing table at one time. The
memory necessary for the The return value is non-zero if the operation were successful. if the return value is zero something went wrong which probably means the programs runs out of memory. |
void hdestroy_r (struct hsearch_data *htab) | Function |
The hdestroy_r function frees all resources allocated by the
hcreate_r function for this very same object htab. As for
hdestroy it is the programs responsibility to free the strings
for the elements of the table.
|
int hsearch_r (ENTRY item, ACTION action, ENTRY **retval, struct hsearch_data *htab) | Function |
The hsearch_r function is equivalent to hsearch . The
meaning of the first two arguments is identical. But instead of
operating on a single global hashing table the function works on the
table described by the object pointed to by htab (which is
initialized by a call to hcreate_r ).
Another difference to
|