The Single UNIX ® Specification, Version 2
Copyright © 1997 The Open Group

 NAME

hcreate, hdestroy, hsearch - manage hash search table

 SYNOPSIS



#include <search.h>

int hcreate(size_t nel);
void hdestroy(void);
ENTRY *hsearch (ENTRY item, ACTION action);

 DESCRIPTION

The hcreate(), hdestroy() and hsearch() functions manage hash search tables.

The hcreate() function allocates sufficient space for the table, and must be called before hsearch() is used. The nel argument is an estimate of the maximum number of entries that the table will contain. This number may be adjusted upward by the algorithm in order to obtain certain mathematically favourable circumstances.

The hdestroy() function disposes of the search table, and may be followed by another call to hcreate(). After the call to hdestroy(), the data can no longer be considered accessible.

The hsearch() function is a hash-table search routine. It returns a pointer into a hash table indicating the location at which an entry can be found. The item argument is a structure of type ENTRY (defined in the <search.h> header) containing two pointers: item.key points to the comparison key (a char *), and item.data (a void *) points to any other data to be associated with that key. The comparison function used by hsearch() is strcmp(). The action argument is a member of an enumeration type ACTION indicating the disposition of the entry if it cannot be found in the table. ENTER indicates that the item should be inserted in the table at an appropriate point. FIND indicates that no entry should be made. Unsuccessful resolution is indicated by the return of a null pointer.

The hcreate() and hsearch() functions need not be reentrant.

 RETURN VALUE

The hcreate() function returns 0 if it cannot allocate sufficient space for the table, and returns non-zero otherwise.

The hdestroy() function returns no value.

The hsearch() function returns a null pointer if either the action is FIND and the item could not be found or the action is ENTER and the table is full.

 ERRORS

The hcreate() and hsearch() functions may fail if:
[ENOMEM]
Insufficient storage space is available.

 EXAMPLES

The following example will read in strings followed by two numbers and store them in a hash table, discarding duplicates. It will then read in strings and find the matching entry in the hash table and print it out.

#include <stdio.h>
#include <search.h>
#include <string.h>

struct info {        /* this is the info stored in the table */
    int age, room;   /* other than the key. */
};

#define NUM_EMPL    5000    /* # of elements in search table */

int main(void)
{
    char string_space[NUM_EMPL*20];    /* space to store strings */
    struct info info_space[NUM_EMPL];  /* space to store employee info*/
    char *str_ptr = string_space;      /* next space in string_space */
    struct info *info_ptr = info_space;/* next space in info_space */
    ENTRY item;
    ENTRY *found_item;    /* name to look for in table */
    char name_to_find[30];

    int i = 0;

    /* create table; no error checking is performed */
    (void) hcreate(NUM_EMPL);
    while (scanf("%s%d%d", str_ptr, &info_ptr->age,
           &info_ptr->room) != EOF && i++ < NUM_EMPL) {

        /* put information in structure, and structure in item */
        item.key = str_ptr;
        item.data = info_ptr;
        str_ptr += strlen(str_ptr) + 1;
        info_ptr++;

        /* put item into table */
        (void) hsearch(item, ENTER);
    }

    /* access table */
    item.key = name_to_find;
    while (scanf("%s", item.key) != EOF) {
        if ((found_item = hsearch(item, FIND)) != NULL) {

            /* if item is in the table */
            (void)printf("found %s, age = %d, room = %d\n",
                found_item->key,
                ((struct info *)found_item->data)->age,
                ((struct info *)found_item->data)->room);
        } else
            (void)printf("no such employee %s\n", name_to_find);
    }
    return 0;
}

 APPLICATION USAGE

The hcreate() and hsearch() functions may use malloc() to allocate space.

 FUTURE DIRECTIONS

None.

 SEE ALSO

bsearch(), lsearch(), malloc(), strcmp(), tsearch(), <search.h>.

DERIVATION

Derived from Issue 1 of the SVID.

UNIX ® is a registered Trademark of The Open Group.
Copyright © 1997 The Open Group
[ Main Index | XSH | XCU | XBD | XCURSES | XNS ]