xref: /freebsd/sys/libkern/bsearch.c (revision fbbd9655e5107c68e4e0146ff22b73d7350475bc)
1d6ea0262SWarner Losh /*-
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.
13*fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
143393f8daSKenneth D. Merry  *    may be used to endorse or promote products derived from this software
153393f8daSKenneth D. Merry  *    without specific prior written permission.
163393f8daSKenneth D. Merry  *
173393f8daSKenneth D. Merry  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
183393f8daSKenneth D. Merry  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
193393f8daSKenneth D. Merry  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
203393f8daSKenneth D. Merry  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
213393f8daSKenneth D. Merry  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
223393f8daSKenneth D. Merry  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
233393f8daSKenneth D. Merry  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
243393f8daSKenneth D. Merry  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
253393f8daSKenneth D. Merry  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
263393f8daSKenneth D. Merry  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
273393f8daSKenneth D. Merry  * SUCH DAMAGE.
283393f8daSKenneth D. Merry  */
293393f8daSKenneth D. Merry 
303393f8daSKenneth D. Merry #if defined(LIBC_SCCS) && !defined(lint)
313393f8daSKenneth D. Merry static char sccsid[] = "@(#)bsearch.c	8.1 (Berkeley) 6/4/93";
323393f8daSKenneth D. Merry #endif /* LIBC_SCCS and not lint */
33ab0de15bSDavid E. O'Brien #include <sys/cdefs.h>
34ab0de15bSDavid E. O'Brien __FBSDID("$FreeBSD$");
353393f8daSKenneth D. Merry 
360b1ae809SJulian Elischer #include <sys/param.h>
370b1ae809SJulian Elischer #include <sys/libkern.h>
383393f8daSKenneth D. Merry 
393393f8daSKenneth D. Merry /*
403393f8daSKenneth D. Merry  * Perform a binary search.
413393f8daSKenneth D. Merry  *
423393f8daSKenneth D. Merry  * The code below is a bit sneaky.  After a comparison fails, we
433393f8daSKenneth D. Merry  * divide the work in half by moving either left or right. If lim
443393f8daSKenneth D. Merry  * is odd, moving left simply involves halving lim: e.g., when lim
453393f8daSKenneth D. Merry  * is 5 we look at item 2, so we change lim to 2 so that we will
463393f8daSKenneth D. Merry  * look at items 0 & 1.  If lim is even, the same applies.  If lim
473393f8daSKenneth D. Merry  * is odd, moving right again involes halving lim, this time moving
483393f8daSKenneth D. Merry  * the base up one item past p: e.g., when lim is 5 we change base
493393f8daSKenneth D. Merry  * to item 3 and make lim 2 so that we will look at items 3 and 4.
503393f8daSKenneth D. Merry  * If lim is even, however, we have to shrink it by one before
513393f8daSKenneth D. Merry  * halving: e.g., when lim is 4, we still looked at item 2, so we
523393f8daSKenneth D. Merry  * have to make lim 3, then halve, obtaining 1, so that we will only
533393f8daSKenneth D. Merry  * look at item 3.
543393f8daSKenneth D. Merry  */
553393f8daSKenneth D. Merry void *
563393f8daSKenneth D. Merry bsearch(key, base0, nmemb, size, compar)
57484820d4SConrad Meyer 	const void *key;
583393f8daSKenneth D. Merry 	const void *base0;
593393f8daSKenneth D. Merry 	size_t nmemb;
60484820d4SConrad Meyer 	size_t size;
61484820d4SConrad Meyer 	int (*compar)(const void *, const void *);
623393f8daSKenneth D. Merry {
63484820d4SConrad Meyer 	const char *base = base0;
64484820d4SConrad Meyer 	size_t lim;
65484820d4SConrad Meyer 	int cmp;
66484820d4SConrad Meyer 	const void *p;
673393f8daSKenneth D. Merry 
683393f8daSKenneth D. Merry 	for (lim = nmemb; lim != 0; lim >>= 1) {
693393f8daSKenneth D. Merry 		p = base + (lim >> 1) * size;
703393f8daSKenneth D. Merry 		cmp = (*compar)(key, p);
713393f8daSKenneth D. Merry 		if (cmp == 0)
72531c9dd5SPeter Wemm 			return ((void *)(uintptr_t)p);
733393f8daSKenneth D. Merry 		if (cmp > 0) {	/* key > p: move right */
743393f8daSKenneth D. Merry 			base = (const char *)p + size;
753393f8daSKenneth D. Merry 			lim--;
763393f8daSKenneth D. Merry 		}		/* else move left */
773393f8daSKenneth D. Merry 	}
783393f8daSKenneth D. Merry 	return (NULL);
793393f8daSKenneth D. Merry }
80