1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
.\" Hey Emacs! This file is -*- nroff -*- source.
.\" Copyright 1993 Ulrich Drepper (drepper@karlsruhe.gmd.de)
.\"
.\" This is free documentation; you can redistribute it and/or
.\" modify it under the terms of the GNU General Public License as
.\" published by the Free Software Foundation; either version 2 of
.\" the License, or (at your option) any later version.
.\"
.\" The GNU General Public License's references to "object code"
.\" and "executables" are to be interpreted as the output of any
.\" document formatting or typesetting system, including
.\" intermediate and printed output.
.\"
.\" This manual is distributed in the hope that it will be useful,
.\" but WITHOUT ANY WARRANTY; without even the implied warranty of
.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
.\" GNU General Public License for more details.
.\"
.\" You should have received a copy of the GNU General Public
.\" License along with this manual; if not, write to the Free
.\" Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111,
.\" USA.
.\"
.\" References consulted:
.\" SunOS 4.1.1 man pages
.\" Modified Sat Sep 30 21:52:01 1995 by Jim Van Zandt <jrv@vanzandt.mv.com>
.\" Remarks from dhw@gamgee.acad.emich.edu Fri Jun 19 06:46:31 1998
.\" Modified 2001-12-26, aeb
.\"
.\" Japanese Version Copyright (c) 1998 George Momma
.\" all rights reserved.
.\" Translated Sat May 23 11:01:40 JST 1998
.\" by George Momma <momma@wakhok.ac.jp>
.\" Updated & Modified Mon Oct 15 19:06:13 JST 2001
.\" by Yuichi SATO <ysato@h4.dion.ne.jp>
.\" Updated & Modified Thu Jan 3 16:21:49 JST 2002 by Yuichi SATO
.\"
.\"WORD: hash table ϥåơ֥
.\"WORD: entry ȥ
.\"WORD: allocate
.\"WORD: NUL-terminated ̥ʸ \0 ǽü줿
.\"WORD: pointer ݥ
.\"WORD: character ʸ
.\"WORD: item
.TH HSEARCH 3 2001-12-26 "GNU" "Linux Programmer's Manual"
.SH ̾
hcreate, hdestroy, hsearch \- ϥåơ֥δ
.SH
.B #include <search.h>
.sp
.BI "int hcreate(size_t " nel );
.sp
.BI "ENTRY *hsearch(ENTRY " item ", ACTION " action );
.sp
.B "void hdestroy(void);"
.sp 2
.B #define _GNU_SOURCE
.br
.B #include <search.h>
.sp
.BI "int hcreate_r(size_t " nel ", struct hsearch_data *" tab );
.sp
.BI "int *hsearch_r(ENTRY " item ", ACTION " action ,
.BI "ENTRY **" ret ", struct hsearch_data *" tab );
.sp
.BI "void hdestroy_r(struct hsearch_data *" tab );
.SH
3 Ĥδؿ
.BR hcreate ,
.BR hsearch ,
.BR hdestroy
Ѥȡ桼ϥǤդΥǡϢŤ
(1 ٤ 1 Ĥ) ϥåơ֥뤳ȤǤ롣
3 Ĥδؿ
.BR hcreate_r ,
.BR hsearch_r ,
.BR hdestroy_r
ϥꥨȥȤʴؿǡ2 İʾΥơ֥Ѥ뤳ȤǤ롣
.PP
ǽˡؿ \fBhcreate()\fP ˤäƥơ֥ʤФʤʤ
\fInel\fP ϡơ֥ΥȥͽۤͤǤ롣
ؿ \fBhcreate()\fP ϡϥåơ֥ǽ夵뤿
ͤ䤹⤢롣
.PP
б \fBhdestroy()\fP ؿϡ
ϥåơ֥ˤäͭƤ
ơ֥Ǥ褦ˤ롣
.PP
\fIitem\fP \fBENTRY\fP Ǥꡢ \fI<search.h>\fP
typedef ƤꡢǤޤǤ:
.sp
.nf
typedef struct entry {
char *\fIkey\fP;
void *\fIdata\fP;
} ENTRY;
.fi
.sp
ե \fIkey\fP ϡ̥ʸ \\0 ǽü줿ʸؤ
Ȥʤ롣
ե \fIdata\fP ϡΥбǡؤ
ؿ \fBhsearch()\fP ϡ\fIitem\fP ƱĹܤ
ϥåơ֥椫õФιܤؤΥݥ֤
(ƱǤ뤫פ
.BR strcmp (3)
ȤäƷꤵ)
õԤ硢 \fIaction\fP ˤä
\fBhsearch()\fP ưޤ롣
\fBENTER\fP \fIitem\fP Υԡ
\fBFIND\fP \fBNULL\fP ֤Ȥ̣롣
.SH ֤
ϥåơ֥ѤΥ꤬ݤԤ硢
\fBhcreate()\fP \fBhcreate_r()\fP 0 ֤
0 ʳ֤ͤ
.LP
\fBhsearch()\fP ϡ\fIaction\fP \fBENTER\fP
ϥåơ֥뤬äѤξ硢
ޤ \fIaction\fP \fBFIND\fP
\fIitem\fP ϥåơ֥˸Ĥʤ硢
\fBNULL\fP ֤
.LP
\fBhsearch_r()\fP ϡ\fIaction\fP \fBENTER\fP
ϥåơ֥뤬äѤξ硢0 ֤
ʳξ 0 ʳ֤ͤ
.SH 顼
.B ENOMEM
꤬ʤ
.SH
ؿ
.BR hcreate ,
.BR hsearch ,
.BR hdestroy
SVID Ƴ줿ΤǡPOSIX 1003.1-2001 ˵ҤƤ롣
ؿ
.BR hcreate_r ,
.BR hsearch_r ,
.BR hdestroy_r
GNU γĥǤ롣
.SH Х
SVID POSIX 1003.1-2001 Ǥϡ
\fIaction\fP ϸԤȤˤ̣ġ
äƸ硢\fIaction\fP ͤ ENTER Ǥⲿ⤹٤ǤϤʤ
ξ硢libc glibc μǤ
\fIkey\fP Ϳ \fIdata\fP 롣
.LP
ġΥϥåơ֥륨ȥɲäǤ뤬Ǥʤ
.SH
.PP
Υץϡϥåơ֥ 24 Ĥιܤ
줫餽ΤΤĤɽ롣
.nf
#include <stdio.h>
#include <search.h>
char *data[] = { "alpha", "bravo", "charlie", "delta",
"echo", "foxtrot", "golf", "hotel", "india", "juliet",
"kilo", "lima", "mike", "november", "oscar", "papa",
"quebec", "romeo", "sierra", "tango", "uniform",
"victor", "whisky", "x-ray", "yankee", "zulu"
};
int main() {
ENTRY e, *ep;
int i;
/* ʥơ֥dzϤ礭ϡޤưʤ*/
hcreate(30);
for (i = 0; i < 24; i++) {
e.key = data[i];
/* ǡϡݥǤϤʤñʤͤǤ롣*/
e.data = (char *)i;
ep = hsearch(e, ENTER);
/* 顼ϵʤϤǤ롣*/
if (ep == NULL) {
fprintf(stderr, "entry failed\\n");
exit(1);
}
}
for (i = 22; i < 26; i++) {
/* ơ֥ˤ 2 ĤΥȥɽ
Ȥ 2 Ĥơ֥ˤʤȤ*/
e.key = data[i];
ep = hsearch(e, FIND);
printf("%9.9s -> %9.9s:%d\\n", e.key,
ep ? ep->key : "NULL",
ep ? (int)(ep->data) : 0);
}
return 0;
}
.fi
.SH Ϣ
.BR bsearch (3),
.BR lsearch (3),
.BR tsearch (3),
.BR malloc (3)
|