]>
Commit | Line | Data |
---|---|---|
1f2f436a A |
1 | .\"- |
2 | .\" Copyright (c) 1999 The NetBSD Foundation, Inc. | |
3 | .\" All rights reserved. | |
5b2abdfb | 4 | .\" |
1f2f436a A |
5 | .\" This code is derived from software contributed to The NetBSD Foundation |
6 | .\" by Klaus Klein. | |
7 | .\" | |
8 | .\" Redistribution and use in source and binary forms, with or without | |
9 | .\" modification, are permitted provided that the following conditions | |
10 | .\" are met: | |
11 | .\" 1. Redistributions of source code must retain the above copyright | |
12 | .\" notice, this list of conditions and the following disclaimer. | |
13 | .\" 2. Redistributions in binary form must reproduce the above copyright | |
14 | .\" notice, this list of conditions and the following disclaimer in the | |
15 | .\" documentation and/or other materials provided with the distribution. | |
16 | .\" | |
17 | .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS | |
18 | .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | |
19 | .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
20 | .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | |
21 | .\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
22 | .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
23 | .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
24 | .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
25 | .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
26 | .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
27 | .\" POSSIBILITY OF SUCH DAMAGE. | |
28 | .\" | |
29 | .\" $FreeBSD: src/lib/libc/stdlib/hcreate.3,v 1.7 2008/07/06 17:03:37 danger Exp $ | |
30 | .\" | |
31 | .Dd July 6, 2008 | |
5b2abdfb A |
32 | .Os |
33 | .Dt HCREATE 3 | |
34 | .Sh NAME | |
35 | .Nm hcreate , hdestroy , hsearch | |
36 | .Nd manage hash search table | |
37 | .Sh LIBRARY | |
38 | .Lb libc | |
39 | .Sh SYNOPSIS | |
40 | .In search.h | |
41 | .Ft int | |
42 | .Fn hcreate "size_t nel" | |
43 | .Ft void | |
44 | .Fn hdestroy void | |
45 | .Ft ENTRY * | |
46 | .Fn hsearch "ENTRY item" "ACTION action" | |
47 | .Sh DESCRIPTION | |
48 | The | |
49 | .Fn hcreate , | |
50 | .Fn hdestroy , | |
51 | and | |
52 | .Fn hsearch | |
53 | functions manage hash search tables. | |
54 | .Pp | |
55 | The | |
56 | .Fn hcreate | |
57 | function allocates sufficient space for the table, and the application should | |
58 | ensure it is called before | |
59 | .Fn hsearch | |
60 | is used. | |
61 | The | |
62 | .Fa nel | |
63 | argument is an estimate of the maximum | |
64 | number of entries that the table should contain. | |
65 | This number may be adjusted upward by the | |
66 | algorithm in order to obtain certain mathematically favorable circumstances. | |
67 | .Pp | |
68 | The | |
69 | .Fn hdestroy | |
70 | function disposes of the search table, and may be followed by another call to | |
71 | .Fn hcreate . | |
72 | After the call to | |
73 | .Fn hdestroy , | |
74 | the data can no longer be considered accessible. | |
9385eb3d A |
75 | The |
76 | .Fn hdestroy | |
77 | function calls | |
78 | .Xr free 3 | |
79 | for each comparison key in the search table | |
80 | but not the data item associated with the key. | |
5b2abdfb A |
81 | .Pp |
82 | The | |
83 | .Fn hsearch | |
84 | function is a hash-table search routine. | |
85 | It returns a pointer into a hash table | |
86 | indicating the location at which an entry can be found. | |
87 | The | |
88 | .Fa item | |
89 | argument is a structure of type | |
90 | .Vt ENTRY | |
91 | (defined in the | |
3d9156a7 | 92 | .In search.h |
5b2abdfb A |
93 | header) containing two pointers: |
94 | .Fa item.key | |
95 | points to the comparison key (a | |
96 | .Vt "char *" ) , | |
97 | and | |
98 | .Fa item.data | |
99 | (a | |
100 | .Vt "void *" ) | |
101 | points to any other data to be associated with | |
102 | that key. | |
103 | The comparison function used by | |
104 | .Fn hsearch | |
105 | is | |
106 | .Xr strcmp 3 . | |
107 | The | |
108 | .Fa action | |
109 | argument is a | |
110 | member of an enumeration type | |
111 | .Vt ACTION | |
112 | indicating the disposition of the entry if it cannot be | |
113 | found in the table. | |
114 | .Dv ENTER | |
115 | indicates that the | |
116 | .Fa item | |
117 | should be inserted in the table at an | |
118 | appropriate point. | |
119 | .Dv FIND | |
120 | indicates that no entry should be made. | |
121 | Unsuccessful resolution is | |
122 | indicated by the return of a | |
123 | .Dv NULL | |
124 | pointer. | |
9385eb3d A |
125 | .Pp |
126 | The comparison key (passed to | |
127 | .Fn hsearch | |
128 | as | |
129 | .Fa item.key ) | |
130 | must be allocated using | |
131 | .Xr malloc 3 | |
132 | if | |
133 | .Fa action | |
134 | is | |
135 | .Dv ENTER | |
136 | and | |
137 | .Fn hdestroy | |
138 | is called. | |
5b2abdfb A |
139 | .Sh RETURN VALUES |
140 | The | |
141 | .Fn hcreate | |
1f2f436a A |
142 | function returns 0 if the table creation failed and the global variable |
143 | .Va errno | |
144 | is set to indicate the error; | |
145 | otherwise, a non-zero value is returned. | |
5b2abdfb A |
146 | .Pp |
147 | The | |
148 | .Fn hdestroy | |
149 | function does not return a value. | |
150 | .Pp | |
151 | The | |
152 | .Fn hsearch | |
153 | function returns a | |
154 | .Dv NULL | |
155 | pointer if either the | |
156 | .Fa action | |
157 | is | |
158 | .Dv FIND | |
159 | and the | |
160 | .Fa item | |
161 | could not be found or the | |
162 | .Fa action | |
163 | is | |
164 | .Dv ENTER | |
165 | and the table is full. | |
5b2abdfb A |
166 | .Sh EXAMPLES |
167 | The following example reads in strings followed by two numbers | |
168 | and stores them in a hash table, discarding duplicates. | |
169 | It then reads in strings and finds the matching entry in the hash | |
170 | table and prints it out. | |
171 | .Bd -literal | |
172 | #include <stdio.h> | |
173 | #include <search.h> | |
174 | #include <string.h> | |
9385eb3d | 175 | #include <stdlib.h> |
5b2abdfb A |
176 | |
177 | struct info { /* This is the info stored in the table */ | |
178 | int age, room; /* other than the key. */ | |
179 | }; | |
180 | ||
181 | #define NUM_EMPL 5000 /* # of elements in search table. */ | |
182 | ||
183 | int | |
184 | main(void) | |
185 | { | |
9385eb3d | 186 | char str[BUFSIZ]; /* Space to read string */ |
5b2abdfb | 187 | struct info info_space[NUM_EMPL]; /* Space to store employee info. */ |
5b2abdfb A |
188 | struct info *info_ptr = info_space; /* Next space in info_space. */ |
189 | ENTRY item; | |
190 | ENTRY *found_item; /* Name to look for in table. */ | |
191 | char name_to_find[30]; | |
192 | int i = 0; | |
193 | ||
194 | /* Create table; no error checking is performed. */ | |
195 | (void) hcreate(NUM_EMPL); | |
196 | ||
9385eb3d | 197 | while (scanf("%s%d%d", str, &info_ptr->age, |
5b2abdfb A |
198 | &info_ptr->room) != EOF && i++ < NUM_EMPL) { |
199 | /* Put information in structure, and structure in item. */ | |
9385eb3d | 200 | item.key = strdup(str); |
5b2abdfb | 201 | item.data = info_ptr; |
5b2abdfb A |
202 | info_ptr++; |
203 | /* Put item into table. */ | |
204 | (void) hsearch(item, ENTER); | |
205 | } | |
206 | ||
207 | /* Access table. */ | |
208 | item.key = name_to_find; | |
209 | while (scanf("%s", item.key) != EOF) { | |
210 | if ((found_item = hsearch(item, FIND)) != NULL) { | |
211 | /* If item is in the table. */ | |
212 | (void)printf("found %s, age = %d, room = %d\en", | |
213 | found_item->key, | |
214 | ((struct info *)found_item->data)->age, | |
215 | ((struct info *)found_item->data)->room); | |
216 | } else | |
217 | (void)printf("no such employee %s\en", name_to_find); | |
218 | } | |
9385eb3d | 219 | hdestroy(); |
5b2abdfb A |
220 | return 0; |
221 | } | |
222 | .Ed | |
1f2f436a A |
223 | .Sh ERRORS |
224 | The | |
225 | .Fn hcreate | |
226 | and | |
227 | .Fn hsearch | |
228 | functions may fail if: | |
229 | .Bl -tag -width Er | |
230 | .It Bq Er ENOMEM | |
231 | Insufficient storage space is available. | |
232 | .It Bq Er EINVAL | |
233 | A table already exists. | |
234 | .El | |
5b2abdfb A |
235 | .Sh SEE ALSO |
236 | .Xr bsearch 3 , | |
237 | .Xr lsearch 3 , | |
238 | .Xr malloc 3 , | |
239 | .Xr strcmp 3 , | |
240 | .Xr tsearch 3 | |
241 | .Sh STANDARDS | |
242 | The | |
243 | .Fn hcreate , | |
244 | .Fn hdestroy , | |
245 | and | |
246 | .Fn hsearch | |
247 | functions conform to | |
248 | .St -xpg4.2 . | |
249 | .Sh HISTORY | |
250 | The | |
251 | .Fn hcreate , | |
252 | .Fn hdestroy , | |
253 | and | |
254 | .Fn hsearch | |
255 | functions first appeared in | |
256 | .At V . | |
257 | .Sh BUGS | |
258 | The interface permits the use of only one hash table at a time. |