OSDN Git Service

(split) LDP v3.30, v3.31 の定型的な変更内容を反映。
[linuxjm/LDP_man-pages.git] / draft / man3 / bsearch.3
1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\"
3 .\" Permission is granted to make and distribute verbatim copies of this
4 .\" manual provided the copyright notice and this permission notice are
5 .\" preserved on all copies.
6 .\"
7 .\" Permission is granted to copy and distribute modified versions of this
8 .\" manual under the conditions for verbatim copying, provided that the
9 .\" entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one.
11 .\"
12 .\" Since the Linux kernel and libraries are constantly changing, this
13 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
14 .\" responsibility for errors or omissions, or for damages resulting from
15 .\" the use of the information contained herein.  The author(s) may not
16 .\" have taken the same level of care in the production of this manual,
17 .\" which is licensed free of charge, as they might when working
18 .\" professionally.
19 .\"
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
22 .\"
23 .\" References consulted:
24 .\"     Linux libc source code
25 .\"     Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
26 .\"     386BSD man pages
27 .\" Modified Mon Mar 29 22:41:16 1993, David Metcalfe
28 .\" Modified Sat Jul 24 21:35:16 1993, Rik Faith (faith@cs.unc.edu)
29 .\"
30 .\" Japanese Version Copyright (c) 1998 NAKANO Takeo all rights reserved.
31 .\" Translated 1998-03-18, NAKANO Takeo <nakano@apm.seikei.ac.jp>
32 .\" Updated 2005-02-26, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
33 .\"
34 .TH BSEARCH 3  2003-11-01 "" "Linux Programmer's Manual"
35 .\"O .SH NAME
36 .SH Ì¾Á°
37 .\"O bsearch \- binary search of a sorted array
38 bsearch \- ¥½¡¼¥È¤µ¤ì¤¿ÇÛÎó¤òÆóʬÌÚ¸¡º÷ (binary search) ¤¹¤ë
39 .\"O .SH SYNOPSIS
40 .SH ½ñ¼°
41 .nf
42 .B #include <stdlib.h>
43 .sp
44 .BI "void *bsearch(const void *" key ", const void *" base ,
45 .BI "              size_t " nmemb ", size_t " size ,
46 .BI "              int (*" compar ")(const void *, const void *));"
47 .fi
48 .\"O .SH DESCRIPTION
49 .SH ÀâÌÀ
50 .\"O The
51 .\"O .BR bsearch ()
52 .\"O function searches an array of \fInmemb\fP objects,
53 .\"O the initial member of which is pointed to by \fIbase\fP, for a member
54 .\"O that matches the object pointed to by \fIkey\fP.
55 .\"O The size of each member
56 .\"O of the array is specified by \fIsize\fP.
57 .BR bsearch ()
58 ´Ø¿ô¤Ï \fInmemb\fP ¸Ä¤Î¥ª¥Ö¥¸¥§¥¯¥È¤«¤é¤Ê¤ëÇÛÎó¤ò¸¡º÷
59 ¤¹¤ë¡£ÇÛÎó¤ÎºÇ½é¤Î¥á¥ó¥Ð¡¼¤Ø¤Î¥Ý¥¤¥ó¥¿¤Ï \fIbase\fP ¤Ë¤è¤Ã¤ÆÍ¿¤¨¤ë¡£
60 ¥Ý¥¤¥ó¥¿ \fIkey\fP ¤Ç»²¾È¤µ¤ì¤ë¥ª¥Ö¥¸¥§¥¯¥È¤È°ìÃפ¹¤ë¥á¥ó¥Ð¡¼¤¬ÊÖ¤µ¤ì¤ë¡£
61 ÇÛÎóÃæ¤Î³Æ¡¹¤Î¥á¥ó¥Ð¡¼¤Î¥µ¥¤¥º¤Ï \fIsize\fP ¤Ë¤è¤Ã¤Æ»ØÄꤹ¤ë¡£
62 .PP
63 .\"O The contents of the array should be in ascending sorted order according
64 .\"O to the comparison function referenced by \fIcompar\fP.
65 .\"O The \fIcompar\fP
66 .\"O routine is expected to have two arguments which point to the \fIkey\fP
67 .\"O object and to an array member, in that order, and should return an integer
68 .\"O less than, equal to, or greater than zero if the \fIkey\fP object is found,
69 .\"O respectively, to be less than, to match, or be greater than the array
70 .\"O member.
71 ÇÛÎó¤ÎÆâÍƤÏÈæ³Ó´Ø¿ô \fIcompar\fP ¤Ë´ð¤Å¤­¡¢¾º½ç¤Ë¥½¡¼¥È¤µ¤ì¤Æ¤¤¤Ê¤±¤ì
72 ¤Ð¤Ê¤é¤Ê¤¤¡£ \fIcompar\fP ¥ë¡¼¥Á¥ó¤ÏÆó¤Ä¤Î°ú¿ô¤ò¼è¤ë´Ø¿ô¤Ç¡¢°ì¤Ä
73 Ìܤˠ\fIkey\fP ¤Ø¤Î¥Ý¥¤¥ó¥¿¡¢¼¡¤ËÇÛÎó¤Î¥á¥ó¥Ð¡¼¤Ø¤Î¥Ý¥¤¥ó¥¿¤ò¼è¤ë¡£
74 ¤³¤Î½ç¤Ë»ØÄꤷ¤¿¤È¤­¡¢ \fIkey\fP ¤¬ÇÛÎó¥á¥ó¥Ð¡¼¤è¤ê¾®¤µ¤¤¤È¤­¤Ë¤Ï
75 Éé¤ÎÀ°¿ô¤ò¡¢Â礭¤¤¤È¤­¤Ë¤ÏÀµ¤ÎÀ°¿ô¤ò¡¢°ìÃפ·¤¿¤È¤­¤Ë¤Ï 0 ¤ò¡¢¤½¤ì¤¾¤ì
76 \fIcompar\fP ¤ÏÊÖ¤µ¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
77 .\"O .SH "RETURN VALUE"
78 .SH ÊÖ¤êÃÍ
79 .\"O The
80 .\"O .BR bsearch ()
81 .\"O function returns a pointer to a matching member of the
82 .\"O array, or NULL if no match is found.  If there are multiple elements that
83 .\"O match the key, the element returned is unspecified.
84 .BR bsearch ()
85 ´Ø¿ô¤Ï¡¢ÇÛÎó¤Î¥á¥ó¥Ð¡¼¤Î¤¦¤Á¡¢°ìÃפ·¤¿¤â¤Î¤Ø¤Î¥Ý¥¤¥ó¥¿¤ò
86 ÊÖ¤¹¡£¸«¤Ä¤«¤é¤Ê¤«¤Ã¤¿¤È¤­¤Ï NULL ¤òÊÖ¤¹¡£ \fIkey\fP ¤È°ìÃפ·¤¿¥á¥ó¥Ð¡¼¤¬
87 Ê£¿ô¤¢¤ë¤È¤­¡¢¤½¤Î¤¦¤Á¤Î¤É¤Î¥á¥ó¥Ð¡¼¤¬ÊÖ¤µ¤ì¤ë¤«¤Ï¤ï¤«¤é¤Ê¤¤¡£
88 .\"O .SH "CONFORMING TO"
89 .SH ½àµò
90 SVr4, 4.3BSD, POSIX.1-2001, C89, C99.
91 .\"O .SH EXAMPLE
92 .SH Îã
93 .\"O The example below first sorts an array of structures using
94 .\"O .BR qsort (3),
95 .\"O then retrieves desired elements using
96 .\"O .BR bsearch ().
97 °Ê²¼¤ÎÎã¤Ï¡¢
98 .BR qsort (3)
99 ¤ò»È¤Ã¤Æ¹½Â¤ÂΤÎÇÛÎó¤ÎʤӴ¹¤¨¤ò¹Ô¤Ã¤¿¸å¡¢
100 ½ê˾¤ÎÍ×ÁǤò
101 .BR bsearch ()
102 ¤ò»È¤Ã¤Æ¼èÆÀ¤¹¤ë¤â¤Î¤Ç¤¢¤ë¡£
103 .sp
104 .nf
105 #include <stdio.h>
106 #include <stdlib.h>
107 #include <string.h>
108
109 struct mi {
110     int nr;
111     char *name;
112 } months[] = {
113     { 1, "jan" }, { 2, "feb" }, { 3, "mar" }, { 4, "apr" },
114     { 5, "may" }, { 6, "jun" }, { 7, "jul" }, { 8, "aug" },
115     { 9, "sep" }, {10, "oct" }, {11, "nov" }, {12, "dec" }
116 };
117
118 #define nr_of_months (sizeof(months)/sizeof(months[0]))
119
120 static int
121 compmi(const void *m1, const void *m2)
122 {
123     struct mi *mi1 = (struct mi *) m1;
124     struct mi *mi2 = (struct mi *) m2;
125     return strcmp(mi1\->name, mi2\->name);
126 }
127
128 int
129 main(int argc, char **argv)
130 {
131     int i;
132
133     qsort(months, nr_of_months, sizeof(struct mi), compmi);
134     for (i = 1; i < argc; i++) {
135         struct mi key, *res;
136         key.name = argv[i];
137         res = bsearch(&key, months, nr_of_months,
138                       sizeof(struct mi), compmi);
139         if (res == NULL)
140             printf("\(aq%s\(aq: unknown month\en", argv[i]);
141         else
142             printf("%s: month #%d\en", res\->name, res\->nr);
143     }
144     exit(EXIT_SUCCESS);
145 }
146 .fi
147 .\"O .\" this example referred to in qsort.3
148 .\" ¤³¤ÎÎã¤Ï qsort.3 ¤Ç»²¾È¤µ¤ì¤Æ¤¤¤ë¡£
149 .\"O .SH "SEE ALSO"
150 .SH ´ØÏ¢¹àÌÜ
151 .BR hsearch (3),
152 .BR lsearch (3),
153 .BR qsort (3),
154 .BR tsearch (3)