]> git.saurik.com Git - apple/libc.git/blame - stdlib/FreeBSD/hcreate.3
Libc-339.tar.gz
[apple/libc.git] / stdlib / FreeBSD / hcreate.3
CommitLineData
9385eb3d 1.\" $FreeBSD: src/lib/libc/stdlib/hcreate.3,v 1.3 2003/03/12 14:18:14 dwmalone Exp $
5b2abdfb
A
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
9.Sh LIBRARY
10.Lb libc
11.Sh SYNOPSIS
12.In search.h
13.Ft int
14.Fn hcreate "size_t nel"
15.Ft void
16.Fn hdestroy void
17.Ft ENTRY *
18.Fn hsearch "ENTRY item" "ACTION action"
19.Sh DESCRIPTION
20The
21.Fn hcreate ,
22.Fn hdestroy ,
23and
24.Fn hsearch
25functions manage hash search tables.
26.Pp
27The
28.Fn hcreate
29function allocates sufficient space for the table, and the application should
30ensure it is called before
31.Fn hsearch
32is used.
33The
34.Fa nel
35argument is an estimate of the maximum
36number of entries that the table should contain.
37This number may be adjusted upward by the
38algorithm in order to obtain certain mathematically favorable circumstances.
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.
9385eb3d
A
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.
5b2abdfb
A
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
61argument is a structure of type
62.Vt ENTRY
63(defined in the
64.Aq Pa search.h
65header) containing two pointers:
66.Fa item.key
67points to the comparison key (a
68.Vt "char *" ) ,
69and
70.Fa item.data
71(a
72.Vt "void *" )
73points to any other data to be associated with
74that key.
75The comparison function used by
76.Fn hsearch
77is
78.Xr strcmp 3 .
79The
80.Fa action
81argument is a
82member of an enumeration type
83.Vt ACTION
84indicating the disposition of the entry if it cannot be
85found in the table.
86.Dv ENTER
87indicates that the
88.Fa item
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.
9385eb3d
A
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.
5b2abdfb
A
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
119function does not return a value.
120.Pp
121The
122.Fn hsearch
123function returns a
124.Dv NULL
125pointer if either the
126.Fa action
127is
128.Dv FIND
129and the
130.Fa item
131could not be found or the
132.Fa action
133is
134.Dv ENTER
135and the table is full.
136.Sh ERRORS
137The
138.Fn hcreate
139and
140.Fn hsearch
141functions may fail if:
142.Bl -tag -width Er
143.It Bq Er ENOMEM
144Insufficient storage space is available.
145.El
146.Sh EXAMPLES
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>
9385eb3d 155#include <stdlib.h>
5b2abdfb
A
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{
9385eb3d 166 char str[BUFSIZ]; /* Space to read string */
5b2abdfb 167 struct info info_space[NUM_EMPL]; /* Space to store employee info. */
5b2abdfb
A
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
9385eb3d 177 while (scanf("%s%d%d", str, &info_ptr->age,
5b2abdfb
A
178 &info_ptr->room) != EOF && i++ < NUM_EMPL) {
179 /* Put information in structure, and structure in item. */
9385eb3d 180 item.key = strdup(str);
5b2abdfb 181 item.data = info_ptr;
5b2abdfb
A
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 }
9385eb3d 199 hdestroy();
5b2abdfb
A
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 ,
208.Xr tsearch 3
209.Sh STANDARDS
210The
211.Fn hcreate ,
212.Fn hdestroy ,
213and
214.Fn hsearch
215functions conform to
216.St -xpg4.2 .
217.Sh HISTORY
218The
219.Fn hcreate ,
220.Fn hdestroy ,
221and
222.Fn hsearch
223functions first appeared in
224.At V .
225.Sh BUGS
226The interface permits the use of only one hash table at a time.