OSDN Git Service

(split) Apply minor changes from v3.32 to v3.35 in the upstream.
[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 2011-09-10 "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 .\"O .BR "#define _GNU_SOURCE" "         /* See feature_test_macros(7) */"
72 .BR "#define _GNU_SOURCE" "         /* feature_test_macros(7) »²¾È */"
73 .br
74 .B #include <search.h>
75 .sp
76 .BI "int hcreate_r(size_t " nel ", struct hsearch_data *" htab );
77 .sp
78 .BI "int hsearch_r(ENTRY " item ", ACTION " action ", ENTRY **" retval ,
79 .BI "              struct hsearch_data *" htab );
80 .sp
81 .BI "void hdestroy_r(struct hsearch_data *" htab );
82 .fi
83 .\"O .SH DESCRIPTION
84 .SH ÀâÌÀ
85 .\"O The three functions
86 .\"O .BR hcreate (),
87 .\"O .BR hsearch (),
88 .\"O and
89 .\"O .BR hdestroy ()
90 .\"O allow the caller to create and manage a hash search table
91 .\"O containing entries consisting of a key (a string) and associated data.
92 .\"O Using these functions, only one hash table can be used at a time.
93 .BR hcreate (),
94 .BR hsearch (),
95 .BR hdestroy ()
96 ¤Î 3 ¤Ä¤Î´Ø¿ô¤òÍøÍѤ¹¤ë¤È¡¢¥­¡¼ (ʸ»úÎó) ¤ÈÂбþ¤¹¤ë¥Ç¡¼¥¿¤«¤é¹½À®¤µ¤ì¤ë
97 ¥¨¥ó¥È¥ê¤ò³ÊǼ¤Ç¤­¤ë¥Ï¥Ã¥·¥å¸¡º÷¥Æ¡¼¥Ö¥ë¤òºîÀ®¡¢´ÉÍý¤¹¤ë¤³¤È¤¬¤Ç¤­¤ë¡£
98 ¤³¤ì¤é¤Î´Ø¿ô¤ò»È¤Ã¤Æ¡¢°ìÅ٤˻ÈÍѤǤ­¤ë¤Î¤Ï°ì¤Ä¤Î¥Ï¥Ã¥·¥å¥Æ¡¼¥Ö¥ë¤À¤±¤Ç¤¢¤ë¡£
99
100 .\"O The three functions
101 .\"O .BR hcreate_r (),
102 .\"O .BR hsearch_r (),
103 .\"O .BR hdestroy_r ()
104 .\"O are reentrant versions that allow a program to use
105 .\"O more than one hash search table at the same time.
106 .\"O The last argument,
107 .\"O .IR htab ,
108 .\"O points to a structure that describes the table
109 .\"O on which the function is to operate.
110 .\"O The programmer should treat this structure as opaque
111 .\"O (i.e., do not attempt to directly access or modify
112 .\"O the fields in this structure).
113 .BR hcreate_r (),
114 .BR hsearch_r (),
115 .BR hdestroy_r ()
116 ¤Î 3 ¤Ä¤Î´Ø¿ô¤Ï¥ê¥¨¥ó¥È¥é¥ó¥ÈÈǤǡ¢¤³¤ì¤é¤òÍøÍѤ¹¤ë¤È¡¢
117 °ì¤Ä¤Î¥×¥í¥°¥é¥à¤ÇƱ»þ¤ËÊ£¿ô¤Î¥Ï¥Ã¥·¥å¥Æ¡¼¥Ö¥ë¤ò»È¤¦¤³¤È¤¬¤Ç¤­¤ë¡£
118 ºÇ¸å¤Î°ú¤­¿ô
119 .I htab
120 ¤Ï´Ø¿ô¤ÎÁàºîÂоݤȤʤë¥Æ¡¼¥Ö¥ë¤ò¼¨¤¹¹½Â¤ÂΤؤΥݥ¤¥ó¥¿¤Ç¤¢¤ë¡£
121 ¥×¥í¥°¥é¥Þ¤Ï¤³¤Î¹½Â¤ÂΤò¥Ö¥é¥Ã¥¯¥Ü¥Ã¥¯¥¹¤È¤·¤Æ°·¤¦¤Ù¤­¤Ç¤¢¤ë
122 (¤Ä¤Þ¤ê¡¢¤³¤Î¹½Â¤ÂΤΥե£¡¼¥ë¥É¤ËľÀÜ¥¢¥¯¥»¥¹¤·¤¿¤êÊѹ¹¤·¤¿¤ê
123 ¤·¤Ê¤¤¤³¤È)¡£
124
125 .\"O First a hash table must be created using
126 .\"O .BR hcreate ().
127 .\"O The argument \fInel\fP specifies the maximum number of entries
128 .\"O in the table.
129 .\"O (This maximum cannot be changed later, so choose it wisely.)
130 .\"O The implementation may adjust this value upward to improve the
131 .\"O performance of the resulting hash table.
132 ºÇ½é¤Ë¡¢
133 .BR hcreate ()
134 ´Ø¿ô¤Ë¤è¤Ã¤Æ¥Ï¥Ã¥·¥å¥Æ¡¼¥Ö¥ë¤òºîÀ®¤·¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
135 °ú¤­¿ô \fInel\fP ¤Ç¥Æ¡¼¥Ö¥ë¤ÎºÇÂ票¥ó¥È¥ê¿ô¤ò»ØÄꤹ¤ë
136 (¤³¤ÎºÇÂçÃͤϸå¤ÇÊѹ¹¤¹¤ë¤³¤È¤Ï¤Ç¤­¤Ê¤¤¤Î¤Ç¡¢¤è¤¯¹Í¤¨¤ÆÁªÂò¤¹¤ë¤³¤È)¡£
137 ºîÀ®¤µ¤ì¤ë¥Ï¥Ã¥·¥å¥Æ¡¼¥Ö¥ë¤ÎÀ­Ç½¤ò¸þ¾å¤µ¤»¤ë¤¿¤á¤Ë¡¢
138 ´Ø¿ôÆâÉô¤Î¼ÂÁõ¤Ë¤è¤ê¤³¤ÎÃͤÏÁý¤ä¤µ¤ì¤ë¾ì¹ç¤â¤¢¤ë¡£
139 .\" e.g., in glibc it is raised to the next higher prime number
140
141 .\"O The
142 .\"O .BR hcreate_r ()
143 .\"O function performs the same task as
144 .\"O .BR hcreate (),
145 .\"O but for the table described by the structure
146 .\"O .IR *htab .
147 .\"O The structure pointed to by
148 .\"O .I htab
149 .\"O must be zeroed before the first call to
150 .\"O .BR hcreate_r ().
151 .BR hcreate_r ()
152 ´Ø¿ô¤Ï
153 .BR hcreate ()
154 ¤ÈƱ¤¸Æ°ºî¤ò¤¹¤ë¤¬¡¢¹½Â¤ÂÎ
155 .I *htab
156 ¤Ç¼¨¤µ¤ì¤ë¥Æ¡¼¥Ö¥ë¤òÂоݤȤ·¤ÆÆ°ºî¤¹¤ë¡£
157 .I htab
158 ¤¬»Ø¤·¼¨¤¹¹½Â¤ÂΤϡ¢
159 .BR hcreate_r ()
160 ¤ò½é¤á¤Æ¸Æ¤Ó½Ð¤¹Á°¤Ë 0 ¤ÇËä¤á¤Æ¤ª¤«¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
161
162 .\"O The function
163 .\"O .BR hdestroy ()
164 .\"O frees the memory occupied by the hash table that was created by
165 .\"O .BR hcreate ().
166 .\"O After calling
167 .\"O .BR hdestroy ()
168 .\"O a new hash table can be created using
169 .\"O .BR hcreate ().
170 .\"O The
171 .\"O .BR hdestroy_r ()
172 .\"O function performs the analogous task for a hash table described by
173 .\"O .IR *htab ,
174 .\"O which was previously created using
175 .\"O .BR hcreate_r ().
176 .BR hdestroy ()
177 ´Ø¿ô¤Ï¡¢
178 .BR hcreate ()
179 ¤ÇºîÀ®¤µ¤ì¤¿¥Ï¥Ã¥·¥å¥Æ¡¼¥Ö¥ë¤¬ÀêÍ­¤·¤Æ¤¤¤¿¥á¥â¥ê¤ò²òÊü¤¹¤ë¡£
180 ¥Ï¥Ã¥·¥å¥Æ¡¼¥Ö¥ë¤Ë¤è¤Ã¤ÆÀêÍ­¤µ¤ì¤Æ¤¤¤¿¥á¥â¥ê¤ò²òÊü¤·¡¢
181 ¿·¤·¤¤¥Æ¡¼¥Ö¥ë¤òºîÀ®¤Ç¤­¤ë¤è¤¦¤Ë¤¹¤ë¡£
182 .BR hdestroy ()
183 ¤ò¸Æ¤Ó½Ð¤¹¤È¡¢¤½¤Î¸å¤Ï
184 .BR hcreate ()
185 ¤ò»È¤Ã¤Æ¿·¤·¤¤¥Ï¥Ã¥·¥å¥Æ¡¼¥Ö¥ë¤òºîÀ®¤¹¤ë¤³¤È¤¬¤Ç¤­¤ë¡£
186 .BR hdestroy_r ()
187 ´Ø¿ô¤Ï¡¢Æ±ÍͤνèÍý¤ò¡¢¤½¤ì°ÊÁ°¤Ë
188 .BR hcreate_r ()
189 ¤ò»È¤Ã¤ÆºîÀ®¤·¤¿
190 .I *htab
191 ¤Ç¼¨¤µ¤ì¤ë¥Ï¥Ã¥·¥å¥Æ¡¼¥Ö¥ë¤ËÂФ·¤Æ¼Â¹Ô¤¹¤ë¡£
192
193 .\"O The
194 .\"O .BR hsearch ()
195 .\"O function searches the hash table for an
196 .\"O item with the same key as \fIitem\fP (where "the same" is determined using
197 .\"O .BR strcmp (3)),
198 .\"O and if successful returns a pointer to it.
199 .BR hsearch ()
200 ´Ø¿ô¤Ï¡¢\fIitem\fP ¤ÈƱ¤¸¥­¡¼¤ò»ý¤Ä¹àÌܤò¥Ï¥Ã¥·¥å¥Æ¡¼¥Ö¥ë¤«¤é
201 ¸¡º÷¤·¡¢¹àÌܤ¬¸«¤Ä¤«¤Ã¤¿¾ì¹ç¤Ë¤Ï¤½¤Î¹àÌܤؤΥݥ¤¥ó¥¿¤òÊÖ¤¹
202 (¡ÖƱ¤¸¡×¤«¤É¤¦¤«¤Ï
203 .BR strcmp (3)
204 ¤ò»È¤Ã¤ÆȽÄꤹ¤ë)¡£
205
206 .\"O The argument \fIitem\fP is of type \fIENTRY\fP, which is defined in
207 .\"O \fI<search.h>\fP as follows:
208 °ú¤­¿ô \fIitem\fP ¤Ï \fBENTRY\fP ·¿¤Ç¤¢¤ê¡¢\fI<search.h>\fP ¤ÎÃæ¤Ç
209 °Ê²¼¤Î¤è¤¦¤ËÄêµÁ¤µ¤ì¤Æ¤¤¤ë¡£
210 .in +4n
211 .sp
212 .nf
213 typedef struct entry {
214     char *key;
215     void *data;
216 } ENTRY;
217 .in
218 .fi
219 .sp
220 .\"O The field \fIkey\fP points to a null-terminated string which is the
221 .\"O search key.
222 .\"O The field \fIdata\fP points to data that is associated with that key.
223 ¥Õ¥£¡¼¥ë¥É \fIkey\fP ¤Ï¸¡º÷¥­¡¼¤È¤Ê¤ë NULL ½ªÃ¼¤µ¤ì¤¿Ê¸»úÎó¤ò»Ø¤¹¡£
224 ¥Õ¥£¡¼¥ë¥É \fIdata\fP ¤Ï¡¢¤³¤Î¥­¡¼¤ËÂбþ¤¹¤ë¥Ç¡¼¥¿¤ò»Ø¤¹¡£
225
226 .\"O The argument \fIaction\fP determines what
227 .\"O .BR hsearch ()
228 .\"O does after an unsuccessful search.
229 ¸¡º÷¤¬¼ºÇÔ¤·¤¿¸å¤ÎÆ°ºî¤Ï¡¢°ú¤­¿ô \fIaction\fP ¤Ë¤è¤ê·è¤Þ¤ë¡£
230 .\"O This argument must either have the value
231 .\"O .BR ENTER ,
232 .\"O meaning insert a copy of
233 .\"O .IR item
234 .\"O (and return a pointer to the new hash table entry as the function result),
235 .\"O or the value
236 .\"O .BR FIND ,
237 .\"O meaning that NULL should be returned.
238 .\"O (If
239 .\"O .I action
240 .\"O is
241 .\"O .BR FIND ,
242 .\"O then
243 .\"O .I data
244 .\"O is ignored.)
245 ¤³¤Î°ú¤­¿ô¤Ë¤Ï
246 .B ENTER
247 ¤«
248 .B FIND
249 ¤Î¤¤¤º¤ì¤«¤ÎÃͤò»ØÄꤷ¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
250 .B ENTER
251 ¤Ï
252 .I item
253 ¤Î¥³¥Ô¡¼¤òÁÞÆþ¤¹¤ë¤³¤È¤ò
254 (´Ø¿ô¤Î·ë²Ì¤È¤·¤Æ¿·¤·¤¤¥Ï¥Ã¥·¥å¥Æ¡¼¥Ö¥ë¥¨¥ó¥È¥ê¤Ø¤Î¥Ý¥¤¥ó¥¿¤òÊÖ¤¹)¡¢
255 .B FIND
256 ¤Ï NULL ¤òÊÖ¤¹¤³¤È¤ò°ÕÌ£¤¹¤ë
257 .RI ( action
258 ¤¬
259 .B FIND
260 ¤Î¾ì¹ç¡¢
261 .I data
262 ¤Ï̵»ë¤µ¤ì¤ë)¡£
263
264 .\"O The
265 .\"O .BR hsearch_r ()
266 .\"O function is like
267 .\"O .BR hsearch ()
268 .\"O but operates on the hash table described by
269 .\"O .IR *htab .
270 .\"O The
271 .\"O .BR hsearch_r ()
272 .\"O function differs from
273 .\"O .BR hsearch ()
274 .\"O in that a pointer to the found item is returned in
275 .\"O .IR *retval ,
276 .\"O rather than as the function result.
277 .BR hsearch_r ()
278 ´Ø¿ô¤Ï
279 .BR hsearch ()
280 ¤ÈƱÍͤÀ¤¬¡¢
281 .I *htab
282 ¤Ç¼¨¤µ¤ì¤ë¥Ï¥Ã¥·¥å¥Æ¡¼¥Ö¥ë¤ËÂФ·¤Æ½èÍý¤ò¹Ô¤¦¡£
283 .BR hsearch_r ()
284 ´Ø¿ô¤¬
285 .BR hsearch ()
286 ¤È°Û¤Ê¤ë¤Î¤Ï¡¢¸«¤Ä¤«¤Ã¤¿¹àÌܤؤΥݥ¤¥ó¥¿¤ò¡¢
287 ´Ø¿ô¤Î·ë²Ì¤È¤·¤Æ¤Ç¤Ï¤Ê¤¯¡¢
288 .I *retval
289 ¤Ë³ÊǼ¤·¤ÆÊÖ¤¹ÅÀ¤Ç¤¢¤ë¡£
290 .\"O .SH "RETURN VALUE"
291 .SH ÊÖ¤êÃÍ
292 .\"O .BR hcreate ()
293 .\"O and
294 .\"O .BR hcreate_r ()
295 .\"O return nonzero on success.
296 .\"O They return 0 on error.
297 .BR hcreate ()
298 ¤È
299 .BR hcreate_r ()
300 ¤Ï¡¢À®¸ù¤·¤¿¾ì¹ç 0 °Ê³°¤ÎÃͤòÊÖ¤·¡¢
301 ¥¨¥é¡¼¤Î¾ì¹ç 0 ¤òÊÖ¤¹¡£
302
303 .\"O On success,
304 .\"O .BR hsearch ()
305 .\"O returns a pointer to an entry in the hash table.
306 .\"O .BR hsearch ()
307 .\"O returns NULL on error, that is,
308 .\"O if \fIaction\fP is \fBENTER\fP and
309 .\"O the hash table is full, or \fIaction\fP is \fBFIND\fP and \fIitem\fP
310 .\"O cannot be found in the hash table.
311 À®¸ù¤¹¤ë¤È¡¢
312 .BR hsearch ()
313 ¤Ï¡¢¥Ï¥Ã¥·¥å¥Æ¡¼¥Ö¥ëÆâ¤Î¥¨¥ó¥È¥ê¤Ø¤Î¥Ý¥¤¥ó¥¿¤òÊÖ¤¹¡£
314 ¥¨¥é¡¼¤Î¾ì¹ç¡¢
315 .BR hsearch ()
316 ¤Ï NULL ¤òÊÖ¤¹¡£
317 ¥¨¥é¡¼¤È¤Ê¤ë¤Î¤Ï¡¢
318 \fIaction\fP ¤¬ \fBENTER\fP ¤Ç¥Ï¥Ã¥·¥å¥Æ¡¼¥Ö¥ë¤¬¤¤¤Ã¤Ñ¤¤¤Î¾ì¹ç¤«¡¢
319 \fIaction\fP ¤¬ \fBFIND\fP ¤Ç \fIitem\fP ¤¬¥Ï¥Ã¥·¥å¥Æ¡¼¥Ö¥ëÆâ¤Ë
320 ¸«¤Ä¤«¤é¤Ê¤¤¾ì¹ç¤Ç¤¢¤ë¡£
321 .\"O .BR hsearch_r ()
322 .\"O returns nonzero on success, and 0 on error.
323 .BR hsearch_r ()
324 ¤Ï¡¢À®¸ù¤¹¤ë¤È 0 °Ê³°¤òÊÖ¤·¡¢¥¨¥é¡¼¤Î¾ì¹ç 0 ¤òÊÖ¤¹¡£
325 .\"O .SH ERRORS
326 .SH ¥¨¥é¡¼
327 .LP
328 .\"O .BR hcreate_r ()
329 .\"O and
330 .\"O .BR hdestroy_r ()
331 .\"O can fail for the following reasons:
332 .BR hcreate_r ()
333 ¤È
334 .BR hdestroy_r ()
335 ¤Ï°Ê²¼¤ÎÍýͳ¤Ç¼ºÇÔ¤¹¤ë²ÄǽÀ­¤¬¤¢¤ë¡£
336 .TP
337 .B EINVAL
338 .\"O .I htab
339 .\"O is NULL.
340 .I htab
341 ¤¬ NULL ¤Ç¤¢¤ë¡£
342 .PP
343 .\"O .BR hsearch ()
344 .\"O and
345 .\"O .BR hsearch_r ()
346 .\"O can fail for the following reasons:
347 .BR hsearch ()
348 ¤È
349 .BR hsearch_r ()
350 ¤Ï°Ê²¼¤ÎÍýͳ¤Ç¼ºÇÔ¤¹¤ë²ÄǽÀ­¤¬¤¢¤ë¡£
351 .TP
352 .B ENOMEM
353 .\"O .I action
354 .\"O was
355 .\"O .BR ENTER ,
356 .\"O .I key
357 .\"O was not found in the table,
358 .\"O and there was no room in the table to add a new entry.
359 .I action
360 ¤¬
361 .B ENTER
362 ¤Ç¡¢
363 .I key
364 ¤¬¥Æ¡¼¥Ö¥ëÆâ¤Ë¸«¤Ä¤«¤é¤º¡¢
365 ¥Æ¡¼¥Ö¥ë¤Ë¿·¤·¤¤¥¨¥ó¥È¥ê¤òÄɲ乤ë;ÃϤ¬¤Ê¤«¤Ã¤¿¡£
366 .TP
367 .B ESRCH
368 .\"O .I action
369 .\"O was
370 .\"O .BR FIND ,
371 .\"O and
372 .\"O .I key
373 .\"O was not found in the table.
374 .I action
375 ¤¬
376 .B FIND
377 ¤Ç¡¢
378 .I key
379 ¤¬¥Æ¡¼¥Ö¥ëÆâ¤Ë¸«¤Ä¤«¤é¤Ê¤«¤Ã¤¿¡£
380 .PP
381 .\"O POSIX.1-2001 only specifies the
382 .\"O .B ENOMEM
383 .\"O error.
384 POSIX.1-2001 ¤¬µ¬Äꤷ¤Æ¤¤¤ë¤Î¤Ï¡¢¥¨¥é¡¼
385 .B ENOMEM
386 ¤À¤±¤Ç¤¢¤ë¡£
387 .\"O .SH "CONFORMING TO"
388 .SH ½àµò
389 .\"O The functions
390 .\"O .BR hcreate (),
391 .\"O .BR hsearch (),
392 .\"O and
393 .\"O .BR hdestroy ()
394 .\"O are from SVr4, and are described in POSIX.1-2001.
395 ´Ø¿ô
396 .BR hcreate (),
397 .BR hsearch (),
398 .BR hdestroy ()
399 ¤Ï SVr4 ¤«¤éƳÆþ¤µ¤ì¤¿¤â¤Î¤Ç¡¢POSIX.1-2001 ¤Ëµ­½Ò¤µ¤ì¤Æ¤¤¤ë¡£
400 .\"O The functions
401 .\"O .BR hcreate_r ,
402 .\"O .BR hsearch_r ,
403 .\"O and
404 .\"O .BR hdestroy_r
405 .\"O are GNU extensions.
406 ´Ø¿ô
407 .BR hcreate_r ,
408 .BR hsearch_r ,
409 .B hdestroy_r
410 ¤Ï GNU ¤Î³ÈÄ¥¤Ç¤¢¤ë¡£
411 .\"O .SH NOTES
412 .SH Ãí°Õ
413 .\"O Hash table implementations are usually more efficient when the
414 .\"O table contains enough free space to minimize collisions.
415 .\"O Typically, this means that
416 .\"O .I nel
417 .\"O should be at least 25% larger than the maximum number of elements
418 .\"O that the caller expects to store in the table.
419 Ä̾¥Ï¥Ã¥·¥å¥Æ¡¼¥Ö¥ë¤Î¼ÂÁõ¤Ï¡¢¾×ÆͤòºÇ¾®¸Â¤Ë¤¹¤ë¤¿¤á¤Ë
420 ¥Æ¡¼¥Ö¥ë¤Ë½½Ê¬¤Ê¶õ¤­Îΰ褬¤¢¤ë¾ì¹ç¤Ë¸úΨ¤¬¤è¤¯¤Ê¤ë¡£
421 ¤³¤Î¤¿¤á¡¢ÉáÄ̤ϡ¢
422 .I nel
423 ¤ò¡¢¸Æ¤Ó½Ð¤·Â¦¤¬¥Æ¡¼¥Ö¥ë¤Ë³ÊǼ¤·¤è¤¦¤È»×¤Ã¤Æ¤¤¤ë
424 ¥¨¥ó¥È¥ê¤ÎºÇÂç¿ô¤è¤ê¾¯¤Ê¤¯¤È¤â 25% ¤ÏÂ礭¤ÊÃͤˤ¹¤Ù¤­¤Ç¤¢¤ë¡£
425
426 .\"O The
427 .\"O .BR hdestroy ()
428 .\"O and
429 .\"O .BR hdestroy_r ()
430 .\"O functions do not free the buffers pointed to by the
431 .\"O .I key
432 .\"O and
433 .\"O .I data
434 .\"O elements of the hash table entries.
435 .\"O (It can't do this because it doesn't know
436 .\"O whether these buffers were allocated dynamically.)
437 .BR hdestroy ()
438 ¤È
439 .BR hdestroy_r ()
440 ¤Ï¡¢¥Ï¥Ã¥·¥å¥Æ¡¼¥Ö¥ë¤Î¥¨¥ó¥È¥ê¤ÎÍ×ÁǤǤ¢¤ë
441 .I key
442 ¤È
443 .I data
444 ¤¬»Ø¤¹¥Ð¥Ã¥Õ¥¡¤ò²òÊü¤·¤Ê¤¤
445 (¤³¤ì¤¬¤Ç¤­¤Ê¤¤¤Î¤Ï¡¢¤³¤ì¤é¤Î¥Ð¥Ã¥Õ¥¡¤¬Æ°Åª¤Ë³ä¤êÅö¤Æ¤é¤ì¤¿¤Î¤«¤ò
446 ÃΤ뤳¤È¤¬¤Ç¤­¤Ê¤¤¤«¤é¤Ç¤¢¤ë)¡£
447 .\"O If these buffers need to be freed (perhaps because the program
448 .\"O is repeatedly creating and destroying hash tables,
449 .\"O rather than creating a single table whose lifetime
450 .\"O matches that of the program),
451 .\"O then the program must maintain bookkeeping data structures that
452 .\"O allow it to free them.
453 ¤³¤ì¤é¤Î¥Ð¥Ã¥Õ¥¡¤ò²òÊü¤¹¤ëɬÍפ¬¤¢¤ë¾ì¹ç¡¢
454 ¥×¥í¥°¥é¥à¤Ç¤Ï¡¢¤³¤ì¤é¤Î¥Ð¥Ã¥Õ¥¡¤ò²òÊü¤Ç¤­¤ë¤è¤¦¤Ë´ÉÍýÍѤΥǡ¼¥¿¹½Â¤¤ò
455 Àߤ±¤Æ¡¢¤³¤ì¤ò´ÉÍý¤·¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤
456 (²òÊü¤¬É¬ÍפȤʤëÍýͳ¤Ï¡¢¤¿¤¤¤Æ¤¤¤Ï¡¢¥×¥í¥°¥é¥à¼«¿È¤ÈÀ¸Â¸´ü´Ö¤¬Æ±¤¸
457 ¥Ï¥Ã¥·¥å¥Æ¡¼¥Ö¥ë¤ò°ì¤Ä¤À¤±ºîÀ®¤¹¤ë¤Î¤Ç¤Ï¤Ê¤¯¡¢¤½¤Î¥×¥í¥°¥é¥à¤Ç¤ÏÊ£¿ô¤Î
458 ¥Ï¥Ã¥·¥å¥Æ¡¼¥Ö¥ë¤ò·«¤êÊÖ¤·¤ÆºîÀ®¤·¤¿¤êÇË´þ¤·¤¿¤ê¤¹¤ë¤«¤é¤Ç¤¢¤í¤¦)¡£
459 .\"O .SH BUGS
460 .SH ¥Ð¥°
461 .\"O SVr4 and POSIX.1-2001 specify that \fIaction\fP
462 .\"O is significant only for unsuccessful searches, so that an \fBENTER\fP
463 .\"O should not do anything for a successful search.
464 .\"O In libc and glibc (before version 2.3), the
465 .\"O implementation violates the specification,
466 .\"O updating the \fIdata\fP for the given \fIkey\fP in this case.
467 SVr4 ¤È POSIX.1-2001 ¤Îµ¬Äê¤Ç¤Ï¡¢
468 \fIaction\fP ¤Ï¸¡º÷¤¬¼ºÇÔ¤·¤¿¤È¤­¤Ë¤À¤±°ÕÌ£¤ò»ý¤Ä¤È¤Ê¤Ã¤Æ¤¤¤ë¡£
469 ¤è¤Ã¤Æ¡¢¸¡º÷¤¬À®¸ù¤·¤¿¾ì¹ç¡¢\fIaction\fP ¤ÎÃͤ¬ \fBENTER\fP ¤Ç¤â
470 ²¿¤â¤¹¤Ù¤­¤Ç¤Ï¤Ê¤¤¡£
471 (¥Ð¡¼¥¸¥ç¥ó 2.3 ¤è¤êÁ°¤Î) libc ¤È glibc ¤Î¼ÂÁõ¤Ï¤³¤Îµ¬³Ê¤Ë°ãÈ¿¤·¤Æ¤ª¤ê¡¢
472 ¤³¤Î¾õ¶·¤Ç¡¢»ØÄꤵ¤ì¤¿ \fIkey\fP ¤ËÂбþ¤¹¤ë \fIdata\fP ¤¬¹¹¿·¤µ¤ì¤ë¡£
473
474 .\"O Individual hash table entries can be added, but not deleted.
475 ¥Ï¥Ã¥·¥å¥Æ¡¼¥Ö¥ë¥¨¥ó¥È¥ê¡¼¤ÎÄɲäϤǤ­¤ë¤¬¡¢ºï½ü¤¬¤Ç¤­¤Ê¤¤¡£
476 .\"O .SH EXAMPLE
477 .SH Îã
478 .PP
479 .\"O The following program inserts 24 items into a hash table, then prints
480 .\"O some of them.
481 ¼¡¤Î¥×¥í¥°¥é¥à¤Ï¡¢¥Ï¥Ã¥·¥å¥Æ¡¼¥Ö¥ë¤Ë 24 ¸Ä¤Î¹àÌܤòÁÞÆþ¤·¡¢
482 ¤½¤ì¤«¤é¤½¤Î¤¦¤Á¤Î¤¤¤¯¤Ä¤«¤òɽ¼¨¤¹¤ë¡£
483 .nf
484
485 #include <stdio.h>
486 #include <stdlib.h>
487 #include <search.h>
488
489 char *data[] = { "alpha", "bravo", "charlie", "delta",
490      "echo", "foxtrot", "golf", "hotel", "india", "juliet",
491      "kilo", "lima", "mike", "november", "oscar", "papa",
492      "quebec", "romeo", "sierra", "tango", "uniform",
493      "victor", "whisky", "x\-ray", "yankee", "zulu"
494 };
495
496 int main()
497 {
498     ENTRY e, *ep;
499     int i;
500
501     hcreate(30);
502
503     for (i = 0; i < 24; i++) {
504         e.key = data[i];
505 .\"O         /* data is just an integer, instead of a
506 .\"O            pointer to something */
507         /* ¥Ç¡¼¥¿¤Ï¡¢¥Ý¥¤¥ó¥¿¤Ç¤Ï¤Ê¤¯¡¢Ã±¤Ê¤ëÀ°¿ôÃͤǤ¢¤ë¡£ */
508         e.data = (void *) i;
509         ep = hsearch(e, ENTER);
510 .\"O         /* there should be no failures */
511         /* ¥¨¥é¡¼¤Ïµ¯¤³¤é¤Ê¤¤¤Ï¤º¤Ç¤¢¤ë¡£ */
512         if (ep == NULL) {
513             fprintf(stderr, "entry failed\\n");
514             exit(EXIT_FAILURE);
515         }
516     }
517
518     for (i = 22; i < 26; i++) {
519 .\"O         /* print two entries from the table, and
520 .\"O            show that two are not in the table */
521         /* ¥Æ¡¼¥Ö¥ë¤Ë¤¢¤ë 2 ¤Ä¤Î¥¨¥ó¥È¥ê¤òɽ¼¨¤·¡¢
522            ¤¢¤È¤Î 2 ¤Ä¤¬¥Æ¡¼¥Ö¥ë¤Ë¤Ê¤¤¤³¤È¤ò¼¨¤¹¡£ */
523         e.key = data[i];
524         ep = hsearch(e, FIND);
525         printf("%9.9s \-> %9.9s:%d\\n", e.key,
526                ep ? ep\->key : "NULL", ep ? (int)(ep\->data) : 0);
527     }
528     hdestroy();
529     exit(EXIT_SUCCESS);
530 }
531 .fi
532 .\"O .SH "SEE ALSO"
533 .SH ´ØÏ¢¹àÌÜ
534 .BR bsearch (3),
535 .BR lsearch (3),
536 .BR malloc (3),
537 .BR tsearch (3)