bsearch.c (83289) | bsearch.c (92741) |
---|---|
1/* 2 * Copyright (c) 1990, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 16 unchanged lines hidden (view full) --- 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * | 1/* 2 * Copyright (c) 1990, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 16 unchanged lines hidden (view full) --- 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * |
33 * $FreeBSD: head/sys/libkern/bsearch.c 83289 2001-09-10 10:33:46Z peter $ | 33 * $FreeBSD: head/sys/libkern/bsearch.c 92741 2002-03-20 02:15:46Z alfred $ |
34 */ 35 36#if defined(LIBC_SCCS) && !defined(lint) 37static char sccsid[] = "@(#)bsearch.c 8.1 (Berkeley) 6/4/93"; 38#endif /* LIBC_SCCS and not lint */ 39 40#include <sys/param.h> 41#include <machine/ansi.h> --- 16 unchanged lines hidden (view full) --- 58 * look at item 3. 59 */ 60void * 61bsearch(key, base0, nmemb, size, compar) 62 register const void *key; 63 const void *base0; 64 size_t nmemb; 65 register size_t size; | 34 */ 35 36#if defined(LIBC_SCCS) && !defined(lint) 37static char sccsid[] = "@(#)bsearch.c 8.1 (Berkeley) 6/4/93"; 38#endif /* LIBC_SCCS and not lint */ 39 40#include <sys/param.h> 41#include <machine/ansi.h> --- 16 unchanged lines hidden (view full) --- 58 * look at item 3. 59 */ 60void * 61bsearch(key, base0, nmemb, size, compar) 62 register const void *key; 63 const void *base0; 64 size_t nmemb; 65 register size_t size; |
66 register int (*compar) __P((const void *, const void *)); | 66 register int (*compar)(const void *, const void *); |
67{ 68 register const char *base = base0; 69 register size_t lim; 70 register int cmp; 71 register const void *p; 72 73 for (lim = nmemb; lim != 0; lim >>= 1) { 74 p = base + (lim >> 1) * size; 75 cmp = (*compar)(key, p); 76 if (cmp == 0) 77 return ((void *)(uintptr_t)p); 78 if (cmp > 0) { /* key > p: move right */ 79 base = (const char *)p + size; 80 lim--; 81 } /* else move left */ 82 } 83 return (NULL); 84} | 67{ 68 register const char *base = base0; 69 register size_t lim; 70 register int cmp; 71 register const void *p; 72 73 for (lim = nmemb; lim != 0; lim >>= 1) { 74 p = base + (lim >> 1) * size; 75 cmp = (*compar)(key, p); 76 if (cmp == 0) 77 return ((void *)(uintptr_t)p); 78 if (cmp > 0) { /* key > p: move right */ 79 base = (const char *)p + size; 80 lim--; 81 } /* else move left */ 82 } 83 return (NULL); 84} |