OSDN Git Service

edd36a58385415f1d9d3c784b779aac292310861
[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)