1
0

bsearch.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*++
  2. Copyright (c) 2013 Minoca Corp.
  3. This file is licensed under the terms of the GNU General Public License
  4. version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details. See the LICENSE file at the root of this
  6. project for complete licensing information.
  7. Module Name:
  8. bsearch.c
  9. Abstract:
  10. This module implements support for the binary search function.
  11. Author:
  12. Evan Green 20-Aug-2013
  13. Environment:
  14. User Mode C Library
  15. --*/
  16. //
  17. // ------------------------------------------------------------------- Includes
  18. //
  19. #include "libcp.h"
  20. #include <assert.h>
  21. #include <stdlib.h>
  22. //
  23. // ---------------------------------------------------------------- Definitions
  24. //
  25. //
  26. // ------------------------------------------------------ Data Type Definitions
  27. //
  28. //
  29. // ----------------------------------------------- Internal Function Prototypes
  30. //
  31. //
  32. // -------------------------------------------------------------------- Globals
  33. //
  34. //
  35. // ------------------------------------------------------------------ Functions
  36. //
  37. LIBC_API
  38. void *
  39. bsearch (
  40. const void *Key,
  41. const void *Base,
  42. size_t ElementCount,
  43. size_t ElementSize,
  44. int (*CompareFunction)(const void *, const void *)
  45. )
  46. /*++
  47. Routine Description:
  48. This routine searches an array of sorted objects for one matching the given
  49. key.
  50. Arguments:
  51. Key - Supplies a pointer to the element to match against in the given
  52. array.
  53. Base - Supplies a pointer to the base of the array to search.
  54. ElementCount - Supplies the number of elements in the array. Searching an
  55. element with a count of zero shall return NULL.
  56. ElementSize - Supplies the size of each element in the array.
  57. CompareFunction - Supplies a pointer to a function that will be called to
  58. compare elements. The function takes in two pointers that will point
  59. to elements within the array. It shall return less than zero if the
  60. left element is considered less than the right object, zero if the left
  61. object is considered equal to the right object, and greater than zero
  62. if the left object is considered greater than the right object.
  63. Return Value:
  64. Returns a pointer to the element within the array matching the given key.
  65. NULL if no such element exists or the element count was zero.
  66. --*/
  67. {
  68. size_t CompareIndex;
  69. const void *ComparePointer;
  70. int CompareResult;
  71. size_t Distance;
  72. size_t Maximum;
  73. size_t Minimum;
  74. if (ElementCount == 0) {
  75. return NULL;
  76. }
  77. Minimum = 0;
  78. Maximum = ElementCount;
  79. //
  80. // Loop as long as the indices don't cross. The maximum index is exclusive
  81. // (so 0,1 includes only 0).
  82. //
  83. while (Minimum < Maximum) {
  84. Distance = (Maximum - Minimum) / 2;
  85. CompareIndex = Minimum + Distance;
  86. ComparePointer = Base + (CompareIndex * ElementSize);
  87. CompareResult = CompareFunction(Key, ComparePointer);
  88. if (CompareResult == 0) {
  89. return (void *)ComparePointer;
  90. } else if (CompareResult > 0) {
  91. Minimum = CompareIndex + 1;
  92. } else {
  93. Maximum = CompareIndex;
  94. }
  95. }
  96. return NULL;
  97. }
  98. //
  99. // --------------------------------------------------------- Internal Functions
  100. //