1HCREATE(P)                 POSIX Programmer's Manual                HCREATE(P)
2
3
4

NAME

6       hcreate, hdestroy, hsearch - manage hash search table
7

SYNOPSIS

9       #include <search.h>
10
11       int hcreate(size_t nel);
12       void hdestroy(void);
13       ENTRY *hsearch(ENTRY item, ACTION action);
14
15

DESCRIPTION

17       The  hcreate(),  hdestroy(),  and hsearch() functions shall manage hash
18       search tables.
19
20       The hcreate() function shall allocate sufficient space for  the  table,
21       and the application shall ensure it is called before hsearch() is used.
22       The nel argument is an estimate of the maximum number of  entries  that
23       the  table  shall  contain.  This  number may be adjusted upward by the
24       algorithm in order to obtain certain mathematically  favorable  circum‐
25       stances.
26
27       The  hdestroy()  function shall dispose of the search table, and may be
28       followed by another call to hcreate(). After the  call  to  hdestroy(),
29       the data can no longer be considered accessible.
30
31       The  hsearch() function is a hash-table search routine. It shall return
32       a pointer into a hash table indicating the location at which  an  entry
33       can  be  found. The item argument is a structure of type ENTRY (defined
34       in the <search.h> header) containing two pointers: item.key  points  to
35       the  comparison  key (a char *), and item.data (a void *) points to any
36       other data to be associated with that key. The comparison function used
37       by hsearch() is strcmp(). The action argument is a member of an enumer‐
38       ation type ACTION indicating the disposition of the entry if it  cannot
39       be found in the table. ENTER indicates that the item should be inserted
40       in the table at an appropriate point.  FIND  indicates  that  no  entry
41       should  be made.  Unsuccessful resolution is indicated by the return of
42       a null pointer.
43
44       These functions need not be reentrant. A function that is not  required
45       to be reentrant is not required to be thread-safe.
46

RETURN VALUE

48       The  hcreate() function shall return 0 if it cannot allocate sufficient
49       space for the table; otherwise, it shall return non-zero.
50
51       The hdestroy() function shall not return a value.
52
53       The hsearch() function shall return a null pointer if either the action
54       is  FIND and the item could not be found or the action is ENTER and the
55       table is full.
56

ERRORS

58       The hcreate() and hsearch() functions may fail if:
59
60       ENOMEM Insufficient storage space is available.
61
62
63       The following sections are informative.
64

EXAMPLES

66       The following example reads in strings  followed  by  two  numbers  and
67       stores  them  in  a hash table, discarding duplicates. It then reads in
68       strings and finds the matching entry in the hash table  and  prints  it
69       out.
70
71
72              #include <stdio.h>
73              #include <search.h>
74              #include <string.h>
75
76
77              struct info {        /* This is the info stored in the table */
78                  int age, room;   /* other than the key. */
79              };
80
81
82              #define NUM_EMPL    5000    /* # of elements in search table. */
83
84
85
86              int main(void)
87              {
88                  char string_space[NUM_EMPL*20];   /* Space to store strings. */
89                  struct info info_space[NUM_EMPL]; /* Space to store employee info. */
90                  char *str_ptr = string_space;     /* Next space in string_space. */
91                  struct info *info_ptr = info_space;
92                                                    /* Next space in info_space. */
93                  ENTRY item;
94                  ENTRY *found_item; /* Name to look for in table. */
95                  char name_to_find[30];
96
97
98                  int i = 0;
99
100
101                  /* Create table; no error checking is performed. */
102                  (void) hcreate(NUM_EMPL);
103                  while (scanf("%s%d%d", str_ptr, &info_ptr->age,
104                         &info_ptr->room) != EOF && i++ < NUM_EMPL) {
105
106
107                      /* Put information in structure, and structure in item. */
108                      item.key = str_ptr;
109                      item.data = info_ptr;
110                      str_ptr += strlen(str_ptr) + 1;
111                      info_ptr++;
112
113
114                      /* Put item into table. */
115                      (void) hsearch(item, ENTER);
116                  }
117
118
119                  /* Access table. */
120                  item.key = name_to_find;
121                  while (scanf("%s", item.key) != EOF) {
122                      if ((found_item = hsearch(item, FIND)) != NULL) {
123
124
125                          /* If item is in the table. */
126                          (void)printf("found %s, age = %d, room = %d\n",
127                              found_item->key,
128                              ((struct info *)found_item->data)->age,
129                              ((struct info *)found_item->data)->room);
130                      } else
131                          (void)printf("no such employee %s\n", name_to_find);
132                  }
133                  return 0;
134              }
135

APPLICATION USAGE

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

RATIONALE

141       None.
142

FUTURE DIRECTIONS

144       None.
145

SEE ALSO

147       bsearch() , lsearch() , malloc() , strcmp() , tsearch() , the Base Def‐
148       initions volume of IEEE Std 1003.1-2001, <search.h>
149
151       Portions  of  this text are reprinted and reproduced in electronic form
152       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
153       --  Portable  Operating  System  Interface (POSIX), The Open Group Base
154       Specifications Issue 6, Copyright (C) 2001-2003  by  the  Institute  of
155       Electrical  and  Electronics  Engineers, Inc and The Open Group. In the
156       event of any discrepancy between this version and the original IEEE and
157       The  Open Group Standard, the original IEEE and The Open Group Standard
158       is the referee document. The original Standard can be  obtained  online
159       at http://www.opengroup.org/unix/online.html .
160
161
162
163IEEE/The Open Group                  2003                           HCREATE(P)
Impressum