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