hcreate.3 (e25e8ab41ca2d3789a44d2d4009dea8fd2cf7d95) hcreate.3 (5560a5abb3f2c1612e3991c24fbf8bc0deb266ba)
1.\" $FreeBSD$
2.\"
3.Dd May 8, 2001
4.Os
5.Dt HCREATE 3
6.Sh NAME
7.Nm hcreate , hdestroy , hsearch
8.Nd manage hash search table

--- 30 unchanged lines hidden (view full) ---

39.Pp
40The
41.Fn hdestroy
42function disposes of the search table, and may be followed by another call to
43.Fn hcreate .
44After the call to
45.Fn hdestroy ,
46the data can no longer be considered accessible.
1.\" $FreeBSD$
2.\"
3.Dd May 8, 2001
4.Os
5.Dt HCREATE 3
6.Sh NAME
7.Nm hcreate , hdestroy , hsearch
8.Nd manage hash search table

--- 30 unchanged lines hidden (view full) ---

39.Pp
40The
41.Fn hdestroy
42function disposes of the search table, and may be followed by another call to
43.Fn hcreate .
44After the call to
45.Fn hdestroy ,
46the data can no longer be considered accessible.
47The
48.Fn hdestroy
49function calls
50.Xr free 3
51for each comparison key in the search table
52but not the data item associated with the key.
47.Pp
48The
49.Fn hsearch
50function is a hash-table search routine.
51It returns a pointer into a hash table
52indicating the location at which an entry can be found.
53The
54.Fa item

--- 28 unchanged lines hidden (view full) ---

83should be inserted in the table at an
84appropriate point.
85.Dv FIND
86indicates that no entry should be made.
87Unsuccessful resolution is
88indicated by the return of a
89.Dv NULL
90pointer.
53.Pp
54The
55.Fn hsearch
56function is a hash-table search routine.
57It returns a pointer into a hash table
58indicating the location at which an entry can be found.
59The
60.Fa item

--- 28 unchanged lines hidden (view full) ---

89should be inserted in the table at an
90appropriate point.
91.Dv FIND
92indicates that no entry should be made.
93Unsuccessful resolution is
94indicated by the return of a
95.Dv NULL
96pointer.
97.Pp
98The comparison key (passed to
99.Fn hsearch
100as
101.Fa item.key )
102must be allocated using
103.Xr malloc 3
104if
105.Fa action
106is
107.Dv ENTER
108and
109.Fn hdestroy
110is called.
91.Sh RETURN VALUES
92The
93.Fn hcreate
94function returns 0 if it cannot allocate sufficient space for the table;
95otherwise, it returns non-zero.
96.Pp
97The
98.Fn hdestroy

--- 28 unchanged lines hidden (view full) ---

127The following example reads in strings followed by two numbers
128and stores them in a hash table, discarding duplicates.
129It then reads in strings and finds the matching entry in the hash
130table and prints it out.
131.Bd -literal
132#include <stdio.h>
133#include <search.h>
134#include <string.h>
111.Sh RETURN VALUES
112The
113.Fn hcreate
114function returns 0 if it cannot allocate sufficient space for the table;
115otherwise, it returns non-zero.
116.Pp
117The
118.Fn hdestroy

--- 28 unchanged lines hidden (view full) ---

147The following example reads in strings followed by two numbers
148and stores them in a hash table, discarding duplicates.
149It then reads in strings and finds the matching entry in the hash
150table and prints it out.
151.Bd -literal
152#include <stdio.h>
153#include <search.h>
154#include <string.h>
155#include <stdlib.h>
135
136struct info { /* This is the info stored in the table */
137 int age, room; /* other than the key. */
138};
139
140#define NUM_EMPL 5000 /* # of elements in search table. */
141
142int
143main(void)
144{
156
157struct info { /* This is the info stored in the table */
158 int age, room; /* other than the key. */
159};
160
161#define NUM_EMPL 5000 /* # of elements in search table. */
162
163int
164main(void)
165{
145 char string_space[NUM_EMPL*20]; /* Space to store strings. */
166 char str[BUFSIZ]; /* Space to read string */
146 struct info info_space[NUM_EMPL]; /* Space to store employee info. */
167 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. */
149 ENTRY item;
150 ENTRY *found_item; /* Name to look for in table. */
151 char name_to_find[30];
152 int i = 0;
153
154 /* Create table; no error checking is performed. */
155 (void) hcreate(NUM_EMPL);
156
168 struct info *info_ptr = info_space; /* Next space in info_space. */
169 ENTRY item;
170 ENTRY *found_item; /* Name to look for in table. */
171 char name_to_find[30];
172 int i = 0;
173
174 /* Create table; no error checking is performed. */
175 (void) hcreate(NUM_EMPL);
176
157 while (scanf("%s%d%d", str_ptr, &info_ptr->age,
177 while (scanf("%s%d%d", str, &info_ptr->age,
158 &info_ptr->room) != EOF && i++ < NUM_EMPL) {
159 /* Put information in structure, and structure in item. */
178 &info_ptr->room) != EOF && i++ < NUM_EMPL) {
179 /* Put information in structure, and structure in item. */
160 item.key = str_ptr;
180 item.key = strdup(str);
161 item.data = info_ptr;
181 item.data = info_ptr;
162 str_ptr += strlen(str_ptr) + 1;
163 info_ptr++;
164 /* Put item into table. */
165 (void) hsearch(item, ENTER);
166 }
167
168 /* Access table. */
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",
174 found_item->key,
175 ((struct info *)found_item->data)->age,
176 ((struct info *)found_item->data)->room);
177 } else
178 (void)printf("no such employee %s\en", name_to_find);
179 }
182 info_ptr++;
183 /* Put item into table. */
184 (void) hsearch(item, ENTER);
185 }
186
187 /* Access table. */
188 item.key = name_to_find;
189 while (scanf("%s", item.key) != EOF) {
190 if ((found_item = hsearch(item, FIND)) != NULL) {
191 /* If item is in the table. */
192 (void)printf("found %s, age = %d, room = %d\en",
193 found_item->key,
194 ((struct info *)found_item->data)->age,
195 ((struct info *)found_item->data)->room);
196 } else
197 (void)printf("no such employee %s\en", name_to_find);
198 }
199 hdestroy();
180 return 0;
181}
182.Ed
183.Sh SEE ALSO
184.Xr bsearch 3 ,
185.Xr lsearch 3 ,
186.Xr malloc 3 ,
187.Xr strcmp 3 ,

--- 19 unchanged lines hidden ---
200 return 0;
201}
202.Ed
203.Sh SEE ALSO
204.Xr bsearch 3 ,
205.Xr lsearch 3 ,
206.Xr malloc 3 ,
207.Xr strcmp 3 ,

--- 19 unchanged lines hidden ---