1 .\" $FreeBSD: src/lib/libc/stdlib/hcreate.3,v 1.2.2.2 2003/04/05 13:53:05 dwmalone Exp $
2 .\" $DragonFly: src/lib/libc/stdlib/hcreate.3,v 1.5 2006/05/26 19:39:37 swildner Exp $
11 .Nd manage hash search table
17 .Fn hcreate "size_t nel"
21 .Fn hsearch "ENTRY item" "ACTION action"
28 functions manage hash search tables.
32 function allocates sufficient space for the table, and the application should
33 ensure it is called before
38 argument is an estimate of the maximum
39 number of entries that the table should contain.
40 This number may be adjusted upward by the
41 algorithm in order to obtain certain mathematically favorable circumstances.
45 function disposes of the search table, and may be followed by another call to
49 the data can no longer be considered accessible.
54 for each comparison key in the search table
55 but not the data item associated with the key.
59 function is a hash-table search routine.
60 It returns a pointer into a hash table
61 indicating the location at which an entry can be found.
64 argument is a structure of type
68 header) containing two pointers:
70 points to the comparison key (a
76 points to any other data to be associated with
78 The comparison function used by
85 member of an enumeration type
87 indicating the disposition of the entry if it cannot be
92 should be inserted in the table at an
95 indicates that no entry should be made.
96 Unsuccessful resolution is
97 indicated by the return of a
101 The comparison key (passed to
105 must be allocated using
117 function returns 0 if it cannot allocate sufficient space for the table;
118 otherwise, it returns non-zero.
122 function does not return a value.
128 pointer if either the
134 could not be found or the
138 and the table is full.
140 The following example reads in strings followed by two numbers
141 and stores them in a hash table, discarding duplicates.
142 It then reads in strings and finds the matching entry in the hash
143 table and prints it out.
150 struct info { /* This is the info stored in the table */
151 int age, room; /* other than the key. */
154 #define NUM_EMPL 5000 /* # of elements in search table. */
159 char str[BUFSIZ]; /* Space to read string */
160 struct info info_space[NUM_EMPL]; /* Space to store employee info. */
161 struct info *info_ptr = info_space; /* Next space in info_space. */
163 ENTRY *found_item; /* Name to look for in table. */
164 char name_to_find[30];
167 /* Create table; no error checking is performed. */
168 (void) hcreate(NUM_EMPL);
170 while (scanf("%s%d%d", str, &info_ptr->age,
171 &info_ptr->room) != EOF && i++ < NUM_EMPL) {
172 /* Put information in structure, and structure in item. */
173 item.key = strdup(str);
174 item.data = info_ptr;
176 /* Put item into table. */
177 (void) hsearch(item, ENTER);
181 item.key = name_to_find;
182 while (scanf("%s", item.key) != EOF) {
183 if ((found_item = hsearch(item, FIND)) != NULL) {
184 /* If item is in the table. */
185 (void)printf("found %s, age = %d, room = %d\en",
187 ((struct info *)found_item->data)->age,
188 ((struct info *)found_item->data)->room);
190 (void)printf("no such employee %s\en", name_to_find);
201 functions may fail if:
204 Insufficient storage space is available.
226 functions first appeared in
229 The interface permits the use of only one hash table at a time.