xref: /freebsd/sys/libkern/bsearch.c (revision 531c9dd5f49c6f3c8ab3f2a3f2fd12d4fbcf0443)
13393f8daSKenneth D. Merry /*
23393f8daSKenneth D. Merry  * Copyright (c) 1990, 1993
33393f8daSKenneth D. Merry  *	The Regents of the University of California.  All rights reserved.
43393f8daSKenneth D. Merry  *
53393f8daSKenneth D. Merry  * Redistribution and use in source and binary forms, with or without
63393f8daSKenneth D. Merry  * modification, are permitted provided that the following conditions
73393f8daSKenneth D. Merry  * are met:
83393f8daSKenneth D. Merry  * 1. Redistributions of source code must retain the above copyright
93393f8daSKenneth D. Merry  *    notice, this list of conditions and the following disclaimer.
103393f8daSKenneth D. Merry  * 2. Redistributions in binary form must reproduce the above copyright
113393f8daSKenneth D. Merry  *    notice, this list of conditions and the following disclaimer in the
123393f8daSKenneth D. Merry  *    documentation and/or other materials provided with the distribution.
133393f8daSKenneth D. Merry  * 3. All advertising materials mentioning features or use of this software
143393f8daSKenneth D. Merry  *    must display the following acknowledgement:
153393f8daSKenneth D. Merry  *	This product includes software developed by the University of
163393f8daSKenneth D. Merry  *	California, Berkeley and its contributors.
173393f8daSKenneth D. Merry  * 4. Neither the name of the University nor the names of its contributors
183393f8daSKenneth D. Merry  *    may be used to endorse or promote products derived from this software
193393f8daSKenneth D. Merry  *    without specific prior written permission.
203393f8daSKenneth D. Merry  *
213393f8daSKenneth D. Merry  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
223393f8daSKenneth D. Merry  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
233393f8daSKenneth D. Merry  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
243393f8daSKenneth D. Merry  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
253393f8daSKenneth D. Merry  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
263393f8daSKenneth D. Merry  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
273393f8daSKenneth D. Merry  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
283393f8daSKenneth D. Merry  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
293393f8daSKenneth D. Merry  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
303393f8daSKenneth D. Merry  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
313393f8daSKenneth D. Merry  * SUCH DAMAGE.
323393f8daSKenneth D. Merry  *
333393f8daSKenneth D. Merry  * $FreeBSD$
343393f8daSKenneth D. Merry  */
353393f8daSKenneth D. Merry 
363393f8daSKenneth D. Merry #if defined(LIBC_SCCS) && !defined(lint)
373393f8daSKenneth D. Merry static char sccsid[] = "@(#)bsearch.c	8.1 (Berkeley) 6/4/93";
383393f8daSKenneth D. Merry #endif /* LIBC_SCCS and not lint */
393393f8daSKenneth D. Merry 
400b1ae809SJulian Elischer #include <sys/param.h>
410b1ae809SJulian Elischer #include <machine/ansi.h>
420b1ae809SJulian Elischer #include <sys/libkern.h>
433393f8daSKenneth D. Merry 
443393f8daSKenneth D. Merry /*
453393f8daSKenneth D. Merry  * Perform a binary search.
463393f8daSKenneth D. Merry  *
473393f8daSKenneth D. Merry  * The code below is a bit sneaky.  After a comparison fails, we
483393f8daSKenneth D. Merry  * divide the work in half by moving either left or right. If lim
493393f8daSKenneth D. Merry  * is odd, moving left simply involves halving lim: e.g., when lim
503393f8daSKenneth D. Merry  * is 5 we look at item 2, so we change lim to 2 so that we will
513393f8daSKenneth D. Merry  * look at items 0 & 1.  If lim is even, the same applies.  If lim
523393f8daSKenneth D. Merry  * is odd, moving right again involes halving lim, this time moving
533393f8daSKenneth D. Merry  * the base up one item past p: e.g., when lim is 5 we change base
543393f8daSKenneth D. Merry  * to item 3 and make lim 2 so that we will look at items 3 and 4.
553393f8daSKenneth D. Merry  * If lim is even, however, we have to shrink it by one before
563393f8daSKenneth D. Merry  * halving: e.g., when lim is 4, we still looked at item 2, so we
573393f8daSKenneth D. Merry  * have to make lim 3, then halve, obtaining 1, so that we will only
583393f8daSKenneth D. Merry  * look at item 3.
593393f8daSKenneth D. Merry  */
603393f8daSKenneth D. Merry void *
613393f8daSKenneth D. Merry bsearch(key, base0, nmemb, size, compar)
623393f8daSKenneth D. Merry 	register const void *key;
633393f8daSKenneth D. Merry 	const void *base0;
643393f8daSKenneth D. Merry 	size_t nmemb;
653393f8daSKenneth D. Merry 	register size_t size;
663393f8daSKenneth D. Merry 	register int (*compar) __P((const void *, const void *));
673393f8daSKenneth D. Merry {
683393f8daSKenneth D. Merry 	register const char *base = base0;
693393f8daSKenneth D. Merry 	register size_t lim;
703393f8daSKenneth D. Merry 	register int cmp;
713393f8daSKenneth D. Merry 	register const void *p;
723393f8daSKenneth D. Merry 
733393f8daSKenneth D. Merry 	for (lim = nmemb; lim != 0; lim >>= 1) {
743393f8daSKenneth D. Merry 		p = base + (lim >> 1) * size;
753393f8daSKenneth D. Merry 		cmp = (*compar)(key, p);
763393f8daSKenneth D. Merry 		if (cmp == 0)
77531c9dd5SPeter Wemm 			return ((void *)(uintptr_t)p);
783393f8daSKenneth D. Merry 		if (cmp > 0) {	/* key > p: move right */
793393f8daSKenneth D. Merry 			base = (const char *)p + size;
803393f8daSKenneth D. Merry 			lim--;
813393f8daSKenneth D. Merry 		}		/* else move left */
823393f8daSKenneth D. Merry 	}
833393f8daSKenneth D. Merry 	return (NULL);
843393f8daSKenneth D. Merry }
85