


_O_n_l_i_n_e _M_a_n_u_a_l           Library Functions             hsearch(3c)



NNNNAAAAMMMMEEEE
     hsearch, hcreate, hdestroy - manage hash search tables

SSSSYYYYNNNNTTTTAAAAXXXX
     ####iiiinnnncccclllluuuuddddeeee <<<<sssseeeeaaaarrrrcccchhhh....hhhh>>>>

     EEEENNNNTTTTRRRRYYYY ****hhhhsssseeeeaaaarrrrcccchhhh((((iiiitttteeeemmmm,,,, aaaaccccttttiiiioooonnnn))))
     EEEENNNNTTTTRRRRYYYY iiiitttteeeemmmm;;;;
     AAAACCCCTTTTIIIIOOOONNNN aaaaccccttttiiiioooonnnn;;;;

     iiiinnnntttt hhhhccccrrrreeeeaaaatttteeee((((nnnneeeellll))))
     uuuunnnnssssiiiiggggnnnneeeedddd nnnneeeellll;;;;

     vvvvooooiiiidddd hhhhddddeeeessssttttrrrrooooyyyy(((())))

DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
     _h_s_e_a_r_c_h is a hash-table search routine.  It returns a
     pointer into a hash table indicating the location at which
     an entry can be found.  _i_t_e_m is a structure of type EEEENNNNTTTTRRRRYYYY
     (defined in the <<<<sssseeeeaaaarrrrcccchhhh....hhhh>>>> header file) containing two
     pointers: _i_t_e_m._k_e_y points to the comparison key, and
     _i_t_e_m._d_a_t_a points to any other data to be associated with
     that key.  (Pointers to types other than void should be cast
     to pointer-to-void.) _a_c_t_i_o_n is a member of an enumeration
     type AAAACCCCTTTTIIIIOOOONNNN (defined in <<<<sssseeeeaaaarrrrcccchhhh....hhhh>>>>) indicating the disposi-
     tion of the entry if it cannot be found in the table.  EEEENNNNTTTTEEEERRRR
     indicates that the item should be inserted in the table at
     an appropriate point.  Given a duplicate of an existing
     item, the new item is not entered and _h_s_e_a_r_c_h returns a
     pointer to the existing item.  FFFFIIIINNNNDDDD indicates that no entry
     should be made.  Unsuccessful resolution is indicated by the
     return of a null pointer.

     _h_c_r_e_a_t_e allocates sufficient space for the table, and must
     be called before _h_s_e_a_r_c_h is used.  _n_e_l 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 favorable circumstances.

     _h_d_e_s_t_r_o_y destroys the search table, and may be followed by
     another call to _h_c_r_e_a_t_e.

EEEEXXXXAAAAMMMMPPPPLLLLEEEE
     The following example will read in strings followed by two
     numbers and store them in a hash table, discarding dupli-
     cates.  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>

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

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

          main( )
          {






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

               int i = 0;

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

                    /* put info in structure, and structure in item */
                    item.key = str_ptr;
                    item.data = (void *)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;
          }

SSSSEEEEEEEE AAAALLLLSSSSOOOO
     Library Functions (3): bsearch, lsearch, malloc, tsearch

DDDDIIIIAAAAGGGGNNNNOOOOSSSSTTTTIIIICCCCSSSS
     _h_s_e_a_r_c_h returns a null pointer if either the action is FFFFIIIINNNNDDDD
     and the item could not be found or the action is EEEENNNNTTTTEEEERRRR and
     the table is full.

     _h_c_r_e_a_t_e returns zero if it cannot allocate sufficient space
     for the table.

NNNNOOOOTTTTEEEESSSS
     _h_s_e_a_r_c_h and _h_c_r_e_a_t_e use _m_a_l_l_o_c() to allocate space.






     Only one hash search table may be active at any given time.

March 1989                              TX Programmer's Reference

























































