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.

107 lines
4.1 KiB

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