home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / dev / gcc / ixemulsrc.lha / ixemul / stdlib / bsearch.c < prev    next >
C/C++ Source or Header  |  1996-12-11  |  2KB  |  66 lines

  1. /*
  2.  * Copyright (c) 1990 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that: (1) source distributions retain this entire copyright
  7.  * notice and comment, and (2) distributions including binaries display
  8.  * the following acknowledgement:  ``This product includes software
  9.  * developed by the University of California, Berkeley and its contributors''
  10.  * in the documentation or other materials provided with the distribution
  11.  * and in all advertising materials mentioning features or use of this
  12.  * software. Neither the name of the University nor the names of its
  13.  * contributors may be used to endorse or promote products derived
  14.  * from this software without specific prior written permission.
  15.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  16.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  17.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  18.  */
  19.  
  20. #if defined(LIBC_SCCS) && !defined(lint)
  21. static char sccsid[] = "@(#)bsearch.c    5.3 (Berkeley) 5/17/90";
  22. #endif /* LIBC_SCCS and not lint */
  23.  
  24. #define _KERNEL
  25. #include "ixemul.h"
  26.  
  27. #include <stddef.h>        /* size_t */
  28. #include <stdlib.h>
  29.  
  30. /*
  31.  * Perform a binary search.
  32.  *
  33.  * The code below is a bit sneaky.  After a comparison fails, we
  34.  * divide the work in half by moving either left or right. If lim
  35.  * is odd, moving left simply involves halving lim: e.g., when lim
  36.  * is 5 we look at item 2, so we change lim to 2 so that we will
  37.  * look at items 0 & 1.  If lim is even, the same applies.  If lim
  38.  * is odd, moving right again involes halving lim, this time moving
  39.  * the base up one item past p: e.g., when lim is 5 we change base
  40.  * to item 3 and make lim 2 so that we will look at items 3 and 4.
  41.  * If lim is even, however, we have to shrink it by one before
  42.  * halving: e.g., when lim is 4, we still looked at item 2, so we
  43.  * have to make lim 3, then halve, obtaining 1, so that we will only
  44.  * look at item 3.
  45.  */
  46. void *
  47. bsearch(const void *key, const void *base0, 
  48.     size_t nmemb, size_t size, int (*compar)())
  49. {
  50.     register char *base = (char *)base0;
  51.     register int lim, cmp;
  52.     register void *p;
  53.  
  54.     for (lim = nmemb; lim != 0; lim >>= 1) {
  55.         p = base + (lim >> 1) * size;
  56.         cmp = (*compar)(key, p);
  57.         if (cmp == 0)
  58.             return (p);
  59.         if (cmp > 0) {    /* key > p: move right */
  60.             base = (char *)p + size;
  61.             lim--;
  62.         } /* else move left */
  63.     }
  64.     return (NULL);
  65. }
  66.