1*7d62b00eSchristos /*
2*7d62b00eSchristos * Copyright (c) 1990 Regents of the University of California.
3*7d62b00eSchristos * All rights reserved.
4*7d62b00eSchristos *
5*7d62b00eSchristos * Redistribution and use in source and binary forms, with or without
6*7d62b00eSchristos * modification, are permitted provided that the following conditions
7*7d62b00eSchristos * are met:
8*7d62b00eSchristos * 1. Redistributions of source code must retain the above copyright
9*7d62b00eSchristos * notice, this list of conditions and the following disclaimer.
10*7d62b00eSchristos * 2. Redistributions in binary form must reproduce the above copyright
11*7d62b00eSchristos * notice, this list of conditions and the following disclaimer in the
12*7d62b00eSchristos * documentation and/or other materials provided with the distribution.
13*7d62b00eSchristos * 3. [rescinded 22 July 1999]
14*7d62b00eSchristos * 4. Neither the name of the University nor the names of its contributors
15*7d62b00eSchristos * may be used to endorse or promote products derived from this software
16*7d62b00eSchristos * without specific prior written permission.
17*7d62b00eSchristos *
18*7d62b00eSchristos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19*7d62b00eSchristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*7d62b00eSchristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*7d62b00eSchristos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22*7d62b00eSchristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23*7d62b00eSchristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24*7d62b00eSchristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25*7d62b00eSchristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26*7d62b00eSchristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27*7d62b00eSchristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28*7d62b00eSchristos * SUCH DAMAGE.
29*7d62b00eSchristos */
30*7d62b00eSchristos
31*7d62b00eSchristos /*
32*7d62b00eSchristos
33*7d62b00eSchristos @deftypefn Supplemental void* bsearch_r (const void *@var{key}, @
34*7d62b00eSchristos const void *@var{base}, size_t @var{nmemb}, size_t @var{size}, @
35*7d62b00eSchristos int (*@var{compar})(const void *, const void *, void *), void *@var{arg})
36*7d62b00eSchristos
37*7d62b00eSchristos Performs a search over an array of @var{nmemb} elements pointed to by
38*7d62b00eSchristos @var{base} for a member that matches the object pointed to by @var{key}.
39*7d62b00eSchristos The size of each member is specified by @var{size}. The array contents
40*7d62b00eSchristos should be sorted in ascending order according to the @var{compar}
41*7d62b00eSchristos comparison function. This routine should take three arguments: the first
42*7d62b00eSchristos two point to the @var{key} and to an array member, and the last is passed
43*7d62b00eSchristos down unchanged from @code{bsearch_r}'s last argument. It should return an
44*7d62b00eSchristos integer less than, equal to, or greater than zero if the @var{key} object
45*7d62b00eSchristos is respectively less than, matching, or greater than the array member.
46*7d62b00eSchristos
47*7d62b00eSchristos @end deftypefn
48*7d62b00eSchristos
49*7d62b00eSchristos */
50*7d62b00eSchristos
51*7d62b00eSchristos #include "config.h"
52*7d62b00eSchristos #include "ansidecl.h"
53*7d62b00eSchristos #include <sys/types.h> /* size_t */
54*7d62b00eSchristos #include <stdio.h>
55*7d62b00eSchristos
56*7d62b00eSchristos /*
57*7d62b00eSchristos * Perform a binary search.
58*7d62b00eSchristos *
59*7d62b00eSchristos * The code below is a bit sneaky. After a comparison fails, we
60*7d62b00eSchristos * divide the work in half by moving either left or right. If lim
61*7d62b00eSchristos * is odd, moving left simply involves halving lim: e.g., when lim
62*7d62b00eSchristos * is 5 we look at item 2, so we change lim to 2 so that we will
63*7d62b00eSchristos * look at items 0 & 1. If lim is even, the same applies. If lim
64*7d62b00eSchristos * is odd, moving right again involes halving lim, this time moving
65*7d62b00eSchristos * the base up one item past p: e.g., when lim is 5 we change base
66*7d62b00eSchristos * to item 3 and make lim 2 so that we will look at items 3 and 4.
67*7d62b00eSchristos * If lim is even, however, we have to shrink it by one before
68*7d62b00eSchristos * halving: e.g., when lim is 4, we still looked at item 2, so we
69*7d62b00eSchristos * have to make lim 3, then halve, obtaining 1, so that we will only
70*7d62b00eSchristos * look at item 3.
71*7d62b00eSchristos */
72*7d62b00eSchristos void *
bsearch_r(const void * key,const void * base0,size_t nmemb,size_t size,int (* compar)(const void *,const void *,void *),void * arg)73*7d62b00eSchristos bsearch_r (const void *key, const void *base0,
74*7d62b00eSchristos size_t nmemb, size_t size,
75*7d62b00eSchristos int (*compar)(const void *, const void *, void *),
76*7d62b00eSchristos void *arg)
77*7d62b00eSchristos {
78*7d62b00eSchristos const char *base = (const char *) base0;
79*7d62b00eSchristos int lim, cmp;
80*7d62b00eSchristos const void *p;
81*7d62b00eSchristos
82*7d62b00eSchristos for (lim = nmemb; lim != 0; lim >>= 1) {
83*7d62b00eSchristos p = base + (lim >> 1) * size;
84*7d62b00eSchristos cmp = (*compar)(key, p, arg);
85*7d62b00eSchristos if (cmp == 0)
86*7d62b00eSchristos return (void *)p;
87*7d62b00eSchristos if (cmp > 0) { /* key > p: move right */
88*7d62b00eSchristos base = (const char *)p + size;
89*7d62b00eSchristos lim--;
90*7d62b00eSchristos } /* else move left */
91*7d62b00eSchristos }
92*7d62b00eSchristos return (NULL);
93*7d62b00eSchristos }
94