xref: /dragonfly/lib/libc/stdlib/hcreate.3 (revision 0bb9290e)
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 $
3.\"
4.Dd May 8, 2001
5.Os
6.Dt HCREATE 3
7.Sh NAME
8.Nm hcreate ,
9.Nm hdestroy ,
10.Nm hsearch
11.Nd manage hash search table
12.Sh LIBRARY
13.Lb libc
14.Sh SYNOPSIS
15.In search.h
16.Ft int
17.Fn hcreate "size_t nel"
18.Ft void
19.Fn hdestroy void
20.Ft ENTRY *
21.Fn hsearch "ENTRY item" "ACTION action"
22.Sh DESCRIPTION
23The
24.Fn hcreate ,
25.Fn hdestroy ,
26and
27.Fn hsearch
28functions manage hash search tables.
29.Pp
30The
31.Fn hcreate
32function allocates sufficient space for the table, and the application should
33ensure it is called before
34.Fn hsearch
35is used.
36The
37.Fa nel
38argument is an estimate of the maximum
39number of entries that the table should contain.
40This number may be adjusted upward by the
41algorithm in order to obtain certain mathematically favorable circumstances.
42.Pp
43The
44.Fn hdestroy
45function disposes of the search table, and may be followed by another call to
46.Fn hcreate .
47After the call to
48.Fn hdestroy ,
49the data can no longer be considered accessible.
50The
51.Fn hdestroy
52function calls
53.Xr free 3
54for each comparison key in the search table
55but not the data item associated with the key.
56.Pp
57The
58.Fn hsearch
59function is a hash-table search routine.
60It returns a pointer into a hash table
61indicating the location at which an entry can be found.
62The
63.Fa item
64argument is a structure of type
65.Vt ENTRY
66(defined in the
67.In search.h
68header) containing two pointers:
69.Fa item.key
70points to the comparison key (a
71.Vt "char *" ) ,
72and
73.Fa item.data
74(a
75.Vt "void *" )
76points to any other data to be associated with
77that key.
78The comparison function used by
79.Fn hsearch
80is
81.Xr strcmp 3 .
82The
83.Fa action
84argument is a
85member of an enumeration type
86.Vt ACTION
87indicating the disposition of the entry if it cannot be
88found in the table.
89.Dv ENTER
90indicates that the
91.Fa item
92should be inserted in the table at an
93appropriate point.
94.Dv FIND
95indicates that no entry should be made.
96Unsuccessful resolution is
97indicated by the return of a
98.Dv NULL
99pointer.
100.Pp
101The comparison key (passed to
102.Fn hsearch
103as
104.Fa item.key )
105must be allocated using
106.Xr malloc 3
107if
108.Fa action
109is
110.Dv ENTER
111and
112.Fn hdestroy
113is called.
114.Sh RETURN VALUES
115The
116.Fn hcreate
117function returns 0 if it cannot allocate sufficient space for the table;
118otherwise, it returns non-zero.
119.Pp
120The
121.Fn hdestroy
122function does not return a value.
123.Pp
124The
125.Fn hsearch
126function returns a
127.Dv NULL
128pointer if either the
129.Fa action
130is
131.Dv FIND
132and the
133.Fa item
134could not be found or the
135.Fa action
136is
137.Dv ENTER
138and the table is full.
139.Sh EXAMPLES
140The following example reads in strings followed by two numbers
141and stores them in a hash table, discarding duplicates.
142It then reads in strings and finds the matching entry in the hash
143table and prints it out.
144.Bd -literal
145#include <stdio.h>
146#include <search.h>
147#include <string.h>
148#include <stdlib.h>
149
150struct info {			/* This is the info stored in the table */
151	int age, room;		/* other than the key. */
152};
153
154#define NUM_EMPL	5000	/* # of elements in search table. */
155
156int
157main(void)
158{
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. */
162	ENTRY item;
163	ENTRY *found_item; /* Name to look for in table. */
164	char name_to_find[30];
165	int i = 0;
166
167	/* Create table; no error checking is performed. */
168	(void) hcreate(NUM_EMPL);
169
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;
175		info_ptr++;
176		/* Put item into table. */
177		(void) hsearch(item, ENTER);
178	}
179
180	/* Access table. */
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",
186			    found_item->key,
187			    ((struct info *)found_item->data)->age,
188			    ((struct info *)found_item->data)->room);
189		} else
190			(void)printf("no such employee %s\en", name_to_find);
191	}
192	hdestroy();
193	return 0;
194}
195.Ed
196.Sh ERRORS
197The
198.Fn hcreate
199and
200.Fn hsearch
201functions may fail if:
202.Bl -tag -width Er
203.It Bq Er ENOMEM
204Insufficient storage space is available.
205.El
206.Sh SEE ALSO
207.Xr bsearch 3 ,
208.Xr lsearch 3 ,
209.Xr malloc 3 ,
210.Xr strcmp 3 ,
211.Xr tsearch 3
212.Sh STANDARDS
213The
214.Fn hcreate ,
215.Fn hdestroy ,
216and
217.Fn hsearch
218functions conform to
219.St -xpg4.2 .
220.Sh HISTORY
221The
222.Fn hcreate ,
223.Fn hdestroy ,
224and
225.Fn hsearch
226functions first appeared in
227.At V .
228.Sh BUGS
229The interface permits the use of only one hash table at a time.
230