HCREATE(3)                 POSIX Programmer's Manual                HCREATE(3)



PPRROOLLOOGG
       This manual page is part of the POSIX Programmer's Manual.  The OS
       implementation of this interface may differ (consult the corresponding
       OS manual page for details of OS behavior), or the interface may not be
       implemented on OS.


NNAAMMEE
       hcreate, hdestroy, hsearch -- manage hash search table

SSYYNNOOPPSSIISS
       #include <search.h>

       int hcreate(size_t _n_e_l);
       void hdestroy(void);
       ENTRY *hsearch(ENTRY _i_t_e_m, ACTION _a_c_t_i_o_n);

DDEESSCCRRIIPPTTIIOONN
       The _h_c_r_e_a_t_e(), _h_d_e_s_t_r_o_y(), and _h_s_e_a_r_c_h() functions shall manage hash
       search tables.

       The _h_c_r_e_a_t_e() function shall allocate sufficient space for the table,
       and the application shall ensure it is called before _h_s_e_a_r_c_h() is used.
       The _n_e_l argument is an estimate of the maximum number of entries that
       the table shall contain. This number may be adjusted upward by the
       algorithm in order to obtain certain mathematically favorable
       circumstances.

       The _h_d_e_s_t_r_o_y() function shall dispose of the search table, and may be
       followed by another call to _h_c_r_e_a_t_e().  After the call to _h_d_e_s_t_r_o_y(),
       the data can no longer be considered accessible.

       The _h_s_e_a_r_c_h() function is a hash-table search routine. It shall return
       a pointer into a hash table indicating the location at which an entry
       can be found. The _i_t_e_m argument is a structure of type EENNTTRRYY (defined
       in the _<_s_e_a_r_c_h_._h_> header) containing two pointers: _i_t_e_m_._k_e_y points to
       the comparison key (a cchhaarr **), and _i_t_e_m_._d_a_t_a (a vvooiidd **) points to any
       other data to be associated with that key. The comparison function used
       by _h_s_e_a_r_c_h() is _s_t_r_c_m_p().  The _a_c_t_i_o_n argument is a member of an
       enumeration type AACCTTIIOONN 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.

       These functions need not be thread-safe.

RREETTUURRNN VVAALLUUEE
       The _h_c_r_e_a_t_e() function shall return 0 if it cannot allocate sufficient
       space for the table; otherwise, it shall return non-zero.

       The _h_d_e_s_t_r_o_y() function shall not return a value.

       The _h_s_e_a_r_c_h() function shall return 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 _h_c_r_e_a_t_e() and _h_s_e_a_r_c_h() functions may fail if:

       EENNOOMMEEMM Insufficient storage space is available.

       _T_h_e _f_o_l_l_o_w_i_n_g _s_e_c_t_i_o_n_s _a_r_e _i_n_f_o_r_m_a_t_i_v_e_.

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.

           ##iinncclluuddee <<ssttddiioo..hh>>
           ##iinncclluuddee <<sseeaarrcchh..hh>>
           ##iinncclluuddee <<ssttrriinngg..hh>>

           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;
           }

AAPPPPLLIICCAATTIIOONN UUSSAAGGEE
       The _h_c_r_e_a_t_e() and _h_s_e_a_r_c_h() functions may use _m_a_l_l_o_c() to allocate
       space.

RRAATTIIOONNAALLEE
       None.

FFUUTTUURREE DDIIRREECCTTIIOONNSS
       None.

SSEEEE AALLSSOO
       _b_s_e_a_r_c_h(), _l_s_e_a_r_c_h(), _m_a_l_l_o_c(), _s_t_r_c_m_p(), _t_d_e_l_e_t_e()

       The Base Definitions volume of POSIX.1-2008, <<sseeaarrcchh..hh>>

CCOOPPYYRRIIGGHHTT
       Portions of this text are reprinted and reproduced in electronic form
       from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
       -- Portable Operating System Interface (POSIX), The Open Group Base
       Specifications Issue 7, Copyright (C) 2013 by the Institute of
       Electrical and Electronics Engineers, Inc and The Open Group.  (This is
       POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
       event of any discrepancy between this version and the original IEEE and
       The Open Group Standard, the original IEEE and The Open Group Standard
       is the referee document. The original Standard can be obtained online
       at http://www.unix.org/online.html .

       Any typographical or formatting errors that appear in this page are
       most likely to have been introduced during the conversion of the source
       files to man page format. To report such errors, see
       https://www.kernel.org/doc/man-pages/reporting_bugs.html .



IEEE/The Open Group                  2013                           HCREATE(3)
