1 .\" $FreeBSD: src/lib/libc/stdlib/hcreate.3,v 1.2 2001/07/09 15:54:36 ru Exp $
7 .Nm hcreate , hdestroy , hsearch
8 .Nd manage hash search table
14 .Fn hcreate "size_t nel"
18 .Fn hsearch "ENTRY item" "ACTION action"
25 functions manage hash search tables.
29 function allocates sufficient space for the table, and the application should
30 ensure it is called before
35 argument is an estimate of the maximum
36 number of entries that the table should contain.
37 This number may be adjusted upward by the
38 algorithm in order to obtain certain mathematically favorable circumstances.
42 function disposes of the search table, and may be followed by another call to
46 the data can no longer be considered accessible.
50 function is a hash-table search routine.
51 It returns a pointer into a hash table
52 indicating the location at which an entry can be found.
55 argument is a structure of type
59 header) containing two pointers:
61 points to the comparison key (a
67 points to any other data to be associated with
69 The comparison function used by
76 member of an enumeration type
78 indicating the disposition of the entry if it cannot be
83 should be inserted in the table at an
86 indicates that no entry should be made.
87 Unsuccessful resolution is
88 indicated by the return of a
94 function returns 0 if it cannot allocate sufficient space for the table;
95 otherwise, it returns non-zero.
99 function does not return a value.
105 pointer if either the
111 could not be found or the
115 and the table is full.
121 functions may fail if:
124 Insufficient storage space is available.
127 The following example reads in strings followed by two numbers
128 and stores them in a hash table, discarding duplicates.
129 It then reads in strings and finds the matching entry in the hash
130 table and prints it out.
136 struct info { /* This is the info stored in the table */
137 int age, room; /* other than the key. */
140 #define NUM_EMPL 5000 /* # of elements in search table. */
145 char string_space[NUM_EMPL*20]; /* Space to store strings. */
146 struct info info_space[NUM_EMPL]; /* Space to store employee info. */
147 char *str_ptr = string_space; /* Next space in string_space. */
148 struct info *info_ptr = info_space; /* Next space in info_space. */
150 ENTRY *found_item; /* Name to look for in table. */
151 char name_to_find[30];
154 /* Create table; no error checking is performed. */
155 (void) hcreate(NUM_EMPL);
157 while (scanf("%s%d%d", str_ptr, &info_ptr->age,
158 &info_ptr->room) != EOF && i++ < NUM_EMPL) {
159 /* Put information in structure, and structure in item. */
161 item.data = info_ptr;
162 str_ptr += strlen(str_ptr) + 1;
164 /* Put item into table. */
165 (void) hsearch(item, ENTER);
169 item.key = name_to_find;
170 while (scanf("%s", item.key) != EOF) {
171 if ((found_item = hsearch(item, FIND)) != NULL) {
172 /* If item is in the table. */
173 (void)printf("found %s, age = %d, room = %d\en",
175 ((struct info *)found_item->data)->age,
176 ((struct info *)found_item->data)->room);
178 (void)printf("no such employee %s\en", name_to_find);
203 functions first appeared in
206 The interface permits the use of only one hash table at a time.