home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / programming / gnuc / stdlib / rcs / bsearch.c,v < prev    next >
Encoding:
Text File  |  1992-07-04  |  2.6 KB  |  91 lines

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