Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.7 KiB

  1. /***
  2. *bsearch.c - do a binary search
  3. *
  4. * Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * defines bsearch() - do a binary search an an array
  8. *
  9. *Revision History:
  10. * 07-05-84 RN initial version
  11. * 06-19-85 TC put in ifdefs to handle case of multiplication
  12. * in large/huge model.
  13. * 04-13-87 JCR added const to declaration
  14. * 08-04-87 JCR Added "long" cast to mid= assignment for large/huge
  15. * model.
  16. * 11-10-87 SKS Removed IBMC20 switch
  17. * 12-11-87 JCR Added "_LOAD_DS" to declaration
  18. * 01-21-88 JCR Backed out _LOAD_DS...
  19. * 02-22-88 JCR Added cast to get rid of cl const warning
  20. * 10-20-89 JCR Added _cdecl to prototype, changed 'char' to 'void'
  21. * 03-14-90 GJF Replaced _cdecl with _CALLTYPE1, added #include
  22. * <cruntime.h>, removed #include <register.h> and fixed
  23. * the copyright. Also, cleaned up the formatting a bit.
  24. * 04-05-90 GJF Added #include <stdlib.h> and #include <search.h>.
  25. * Fixed some resulting compiler warnings (at -W3).
  26. * Also, removed #include <sizeptr.h>.
  27. * 07-25-90 SBM Removed redundant include (stdio.h), made args match
  28. * prototype
  29. * 10-04-90 GJF New-style function declarator.
  30. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  31. * 12-18-98 GJF Changes for 64-bit size_t.
  32. *
  33. *******************************************************************************/
  34. #include <cruntime.h>
  35. #include <stdlib.h>
  36. #include <search.h>
  37. /***
  38. *char *bsearch() - do a binary search on an array
  39. *
  40. *Purpose:
  41. * Does a binary search of a sorted array for a key.
  42. *
  43. *Entry:
  44. * const char *key - key to search for
  45. * const char *base - base of sorted array to search
  46. * unsigned int num - number of elements in array
  47. * unsigned int width - number of bytes per element
  48. * int (*compare)() - pointer to function that compares two array
  49. * elements, returning neg when #1 < #2, pos when #1 > #2, and
  50. * 0 when they are equal. Function is passed pointers to two
  51. * array elements.
  52. *
  53. *Exit:
  54. * if key is found:
  55. * returns pointer to occurrence of key in array
  56. * if key is not found:
  57. * returns NULL
  58. *
  59. *Exceptions:
  60. *
  61. *******************************************************************************/
  62. void * __cdecl bsearch (
  63. REG4 const void *key,
  64. const void *base,
  65. size_t num,
  66. size_t width,
  67. int (__cdecl *compare)(const void *, const void *)
  68. )
  69. {
  70. REG1 char *lo = (char *)base;
  71. REG2 char *hi = (char *)base + (num - 1) * width;
  72. REG3 char *mid;
  73. size_t half;
  74. int result;
  75. while (lo <= hi)
  76. if (half = num / 2)
  77. {
  78. mid = lo + (num & 1 ? half : (half - 1)) * width;
  79. if (!(result = (*compare)(key,mid)))
  80. return(mid);
  81. else if (result < 0)
  82. {
  83. hi = mid - width;
  84. num = num & 1 ? half : half-1;
  85. }
  86. else {
  87. lo = mid + width;
  88. num = half;
  89. }
  90. }
  91. else if (num)
  92. return((*compare)(key,lo) ? NULL : lo);
  93. else
  94. break;
  95. return(NULL);
  96. }