177298Sobrien/*
277298Sobrien * Copyright (c) 1990 Regents of the University of California.
377298Sobrien * All rights reserved.
477298Sobrien *
577298Sobrien * Redistribution and use in source and binary forms, with or without
677298Sobrien * modification, are permitted provided that the following conditions
777298Sobrien * are met:
877298Sobrien * 1. Redistributions of source code must retain the above copyright
977298Sobrien *    notice, this list of conditions and the following disclaimer.
1077298Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1177298Sobrien *    notice, this list of conditions and the following disclaimer in the
1277298Sobrien *    documentation and/or other materials provided with the distribution.
1377298Sobrien * 3. [rescinded 22 July 1999]
1477298Sobrien * 4. Neither the name of the University nor the names of its contributors
1577298Sobrien *    may be used to endorse or promote products derived from this software
1677298Sobrien *    without specific prior written permission.
1777298Sobrien *
1877298Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1977298Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2077298Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2177298Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2277298Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2377298Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2477298Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2577298Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2677298Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2777298Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2877298Sobrien * SUCH DAMAGE.
2977298Sobrien */
3077298Sobrien
3189857Sobrien/*
3289857Sobrien
3389857Sobrien@deftypefn Supplemental void* bsearch (const void *@var{key}, const void *@var{base}, size_t @var{nmemb}, size_t @var{size}, int (*@var{compar})(const void *, const void *))
3489857Sobrien
3589857SobrienPerforms a search over an array of @var{nmemb} elements pointed to by
3689857Sobrien@var{base} for a member that matches the object pointed to by @var{key}.
3789857SobrienThe size of each member is specified by @var{size}.  The array contents
3889857Sobrienshould be sorted in ascending order according to the @var{compar}
3989857Sobriencomparison function.  This routine should take two arguments pointing to
4089857Sobrienthe @var{key} and to an array member, in that order, and should return an
4189857Sobrieninteger less than, equal to, or greater than zero if the @var{key} object
4289857Sobrienis respectively less than, matching, or greater than the array member.
4389857Sobrien
4489857Sobrien@end deftypefn
4589857Sobrien
4689857Sobrien*/
4789857Sobrien
4877298Sobrien#include "config.h"
4977298Sobrien#include "ansidecl.h"
5077298Sobrien#include <sys/types.h>		/* size_t */
5177298Sobrien#include <stdio.h>
5277298Sobrien
5377298Sobrien/*
5477298Sobrien * Perform a binary search.
5577298Sobrien *
5677298Sobrien * The code below is a bit sneaky.  After a comparison fails, we
5777298Sobrien * divide the work in half by moving either left or right. If lim
5877298Sobrien * is odd, moving left simply involves halving lim: e.g., when lim
5977298Sobrien * is 5 we look at item 2, so we change lim to 2 so that we will
6077298Sobrien * look at items 0 & 1.  If lim is even, the same applies.  If lim
6177298Sobrien * is odd, moving right again involes halving lim, this time moving
6277298Sobrien * the base up one item past p: e.g., when lim is 5 we change base
6377298Sobrien * to item 3 and make lim 2 so that we will look at items 3 and 4.
6477298Sobrien * If lim is even, however, we have to shrink it by one before
6577298Sobrien * halving: e.g., when lim is 4, we still looked at item 2, so we
6677298Sobrien * have to make lim 3, then halve, obtaining 1, so that we will only
6777298Sobrien * look at item 3.
6877298Sobrien */
6977298Sobrienvoid *
70218822Sdimbsearch (register const void *key, const void *base0,
71218822Sdim         size_t nmemb, register size_t size,
72218822Sdim         register int (*compar)(const void *, const void *))
7377298Sobrien{
74218822Sdim	register const char *base = (const char *) base0;
7577298Sobrien	register int lim, cmp;
76218822Sdim	register const void *p;
7777298Sobrien
7877298Sobrien	for (lim = nmemb; lim != 0; lim >>= 1) {
7977298Sobrien		p = base + (lim >> 1) * size;
8077298Sobrien		cmp = (*compar)(key, p);
8177298Sobrien		if (cmp == 0)
82218822Sdim			return (void *)p;
8377298Sobrien		if (cmp > 0) {	/* key > p: move right */
84218822Sdim			base = (const char *)p + size;
8577298Sobrien			lim--;
8677298Sobrien		} /* else move left */
8777298Sobrien	}
8877298Sobrien	return (NULL);
8977298Sobrien}
90