*nix Documentation Project
·  Home
 +   man pages
·  Linux HOWTOs
·  FreeBSD Tips
·  *niX Forums

  man pages->FreeBSD man pages -> hdestroy (3)              



NAME    [Toc]    [Back]

     hcreate, hdestroy, hsearch -- manage hash search table

LIBRARY    [Toc]    [Back]

     Standard C Library (libc, -lc)

SYNOPSIS    [Toc]    [Back]

     #include <search.h>

     hcreate(size_t nel);


     ENTRY *
     hsearch(ENTRY item, ACTION action);

DESCRIPTION    [Toc]    [Back]

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

     The hcreate() function allocates sufficient space for the table, and the
     application should ensure it is called before hsearch() 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 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 hdestroy() function calls
     free(3) for each comparison key in the search table but not the data item
     associated with the key.

     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(3).  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

     The comparison key (passed to hsearch() as item.key) must be allocated
     using malloc(3) if action is ENTER and hdestroy() is called.

RETURN VALUES    [Toc]    [Back]

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

     The hdestroy() function does not return a 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    [Toc]    [Back]

     The hcreate() and hsearch() functions may fail if:

     [ENOMEM]		Insufficient storage space is available.

EXAMPLES    [Toc]    [Back]

     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 <stdio.h>
     #include <search.h>
     #include <string.h>
     #include <stdlib.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. */

	     char str[BUFSIZ]; /* Space to read string */
	     struct info info_space[NUM_EMPL]; /* Space to store employee info. */
	     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, &info_ptr->age,
		 &info_ptr->room) != EOF && i++ < NUM_EMPL) {
		     /* Put information in structure, and structure in item. */
		     item.key = strdup(str);
		     item.data = 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",
				 ((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;

SEE ALSO    [Toc]    [Back]

     bsearch(3), lsearch(3), malloc(3), strcmp(3), tsearch(3)

STANDARDS    [Toc]    [Back]

     The hcreate(), hdestroy(), and hsearch() functions conform to X/Open
     Portability Guide Issue 4.2 (``XPG4.2'').

HISTORY    [Toc]    [Back]

     The hcreate(), hdestroy(), and hsearch() functions first appeared in AT&T
     System V UNIX.

BUGS    [Toc]    [Back]

     The interface permits the use of only one hash table at a time.

FreeBSD 5.2.1			  May 8, 2001			 FreeBSD 5.2.1
[ Back ]
 Similar pages
Name OS Title
hsearch IRIX manage hash search tables
bsearch NetBSD binary search of a sorted table
bsearch IRIX binary search a sorted table
bsearch FreeBSD binary search of a sorted table
bsearch OpenBSD binary search of a sorted table
lh_error OpenBSD dynamic hash table
lh_retrieve Tru64 Dynamic hash table
lhash Tru64 Dynamic hash table
lh_new Tru64 Dynamic hash table
lh_insert Tru64 Dynamic hash table
Copyright © 2004-2005 DeniX Solutions SRL
newsletter delivery service