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