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>
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.
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.
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.
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,
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>
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>
45 .\"WORD: hash table ハッシュテーブル
47 .\"WORD: allocate 割り当て
48 .\"WORD: NUL-terminated ヌル文字 \0 で終端された
50 .\"WORD: character 文字型
53 .TH HSEARCH 3 2011-09-10 "GNU" "Linux Programmer's Manual"
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 \- ハッシュテーブルの管理
63 .B #include <search.h>
65 .BI "int hcreate(size_t " nel );
67 .BI "ENTRY *hsearch(ENTRY " item ", ACTION " action );
69 .B "void hdestroy(void);"
71 .\"O .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
72 .BR "#define _GNU_SOURCE" " /* feature_test_macros(7) 参照 */"
74 .B #include <search.h>
76 .BI "int hcreate_r(size_t " nel ", struct hsearch_data *" htab );
78 .BI "int hsearch_r(ENTRY " item ", ACTION " action ", ENTRY **" retval ,
79 .BI " struct hsearch_data *" htab );
81 .BI "void hdestroy_r(struct hsearch_data *" htab );
85 .\"O The three functions
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.
96 の 3 つの関数を利用すると、キー (文字列) と対応するデータから構成される
97 エントリを格納できるハッシュ検索テーブルを作成、管理することができる。
98 これらの関数を使って、一度に使用できるのは一つのハッシュテーブルだけである。
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,
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).
116 の 3 つの関数はリエントラント版で、これらを利用すると、
117 一つのプログラムで同時に複数のハッシュテーブルを使うことができる。
120 は関数の操作対象となるテーブルを示す構造体へのポインタである。
121 プログラマはこの構造体をブラックボックスとして扱うべきである
122 (つまり、この構造体のフィールドに直接アクセスしたり変更したり
125 .\"O First a hash table must be created using
127 .\"O The argument \fInel\fP specifies the maximum number of entries
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.
134 関数によってハッシュテーブルを作成しなければならない。
135 引き数 \fInel\fP でテーブルの最大エントリ数を指定する
136 (この最大値は後で変更することはできないので、よく考えて選択すること)。
137 作成されるハッシュテーブルの性能を向上させるために、
138 関数内部の実装によりこの値は増やされる場合もある。
139 .\" e.g., in glibc it is raised to the next higher prime number
142 .\"O .BR hcreate_r ()
143 .\"O function performs the same task as
145 .\"O but for the table described by the structure
147 .\"O The structure pointed to by
149 .\"O must be zeroed before the first call to
150 .\"O .BR hcreate_r ().
160 を初めて呼び出す前に 0 で埋めておかなければならない。
164 .\"O frees the memory occupied by the hash table that was created by
168 .\"O a new hash table can be created using
171 .\"O .BR hdestroy_r ()
172 .\"O function performs the analogous task for a hash table described by
174 .\"O which was previously created using
175 .\"O .BR hcreate_r ().
179 で作成されたハッシュテーブルが占有していたメモリを解放する。
180 ハッシュテーブルによって占有されていたメモリを解放し、
185 を使って新しいハッシュテーブルを作成することができる。
191 で示されるハッシュテーブルに対して実行する。
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.
200 関数は、\fIitem\fP と同じキーを持つ項目をハッシュテーブルから
201 検索し、項目が見つかった場合にはその項目へのポインタを返す
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 の中で
213 typedef struct entry {
220 .\"O The field \fIkey\fP points to a null-terminated string which is the
222 .\"O The field \fIdata\fP points to data that is associated with that key.
223 フィールド \fIkey\fP は検索キーとなる NULL 終端された文字列を指す。
224 フィールド \fIdata\fP は、このキーに対応するデータを指す。
226 .\"O The argument \fIaction\fP determines what
228 .\"O does after an unsuccessful search.
229 検索が失敗した後の動作は、引き数 \fIaction\fP により決まる。
230 .\"O This argument must either have the value
232 .\"O meaning insert a copy of
234 .\"O (and return a pointer to the new hash table entry as the function result),
237 .\"O meaning that NULL should be returned.
254 (関数の結果として新しいハッシュテーブルエントリへのポインタを返す)、
265 .\"O .BR hsearch_r ()
266 .\"O function is like
268 .\"O but operates on the hash table described by
271 .\"O .BR hsearch_r ()
272 .\"O function differs from
274 .\"O in that a pointer to the found item is returned in
276 .\"O rather than as the function result.
282 で示されるハッシュテーブルに対して処理を行う。
286 と異なるのは、見つかった項目へのポインタを、
290 .\"O .SH "RETURN VALUE"
294 .\"O .BR hcreate_r ()
295 .\"O return nonzero on success.
296 .\"O They return 0 on error.
305 .\"O returns a pointer to an entry in the hash table.
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.
313 は、ハッシュテーブル内のエントリへのポインタを返す。
318 \fIaction\fP が \fBENTER\fP でハッシュテーブルがいっぱいの場合か、
319 \fIaction\fP が \fBFIND\fP で \fIitem\fP がハッシュテーブル内に
321 .\"O .BR hsearch_r ()
322 .\"O returns nonzero on success, and 0 on error.
324 は、成功すると 0 以外を返し、エラーの場合 0 を返す。
328 .\"O .BR hcreate_r ()
330 .\"O .BR hdestroy_r ()
331 .\"O can fail for the following reasons:
345 .\"O .BR hsearch_r ()
346 .\"O can fail for the following reasons:
357 .\"O was not found in the table,
358 .\"O and there was no room in the table to add a new entry.
365 テーブルに新しいエントリを追加する余地がなかった。
373 .\"O was not found in the table.
381 .\"O POSIX.1-2001 only specifies the
384 POSIX.1-2001 が規定しているのは、エラー
387 .\"O .SH "CONFORMING TO"
394 .\"O are from SVr4, and are described in POSIX.1-2001.
399 は SVr4 から導入されたもので、POSIX.1-2001 に記述されている。
405 .\"O are GNU extensions.
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
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 テーブルに十分な空き領域がある場合に効率がよくなる。
423 を、呼び出し側がテーブルに格納しようと思っている
424 エントリの最大数より少なくとも 25% は大きな値にすべきである。
429 .\"O .BR hdestroy_r ()
430 .\"O functions do not free the buffers pointed to by the
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.)
440 は、ハッシュテーブルのエントリの要素である
445 (これができないのは、これらのバッファが動的に割り当てられたのかを
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 プログラムでは、これらのバッファを解放できるように管理用のデータ構造を
456 (解放が必要となる理由は、たいていは、プログラム自身と生存期間が同じ
457 ハッシュテーブルを一つだけ作成するのではなく、そのプログラムでは複数の
458 ハッシュテーブルを繰り返して作成したり破棄したりするからであろう)。
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 でも
471 (バージョン 2.3 より前の) libc と glibc の実装はこの規格に違反しており、
472 この状況で、指定された \fIkey\fP に対応する \fIdata\fP が更新される。
474 .\"O Individual hash table entries can be added, but not deleted.
475 ハッシュテーブルエントリーの追加はできるが、削除ができない。
479 .\"O The following program inserts 24 items into a hash table, then prints
481 次のプログラムは、ハッシュテーブルに 24 個の項目を挿入し、
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"
503 for (i = 0; i < 24; i++) {
505 .\"O /* data is just an integer, instead of a
506 .\"O pointer to something */
507 /* データは、ポインタではなく、単なる整数値である。 */
509 ep = hsearch(e, ENTER);
510 .\"O /* there should be no failures */
511 /* エラーは起こらないはずである。 */
513 fprintf(stderr, "entry failed\\n");
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 つがテーブルにないことを示す。 */
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);