1 /*
2 Copyright (c) 1991, 1993, 1994
3 The Regents of the University of California. All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8 1. Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 3. Neither the name of the University nor the names of its contributors
14 may be used to endorse or promote products derived from this software
15 without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 SUCH DAMAGE.
28 */
29 /*
30 * bsearch.c
31 * Original Author: G. Haley
32 * Rewritten by: G. Noer
33 *
34 * Searches an array of nmemb members, the initial member of which is pointed
35 * to by base, for a member that matches the object pointed to by key. The
36 * contents of the array shall be in ascending order according to a comparison
37 * function pointed to by compar. The function shall return an integer less
38 * than, equal to or greater than zero if the first argument is considered to be
39 * respectively less than, equal to or greater than the second. Returns a
40 * pointer to the matching member of the array, or a null pointer if no match
41 * is found.
42 */
43
44 /*
45 FUNCTION
46 <<bsearch>>---binary search
47
48 INDEX
49 bsearch
50
51 SYNOPSIS
52 #include <stdlib.h>
53 void *bsearch(const void *<[key]>, const void *<[base]>,
54 size_t <[nmemb]>, size_t <[size]>,
55 int (*<[compar]>)(const void *, const void *));
56
57 DESCRIPTION
58 <<bsearch>> searches an array beginning at <[base]> for any element
59 that matches <[key]>, using binary search. <[nmemb]> is the element
60 count of the array; <[size]> is the size of each element.
61
62 The array must be sorted in ascending order with respect to the
63 comparison function <[compar]> (which you supply as the last argument of
64 <<bsearch>>).
65
66 You must define the comparison function <<(*<[compar]>)>> to have two
67 arguments; its result must be negative if the first argument is
68 less than the second, zero if the two arguments match, and
69 positive if the first argument is greater than the second (where
70 ``less than'' and ``greater than'' refer to whatever arbitrary
71 ordering is appropriate).
72
73 RETURNS
74 Returns a pointer to an element of <[array]> that matches <[key]>. If
75 more than one matching element is available, the result may point to
76 any of them.
77
78 PORTABILITY
79 <<bsearch>> is ANSI.
80
81 No supporting OS subroutines are required.
82 */
83
84 #include <stdlib.h>
85
86 void *
bsearch(const void * key,const void * base,size_t nmemb,size_t size,int (* compar)(const void *,const void *))87 bsearch (const void *key,
88 const void *base,
89 size_t nmemb,
90 size_t size,
91 int (*compar) (const void *, const void *))
92 {
93 void *current;
94 size_t lower = 0;
95 size_t upper = nmemb;
96 size_t index;
97 int result;
98
99 if (nmemb == 0 || size == 0)
100 return NULL;
101
102 while (lower < upper)
103 {
104 index = (lower + upper) / 2;
105 current = (void *) (((char *) base) + (index * size));
106
107 result = compar (key, current);
108
109 if (result < 0)
110 upper = index;
111 else if (result > 0)
112 lower = index + 1;
113 else
114 return current;
115 }
116
117 return NULL;
118 }
119
120