xref: /onnv-gate/usr/src/common/util/bsearch.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
53836Sat204353  * Common Development and Distribution License (the "License").
63836Sat204353  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*6812Sraf 
22*6812Sraf /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24*6812Sraf  * Use is subject to license terms.
25*6812Sraf  */
26*6812Sraf 
270Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate  * Binary search algorithm, generalized from Knuth (6.2.1) Algorithm B.
340Sstevel@tonic-gate  */
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #if !defined(_BOOT) && !defined(_KMDB)
37*6812Sraf #include "lint.h"
380Sstevel@tonic-gate #endif /* !_BOOT && !_KMDB */
390Sstevel@tonic-gate #include <stddef.h>
400Sstevel@tonic-gate #include <stdlib.h>
410Sstevel@tonic-gate #include <sys/types.h>
420Sstevel@tonic-gate 
430Sstevel@tonic-gate void *
bsearch(const void * ky,const void * bs,size_t nel,size_t width,int (* compar)(const void *,const void *))440Sstevel@tonic-gate bsearch(const void *ky,		/* Key to be located */
450Sstevel@tonic-gate 	const void *bs,		/* Beginning of table */
460Sstevel@tonic-gate 	size_t nel,		/* Number of elements in the table */
470Sstevel@tonic-gate 	size_t width,		/* Width of an element (bytes) */
480Sstevel@tonic-gate 	int (*compar)(const void *, const void *)) /* Comparison function */
490Sstevel@tonic-gate {
500Sstevel@tonic-gate 	char *base;
513836Sat204353 	size_t two_width;
520Sstevel@tonic-gate 	char *last;		/* Last element in table */
530Sstevel@tonic-gate 
540Sstevel@tonic-gate 	if (nel == 0)
550Sstevel@tonic-gate 		return (NULL);
560Sstevel@tonic-gate 
570Sstevel@tonic-gate 	base = (char *)bs;
580Sstevel@tonic-gate 	two_width = width + width;
590Sstevel@tonic-gate 	last = base + width * (nel - 1);
600Sstevel@tonic-gate 
610Sstevel@tonic-gate 	while (last >= base) {
620Sstevel@tonic-gate 
630Sstevel@tonic-gate 		char *p = base + width * ((last - base)/two_width);
640Sstevel@tonic-gate 		int res = (*compar)(ky, (void *)p);
650Sstevel@tonic-gate 
660Sstevel@tonic-gate 		if (res == 0)
670Sstevel@tonic-gate 			return (p);	/* Key found */
680Sstevel@tonic-gate 		if (res < 0)
690Sstevel@tonic-gate 			last = p - width;
700Sstevel@tonic-gate 		else
710Sstevel@tonic-gate 			base = p + width;
720Sstevel@tonic-gate 	}
730Sstevel@tonic-gate 	return (NULL);		/* Key not found */
740Sstevel@tonic-gate }
75