1 .\" Hey Emacs! This file is -*- nroff -*- source.
2 .\" Copyright 1993 Ulrich Drepper (drepper@karlsruhe.gmd.de)
4 .\" This is free documentation; you can redistribute it and/or
5 .\" modify it under the terms of the GNU General Public License as
6 .\" published by the Free Software Foundation; either version 2 of
7 .\" the License, or (at your option) any later version.
9 .\" The GNU General Public License's references to "object code"
10 .\" and "executables" are to be interpreted as the output of any
11 .\" document formatting or typesetting system, including
12 .\" intermediate and printed output.
14 .\" This manual is distributed in the hope that it will be useful,
15 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
16 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 .\" GNU General Public License for more details.
19 .\" You should have received a copy of the GNU General Public
20 .\" License along with this manual; if not, write to the Free
21 .\" Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111,
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
30 .TH HSEARCH 3 2004-05-20 "GNU" "Linux Programmer's Manual"
32 hcreate, hdestroy, hsearch \- hash table management
34 .B #include <search.h>
36 .BI "int hcreate(size_t " nel );
38 .BI "ENTRY *hsearch(ENTRY " item ", ACTION " action );
40 .B "void hdestroy(void);"
42 .B #define _GNU_SOURCE
44 .B #include <search.h>
46 .BI "int hcreate_r(size_t " nel ", struct hsearch_data *" tab );
48 .BI "int *hsearch_r(ENTRY " item ", ACTION " action ,
49 .BI "ENTRY **" ret ", struct hsearch_data *" tab );
51 .BI "void hdestroy_r(struct hsearch_data *" tab );
58 allow the user to create a hash table (only one at a time)
59 which associates a key with any data.
61 First the table must be created with the function \fBhcreate()\fP.
62 The argument \fInel\fP is an estimate of the maximum number of entries
64 The function \fBhcreate()\fP may adjust this value upward to improve the
65 performance of the resulting hash table.
67 The corresponding function \fBhdestroy()\fP frees the memory occupied by
68 the hash table so that a new table can be constructed.
70 The argument \fIitem\fP is of type \fBENTRY\fP, which is a typedef defined in
71 \fI<search.h>\fP and includes these elements:
74 typedef struct entry {
80 The field \fIkey\fP points to the NUL-terminated string which is the
82 The field \fIdata\fP points to the data associated with that key.
83 The function \fBhsearch()\fP searches the hash table for an
84 item with the same key as \fIitem\fP (where "the same" is determined using
86 and if successful returns a pointer to it.
87 The argument \fIaction\fP determines what \fBhsearch()\fP does
88 after an unsuccessful search. A value of \fBENTER\fP instructs it to
89 insert a copy of \fIitem\fP, while a value of \fBFIND\fP means to return
96 are reentrant versions that allow the use of more than one table.
97 The last argument used identifies the table. The struct it points to
98 must be zeroed before the first call to
101 \fBhcreate()\fP and \fBhcreate_r()\fP return 0 when allocation of the memory
102 for the hash table fails, non-zero otherwise.
104 \fBhsearch()\fP returns \fBNULL\fP if \fIaction\fP is \fBENTER\fP and
105 the hash table is full, or \fIaction\fP is \fBFIND\fP and \fIitem\fP
106 cannot be found in the hash table.
108 \fBhsearch_r()\fP returns 0 if \fIaction\fP is \fBENTER\fP and
109 the hash table is full, and non-zero otherwise.
116 The glibc implementation will return the following two errors.
119 Table full with \fIaction\fP set to \fBENTER\fP.
122 The \fIaction\fP parameter is \fBFIND\fP and no corresponding element
123 is found in the table.
130 are from SVID, and are described in POSIX 1003.1-2001.
137 SVID and POSIX 1003.1-2001 specify that \fIaction\fP
138 is significant only for unsuccessful searches, so that an ENTER
139 should not do anything for a successful search. The libc and glibc
140 implementations update the \fIdata\fP for the given \fIkey\fP
142 .\" Tue Jan 29 09:27:40 2002: fixed in latest glibc snapshot
144 Individual hash table entries can be added, but not deleted.
147 The following program inserts 24 items in to a hash table, then prints
155 char *data[] = { "alpha", "bravo", "charlie", "delta",
156 "echo", "foxtrot", "golf", "hotel", "india", "juliet",
157 "kilo", "lima", "mike", "november", "oscar", "papa",
158 "quebec", "romeo", "sierra", "tango", "uniform",
159 "victor", "whisky", "x-ray", "yankee", "zulu"
166 /* starting with small table, and letting it grow does not work */
168 for (i = 0; i < 24; i++) {
170 /* data is just an integer, instead of a
171 pointer to something */
173 ep = hsearch(e, ENTER);
174 /* there should be no failures */
176 fprintf(stderr, "entry failed\\n");
180 for (i = 22; i < 26; i++) {
181 /* print two entries from the table, and
182 show that two are not in the table */
184 ep = hsearch(e, FIND);
185 printf("%9.9s \-> %9.9s:%d\\n", e.key,
186 ep ? ep\->key : "NULL",
187 ep ? (int)(ep->data) : 0);