binary-search.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* -*- Mode: js; js-indent-level: 2; -*- */
  2. /*
  3. * Copyright 2011 Mozilla Foundation and contributors
  4. * Licensed under the New BSD license. See LICENSE or:
  5. * http://opensource.org/licenses/BSD-3-Clause
  6. */
  7. if (typeof define !== 'function') {
  8. var define = require('amdefine')(module, require);
  9. }
  10. define(function (require, exports, module) {
  11. exports.GREATEST_LOWER_BOUND = 1;
  12. exports.LEAST_UPPER_BOUND = 2;
  13. /**
  14. * Recursive implementation of binary search.
  15. *
  16. * @param aLow Indices here and lower do not contain the needle.
  17. * @param aHigh Indices here and higher do not contain the needle.
  18. * @param aNeedle The element being searched for.
  19. * @param aHaystack The non-empty array being searched.
  20. * @param aCompare Function which takes two elements and returns -1, 0, or 1.
  21. * @param aBias Either 'binarySearch.GREATEST_LOWER_BOUND' or
  22. * 'binarySearch.LEAST_UPPER_BOUND'. Specifies whether to return the
  23. * closest element that is smaller than or greater than the one we are
  24. * searching for, respectively, if the exact element cannot be found.
  25. */
  26. function recursiveSearch(aLow, aHigh, aNeedle, aHaystack, aCompare, aBias) {
  27. // This function terminates when one of the following is true:
  28. //
  29. // 1. We find the exact element we are looking for.
  30. //
  31. // 2. We did not find the exact element, but we can return the index of
  32. // the next-closest element.
  33. //
  34. // 3. We did not find the exact element, and there is no next-closest
  35. // element than the one we are searching for, so we return -1.
  36. var mid = Math.floor((aHigh - aLow) / 2) + aLow;
  37. var cmp = aCompare(aNeedle, aHaystack[mid], true);
  38. if (cmp === 0) {
  39. // Found the element we are looking for.
  40. return mid;
  41. }
  42. else if (cmp > 0) {
  43. // Our needle is greater than aHaystack[mid].
  44. if (aHigh - mid > 1) {
  45. // The element is in the upper half.
  46. return recursiveSearch(mid, aHigh, aNeedle, aHaystack, aCompare, aBias);
  47. }
  48. // The exact needle element was not found in this haystack. Determine if
  49. // we are in termination case (3) or (2) and return the appropriate thing.
  50. if (aBias == exports.LEAST_UPPER_BOUND) {
  51. return aHigh < aHaystack.length ? aHigh : -1;
  52. } else {
  53. return mid;
  54. }
  55. }
  56. else {
  57. // Our needle is less than aHaystack[mid].
  58. if (mid - aLow > 1) {
  59. // The element is in the lower half.
  60. return recursiveSearch(aLow, mid, aNeedle, aHaystack, aCompare, aBias);
  61. }
  62. // we are in termination case (3) or (2) and return the appropriate thing.
  63. if (aBias == exports.LEAST_UPPER_BOUND) {
  64. return mid;
  65. } else {
  66. return aLow < 0 ? -1 : aLow;
  67. }
  68. }
  69. }
  70. /**
  71. * This is an implementation of binary search which will always try and return
  72. * the index of the closest element if there is no exact hit. This is because
  73. * mappings between original and generated line/col pairs are single points,
  74. * and there is an implicit region between each of them, so a miss just means
  75. * that you aren't on the very start of a region.
  76. *
  77. * @param aNeedle The element you are looking for.
  78. * @param aHaystack The array that is being searched.
  79. * @param aCompare A function which takes the needle and an element in the
  80. * array and returns -1, 0, or 1 depending on whether the needle is less
  81. * than, equal to, or greater than the element, respectively.
  82. * @param aBias Either 'binarySearch.GREATEST_LOWER_BOUND' or
  83. * 'binarySearch.LEAST_UPPER_BOUND'. Specifies whether to return the
  84. * closest element that is smaller than or greater than the one we are
  85. * searching for, respectively, if the exact element cannot be found.
  86. * Defaults to 'binarySearch.GREATEST_LOWER_BOUND'.
  87. */
  88. exports.search = function search(aNeedle, aHaystack, aCompare, aBias) {
  89. if (aHaystack.length === 0) {
  90. return -1;
  91. }
  92. var index = recursiveSearch(-1, aHaystack.length, aNeedle, aHaystack,
  93. aCompare, aBias || exports.GREATEST_LOWER_BOUND);
  94. if (index < 0) {
  95. return -1;
  96. }
  97. // We have found either the exact element, or the next-closest element than
  98. // the one we are searching for. However, there may be more than one such
  99. // element. Make sure we always return the smallest of these.
  100. while (index - 1 >= 0) {
  101. if (aCompare(aHaystack[index], aHaystack[index - 1], true) !== 0) {
  102. break;
  103. }
  104. --index;
  105. }
  106. return index;
  107. };
  108. });