Windows NT 4.0 source code leak
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.

99 lines
2.7 KiB

4 years ago
  1. /***
  2. *bsearch.c - do a binary search
  3. *
  4. * Copyright (c) 1985-1992, 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. *
  31. *******************************************************************************/
  32. #include <cruntime.h>
  33. #include <stdlib.h>
  34. #include <search.h>
  35. /***
  36. *char *bsearch() - do a binary search on an array
  37. *
  38. *Purpose:
  39. * Does a binary search of a sorted array for a key.
  40. *
  41. *Entry:
  42. * const char *key - key to search for
  43. * const char *base - base of sorted array to search
  44. * unsigned int num - number of elements in array
  45. * unsigned int width - number of bytes per element
  46. * int (*compare)() - pointer to function that compares two array
  47. * elements, returning neg when #1 < #2, pos when #1 > #2, and
  48. * 0 when they are equal. Function is passed pointers to two
  49. * array elements.
  50. *
  51. *Exit:
  52. * if key is found:
  53. * returns pointer to occurrence of key in array
  54. * if key is not found:
  55. * returns NULL
  56. *
  57. *Exceptions:
  58. *
  59. *******************************************************************************/
  60. void * _CALLTYPE1 bsearch (
  61. REG4 const void *key,
  62. const void *base,
  63. size_t num,
  64. size_t width,
  65. int (_CALLTYPE1 *compare)(const void *, const void *)
  66. )
  67. {
  68. REG1 char *lo = (char *)base;
  69. REG2 char *hi = (char *)base + (num - 1) * width;
  70. REG3 char *mid;
  71. unsigned int half;
  72. int result;
  73. while (lo <= hi)
  74. if (half = num / 2)
  75. {
  76. mid = lo + (num & 1 ? half : (half - 1)) * width;
  77. if (!(result = (*compare)(key,mid)))
  78. return(mid);
  79. else if (result < 0)
  80. {
  81. hi = mid - width;
  82. num = num & 1 ? half : half-1;
  83. }
  84. else {
  85. lo = mid + width;
  86. num = half;
  87. }
  88. }
  89. else if (num)
  90. return((*compare)(key,lo) ? NULL : lo);
  91. else
  92. break;
  93. return(NULL);
  94. }