OSDN Git Service

097d4d66ef6da898dda76ecfa67c76f70e13ee2b
[linuxjm/LDP_man-pages.git] / draft / man3 / hsearch.3
1 .\" Hey Emacs! This file is -*- nroff -*- source.
2 .\" Copyright 1993 Ulrich Drepper (drepper@karlsruhe.gmd.de)
3 .\" and Copyright 2008, Linux Foundation, written by Michael Kerrisk
4 .\"     <mtk.manpages@gmail.com>
5 .\"
6 .\" This is free documentation; you can redistribute it and/or
7 .\" modify it under the terms of the GNU General Public License as
8 .\" published by the Free Software Foundation; either version 2 of
9 .\" the License, or (at your option) any later version.
10 .\"
11 .\" The GNU General Public License's references to "object code"
12 .\" and "executables" are to be interpreted as the output of any
13 .\" document formatting or typesetting system, including
14 .\" intermediate and printed output.
15 .\"
16 .\" This manual is distributed in the hope that it will be useful,
17 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
18 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 .\" GNU General Public License for more details.
20 .\"
21 .\" You should have received a copy of the GNU General Public
22 .\" License along with this manual; if not, write to the Free
23 .\" Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111,
24 .\" USA.
25 .\"
26 .\" References consulted:
27 .\"     SunOS 4.1.1 man pages
28 .\" Modified Sat Sep 30 21:52:01 1995 by Jim Van Zandt <jrv@vanzandt.mv.com>
29 .\" Remarks from dhw@gamgee.acad.emich.edu Fri Jun 19 06:46:31 1998
30 .\" Modified 2001-12-26, 2003-11-28, 2004-05-20, aeb
31 .\" 2008-09-02, mtk: various additions and rewrites
32 .\" 2008-09-03, mtk, restructured somewhat, in part after suggestions from
33 .\"     Timothy S. Nelson <wayland@wayland.id.au>
34 .\"
35 .\" Japanese Version Copyright (c) 1998 George Momma,
36 .\"     Copyright (c) 2001-2005 Yuichi SATO,
37 .\"     and Copyright (c) 2008 Akihiro MOTOKI
38 .\" Translated 1998-05-23, George Momma <momma@wakhok.ac.jp>
39 .\" Updated & Modified 2001-10-15, Yuichi SATO <ysato@h4.dion.ne.jp>
40 .\" Updated & Modified 2002-01-03, Yuichi SATO
41 .\" Updated & Modified 2004-01-17, Yuichi SATO <ysato444@yahoo.co.jp>
42 .\" Updated & Modified 2005-01-10, Yuichi SATO
43 .\" Updated 2008-09-20, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
44 .\"
45 .\"WORD:        hash table              ¥Ï¥Ã¥·¥å¥Æ¡¼¥Ö¥ë
46 .\"WORD:        entry                   ¥¨¥ó¥È¥ê¡¼
47 .\"WORD:        allocate                ³ä¤êÅö¤Æ
48 .\"WORD:        NUL-terminated          ¥Ì¥ëʸ»ú \0 ¤Ç½ªÃ¼¤µ¤ì¤¿
49 .\"WORD:        pointer                 ¥Ý¥¤¥ó¥¿
50 .\"WORD:        character               Ê¸»ú·¿
51 .\"WORD:        item                    ¹àÌÜ
52 .\"
53 .TH HSEARCH 3 2008-10-06 "GNU" "Linux Programmer's Manual"
54 .\"O .SH NAME
55 .SH Ì¾Á°
56 .\"O hcreate, hdestroy, hsearch, hcreate_r, hdestroy_r,
57 .\"O hsearch_r \- hash table management
58 hcreate, hdestroy, hsearch, hcreate_r, hdestroy_r,
59 hsearch_r \- ¥Ï¥Ã¥·¥å¥Æ¡¼¥Ö¥ë¤Î´ÉÍý
60 .\"O .SH SYNOPSIS
61 .SH ½ñ¼°
62 .nf
63 .B #include <search.h>
64 .sp
65 .BI "int hcreate(size_t " nel );
66 .sp
67 .BI "ENTRY *hsearch(ENTRY " item ", ACTION " action );
68 .sp
69 .B "void hdestroy(void);"
70 .sp
71 .B #define _GNU_SOURCE
72 .br
73 .B #include <search.h>
74 .sp
75 .BI "int hcreate_r(size_t " nel ", struct hsearch_data *" htab );
76 .sp
77 .BI "int hsearch_r(ENTRY " item ", ACTION " action ", ENTRY **" retval ,
78 .BI "              struct hsearch_data *" htab );
79 .sp
80 .BI "void hdestroy_r(struct hsearch_data *" htab );
81 .fi
82 .\"O .SH DESCRIPTION
83 .SH ÀâÌÀ
84 .\"O The three functions
85 .\"O .BR hcreate (),
86 .\"O .BR hsearch (),
87 .\"O and
88 .\"O .BR hdestroy ()
89 .\"O allow the caller to create and manage a hash search table
90 .\"O containing entries consisting of a key (a string) and associated data.
91 .\"O Using these functions, only one hash table can be used at a time.
92 .BR hcreate (),
93 .BR hsearch (),
94 .BR hdestroy ()
95 ¤Î 3 ¤Ä¤Î´Ø¿ô¤òÍøÍѤ¹¤ë¤È¡¢¥­¡¼ (ʸ»úÎó) ¤ÈÂбþ¤¹¤ë¥Ç¡¼¥¿¤«¤é¹½À®¤µ¤ì¤ë
96 ¥¨¥ó¥È¥ê¤ò³ÊǼ¤Ç¤­¤ë¥Ï¥Ã¥·¥å¸¡º÷¥Æ¡¼¥Ö¥ë¤òºîÀ®¡¢´ÉÍý¤¹¤ë¤³¤È¤¬¤Ç¤­¤ë¡£
97 ¤³¤ì¤é¤Î´Ø¿ô¤ò»È¤Ã¤Æ¡¢°ìÅ٤˻ÈÍѤǤ­¤ë¤Î¤Ï°ì¤Ä¤Î¥Ï¥Ã¥·¥å¥Æ¡¼¥Ö¥ë¤À¤±¤Ç¤¢¤ë¡£
98
99 .\"O The three functions
100 .\"O .BR hcreate_r (),
101 .\"O .BR hsearch_r (),
102 .\"O .BR hdestroy_r ()
103 .\"O are reentrant versions that allow a program to use
104 .\"O more than one hash search table at the same time.
105 .\"O The last argument,
106 .\"O .IR htab ,
107 .\"O points to a structure that describes the table
108 .\"O on which the function is to operate.
109 .\"O The programmer should treat this structure as opaque
110 .\"O (i.e., do not attempt to directly access or modify
111 .\"O the fields in this structure).
112 .BR hcreate_r (),
113 .BR hsearch_r (),
114 .BR hdestroy_r ()
115 ¤Î 3 ¤Ä¤Î´Ø¿ô¤Ï¥ê¥¨¥ó¥È¥é¥ó¥ÈÈǤǡ¢¤³¤ì¤é¤òÍøÍѤ¹¤ë¤È¡¢
116 °ì¤Ä¤Î¥×¥í¥°¥é¥à¤ÇƱ»þ¤ËÊ£¿ô¤Î¥Ï¥Ã¥·¥å¥Æ¡¼¥Ö¥ë¤ò»È¤¦¤³¤È¤¬¤Ç¤­¤ë¡£
117 ºÇ¸å¤Î°ú¤­¿ô
118 .I htab
119 ¤Ï´Ø¿ô¤ÎÁàºîÂоݤȤʤë¥Æ¡¼¥Ö¥ë¤ò¼¨¤¹¹½Â¤ÂΤؤΥݥ¤¥ó¥¿¤Ç¤¢¤ë¡£
120 ¥×¥í¥°¥é¥Þ¤Ï¤³¤Î¹½Â¤ÂΤò¥Ö¥é¥Ã¥¯¥Ü¥Ã¥¯¥¹¤È¤·¤Æ°·¤¦¤Ù¤­¤Ç¤¢¤ë
121 (¤Ä¤Þ¤ê¡¢¤³¤Î¹½Â¤ÂΤΥե£¡¼¥ë¥É¤ËľÀÜ¥¢¥¯¥»¥¹¤·¤¿¤êÊѹ¹¤·¤¿¤ê
122 ¤·¤Ê¤¤¤³¤È)¡£
123
124 .\"O First a hash table must be created using
125 .\"O .BR hcreate ().
126 .\"O The argument \fInel\fP specifies the maximum number of entries
127 .\"O in the table.
128 .\"O (This maximum cannot be changed later, so choose it wisely.)
129 .\"O The implementation may adjust this value upward to improve the
130 .\"O performance of the resulting hash table.
131 ºÇ½é¤Ë¡¢
132 .BR hcreate ()
133 ´Ø¿ô¤Ë¤è¤Ã¤Æ¥Ï¥Ã¥·¥å¥Æ¡¼¥Ö¥ë¤òºîÀ®¤·¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
134 °ú¤­¿ô \fInel\fP ¤Ç¥Æ¡¼¥Ö¥ë¤ÎºÇÂ票¥ó¥È¥ê¿ô¤ò»ØÄꤹ¤ë
135 (¤³¤ÎºÇÂçÃͤϸå¤ÇÊѹ¹¤¹¤ë¤³¤È¤Ï¤Ç¤­¤Ê¤¤¤Î¤Ç¡¢¤è¤¯¹Í¤¨¤ÆÁªÂò¤¹¤ë¤³¤È)¡£
136 ºîÀ®¤µ¤ì¤ë¥Ï¥Ã¥·¥å¥Æ¡¼¥Ö¥ë¤ÎÀ­Ç½¤ò¸þ¾å¤µ¤»¤ë¤¿¤á¤Ë¡¢
137 ´Ø¿ôÆâÉô¤Î¼ÂÁõ¤Ë¤è¤ê¤³¤ÎÃͤÏÁý¤ä¤µ¤ì¤ë¾ì¹ç¤â¤¢¤ë¡£
138 .\" e.g., in glibc it is raised to the next higher prime number
139
140 .\"O The
141 .\"O .BR hcreate_r ()
142 .\"O function performs the same task as
143 .\"O .BR hcreate (),
144 .\"O but for the table described by the structure
145 .\"O .IR *htab .
146 .\"O The structure pointed to by
147 .\"O .I htab
148 .\"O must be zeroed before the first call to
149 .\"O .BR hcreate_r ().
150 .BR hcreate_r ()
151 ´Ø¿ô¤Ï
152 .BR hcreate ()
153 ¤ÈƱ¤¸Æ°ºî¤ò¤¹¤ë¤¬¡¢¹½Â¤ÂÎ
154 .I *htab
155 ¤Ç¼¨¤µ¤ì¤ë¥Æ¡¼¥Ö¥ë¤òÂоݤȤ·¤ÆÆ°ºî¤¹¤ë¡£
156 .I htab
157 ¤¬»Ø¤·¼¨¤¹¹½Â¤ÂΤϡ¢
158 .BR hcreate_r ()
159 ¤ò½é¤á¤Æ¸Æ¤Ó½Ð¤¹Á°¤Ë 0 ¤ÇËä¤á¤Æ¤ª¤«¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
160
161 .\"O The function
162 .\"O .BR hdestroy ()
163 .\"O frees the memory occupied by the hash table that was created by
164 .\"O .BR hcreate ().
165 .\"O After calling
166 .\"O .BR hdestroy ()
167 .\"O a new hash table can be created using
168 .\"O .BR hcreate ().
169 .\"O The
170 .\"O .BR hdestroy_r ()
171 .\"O function performs the analogous task for a hash table described by
172 .\"O .IR *htab ,
173 .\"O which was previously created using
174 .\"O .BR hcreate_r ().
175 .BR hdestroy ()
176 ´Ø¿ô¤Ï¡¢
177 .BR hcreate ()
178 ¤ÇºîÀ®¤µ¤ì¤¿¥Ï¥Ã¥·¥å¥Æ¡¼¥Ö¥ë¤¬ÀêÍ­¤·¤Æ¤¤¤¿¥á¥â¥ê¤ò²òÊü¤¹¤ë¡£
179 ¥Ï¥Ã¥·¥å¥Æ¡¼¥Ö¥ë¤Ë¤è¤Ã¤ÆÀêÍ­¤µ¤ì¤Æ¤¤¤¿¥á¥â¥ê¤ò²òÊü¤·¡¢
180 ¿·¤·¤¤¥Æ¡¼¥Ö¥ë¤òºîÀ®¤Ç¤­¤ë¤è¤¦¤Ë¤¹¤ë¡£
181 .BR hdestroy ()
182 ¤ò¸Æ¤Ó½Ð¤¹¤È¡¢¤½¤Î¸å¤Ï
183 .BR hcreate ()
184 ¤ò»È¤Ã¤Æ¿·¤·¤¤¥Ï¥Ã¥·¥å¥Æ¡¼¥Ö¥ë¤òºîÀ®¤¹¤ë¤³¤È¤¬¤Ç¤­¤ë¡£
185 .BR hdestroy_r ()
186 ´Ø¿ô¤Ï¡¢Æ±ÍͤνèÍý¤ò¡¢¤½¤ì°ÊÁ°¤Ë
187 .BR hcreate_r ()
188 ¤ò»È¤Ã¤ÆºîÀ®¤·¤¿
189 .I *htab
190 ¤Ç¼¨¤µ¤ì¤ë¥Ï¥Ã¥·¥å¥Æ¡¼¥Ö¥ë¤ËÂФ·¤Æ¼Â¹Ô¤¹¤ë¡£
191
192 .\"O The
193 .\"O .BR hsearch ()
194 .\"O function searches the hash table for an
195 .\"O item with the same key as \fIitem\fP (where "the same" is determined using
196 .\"O .BR strcmp (3)),
197 .\"O and if successful returns a pointer to it.
198 .BR hsearch ()
199 ´Ø¿ô¤Ï¡¢\fIitem\fP ¤ÈƱ¤¸¥­¡¼¤ò»ý¤Ä¹àÌܤò¥Ï¥Ã¥·¥å¥Æ¡¼¥Ö¥ë¤«¤é
200 ¸¡º÷¤·¡¢¹àÌܤ¬¸«¤Ä¤«¤Ã¤¿¾ì¹ç¤Ë¤Ï¤½¤Î¹àÌܤؤΥݥ¤¥ó¥¿¤òÊÖ¤¹
201 (¡ÖƱ¤¸¡×¤«¤É¤¦¤«¤Ï
202 .BR strcmp (3)
203 ¤ò»È¤Ã¤ÆȽÄꤹ¤ë)¡£
204
205 .\"O The argument \fIitem\fP is of type \fIENTRY\fP, which is defined in
206 .\"O \fI<search.h>\fP as follows:
207 °ú¤­¿ô \fIitem\fP ¤Ï \fBENTRY\fP ·¿¤Ç¤¢¤ê¡¢\fI<search.h>\fP ¤ÎÃæ¤Ç
208 °Ê²¼¤Î¤è¤¦¤ËÄêµÁ¤µ¤ì¤Æ¤¤¤ë¡£
209 .in +4n
210 .sp
211 .nf
212 typedef struct entry {
213     char *key;
214     void *data;
215 } ENTRY;
216 .in
217 .fi
218 .sp
219 .\"O The field \fIkey\fP points to a null-terminated string which is the
220 .\"O search key.
221 .\"O The field \fIdata\fP points to data that is associated with that key.
222 ¥Õ¥£¡¼¥ë¥É \fIkey\fP ¤Ï¸¡º÷¥­¡¼¤È¤Ê¤ë NULL ½ªÃ¼¤µ¤ì¤¿Ê¸»úÎó¤ò»Ø¤¹¡£
223 ¥Õ¥£¡¼¥ë¥É \fIdata\fP ¤Ï¡¢¤³¤Î¥­¡¼¤ËÂбþ¤¹¤ë¥Ç¡¼¥¿¤ò»Ø¤¹¡£
224
225 .\"O The argument \fIaction\fP determines what
226 .\"O .BR hsearch ()
227 .\"O does after an unsuccessful search.
228 ¸¡º÷¤¬¼ºÇÔ¤·¤¿¸å¤ÎÆ°ºî¤Ï¡¢°ú¤­¿ô \fIaction\fP ¤Ë¤è¤ê·è¤Þ¤ë¡£
229 .\"O This argument must either have the value
230 .\"O .BR ENTER ,
231 .\"O meaning insert a copy of
232 .\"O .IR item
233 .\"O (and return a pointer to the new hash table entry as the function result),
234 .\"O or the value
235 .\"O .BR FIND ,
236 .\"O meaning that NULL should be returned.
237 .\"O (If
238 .\"O .I action
239 .\"O is
240 .\"O .BR FIND ,
241 .\"O then
242 .\"O .I data
243 .\"O is ignored.)
244 ¤³¤Î°ú¤­¿ô¤Ë¤Ï
245 .B ENTER
246 ¤«
247 .B FIND
248 ¤Î¤¤¤º¤ì¤«¤ÎÃͤò»ØÄꤷ¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
249 .B ENTER
250 ¤Ï
251 .I item
252 ¤Î¥³¥Ô¡¼¤òÁÞÆþ¤¹¤ë¤³¤È¤ò
253 (´Ø¿ô¤Î·ë²Ì¤È¤·¤Æ¿·¤·¤¤¥Ï¥Ã¥·¥å¥Æ¡¼¥Ö¥ë¥¨¥ó¥È¥ê¤Ø¤Î¥Ý¥¤¥ó¥¿¤òÊÖ¤¹)¡¢
254 .B FIND
255 ¤Ï NULL ¤òÊÖ¤¹¤³¤È¤ò°ÕÌ£¤¹¤ë
256 .RI ( action
257 ¤¬
258 .B FIND
259 ¤Î¾ì¹ç¡¢
260 .I data
261 ¤Ï̵»ë¤µ¤ì¤ë)¡£
262
263 .\"O The
264 .\"O .BR hsearch_r ()
265 .\"O function is like
266 .\"O .BR hsearch ()
267 .\"O but operates on the hash table described by
268 .\"O .IR *htab .
269 .\"O The
270 .\"O .BR hsearch_r ()
271 .\"O function differs from
272 .\"O .BR hsearch ()
273 .\"O in that a pointer to the found item is returned in
274 .\"O .IR *retval ,
275 .\"O rather than as the function result.
276 .BR hsearch_r ()
277 ´Ø¿ô¤Ï
278 .BR hsearch ()
279 ¤ÈƱÍͤÀ¤¬¡¢
280 .I *htab
281 ¤Ç¼¨¤µ¤ì¤ë¥Ï¥Ã¥·¥å¥Æ¡¼¥Ö¥ë¤ËÂФ·¤Æ½èÍý¤ò¹Ô¤¦¡£
282 .BR hsearch_r ()
283 ´Ø¿ô¤¬
284 .BR hsearch ()
285 ¤È°Û¤Ê¤ë¤Î¤Ï¡¢¸«¤Ä¤«¤Ã¤¿¹àÌܤؤΥݥ¤¥ó¥¿¤ò¡¢
286 ´Ø¿ô¤Î·ë²Ì¤È¤·¤Æ¤Ç¤Ï¤Ê¤¯¡¢
287 .I *retval
288 ¤Ë³ÊǼ¤·¤ÆÊÖ¤¹ÅÀ¤Ç¤¢¤ë¡£
289 .\"O .SH "RETURN VALUE"
290 .SH ÊÖ¤êÃÍ
291 .\"O .BR hcreate ()
292 .\"O and
293 .\"O .BR hcreate_r ()
294 .\"O return nonzero on success.
295 .\"O They return 0 on error.
296 .BR hcreate ()
297 ¤È
298 .BR hcreate_r ()
299 ¤Ï¡¢À®¸ù¤·¤¿¾ì¹ç 0 °Ê³°¤ÎÃͤòÊÖ¤·¡¢
300 ¥¨¥é¡¼¤Î¾ì¹ç 0 ¤òÊÖ¤¹¡£
301
302 .\"O On success,
303 .\"O .BR hsearch ()
304 .\"O returns a pointer to an entry in the hash table.
305 .\"O .BR hsearch ()
306 .\"O returns NULL on error, that is,
307 .\"O if \fIaction\fP is \fBENTER\fP and
308 .\"O the hash table is full, or \fIaction\fP is \fBFIND\fP and \fIitem\fP
309 .\"O cannot be found in the hash table.
310 À®¸ù¤¹¤ë¤È¡¢
311 .BR hsearch ()
312 ¤Ï¡¢¥Ï¥Ã¥·¥å¥Æ¡¼¥Ö¥ëÆâ¤Î¥¨¥ó¥È¥ê¤Ø¤Î¥Ý¥¤¥ó¥¿¤òÊÖ¤¹¡£
313 ¥¨¥é¡¼¤Î¾ì¹ç¡¢
314 .BR hsearch ()
315 ¤Ï NULL ¤òÊÖ¤¹¡£
316 ¥¨¥é¡¼¤È¤Ê¤ë¤Î¤Ï¡¢
317 \fIaction\fP ¤¬ \fBENTER\fP ¤Ç¥Ï¥Ã¥·¥å¥Æ¡¼¥Ö¥ë¤¬¤¤¤Ã¤Ñ¤¤¤Î¾ì¹ç¤«¡¢
318 \fIaction\fP ¤¬ \fBFIND\fP ¤Ç \fIitem\fP ¤¬¥Ï¥Ã¥·¥å¥Æ¡¼¥Ö¥ëÆâ¤Ë
319 ¸«¤Ä¤«¤é¤Ê¤¤¾ì¹ç¤Ç¤¢¤ë¡£
320 .\"O .BR hsearch_r ()
321 .\"O returns nonzero on success, and 0 on error.
322 .BR hsearch_r ()
323 ¤Ï¡¢À®¸ù¤¹¤ë¤È 0 °Ê³°¤òÊÖ¤·¡¢¥¨¥é¡¼¤Î¾ì¹ç 0 ¤òÊÖ¤¹¡£
324 .\"O .SH ERRORS
325 .SH ¥¨¥é¡¼
326 .LP
327 .\"O .BR hcreate ()
328 .\"O and
329 .\"O .BR hcreate_r ()
330 .\"O can fail for the following reasons:
331 .BR hcreate ()
332 ¤È
333 .BR hcreate_r ()
334 ¤Ï°Ê²¼¤ÎÍýͳ¤Ç¼ºÇÔ¤¹¤ë²ÄǽÀ­¤¬¤¢¤ë¡£
335 .TP
336 .B EINVAL
337 .\"O .RB ( hcreate_r ())
338 .\"O .I htab
339 .\"O is NULL.
340 .RB ( hcreate_r ())
341 .I htab
342 ¤¬ NULL ¤Ç¤¢¤ë¡£
343 .TP
344 .B ENOMEM
345 .\"O Table full with \fIaction\fP set to \fBENTER\fP.
346 \fBENTER\fP ¤ËÀßÄꤵ¤ì¤¿ \fIaction\fP ¤Ç¡¢
347 ¥Æ¡¼¥Ö¥ë¤¬¤¤¤Ã¤Ñ¤¤¤Ë¤Ê¤Ã¤¿¡£
348 .TP
349 .B ESRCH
350 .\"O The \fIaction\fP argument is \fBFIND\fP and no corresponding element
351 .\"O is found in the table.
352 \fIaction\fP °ú¤­¿ô¤¬ \fBFIND\fP ¤Ç¡¢
353 ¤«¤ÄÂбþ¤¹¤ëÍ×ÁǤ¬¥Æ¡¼¥Ö¥ë¤Ë¸«¤Ä¤«¤é¤Ê¤«¤Ã¤¿¡£
354 .\"O .\" hdestroy_r() can set errno to EINVAL if 'tab' is NULL.
355 .\" hdestroy_r() ¤Ï 'tab' ¤¬ NULL ¤Î¾ì¹ç errno ¤Ë EINVAL ¤òÀßÄꤹ¤ë¾ì¹ç¤¬¤¢¤ë¡£
356 .PP
357 .\"O .BR hsearch ()
358 .\"O and
359 .\"O .BR hsearch_r ()
360 .\"O can fail for the following reasons:
361 .BR hsearch ()
362 ¤È
363 .BR hsearch_r ()
364 ¤Ï°Ê²¼¤ÎÍýͳ¤Ç¼ºÇÔ¤¹¤ë²ÄǽÀ­¤¬¤¢¤ë¡£
365 .TP
366 .B ENOMEM
367 .\"O .I action
368 .\"O was
369 .\"O .BR ENTER ,
370 .\"O .I key
371 .\"O was not found in the table,
372 .\"O and there was no room in the table to add a new entry.
373 .I action
374 ¤¬
375 .B ENTER
376 ¤Ç¡¢
377 .I key
378 ¤¬¥Æ¡¼¥Ö¥ëÆâ¤Ë¸«¤Ä¤«¤é¤º¡¢
379 ¥Æ¡¼¥Ö¥ë¤Ë¿·¤·¤¤¥¨¥ó¥È¥ê¤òÄɲ乤ë;ÃϤ¬¤Ê¤«¤Ã¤¿¡£
380 .TP
381 .B ESRCH
382 .\"O .I action
383 .\"O was
384 .\"O .BR FIND ,
385 .\"O and
386 .\"O .I key
387 .\"O was not found in the table.
388 .I action
389 ¤¬
390 .B FIND
391 ¤Ç¡¢
392 .I key
393 ¤¬¥Æ¡¼¥Ö¥ëÆâ¤Ë¸«¤Ä¤«¤é¤Ê¤«¤Ã¤¿¡£
394 .PP
395 .\"O POSIX.1-2001 only specifies the
396 .\"O .B ENOMEM
397 .\"O error.
398 POSIX.1-2001 ¤¬µ¬Äꤷ¤Æ¤¤¤ë¤Î¤Ï¡¢¥¨¥é¡¼
399 .B ENOMEM
400 ¤À¤±¤Ç¤¢¤ë¡£
401 .\"O .SH "CONFORMING TO"
402 .SH ½àµò
403 .\"O The functions
404 .\"O .BR hcreate (),
405 .\"O .BR hsearch (),
406 .\"O and
407 .\"O .BR hdestroy ()
408 .\"O are from SVr4, and are described in POSIX.1-2001.
409 ´Ø¿ô
410 .BR hcreate (),
411 .BR hsearch (),
412 .BR hdestroy ()
413 ¤Ï SVr4 ¤«¤éƳÆþ¤µ¤ì¤¿¤â¤Î¤Ç¡¢POSIX.1-2001 ¤Ëµ­½Ò¤µ¤ì¤Æ¤¤¤ë¡£
414 .\"O The functions
415 .\"O .BR hcreate_r ,
416 .\"O .BR hsearch_r ,
417 .\"O and
418 .\"O .BR hdestroy_r
419 .\"O are GNU extensions.
420 ´Ø¿ô
421 .BR hcreate_r ,
422 .BR hsearch_r ,
423 .B hdestroy_r
424 ¤Ï GNU ¤Î³ÈÄ¥¤Ç¤¢¤ë¡£
425 .\"O .SH NOTES
426 .SH Ãí°Õ
427 .\"O Hash table implementations are usually more efficient when the
428 .\"O table contains enough free space to minimize collisions.
429 .\"O Typically, this means that
430 .\"O .I nel
431 .\"O should be at least 25% larger than the maximum number of elements
432 .\"O that the caller expects to store in the table.
433 Ä̾¥Ï¥Ã¥·¥å¥Æ¡¼¥Ö¥ë¤Î¼ÂÁõ¤Ï¡¢¾×ÆͤòºÇ¾®¸Â¤Ë¤¹¤ë¤¿¤á¤Ë
434 ¥Æ¡¼¥Ö¥ë¤Ë½½Ê¬¤Ê¶õ¤­Îΰ褬¤¢¤ë¾ì¹ç¤Ë¸úΨ¤¬¤è¤¯¤Ê¤ë¡£
435 ¤³¤Î¤¿¤á¡¢ÉáÄ̤ϡ¢
436 .I nel
437 ¤ò¡¢¸Æ¤Ó½Ð¤·Â¦¤¬¥Æ¡¼¥Ö¥ë¤Ë³ÊǼ¤·¤è¤¦¤È»×¤Ã¤Æ¤¤¤ë
438 ¥¨¥ó¥È¥ê¤ÎºÇÂç¿ô¤è¤ê¾¯¤Ê¤¯¤È¤â 25% ¤ÏÂ礭¤ÊÃͤˤ¹¤Ù¤­¤Ç¤¢¤ë¡£
439
440 .\"O The
441 .\"O .BR hdestroy ()
442 .\"O and
443 .\"O .BR hdestroy _r()
444 .\"O functions do not free the buffers pointed to by the
445 .\"O .I key
446 .\"O and
447 .\"O .I data
448 .\"O elements of the hash table entries.
449 .\"O (It can't do this because it doesn't know
450 .\"O whether these buffers were allocated dynamically.)
451 .BR hdestroy ()
452 ¤È
453 .BR hdestroy_r ()
454 ¤Ï¡¢¥Ï¥Ã¥·¥å¥Æ¡¼¥Ö¥ë¤Î¥¨¥ó¥È¥ê¤ÎÍ×ÁǤǤ¢¤ë
455 .I key
456 ¤È
457 .I data
458 ¤¬»Ø¤¹¥Ð¥Ã¥Õ¥¡¤ò²òÊü¤·¤Ê¤¤
459 (¤³¤ì¤¬¤Ç¤­¤Ê¤¤¤Î¤Ï¡¢¤³¤ì¤é¤Î¥Ð¥Ã¥Õ¥¡¤¬Æ°Åª¤Ë³ä¤êÅö¤Æ¤é¤ì¤¿¤Î¤«¤ò
460 ÃΤ뤳¤È¤¬¤Ç¤­¤Ê¤¤¤«¤é¤Ç¤¢¤ë)¡£
461 .\"O If these buffers need to be freed (perhaps because the program
462 .\"O is repeatedly creating and destroying hash tables,
463 .\"O rather than creating a single table whose lifetime
464 .\"O matches that of the program),
465 .\"O then the program must maintain bookkeeping data structures that
466 .\"O allow it to free them.
467 ¤³¤ì¤é¤Î¥Ð¥Ã¥Õ¥¡¤ò²òÊü¤¹¤ëɬÍפ¬¤¢¤ë¾ì¹ç¡¢
468 ¥×¥í¥°¥é¥à¤Ç¤Ï¡¢¤³¤ì¤é¤Î¥Ð¥Ã¥Õ¥¡¤ò²òÊü¤Ç¤­¤ë¤è¤¦¤Ë´ÉÍýÍѤΥǡ¼¥¿¹½Â¤¤ò
469 Àߤ±¤Æ¡¢¤³¤ì¤ò´ÉÍý¤·¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤
470 (²òÊü¤¬É¬ÍפȤʤëÍýͳ¤Ï¡¢¤¿¤¤¤Æ¤¤¤Ï¡¢¥×¥í¥°¥é¥à¼«¿È¤ÈÀ¸Â¸´ü´Ö¤¬Æ±¤¸
471 ¥Ï¥Ã¥·¥å¥Æ¡¼¥Ö¥ë¤ò°ì¤Ä¤À¤±ºîÀ®¤¹¤ë¤Î¤Ç¤Ï¤Ê¤¯¡¢¤½¤Î¥×¥í¥°¥é¥à¤Ç¤ÏÊ£¿ô¤Î
472 ¥Ï¥Ã¥·¥å¥Æ¡¼¥Ö¥ë¤ò·«¤êÊÖ¤·¤ÆºîÀ®¤·¤¿¤êÇË´þ¤·¤¿¤ê¤¹¤ë¤«¤é¤Ç¤¢¤í¤¦)¡£
473 .\"O .SH BUGS
474 .SH ¥Ð¥°
475 .\"O SVr4 and POSIX.1-2001 specify that \fIaction\fP
476 .\"O is significant only for unsuccessful searches, so that an \fBENTER\fP
477 .\"O should not do anything for a successful search.
478 .\"O In libc and glibc (before version 2.3), the
479 .\"O implementation violates the specification,
480 .\"O updating the \fIdata\fP for the given \fIkey\fP in this case.
481 SVr4 ¤È POSIX.1-2001 ¤Îµ¬Äê¤Ç¤Ï¡¢
482 \fIaction\fP ¤Ï¸¡º÷¤¬¼ºÇÔ¤·¤¿¤È¤­¤Ë¤À¤±°ÕÌ£¤ò»ý¤Ä¤È¤Ê¤Ã¤Æ¤¤¤ë¡£
483 ¤è¤Ã¤Æ¡¢¸¡º÷¤¬À®¸ù¤·¤¿¾ì¹ç¡¢\fIaction\fP ¤ÎÃͤ¬ \fBENTER\fP ¤Ç¤â
484 ²¿¤â¤¹¤Ù¤­¤Ç¤Ï¤Ê¤¤¡£
485 (¥Ð¡¼¥¸¥ç¥ó 2.3 ¤è¤êÁ°¤Î) libc ¤È glibc ¤Î¼ÂÁõ¤Ï¤³¤Îµ¬³Ê¤Ë°ãÈ¿¤·¤Æ¤ª¤ê¡¢
486 ¤³¤Î¾õ¶·¤Ç¡¢»ØÄꤵ¤ì¤¿ \fIkey\fP ¤ËÂбþ¤¹¤ë \fIdata\fP ¤¬¹¹¿·¤µ¤ì¤ë¡£
487
488 .\"O Individual hash table entries can be added, but not deleted.
489 ¥Ï¥Ã¥·¥å¥Æ¡¼¥Ö¥ë¥¨¥ó¥È¥ê¡¼¤ÎÄɲäϤǤ­¤ë¤¬¡¢ºï½ü¤¬¤Ç¤­¤Ê¤¤¡£
490 .\"O .SH EXAMPLE
491 .SH Îã
492 .PP
493 .\"O The following program inserts 24 items into a hash table, then prints
494 .\"O some of them.
495 ¼¡¤Î¥×¥í¥°¥é¥à¤Ï¡¢¥Ï¥Ã¥·¥å¥Æ¡¼¥Ö¥ë¤Ë 24 ¸Ä¤Î¹àÌܤòÁÞÆþ¤·¡¢
496 ¤½¤ì¤«¤é¤½¤Î¤¦¤Á¤Î¤¤¤¯¤Ä¤«¤òɽ¼¨¤¹¤ë¡£
497 .nf
498
499 #include <stdio.h>
500 #include <stdlib.h>
501 #include <search.h>
502
503 char *data[] = { "alpha", "bravo", "charlie", "delta",
504      "echo", "foxtrot", "golf", "hotel", "india", "juliet",
505      "kilo", "lima", "mike", "november", "oscar", "papa",
506      "quebec", "romeo", "sierra", "tango", "uniform",
507      "victor", "whisky", "x\-ray", "yankee", "zulu"
508 };
509
510 int main()
511 {
512     ENTRY e, *ep;
513     int i;
514
515     hcreate(30);
516
517     for (i = 0; i < 24; i++) {
518         e.key = data[i];
519 .\"O         /* data is just an integer, instead of a
520 .\"O            pointer to something */
521         /* ¥Ç¡¼¥¿¤Ï¡¢¥Ý¥¤¥ó¥¿¤Ç¤Ï¤Ê¤¯¡¢Ã±¤Ê¤ëÀ°¿ôÃͤǤ¢¤ë¡£ */
522         e.data = (void *) i;
523         ep = hsearch(e, ENTER);
524 .\"O         /* there should be no failures */
525         /* ¥¨¥é¡¼¤Ïµ¯¤³¤é¤Ê¤¤¤Ï¤º¤Ç¤¢¤ë¡£ */
526         if (ep == NULL) {
527             fprintf(stderr, "entry failed\\n");
528             exit(EXIT_FAILURE);
529         }
530     }
531
532     for (i = 22; i < 26; i++) {
533 .\"O         /* print two entries from the table, and
534 .\"O            show that two are not in the table */
535         /* ¥Æ¡¼¥Ö¥ë¤Ë¤¢¤ë 2 ¤Ä¤Î¥¨¥ó¥È¥ê¤òɽ¼¨¤·¡¢
536            ¤¢¤È¤Î 2 ¤Ä¤¬¥Æ¡¼¥Ö¥ë¤Ë¤Ê¤¤¤³¤È¤ò¼¨¤¹¡£ */
537         e.key = data[i];
538         ep = hsearch(e, FIND);
539         printf("%9.9s \-> %9.9s:%d\\n", e.key,
540                ep ? ep\->key : "NULL", ep ? (int)(ep\->data) : 0);
541     }
542     hdestroy();
543     exit(EXIT_SUCCESS);
544 }
545 .fi
546 .\"O .SH "SEE ALSO"
547 .SH ´ØÏ¢¹àÌÜ
548 .BR bsearch (3),
549 .BR lsearch (3),
550 .BR malloc (3),
551 .BR tsearch (3),
552 .BR feature_test_macros (7)