Manual Pages for UNIX Darwin command on man hsearch
MyWebUniversity

Manual Pages for UNIX Darwin command on man hsearch

HCREATE(3) BSD Library Functions Manual HCREATE(3)

NAME

hhccrreeaattee, hhddeessttrrooyy, hhsseeaarrcchh - manage hash search table

LLIIBBRRAARRYY

Standard C Library (libc, -lc)

SYNOPSIS

##iinncclluuddee <>

int hhccrreeaattee(sizet nel); void hhddeessttrrooyy(void); ENTRY * hhsseeaarrcchh(ENTRY item, ACTION action);

DESCRIPTION

The hhccrreeaattee(), hhddeessttrrooyy(), and hhsseeaarrcchh() functions manage hash search tables. The hhccrreeaattee() function allocates sufficient space for the table, and the application should ensure it is called before hhsseeaarrcchh() is used. The nel argument is an estimate of the maximum number of entries that the table should contain. This number may be adjusted upward by the algorithm in order to obtain certain mathematically favorable circumstances. The hhddeessttrrooyy() function disposes of the search table, and may be followed by another call to hhccrreeaattee(). After the call to hhddeessttrrooyy(), the data can no longer be considered accessible. The hhddeessttrrooyy() function calls free(3) for each comparison key in the search table but not the data item associated with the key.

The hhsseeaarrcchh() 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

header) containing two pointers: item.key points to the com-

parison key (a char *), and item.data (a void *) points to any other data to be associated with that key. The comparison function used by

hhsseeaarrcchh() is strcmp(3). The action argument is a member of an enumera-

tion 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 comparison key (passed to hhsseeaarrcchh() as item.key) must be allocated using malloc(3) if action is ENTER and hhddeessttrrooyy() is called.

RETURN VALUES

The hhccrreeaattee() function returns 0 if it cannot allocate sufficient space

for the table; otherwise, it returns non-zero.

The hhddeessttrrooyy() function does not return a value. The hhsseeaarrcchh() 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. EERRRROORRSS The hhccrreeaattee() and hhsseeaarrcchh() functions may fail if: [ENOMEM] Insufficient storage space is available. EEXXAAMMPPLLEESS The following example reads in strings followed by two numbers and stores them in a hash table, discarding duplicates. It then reads in strings and finds the matching entry in the hash table and prints it out.

#include

#include

#include

#include

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

#define NUMEMPL 5000 /* # of elements in search table. */

int main(void) { char str[BUFSIZ]; /* Space to read string */ struct info infospace[NUMEMPL]; /* Space to store employee info. */ struct info *infoptr = infospace; /* Next space in infospace. */ ENTRY item; ENTRY *founditem; /* Name to look for in table. */ char nametofind[30]; int i = 0; /* Create table; no error checking is performed. */ (void) hcreate(NUMEMPL);

while (scanf("%s%d%d", str, &infoptr->age,

&infoptr->room) != EOF && i++ < NUMEMPL) {

/* Put information in structure, and structure in item. */ item.key = strdup(str); item.data = infoptr; infoptr++; /* Put item into table. */

(void) hsearch(item, ENTER);

} /* Access table. */ item.key = nametofind;

while (scanf("%s", item.key) != EOF) {

if ((founditem = hsearch(item, FIND)) != NULL) {

/* If item is in the table. */

(void)printf("found %s, age = %d, room = %d\n",

founditem->key,

((struct info *)founditem->data)->age,

((struct info *)founditem->data)->room);

} else

(void)printf("no such employee %s\n", nametofind);

} hdestroy(); return 0; }

SEE ALSO

bsearch(3), lsearch(3), malloc(3), strcmp(3), tsearch(3) STANDARDS The hhccrreeaattee(), hhddeessttrrooyy(), and hhsseeaarrcchh() functions conform to X/Open Portability Guide Issue 4, Version 2 (``XPG4.2''). HISTORY The hhccrreeaattee(), hhddeessttrrooyy(), and hhsseeaarrcchh() functions first appeared in AT&T System V UNIX.

BUGS

The interface permits the use of only one hash table at a time. BSD May 8, 2001 BSD




Contact us      |      About us      |      Term of use      |       Copyright © 2000-2019 MyWebUniversity.com ™