1 .\" Copyright 1993 Ulrich Drepper (drepper@karlsruhe.gmd.de)
2 .\" and Copyright 2008, Linux Foundation, written by Michael Kerrisk
3 .\" <mtk.manpages@gmail.com>
5 .\" This is free documentation; you can redistribute it and/or
6 .\" modify it under the terms of the GNU General Public License as
7 .\" published by the Free Software Foundation; either version 2 of
8 .\" the License, or (at your option) any later version.
10 .\" The GNU General Public License's references to "object code"
11 .\" and "executables" are to be interpreted as the output of any
12 .\" document formatting or typesetting system, including
13 .\" intermediate and printed output.
15 .\" This manual is distributed in the hope that it will be useful,
16 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
17 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 .\" GNU General Public License for more details.
20 .\" You should have received a copy of the GNU General Public
21 .\" License along with this manual; if not, see
22 .\" <http://www.gnu.org/licenses/>.
24 .\" References consulted:
25 .\" SunOS 4.1.1 man pages
26 .\" Modified Sat Sep 30 21:52:01 1995 by Jim Van Zandt <jrv@vanzandt.mv.com>
27 .\" Remarks from dhw@gamgee.acad.emich.edu Fri Jun 19 06:46:31 1998
28 .\" Modified 2001-12-26, 2003-11-28, 2004-05-20, aeb
29 .\" 2008-09-02, mtk: various additions and rewrites
30 .\" 2008-09-03, mtk, restructured somewhat, in part after suggestions from
31 .\" Timothy S. Nelson <wayland@wayland.id.au>
33 .TH HSEARCH 3 2011-09-10 "GNU" "Linux Programmer's Manual"
35 hcreate, hdestroy, hsearch, hcreate_r, hdestroy_r,
36 hsearch_r \- hash table management
39 .B #include <search.h>
41 .BI "int hcreate(size_t " nel );
43 .BI "ENTRY *hsearch(ENTRY " item ", ACTION " action );
45 .B "void hdestroy(void);"
47 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
49 .B #include <search.h>
51 .BI "int hcreate_r(size_t " nel ", struct hsearch_data *" htab );
53 .BI "int hsearch_r(ENTRY " item ", ACTION " action ", ENTRY **" retval ,
54 .BI " struct hsearch_data *" htab );
56 .BI "void hdestroy_r(struct hsearch_data *" htab );
64 allow the caller to create and manage a hash search table
65 containing entries consisting of a key (a string) and associated data.
66 Using these functions, only one hash table can be used at a time.
72 are reentrant versions that allow a program to use
73 more than one hash search table at the same time.
76 points to a structure that describes the table
77 on which the function is to operate.
78 The programmer should treat this structure as opaque
79 (i.e., do not attempt to directly access or modify
80 the fields in this structure).
82 First a hash table must be created using
84 The argument \fInel\fP specifies the maximum number of entries
86 (This maximum cannot be changed later, so choose it wisely.)
87 The implementation may adjust this value upward to improve the
88 performance of the resulting hash table.
89 .\" e.g., in glibc it is raised to the next higher prime number
93 function performs the same task as
95 but for the table described by the structure
97 The structure pointed to by
99 must be zeroed before the first call to
104 frees the memory occupied by the hash table that was created by
108 a new hash table can be created using
112 function performs the analogous task for a hash table described by
114 which was previously created using
119 function searches the hash table for an
120 item with the same key as \fIitem\fP (where "the same" is determined using
122 and if successful returns a pointer to it.
124 The argument \fIitem\fP is of type \fIENTRY\fP, which is defined in
125 \fI<search.h>\fP as follows:
129 typedef struct entry {
136 The field \fIkey\fP points to a null-terminated string which is the
138 The field \fIdata\fP points to data that is associated with that key.
140 The argument \fIaction\fP determines what
142 does after an unsuccessful search.
143 This argument must either have the value
145 meaning insert a copy of
147 (and return a pointer to the new hash table entry as the function result),
150 meaning that NULL should be returned.
163 but operates on the hash table described by
167 function differs from
169 in that a pointer to the found item is returned in
171 rather than as the function result.
176 return nonzero on success.
177 They return 0 on error.
181 returns a pointer to an entry in the hash table.
183 returns NULL on error, that is,
184 if \fIaction\fP is \fBENTER\fP and
185 the hash table is full, or \fIaction\fP is \fBFIND\fP and \fIitem\fP
186 cannot be found in the hash table.
188 returns nonzero on success, and 0 on error.
194 can fail for the following reasons:
203 can fail for the following reasons:
210 was not found in the table,
211 and there was no room in the table to add a new entry.
219 was not found in the table.
221 POSIX.1-2001 only specifies the
230 are from SVr4, and are described in POSIX.1-2001.
238 Hash table implementations are usually more efficient when the
239 table contains enough free space to minimize collisions.
240 Typically, this means that
242 should be at least 25% larger than the maximum number of elements
243 that the caller expects to store in the table.
249 functions do not free the buffers pointed to by the
253 elements of the hash table entries.
254 (It can't do this because it doesn't know
255 whether these buffers were allocated dynamically.)
256 If these buffers need to be freed (perhaps because the program
257 is repeatedly creating and destroying hash tables,
258 rather than creating a single table whose lifetime
259 matches that of the program),
260 then the program must maintain bookkeeping data structures that
261 allow it to free them.
263 SVr4 and POSIX.1-2001 specify that \fIaction\fP
264 is significant only for unsuccessful searches, so that an \fBENTER\fP
265 should not do anything for a successful search.
266 In libc and glibc (before version 2.3), the
267 implementation violates the specification,
268 updating the \fIdata\fP for the given \fIkey\fP in this case.
270 Individual hash table entries can be added, but not deleted.
273 The following program inserts 24 items into a hash table, then prints
281 char *data[] = { "alpha", "bravo", "charlie", "delta",
282 "echo", "foxtrot", "golf", "hotel", "india", "juliet",
283 "kilo", "lima", "mike", "november", "oscar", "papa",
284 "quebec", "romeo", "sierra", "tango", "uniform",
285 "victor", "whisky", "x\-ray", "yankee", "zulu"
296 for (i = 0; i < 24; i++) {
298 /* data is just an integer, instead of a
299 pointer to something */
301 ep = hsearch(e, ENTER);
302 /* there should be no failures */
304 fprintf(stderr, "entry failed\\n");
309 for (i = 22; i < 26; i++) {
310 /* print two entries from the table, and
311 show that two are not in the table */
313 ep = hsearch(e, FIND);
314 printf("%9.9s \-> %9.9s:%d\\n", e.key,
315 ep ? ep\->key : "NULL", ep ? (int)(ep\->data) : 0);