summaryrefslogtreecommitdiffstats
path: root/lib/libc/stdlib/hcreate.3
blob: 38743afb474695540df8171cb40663fabc4b0aea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
.\" $FreeBSD$
.\"
.Dd May 8, 2001
.Os
.Dt HCREATE 3
.Sh NAME
.Nm hcreate , hdestroy , hsearch
.Nd manage hash search table
.Sh LIBRARY
.Lb libc
.Sh SYNOPSIS
.In search.h
.Ft int
.Fn hcreate "size_t nel"
.Ft void
.Fn hdestroy void
.Ft ENTRY *
.Fn hsearch "ENTRY item" "ACTION action"
.Sh DESCRIPTION
The
.Fn hcreate ,
.Fn hdestroy ,
and
.Fn hsearch
functions manage hash search tables.
.Pp
The
.Fn hcreate
function allocates sufficient space for the table, and the application should
ensure it is called before
.Fn hsearch
is used.
The
.Fa nel
argument is an estimate of the maximum
number of entries that the table should contain.
This number may be adjusted upward by the
algorithm in order to obtain certain mathematically favorable circumstances.
.Pp
The
.Fn hdestroy
function disposes of the search table, and may be followed by another call to
.Fn hcreate .
After the call to
.Fn hdestroy ,
the data can no longer be considered accessible.
The
.Fn hdestroy
function calls
.Xr free 3
for each comparison key in the search table
but not the data item associated with the key.
.Pp
The
.Fn hsearch
function is a hash-table search routine.
It returns a pointer into a hash table
indicating the location at which an entry can be found.
The
.Fa item
argument is a structure of type
.Vt ENTRY
(defined in the
.In search.h
header) containing two pointers:
.Fa item.key
points to the comparison key (a
.Vt "char *" ) ,
and
.Fa item.data
(a
.Vt "void *" )
points to any other data to be associated with
that key.
The comparison function used by
.Fn hsearch
is
.Xr strcmp 3 .
The
.Fa action
argument is a
member of an enumeration type
.Vt ACTION
indicating the disposition of the entry if it cannot be
found in the table.
.Dv ENTER
indicates that the
.Fa item
should be inserted in the table at an
appropriate point.
.Dv FIND
indicates that no entry should be made.
Unsuccessful resolution is
indicated by the return of a
.Dv NULL
pointer.
.Pp
The comparison key (passed to
.Fn hsearch
as
.Fa item.key )
must be allocated using
.Xr malloc 3
if
.Fa action
is
.Dv ENTER
and
.Fn hdestroy
is called.
.Sh RETURN VALUES
The
.Fn hcreate
function returns 0 if it cannot allocate sufficient space for the table;
otherwise, it returns non-zero.
.Pp
The
.Fn hdestroy
function does not return a value.
.Pp
The
.Fn hsearch
function returns a
.Dv NULL
pointer if either the
.Fa action
is
.Dv FIND
and the
.Fa item
could not be found or the
.Fa action
is
.Dv ENTER
and the table is full.
.Sh EXAMPLES
The following example reads in strings followed by two numbers
and stores them in a hash table, discarding duplicates.
It then reads in strings and finds the matching entry in the hash
table and prints it out.
.Bd -literal
#include <stdio.h>
#include <search.h>
#include <string.h>
#include <stdlib.h>

struct info {			/* This is the info stored in the table */
	int age, room;		/* other than the key. */
};

#define NUM_EMPL	5000	/* # of elements in search table. */

int
main(void)
{
	char str[BUFSIZ]; /* Space to read string */
	struct info info_space[NUM_EMPL]; /* Space to store employee info. */
	struct info *info_ptr = info_space; /* Next space in info_space. */
	ENTRY item;
	ENTRY *found_item; /* Name to look for in table. */
	char name_to_find[30];
	int i = 0;

	/* Create table; no error checking is performed. */
	(void) hcreate(NUM_EMPL);

	while (scanf("%s%d%d", str, &info_ptr->age,
	    &info_ptr->room) != EOF && i++ < NUM_EMPL) {
		/* Put information in structure, and structure in item. */
		item.key = strdup(str);
		item.data = info_ptr;
		info_ptr++;
		/* Put item into table. */
		(void) hsearch(item, ENTER);
	}

	/* Access table. */
	item.key = name_to_find;
	while (scanf("%s", item.key) != EOF) {
		if ((found_item = hsearch(item, FIND)) != NULL) {
			/* If item is in the table. */
			(void)printf("found %s, age = %d, room = %d\en",
			    found_item->key,
			    ((struct info *)found_item->data)->age,
			    ((struct info *)found_item->data)->room);
		} else
			(void)printf("no such employee %s\en", name_to_find);
	}
	hdestroy();
	return 0;
}
.Ed
.Sh ERRORS
The
.Fn hcreate
and
.Fn hsearch
functions may fail if:
.Bl -tag -width Er
.It Bq Er ENOMEM
Insufficient storage space is available.
.El
.Sh SEE ALSO
.Xr bsearch 3 ,
.Xr lsearch 3 ,
.Xr malloc 3 ,
.Xr strcmp 3 ,
.Xr tsearch 3
.Sh STANDARDS
The
.Fn hcreate ,
.Fn hdestroy ,
and
.Fn hsearch
functions conform to
.St -xpg4.2 .
.Sh HISTORY
The
.Fn hcreate ,
.Fn hdestroy ,
and
.Fn hsearch
functions first appeared in
.At V .
.Sh BUGS
The interface permits the use of only one hash table at a time.
OpenPOWER on IntegriCloud